1.2.0 • Published 4 years ago

tofs v1.2.0

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

tofs

CircleCI

class-based wrapper for fs module.

File Types

The following file types are implemented,

  • Folder
  • File
  • Unknown

Any

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();

File

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");

Folder

// 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 });

Custom File Type

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)) {
    // ...
  }
}
1.2.0

4 years ago

1.1.0

5 years ago

1.0.0

5 years ago