prepare-write

2.0.0-2 • Public • Published

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

Package Sidebar

Install

npm i prepare-write

Weekly Downloads

7

Version

2.0.0-2

License

ISC

Unpacked Size

4.59 kB

Total Files

4

Last publish

Collaborators

  • shinnn