Coder Social home page Coder Social logo

shinnn / prepare-write Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 150 KB

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

License: ISC License

JavaScript 100.00%
write prepare promise javascript nodejs filesystem mkdir mkdirp check

prepare-write's Introduction

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

prepare-write's People

Contributors

shinnn avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.