shinnn / prepare-write

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

License:ISC License


Languages

Language:JavaScript 100.0%