Filesystem
The Filesystem Aptitude provides the ability to interact with files on the system (including things reading, writing, and deleting).
Each Loop is provided with their own blank "working" directory for this Aptitude, which is what relative paths resolve to. Loops are free to do anything they want to any file/directory inside of this working directory.
Loops cannot access other Loops' working directories.
This working directory persists across Loop version updates.
API
Example
Permissions
Copies a file from one location to another.
import { filesystem } from '@oliveai/ldk';
const SOURCE_PATH = '/var/log/system.log';
const DESTINATION_PATH = '/Users/username/system.log';
// Check to make sure the system log exists
const systemLogExists = await filesystem.exists(SOURCE_PATH);
// Copy the system log to the user folder
if (systemLogExists) {
await filesystem.copy(SOURCE_PATH, DESTINATION_PATH);
}
Returns all files in the specified directory.
import { filesystem } from '@oliveai/ldk';
// Get all files in the /var/log directory
const allLogs = await filesystem.dir('/var/log');
/*
allLogs:
[
{
"name": "foo.txt",
"size": 476,
"mode": "-rw-r--r--",
"modTime": "2021-10-18T12:46:16.234535644-04:00",
"isDir": false
},
{
"name": "bar",
"size": 96,
"mode": "drwxr-xr-x",
"modTime": "2021-09-19T05:25:54.491321266-04:00",
"isDir": true
},
...
]
*/
Return true if a file or directory exists at the specified location.
import { filesystem } from '@oliveai/ldk';
// Check to make sure /var/log directory exists
const logDirExists = await filesystem.exists('/var/log');
// logDirExists === true
// Check to make sure /var/log/system.log file exists
const sysLogExists = await filesystem.exists('/var/log/system.log');
// sysLogExists === true
// Check to make sure /var/log/fakeDirectory directory exists
const fakeDirExists = await filesystem.exists('/var/log/fakeDirectory');
// fakeDirExists === false
Join joins an array of path elements into a single path, separating them with an OS specific Separator. Empty elements are ignored. The result is cleaned. However, if the argument list is empty or all its elements are empty, Join returns an empty string. On Windows, the result will only be a UNC path if the first non-empty element is a UNC path.
import { filesystem } from '@oliveai/ldk';
// Create path string for a local text file that works on both
// Windows and Unix-based systems
const testDirectory = await filesystem.join(['testDirectory', 'foo.txt']);
// Windows: testDirectory == testDirectory\foo.txt
// macOS: testDirectory == testDirectory/foo.txt
Listen for changes to the contents of the directory.
import { filesystem } from '@oliveai/ldk';
// The path of the directory where we want to listen for changes
const PATH = '/var/log';
// The callback function that runs when a change is detected
const callback = (fileEvent) => {
// When a file or directory are added, opened, or updated:
/*
fileEvent:
{
"info": {
"name": "foo.log",
"size": 17,
"mode": "-rwxr-xr-x",
"modTime": "2021-10-27T22:14:24.596962-04:00",
"isDir": false
},
"action": "Create" or "Chmod"
}
*/
// When a file or directory is renamed or removed, it will fire off
// the above fileEvent then another one afterward:
/*
fileEvent:
{
"action": "Rename" or "Remove",
"name": "test.log" // old file name
}
*/
}
const directoryListener = await filesystem.listenDir(PATH, callback);
// Cancel the listener
directoryListen.cancel();
Listen for changes to a specific file.
import { filesystem } from '@oliveai/ldk';
// The path of the file we want to listen to for changes
const PATH = '/var/log/system.log';
// The callback function that runs when a change is detected
const callback = (fileEvent) => {
// When the file is opened or updated:
/*
fileEvent:
{
"info": {
"name": "system.log",
"size": 14592,
"mode": "-rwxr-xr-x",
"modTime": "2021-10-27T22:31:24.215811-04:00",
"isDir": false
},
"action": "Chmod"
}
*/
// When the file is renamed or removed, it will fire off
// the above fileEvent then another one afterward:
/*
fileEvent:
{
"action": "Rename" or "Remove",
"name": "system.log" // old file name
}
*/
}
const fileListener = await filesystem.listenFile(PATH, callback);
// Cancel the listener
fileListener.cancel();
Makes a directory at the specified location.
import { filesystem } from '@oliveai/ldk';
// The path of the directory we want to create
const DESTINATION_PATH = '/var/log/backups';
// The permissions we want to give the directory, in octal format
// https://www.linode.com/docs/guides/modify-file-permissions-with-chmod/
const WRITE_MODE = 0o750
// Check to make sure the directory doesn't already exist
const directoryExists = await filesystem.exists(DESTINATION_PATH);
if (!directoryExists) {
await filesystem.makeDir(DESTINATION_PATH, WRITE_MODE);
}
Moves a file from one location to another.
import { filesystem } from '@oliveai/ldk';
const SOURCE_PATH = '/var/log/system.log';
const DESTINATION_PATH = '/Users/username/system.log';
// Check to make sure the system log exists
const systemLogExists = await filesystem.exists(SOURCE_PATH);
// Move the system log to the user folder
await filesystem.move(SOURCE_PATH, DESTINATION_PATH);
Opens a file using the operating system's default tool for the extension provided in the path parameter, including directories.
Limited to the following file extensions:
.txt
, .xls
, .xlsx
, .csv
, .pdf
, .doc
, .docx
, .jpg
, .jpeg
, .png
, .apng
, .gif
, .tif
, .tiff
, .bmp
, .webp
, .svg
, and .ico
import { filesystem } from '@oliveai/ldk';
// Open foo.txt in the loop directory
await filesystem.openWithDefaultApplication('foo.txt')
Returns the contents of the specified file.
import { filesystem, network } from '@oliveai/ldk';
// Path of the system log that we want to read
const FILE_PATH = '/var/log/system.log';
// Get the contents of the system log, returned as a Uint8Array
const encodedSysLog = await filesystem.readFile(FILE_PATH);