Latest Socket ResearchMalicious Chrome Extension Performs Hidden Affiliate Hijacking.Details
Socket
Book a DemoInstallSign in
Socket

@mojojs/path

Package Overview
Dependencies
Maintainers
4
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mojojs/path

File system paths made fun

Source
npmnpm
Version
1.0.1
Version published
Weekly downloads
1.8K
31.59%
Maintainers
4
Weekly downloads
 
Created
Source

npm

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';

// Current working directory (portable)
const dir = new Path();

// Relative file path "test.txt" (portable)
const file = new Path('test.txt');

// Relative file path "files/test.txt" (portable, POSIX example)
const file = new Path('files', 'test.txt');

// Relative file path "files/test.txt" (not portable)
const file = new Path('files/test.txt');

// Absolute file path "/home/kraih/test.txt" (not portable)
const file = new Path('/home/kraih/test.txt');

// Current file (portable)
const file = Path.currentFile();

// Caller file (portable)
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.

// "/home/kraih/files/test.txt"
new Path('/home/kraih').child('files', 'test.txt');

// "/home/kraih/files/hello.txt"
new Path('/home/kraih/files/test.txt').sibling('hello.txt');

// "test.txt"
new Path('/home/kraih/test.txt').basename();

// "foo/bar"
new Path('/home/kraih/test.txt').dirname();

// ".txt"
new Path('/home/kraih/test.txt').extname();

// ".json"
new Path('/home/kraih/files/test.txt').sibling('hello.json').extname();

// "file:///home/kraih/test.txt"
new Path('/home/kraih/test.txt').toFileURL().toString();

// "['files', 'test.txt']"
new Path('files/test.txt').toArray();

// Caller directory
Path.callerFile().dirname();

Almost all methods will return this or a new instance of Path, depending on what makes most sense.

// Write file (async)
const file = await new Path('/home/kraih/test.txt').writeFile('Hello World!');

// Write file (sync)
const file = new Path('/home/kraih/test.txt').writeFileSync('Hello World!');

// Read file (async)
const content = await new Path('/home/kraih/test.txt').readFile('utf8');

// Read file (sync)
const content = new Path('/home/kraih/test.txt').readFileSync('utf8');

// Create file or update utime
await new Path('/home/kraih/test.txt').touch();
new Path('/home/kraih/test.txt').touchSync();

// Open file (async)
const fh = await new Path('/home/kraih').child('test.txt').open('w');
await fh.write('Hello ');
await fh.write('JavaScript!');
await fh.close();

// Create writable stream
const writable = new Path('test.txt').createWriteStream({encoding: 'utf8'});

// Create readable stream
const readable = new Path('test.txt').createReadStream({encoding: 'utf8'});

// Read lines from file
for await (const line of new Path('test.txt').lines({encoding: 'UTF-8'})) {
 console.log(line);
}

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.

// Create a temporary directory (async)
const dir = await Path.tempDir();
await dir.child('test.txt').touch();
await dir.destroy();

// Create a temporary directory (sync)
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.

// Make file read-only
const file = await new Path('test.txt').chmod(Path.constants.O_RDONLY);
const file = new Path('test.txt').chmodSync(Path.constants.O_RDONLY);

// Check file access (async)
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();

// Check file access (sync)
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();

// Check if file exists
const exists = await new Path('test.txt').exists();
const exists = new Path('test.txt').existsSync();

// Check if file is absolute
const isAbsolute = new Path('test.txt').isAbsolute();
// Resolve path on file system
const real = await new Path('test.txt').realpath();
const real = new Path('test.txt').realpathSync();

// Create directory
const dir = await new Path('test', '123').mkdir({recursive: true});
const dir = new Path('test', '123').mkdirSync({recursive: true});

// Remove directory
await new Path('test').rm({recursive: true});
new Path('test').rmSync({recursive: true});

// List files in directory
for await (const file of new Path('test').list()) {
  console.log(file.toString());
}

// List files recursively
for await (const file of new Path('test').list({recursive: true})) {
  console.log(file.toString());
}
// Update atime and mtime
const file = await new Path('test.txt').utimes(new Date(), new Date());
const file = new Path('test.txt').utimesSync(new Date(), new Date());

// Symlink file
const file = await new Path('foo.txt').symlink(new Path('bar.txt'));
const file = new Path('foo.txt').symlinkSync(new Path('bar.txt'));

// Copy file
const file = await new Path('foo.txt').copyFile(new Path('bar.txt'));
const file = new Path('foo.txt').copyFileSync(new Path('bar.txt'));

// Rename file
await new Path('foo.txt').rename(new Path('bar.txt'));
new Path('foo.txt').renameSync(new Path('bar.txt'));
// Check is file is a directory (async)
const stat = await new Path('test').stat();
const isDirectory = stat.isDirectory();
const lstat = await new Path('test').lstat();
const isDirectory = lstat.isDirectory();

// Check is file is a directory (sync)
const isDirectory = new Path('test').statSync().isDirectory();
const isDirectory = new Path('test').lstatSync().isDirectory();

Installation

All you need is Node.js 16.0.0 (or newer).

$ npm install @mojojs/path

Keywords

fs

FAQs

Package last updated on 06 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts