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

@ygor/file

v5.4.1

Published

A transformable file object.

Downloads

124

Readme

@ygor/file

NPM version Downloads Build Status Coverage Status

A no-frills file object. Built on promises to work wonderfully with async and await in Node.js 8 and above. Part of the Ygor toolkit.

Install

$ npm install --save @ygor/file

Usage

const file = require('@ygor/file');

const foo = file({ path: 'foo/bar.txt' });

console.log(foo.absolute);
// -> /home/jdoe/foo/bar.txt

// Read the contents of the file at the given path.
await foo.read();

// Modify the contents in memory.
file.contents = file.contents.toUpperCase();

// Write the new contents to the file system.
await foo.write();

API

file([options]): File

  • options {Object}
  • options.cwd {String} - Current working directory. (default: process.cwd())
  • options.path {String} - Path to file relative to .cwd. (default: undefined)
  • options.contents {String|Buffer} - File contents. (default: undefined)
  • options.[custom] {any} - Any additional properties will be copied to the returned File object.

Creates a File object.

File Methods

.delete(): Promise<File>

Unlinks the file on the file system. The contents remain in memory if they were set or read.

.read([options]): Promise<File>

  • options {Object} - Same as fs.readFile options with the following change.
  • options.encoding {String|null} - File encoding defaults to utf8. You must specify null for binary files. (default: 'utf8')

Reads the file contents from the file system into the .contents value.

.stat(): Promise<Stats>

Returns an instance of the Node.js fs.Stats class.

.write([options]): Promise<File>

  • options {Object} - Same as fs.writeFile options with the following addition.
  • options.cwd {String} Alternate base path for the write; can be relative to the existing .cwd or an absolute path.

Writes the .contents value as the file contents to the file system.

.toJSON(): Object

Returns .cwd, .path, and .contents as a plain object.

.toString(): String

Returns a console.log-friendly representation of the file.

File Properties

.contents {String|Buffer|undefined}

The contents of the file.

.absolute {String}

The absolute path to the file. Read only.

const foo = file({ path: 'foo/bar.txt' });

console.log(foo.absolute);
// -> /home/jdoe/foo/bar.txt

.cwd {String}

The current working directory of the file. (default process.cwd())

const foo = file({ path: 'foo/bar.txt' });

console.log(foo.absolute);
// -> /home/jdoe/foo/bar.txt

console.log(foo.cwd);
// -> /home/jdoe

foo.cwd = '/Users/jappleseed';

console.log(foo.absolute);
// -> /Users/jappleseed/foo/bar.txt

.path {String}

The path of the file relative to .cwd.

const foo = file({ path: 'foo/bar.txt' });

console.log(foo.absolute);
// -> /home/jdoe/foo/bar.txt

console.log(foo.path);
// -> foo/bar.txt

foo.path = 'baz/qux.txt';

console.log(foo.absolute);
// -> /home/jdoe/baz/qux.txt

.dirname {String}

The directory portion of .path.

const foo = file({ path: 'foo/bar.txt' });

console.log(foo.absolute);
// -> /home/jdoe/foo/bar.txt

console.log(foo.dirname);
// -> foo

foo.dirname = 'baz/qux';

console.log(foo.absolute);
// -> /home/jdoe/baz/qux/bar.txt

.basename {String}

The file portion of .path.

const foo = file({ path: 'foo/bar.txt' });

console.log(foo.absolute);
// -> /home/jdoe/foo/bar.txt

console.log(foo.basename);
// -> bar.txt

foo.basename = 'qux.txt';

console.log(foo.absolute);
// -> /home/jdoe/foo/qux.txt

.stem {String}

The file portion of .path without the extension.

const foo = file({ path: 'foo/bar.txt' });

console.log(foo.absolute);
// -> /home/jdoe/foo/bar.txt

console.log(foo.stem);
// -> bar

foo.stem = 'qux';

console.log(foo.absolute);
// -> /home/jdoe/foo/qux.txt

.extname {String}

The file extension portion of .path.

const foo = file({ path: 'foo/bar.txt' });

console.log(foo.absolute);
// -> /home/jdoe/foo/bar.txt

console.log(foo.extname);
// -> .txt

foo.extname = '.html';

console.log(foo.absolute);
// -> /home/jdoe/foo/bar.html

.history {Array<String>}

The history of .path changes. Generally best treated as read-only.

const foo = file({ path: 'foo/bar.txt' });

console.log(foo.history);
// -> ['foo/bar.txt']

foo.stem = 'qux';

console.log(foo.history);
// -> ['foo/bar.txt', 'foo/qux.txt']


MIT © Shannon Moeller