2.0.0-2 • Published 5 years ago

prepare-write v2.0.0-2

Weekly downloads
417
License
ISC
Repository
github
Last release
5 years ago

prepare-write

npm version Build Status Coverage Status

Prepare for writing a file to the given path – create ancestor directories and verify no directory exists in the path

const {existsSync} = require('fs');
const prepareWrite = require('prepare-write');

(async () => {
  existsSync('dir0'); //=> false

  await prepareWrite('dir0/dir1/dir2/file.txt');

  existsSync('dir0'); //=> true
  existsSync('dir0/dir1'); //=> true
  existsSync('dir0/dir1/dir2'); //=> true
  existsSync('dir0/dir1dir2/file.txt'); //=> false
})();

Installation

Use npm.

npm install prepare-write

API

const prepareWrite = require('prepare-write');

prepareWrite(path)

path: string (directory path)
Return: Promise<string | null>

It ensures you can soon write a file to the given path by:

  1. Creating ancestor directories if they don't exist
  2. Checking if no directory already exists in the path
(async () => {
  // a directory /foo doesn't exist

  await prepareWrite('/foo/bar/baz');

  // a directory /foo/bar now exists

  await prepareWrite('/foo/bar');
  // Error: Tried to create a file as /foo/bar, but a directory with the same name already exists.
})();

License

ISC License © 2017 - 2019 Watanabe Shinnosuke

2.0.0-2

5 years ago

2.0.0-1

5 years ago

2.0.0-0

5 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.6.0

6 years ago

0.5.1

6 years ago

0.5.0

7 years ago

0.4.0

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago

0.0.1

7 years ago

0.0.0

7 years ago