Skip to content

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

License

Notifications You must be signed in to change notification settings

shinnn/prepare-write

Repository files navigation

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published