npm

Need private packages and team management tools?Check out npm Orgs. »

@fcostarodrigo/open-path

5.0.1 • Public • Published

OPEN-PATH

Build Status Maintainability codebeat badge codecov

Node module that creates missing folders in the middle of a path, like mkdir -p.

Description

Let's say you want to create the file docs/UI/button.txt, but the folders UI and docs don't exist.

If you just try to write to docs/UI/button.txt, this will happen:

> fs.writeFileSync('docs/UI/button.txt', 'test')
Thrown:
{ Error: ENOENT: no such file or directory, open 'docs/UI/button.txt'
    at Object.openSync (fs.js:448:3)
    at Object.writeFileSync (fs.js:1210:35)
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path: 'docs/UI/button.txt' }

If you try to create the folder you will get this:

> fs.mkdirSync('docs/UI')
Thrown:
{ Error: ENOENT: no such file or directory, mkdir 'docs/UI'
    at Object.mkdirSync (fs.js:773:3) errno: -2, syscall: 'mkdir', code: 'ENOENT', path: 'docs/UI' }
 

Using this library you can create the inner folders easily:

const openPath = require("@fcostarodrigo/open-path");
 
async function main() {
  await openPath("docs/UI/button.txt", true);
  fs.writeFileSync("docs/UI/button.txt", "test");
}
 
main();

Installation

npm install @fcostarodrigo/open-path

Usage

const openPath = require("@fcostarodrigo/open-path");
 
async function main() {
  await openPath("docs/UI");
  fs.writeFileSync("docs/UI/button.txt", "test");
}
 
main();

CLI

Global

npm install -g @fcostarodrigo/open-path
open-path --help

Local

npm install @fcostarodrigo/open-path
./node_modules/.bin/open-path --help

If installed locally, command open-path is available in scripts in package.json.

Documentation

function openPath(pathToOpen: string, fileInPath?: boolean): Promise<void>;
  • pathToOpen: String with the path.
  • fileInPath: Indicates if the last item of the path is a file.
  • promise: Resolves to nothing after the directories are created or rejects with an error.

Development

Full tests with coverage

npm test

Unit tests and watch for changes

npm run unit-test

License

MIT License

install

npm i @fcostarodrigo/open-path

Downloadsweekly downloads

4

version

5.0.1

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability