class-based wrapper for fs module.
The following file types are implemented,
- Folder
- File
- Unknown
const file = new File("/foo/bar/hoge.jpg");
// Type Guard
file.is(File);
file.path; // "/foo/bar/hoge.jpg"
file.name; // "hoge.jpg"
file.parentPath; // "/foo/bar"
const stat: fs.Stats = await file.stats();
await file.exists(); // true or false
const folder = file.parent();
await folder.rename("/foo/buz");
folder.name; // "buz"
await folder.remove();
const file = new File("./hoge.tar.gz");
file.stem; // "hoge.tar"
file.suffix; // "gz"
file.suffixes; // ["tar", "gz"]
const fd: fs.promises.FileHandler = await file.open("r");
// Make folder
// with static method
const folder = await Folder.make("/foo/bar");
// or
await folder.make();
for (const child of await folder.children()) {
if (child.is(File)) {
const fd = await child.open("r");
// ...
} else if (child.is(Folder)) {
const children = await child.children();
// ...
} else {
console.log(`unknown file type: ${file.path}`);
}
}
// get the specified child file instance
const child: Folder = folder.child("child-name", { as: Folder });
Create a custom file type class by inheriting AnyFile
.
class SymbolicLink exnteds AnyFile {
verify(): Promise<boolean> {
return this.stat.then(stat => stat.isSymbolicLink());
}
}
A custom type can be used for cast within folder.children
as below,
for(const child of folder.children({ castable: [SymbolicLink]})) {
if (child.is(SymbolicLink)) {
// ...
}
}