
A convenient little wrapper around fs and friends. Providing a container class for
file system paths with a friendly API for dealing with different operating systems. Written in TypeScript.
import Path from '@mojojs/path';
const dir = new Path();
const file = new Path('test.txt');
const file = new Path('files', 'test.txt');
const file = new Path('files/test.txt');
const file = new Path('/home/kraih/test.txt');
const file = Path.currentFile();
const file = Path.callerFile();
Paths will be automatically split and joined with the correct separator for the current operating system. For the
following examples we will be using POSIX paths and assume a Linux operating system.
new Path('/home/kraih').child('files', 'test.txt');
new Path('/home/kraih/files/test.txt').sibling('hello.txt');
new Path('/home/kraih/test.txt').basename();
new Path('/home/kraih/test.txt').dirname();
new Path('/home/kraih/test.txt').extname();
new Path('/home/kraih/files/test.txt').sibling('hello.json').extname();
new Path('/home/kraih/test.txt').toFileURL().toString();
new Path('files/test.txt').toArray();
Path.callerFile().dirname();
Almost all methods will return this or a new instance of Path, depending on what makes most sense.
const file = await new Path('/home/kraih/test.txt').writeFile('Hello World!');
const file = new Path('/home/kraih/test.txt').writeFileSync('Hello World!');
const content = await new Path('/home/kraih/test.txt').readFile('utf8');
const content = new Path('/home/kraih/test.txt').readFileSync('utf8');
await new Path('/home/kraih/test.txt').touch();
new Path('/home/kraih/test.txt').touchSync();
const fh = await new Path('/home/kraih').child('test.txt').open('w');
await fh.write('Hello ');
await fh.write('JavaScript!');
await fh.close();
const writable = new Path('test.txt').createWriteStream({encoding: 'utf8'});
const readable = new Path('test.txt').createReadStream({encoding: 'utf8'});
Temporary directories will be deleted automatically when node stops, but can also be removed manually with the destroy
method. They are created relative to the operating system temp directory with node- prefix.
const dir = await Path.tempDir();
await dir.child('test.txt').touch();
await dir.destroy();
const dir = Path.tempDirSync();
dir.child('test.txt').touchSync();
dir.destroySync();
There is a *Sync alternative for almost every method returning a Promise. All fs.constants are available via
Path.constants.
const file = await new Path('test.txt').chmod(Path.constants.O_RDONLY);
const file = new Path('test.txt').chmodSync(Path.constants.O_RDONLY);
const isReadable = await new Path('test.txt').access(Path.constants.R_OK);
const isReadable = await new Path('test.txt').isReadable();
const isWritable = await new Path('test.txt').isWritable();
const isReadable = new Path('test.txt').accessSync(Path.constants.R_OK);
const isReadable = new Path('test.txt').isReadableSync();
const isWritable = new Path('test.txt').isWritableSync();
const exists = await new Path('test.txt').exists();
const exists = new Path('test.txt').existsSync();
const isAbsolute = new Path('test.txt').isAbsolute();
const real = await new Path('test.txt').realpath();
const real = new Path('test.txt').realpathSync();
const dir = await new Path('test', '123').mkdir({recursive: true});
const dir = new Path('test', '123').mkdirSync({recursive: true});
await new Path('test').rm({recursive: true});
new Path('test').rmSync({recursive: true});
const file = await new Path('test.txt').utimes(new Date(), new Date());
const file = new Path('test.txt').utimesSync(new Date(), new Date());
const file = await new Path('foo.txt').symlink(new Path('bar.txt'));
const file = new Path('foo.txt').symlinkSync(new Path('bar.txt'));
const file = await new Path('foo.txt').copyFile(new Path('bar.txt'));
const file = new Path('foo.txt').copyFileSync(new Path('bar.txt'));
await new Path('foo.txt').rename(new Path('bar.txt'));
new Path('foo.txt').renameSync(new Path('bar.txt'));
Installation
All you need is Node.js 16.0.0 (or newer).
$ npm install @mojojs/path