npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

async-file-tried

v1.1.0

Published

A try-catch wrapper around node’s fs/promises.

Downloads

25

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: MIT Node.js Version Build Coverage Status Known Vulnerabilities

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

  • fs.access

    • Typescript implementation: async (path: string|Array<string>, mode?: number|string)
    • Usage example: let [res, err] = await fs.access('file.txt', fs.constants.R_OK);
  • fs.appendFile

    • 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');
  • fs.chmod

    • Typescript implementation: async (path: string|Array<string>, mode?: number|string)
    • Usage example: let [res, err] = await fs.chmod('file.txt', '755');
  • fs.chown

    • Typescript implementation: async (path: string|Array<string>, uid: number, gid: number)
    • Usage example: let [res, err] = await fs.chown('file.txt', 1541, 999);
  • fs.copyFile

    • 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');
  • fs.cp

    • Typescript implementation: async (srcPath: string|Array<string>, destPath: string|Array<string>)
    • Usage example: let [res, err] = await fs.cp('myfolder', 'myfolder-copy', {recursive: true});
  • fs.lchmod

    • Typescript implementation: async (path: string|Array<string>, mode?: number|string)
    • Usage example: ``
  • fs.lchown

    • 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);
  • fs.link

    • 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');
  • fs.lstat

    • Typescript implementation: async (path: string|Array<string>, options?: object)
    • Usage example: let [res, err] = await fs.lstat('symlinkToFile.txt');
  • fs.lutimes

    • 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());
  • fs.mkdir

    • Typescript implementation: async (path: string|Array<string>, options?: number|string)
    • Usage example: let [res, err] = await fs.mkdir('./my-new-folder');
  • fs.mkdtemp

    • Typescript implementation: async (prefix: string, encoding?: Encoding)
    • Usage example: let [res, err] = await fs.mkdtemp('temp-');
  • fs.open

    • Typescript implementation: async (path: string|Array<string>, flags?: Flags, mode?: number|string)
    • Usage example: let [res, err] = await fs.open('file.txt', 'r');
  • fs.opendir

    • 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 } );
  • fs.readFile

    • Typescript implementation: async (path: string|Array<string>, options?: Encoding)
    • Usage example: let [res, err] = await fs.readFile('file.txt', 'utf8');
  • fs.readdir

    • Typescript implementation: async (path: string|Array<string>, options?: object)
    • Usage example: let [res, err] = await fs.readdir('./my-directory');
  • fs.readlink

    • Typescript implementation: async (path: string|Array<string>, options?: object|string)
    • Usage example: let [res, err] = await fs.readlink('symlinkToFile.txt');
  • fs.realpath

    • Typescript implementation: async (path: string|Array<string>, options?: object)
    • Usage example: let [res, err] = await fs.realpath('./my-folder/../' );
  • fs.rename

    • Typescript implementation: async (oldPath: string|Array<string>, newPath: string|Array<string>)
    • Usage example: let [res, err] = await fs.rename('old.txt', 'new.txt' );
  • fs.rm

    • Typescript implementation: async (path: string|Array<string>, options?: object)
    • Usage example: let [res, err] = await fs.rm('file.txt' );
  • fs.rmdir

    • Typescript implementation: async (path: string|Array<string>, options?: object)
    • Usage example: let [res, err] = await fs.rmdir('./my-folder');
  • fs.stat

    • Typescript implementation: async (path: string|Array<string>, options?: object)
    • Usage example: let [res, err] = await fs.stat('file.txt');
  • fs.symlink

    • 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');
  • fs.truncate

    • Typescript implementation: async (path: string|Array<string>, len:number)
    • Usage example: let [res, err] = await fs.truncate('file.txt', 760);
  • fs.unlink

    • Typescript implementation: async (path: string|Array<string>)
    • Usage example: let [res, err] = await fs.unlink('file-symlink.txt');
  • fs.utimes

    • 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());
  • fs.watch

    • 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); });
  • fs.writeFile

    • Typescript implementation: async (path: string|Array<string>, data: any, options?: Encoding)
    • Usage example: let [res, err] = await fs.writeFile('file.txt', 'my text', 'utf8');

The Constants

  • fs.constants

  • fs.__dirname

    • 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));
  • fs.__filename

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

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