OS

OS-agonistic APIs for operating system functionality, such as interacting with the filesystem.

Go reference: https://pkg.go.dev/os

$os

Info

console.log("Platform:", $os.platform); // darwin
console.log("Arch:", $os.arch); // arm64

Directories

$os.tempDir() // $TEMP must be in the allow list
$os.cacheDir() // $CACHE must be in the allow list
$os.configDir() // $CONFIG must be in the allow list
$os.homeDir() // $HOME must be in the allow list

File operations

Example
// C:\Users\user\Downloads\test.txt
Hello World!

// my-plugin.ts
// "C:/Users/user/Downloads/**/*" and "$TEMP/**/*" have been added to 'allowReadPaths' and 'allowWritePaths'

// Access the temp directory
const tempDirPath = $os.tempDir();
console.log("Temp dir:", tempDirPath);

// Read files
const content = $os.readFile("C:\Users\user\Downloads\test.txt");
console.log("File content:", $toString(content)); // Hello World!

// Write/create files
$os.writeFile("C:\Users\user\Downloads\test.txt.new", $toBytes("New content"), 0644);
const newContent = $os.readFile("C:\Users\user\Downloads\test.txt.new");
console.log("New file content:", $toString(newContent)); // New content

// Read directories
const entries = $os.readDir("C:\Users\user\Downloads");
for (const entry of entries) {
	console.log(entry.name()); // test.txt, test.txt.new
}

// Create directories
$os.mkdir("C:\Users\user\Downloads\newdir", 0755);
const newEntries = $os.readDir("C:\Users\user\Downloads");
for (const entry of newEntries) {
	console.log(entry.name()); // test.txt, test.txt.new, newdir
}

// Rename files
$os.rename("C:\Users\user\Downloads\test.txt.new", "C:\Users\user\Downloads\test.txt.renamed");
let renameSuccess = true
try {
	// File exists, no error thrown
	$os.stat("C:\Users\user\Downloads\test.txt.renamed");
} catch(e) {
	renameSuccess = false
}
console.log(renameSuccess); // true

// Remove files
$os.remove("C:\Users\user\Downloads\test.txt.renamed");
let removeSuccess = true;
try {
	$os.stat("C:\Users\user\Downloads\test.txt.renamed");
	removeSuccess = false;
} catch (e) {
	// Error thrown becuase file should not exist
	removeSuccess = true;
}
console.log(removeSuccess); // true

$osExtra

This API gives you additional functionalities

Directories

$osExtra.desktopDir() // $DESKTOP must be in the allow list
$osExtra.documentDir() // $DOCUMENT must be in the allow list
$osExtra.downloadDir() // $DOWNLOAD must be in the allow list

Unarchive files

  • unzipFile, unrarFile

// If "file.zip" contains `folder > file.text`
$osExtra.unzipFile("/path/to/downloaded/file.zip", "/path/to/dest") 
// -> "/path/to/dest/folder/file.txt"

// If "file.rar" contains `file.txt`
$osExtra.unrarFile("/path/to/downloaded/file.zip", "/path/to/dest")
// -> "/path/to/dest/file.txt"

Last updated