fs-gjs
Collection of file system utility functions for Gnome JavaScript (GJS).
Usage
fs-gjs
provides two sets of functions, synchronous and asynchronous. First can be accessed vias SyncFs
class and the latter via Fs
class. Both have almost exactly the same API, with the only difference that asynchronous functions need to be awaited.
- Reading files
- Writing files
- Appending to files
- Copying files
- Moving files
- Deleting files
- Creating directories
- Create symbolic links
- Change file permissions
- Change file ownership
- List directory contents
- Check if file exists
- Get file info
- IOStreams
Reading files
import { Fs } from "./node_modules/fs-gjs/index.js";
const bytes = await Fs.readFile("/path/to/file");
const text = await Fs.readTextFile("/path/to/file");
Writing files
import { Fs } from "./node_modules/fs-gjs/index.js";
const bytes = new Uint8Array([1, 2, 3]);
await Fs.writeFile("/path/to/file", bytes);
const text = "Hello, world!";
await Fs.writeTextFile("/path/to/file", text);
Appending to files
import { Fs } from "./node_modules/fs-gjs/index.js";
const bytes = new Uint8Array([1, 2, 3]);
await Fs.appendFile("/path/to/file", bytes);
const text = "Hello, world!";
await Fs.appendTextFile("/path/to/file", text);
Copying files
import { Fs } from "./node_modules/fs-gjs/index.js";
await Fs.copyFile("/path/to/source", "/path/to/destination");
Moving files
import { Fs } from "./node_modules/fs-gjs/index.js";
await Fs.moveFile("/path/to/source", "/path/to/destination");
Deleting files
import { Fs } from "./node_modules/fs-gjs/index.js";
await Fs.deleteFile("/path/to/file");
await Fs.deleteFile("/path/to/file", { trash: true });
Creating directories
import { Fs } from "./node_modules/fs-gjs/index.js";
await Fs.makeDir("/path/to/directory");
Create symbolic links
import { Fs } from "./node_modules/fs-gjs/index.js";
await Fs.makeLink("/path/to/link", "/path/to/target");
Change file permissions
import { Fs } from "./node_modules/fs-gjs/index.js";
await Fs.chmod("/path/to/file", 0o755);
await Fs.chmod("/path/to/file", "rwxr-xr-x");
await Fs.chmod("/path/to/file", {
owner: { read: true, write: true, execute: true },
group: { read: true, write: false, execute: true },
others: { read: true, write: false, execute: true },
});
Change file ownership
import { Fs } from "./node_modules/fs-gjs/index.js";
await Fs.chown("/path/to/file", 1000, 1000);
List directory contents
import { Fs } from "./node_modules/fs-gjs/index.js";
await Fs.listDir("/path/to/directory");
await Fs.listFilenames("/path/to/directory");
Check if file exists
import { Fs } from "./node_modules/fs-gjs/index.js";
await Fs.fileExists("/path/to/file");
Get file info
import { Fs } from "./node_modules/fs-gjs/index.js";
await Fs.fileInfo("/path/to/file");
IOStreams
IOStreams can be opened in one of three modes:
CREATE
- a new file will be created, will fail if file already existsREPLACE
- a new file will be created, will replace existing file if it existsOPEN
- an existing file will be opened, will fail if file does not exist
import { Fs } from "./node_modules/fs-gjs/index.js";
const stream = await Fs.openIOStream("/path/to/file", "CREATE");
const bytes = await stream.read(1024);
const bytes = await stream.readAll();
const bytes = new Uint8Array([1, 2, 3]);
await stream.write(bytes);
await stream.skip(1024);
await stream.seek(1024);
await stream.seekFromStart(1024);
await stream.seekFromEnd(1024);
await stream.truncate(1024);
const position = await stream.currentPosition();
await stream.close();