async-file-tried
v1.1.0
Published
A try-catch wrapper around node’s fs/promises.
Downloads
25
Maintainers
Readme
Async-file-tried
async-file-tried is a wrapper around node’s fs/promises
that abstracts the try-catch block for you.
Write more linear, better readable code by getting a concise response. TypeScript supported.
License
Copyright (c) 2023 Florian Walzel, MIT License
Install
npm install async-file-tried
Import:
import * as fs from 'async-file-tried';
Usage
Usually we do not want to make calls against the file system without a proper error handling. But the try-catch block is somewhat unelegant because it moves code blocks in the bracket sub-space and thus disturbs the linearity of our programming. async-file-tried returns a tuple with either the response or the error from a fs call and simplifies error handling.
You can write now:
let [res, err] = await fs.readdir('.');
if (err) console.error(err);
else console.log(res);
... instead of:
try {
let res = await fs.readdir('.');
console.log(res);
}
catch (err) {
console.error(err);
}
Extra: joined paths
async-file-tried also has an in-built path.join() so that you can alternatively pass all path arguments as array of sub-elements.
You can write i.e.:
let [res, err] = await fs.appendFile(['fs.__dirname', '..' , 'myfolder', `img_${i}.txt`], 'my text');
... and the file string will be composed automatically.
API
FS Function List
- Typescript implementation:
async (path: string|Array<string>, mode?: number|string)
- Usage example:
let [res, err] = await fs.access('file.txt', fs.constants.R_OK);
- Typescript implementation:
- Typescript implementation:
async (path: string|Array<string>, data: any, options?: { encoding?: Encoding; mode?: number|string; flag?: Flags; })
- Usage example:
let [res, err] = await fs.appendFile('file.txt', 'foo');
- Typescript implementation:
- Typescript implementation:
async (path: string|Array<string>, mode?: number|string)
- Usage example:
let [res, err] = await fs.chmod('file.txt', '755');
- Typescript implementation:
- Typescript implementation:
async (path: string|Array<string>, uid: number, gid: number)
- Usage example:
let [res, err] = await fs.chown('file.txt', 1541, 999);
- Typescript implementation:
- Typescript implementation:
async (srcPath: string|Array<string>, destPath: string|Array<string>,flags?: number)
- Usage example:
let [res, err] = await fs.copyFile('file.txt', 'file-copy.txt');
- Typescript implementation:
- Typescript implementation:
async (srcPath: string|Array<string>, destPath: string|Array<string>)
- Usage example:
let [res, err] = await fs.cp('myfolder', 'myfolder-copy', {recursive: true});
- Typescript implementation:
- Typescript implementation:
async (path: string|Array<string>, mode?: number|string)
- Usage example: ``
- Typescript implementation:
- Typescript implementation:
async (path: string|Array<string>, uid: number, gid: number)
- Usage example:
let [res, err] = await fs.lchown('./test/static-testfiles/symlinkToFile-append.txt', 1200, 1201);
- Typescript implementation:
- Typescript implementation:
async (existingPath: string|Array<string>, newPath: string|Array<string>)
- Usage example:
let [res, err] = await fs.link('file.txt', 'file-hard-link.txt');
- Typescript implementation:
- Typescript implementation:
async (path: string|Array<string>, options?: object)
- Usage example:
let [res, err] = await fs.lstat('symlinkToFile.txt');
- Typescript implementation:
- Typescript implementation:
async (path: string|Array<string>, atime: Date|number, mtime: Date|number)
- Usage example:
let [res, err] = await fs.lutimes('file.txt', new Date(), new Date());
- Typescript implementation:
- Typescript implementation:
async (path: string|Array<string>, options?: number|string)
- Usage example:
let [res, err] = await fs.mkdir('./my-new-folder');
- Typescript implementation:
- Typescript implementation:
async (prefix: string, encoding?: Encoding)
- Usage example:
let [res, err] = await fs.mkdtemp('temp-');
- Typescript implementation:
- Typescript implementation:
async (path: string|Array<string>, flags?: Flags, mode?: number|string)
- Usage example:
let [res, err] = await fs.open('file.txt', 'r');
- Typescript implementation:
- Typescript implementation:
async (path: string|Array<string>, flags?: Flags, mode?: number|string)
- Usage example:
let [res, err] = await fs.opendir('./test', { encoding: "utf8", bufferSize: 64 } );
- Typescript implementation:
- Typescript implementation:
async (path: string|Array<string>, options?: Encoding)
- Usage example:
let [res, err] = await fs.readFile('file.txt', 'utf8');
- Typescript implementation:
- Typescript implementation:
async (path: string|Array<string>, options?: object)
- Usage example:
let [res, err] = await fs.readdir('./my-directory');
- Typescript implementation:
- Typescript implementation:
async (path: string|Array<string>, options?: object|string)
- Usage example:
let [res, err] = await fs.readlink('symlinkToFile.txt');
- Typescript implementation:
- Typescript implementation:
async (path: string|Array<string>, options?: object)
- Usage example:
let [res, err] = await fs.realpath('./my-folder/../' );
- Typescript implementation:
- Typescript implementation:
async (oldPath: string|Array<string>, newPath: string|Array<string>)
- Usage example:
let [res, err] = await fs.rename('old.txt', 'new.txt' );
- Typescript implementation:
- Typescript implementation:
async (path: string|Array<string>, options?: object)
- Usage example:
let [res, err] = await fs.rm('file.txt' );
- Typescript implementation:
- Typescript implementation:
async (path: string|Array<string>, options?: object)
- Usage example:
let [res, err] = await fs.rmdir('./my-folder');
- Typescript implementation:
- Typescript implementation:
async (path: string|Array<string>, options?: object)
- Usage example:
let [res, err] = await fs.stat('file.txt');
- Typescript implementation:
- Typescript implementation:
async (target: string|Array<string>, path: string|Array<string>, type?: string)
- Usage example:
let [res, err] = await fs.symlink('file.txt', 'file-symlink.txt', 'file');
- Typescript implementation:
- Typescript implementation:
async (path: string|Array<string>, len:number)
- Usage example:
let [res, err] = await fs.truncate('file.txt', 760);
- Typescript implementation:
- Typescript implementation:
async (path: string|Array<string>)
- Usage example:
let [res, err] = await fs.unlink('file-symlink.txt');
- Typescript implementation:
- Typescript implementation:
async (path: string|Array<string>, atime: number|string|Date, mtime: number|string|Date)
- Usage example:
let [res, err] = await fs.utimes('file.txt', new Date(), new Date());
- Typescript implementation:
- Typescript implementation:
async (filename: string|Array<string>, options?: object|string)
- Usage example:
let [res, err] = await fs.watch('file.txt', (ev, file) => { console.log("Watcher: " + file); });
- Typescript implementation:
- Typescript implementation:
async (path: string|Array<string>, data: any, options?: Encoding)
- Usage example:
let [res, err] = await fs.writeFile('file.txt', 'my text', 'utf8');
- Typescript implementation:
The Constants
- The FS constants object
- The equivalent to node’s
__dirname
usable within ES6 Module Syntax. Returns the directory name of the current module. - Implementation:
path.dirname(fileURLToPath(import.meta.url));
- The equivalent to node’s
- The equivalent to node’s
__filename
usable within ES6 Module Syntax. Returns the filename of the code which is executed. - Implementation:
fileURLToPath(import.meta.url);
- The equivalent to node’s
Bonus Functions
fs.readJson
- Reads a JSON file and returns it as parsed Javascript object.
- Typescript implementation:
async (path: string|Array<string>, options?: Encoding)
- Usage example:
let [res, err] = await fs.readJson('my.json');
fs.writeJson
- Expects a Javascript object and will stringify and write it out.
- Typescript implementation:
async (path: string|Array<string>, data: Object, options?: Encoding)
- Usage example:
let [res, err] = await fs.writeJson('./test/static-testfiles/test.json', { key : "value" });