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 })();
npm install prepare-write
const prepareWrite = require('prepare-write');
path: string (directory path)
Return: Promise<string | null>
It ensures you can soon write a file to the given path by:
- Creating ancestor directories if they don't exist
- 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. })();
ISC License © 2017 - 2019 Watanabe Shinnosuke