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

@danielcobo/fs

v1.2.2

Published

Write, read, clone and remove files and folders.

Downloads

8

Readme

fs

Write, read, clone and remove files and folders without much thinking.

The built-in NodeJS fs module is often too broad and low-level. Unlike the native fs or other packages @danielcobo/fs comes with useful defaults and just a handful of methods to learn.

🧭 Table of contents

✨ Benefits

  • [x] Only a few methods to remember
  • [x] Uses async/await
  • [x] Includes tests
  • [x] MIT license

🎒 Requierments

To use this package you will need:

🚀 Quickstart

Install

npm install @danielcobo/fs

Note: In case you're wondering, @danielcobo/ is just a namespace scope - an NPM feature. Scopes make it easier to name modules and improve security.

Require the module

const fs = require('@danielcobo/fs');

Make

//Make a directory
await fs.mk('./some/folder');

//Make a file (including any missing path directories)
const path = './some/other/folder/helloworld.txt';
const content = '@danielcobo/fs is awesome!';
await fs.mk(path, content);

Note: we use the same method for directories and files. We can do that with all the methods. Less to remember, yay!

Read

//Read directory subpaths
const tree = await fs.read('./some');

console.log(tree);
/*
{
    dirs: [folder, other/folder],
    files:[other/folder/helloworld.txt]
}
*/

//Read a file
const path = './some/other/folder/helloworld.txt';
const content = await fs.read(path);

console.log(content); //@danielcobo/fs is awesome!

Clone (aka. copy & paste)

//Clone a directory and all it's contents
const original = './some/folder';
const clone = './some/clone/folder';
await fs.clone(original, clone);

//Clone a file
const originalFile = './some/other/folder/helloworld.txt';
const cloneFile = './some/other/clone/folder/helloworld.txt';
await fs.clone(originalFile, cloneFile);

Remove (aka. delete)

//Remove a directory and all it's contents
await fs.rm('./some/folder');

//Remove a file
await fs.rm('./some/other/folder/helloworld.txt');

For details see documentation below.

📘 Documentation

.mk()

Write a file or create a directory path.

| Param | Type | Default | Description | | --------- | -------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------- | | mkPath | string | | path to write to | | content | string | | file text content (ignored for directory) | | [options] | string | Object | 'utf8' | encoding, mode, flag, signal. See NodeJS docs. |

.make()

Alias of .mk()

.read()

Read file or directory.

| Param | Type | Default | Description | | --------- | ----------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------- | | readPath | string | Array.<string> | | path to read | | [options] | string | Object | 'utf8' | encoding, mode, flag, signal. See NodeJS docs. |

| Returns | Type | Description | | ------------------- | -------------------- | ------------------------------------------------------------------------------------------------ | | fileContent or tree | string | Tree | File text content or tree object containing paths within given tree/s. See Tree |

.clone()

Clone file or directory.

| Param | Type | Description | | -------------------------- | --------- | -------------------------------------- | | originalPath | string | original path | | destinationPath | string | clone path | | [options] | Object | Options object | | [options.overwrite=true] | boolean | If false do not overwrite existng files |

.rm()

Remove file or directory.

| Param | Type | Description | | ------ | -------- | ---------------------------------------------- | | rmPath | string | path to remove - can be file or directory path |

.remove()

Alias of .rm()

Tree : Object

Paths within a given tree

| Name | Type | Description | | ---------- | ---------------- | ------------------------------------ | | dirs | Array.<string> | paths of subdirectories | | files | Array.<string> | filepaths | | root | Array.<string> | path/s of tree/s being read | | prunedTree | prunedTree | see prunedTree |

PrunedTree : Object

Tree subpaths without root path

| Name | Type | Description | | ----- | ---------------- | ----------------------------------------- | | dirs | Array.<string> | paths of subdirectories without root path | | files | Array.<string> | filepaths without root path |

🆘 Troubleshooting

Remember await must be used inside an async function unless:

  • you add "type":"module" to your project's package.json.
  • change the file extension to .mjs
  • wrap any code using await into an asynchronous immediately invoked function expression (my gosh that's a mouthful..). Example:
(async function () {
  //Code using await goes here
})();

The reason for these acrobatics is top-level await works in ECMAScript modules. However, NodeJS uses CommonJS modules by default. Adding "type":"module" to your project's package.json makes NodeJS parse all your .js files as ECMAScript modules. If you want it to only parse individual files as ECMAScript modules, you can do so by giving them an .mjs extension.

🤝 Contributing

Anyone can contribute

Contributions come in many shapes and sizes. All are welcome. You can contribute by:

  • asking questions
  • suggesting features
  • sharing this repo with friends
  • improving documentation (even fixing typos counts 😉)
  • providing tutorials (if you do, please let me know, I would love to read them)
  • improving tests
  • contributing code (new features, performance boosts, code readability improvements..)

Rules for contributions

General guidelines:

  • there are no dumb questions
  • be polite and respectful to others
  • do good

When coding remember:

  • working > maintainability > performance
  • best code is no code
  • be descriptive when naming
  • keep it DRY
  • do test

Contribution licence: All contributions are considered to be under same license as this repository.

🧪 Testing

Testing suite: 🃏 Jest | Test command: npm test

Mutation testing suite: 👽 Stryker Mutator | Mutation test command: npm run mutation

If you intend to develop further or contribute code, then please ensure to write and use testing. Strive for 100% code coverage and high mutation scores. Mutation score 100 is great, but it's not always neccessary (if there are valid reasons).

⚖️ License

MIT License