@wrote/ensure-path
Advanced tools
Comparing version 1.0.8 to 1.1.0
@@ -1,11 +0,11 @@ | ||
const { mkdir } = require('fs'); | ||
let makePromise = require('makepromise'); if (makePromise && makePromise.__esModule) makePromise = makePromise.default; | ||
const { mkdir, mkdirSync } = require('fs'); | ||
const makePromise = require('makepromise'); | ||
const { dirname } = require('path'); | ||
/** | ||
* Makes sure that a file can be created by creating all directories to which it belongs, e.g., `ensurePath('~/path/to/wrote.data')` will attempt to create `~/path/to` directory recursively. | ||
* @param {string} path Path to the file | ||
* @throws {Error} When the first folder in the path is non-executable | ||
* Makes sure that a file can be created by creating all directories to which it belongs to, e.g., `ensurePath('~/path/to/wrote.data')` will attempt to create `~/path/to` directory recursively. | ||
* @param {string} path The path to the file. | ||
* @throws {Error} When the first folder in the path is non-executable. | ||
*/ | ||
async function ensurePath(path) { | ||
async function ensurePath(path) { | ||
const dir = dirname(path) | ||
@@ -41,3 +41,39 @@ try { | ||
/** | ||
* Makes sure that a file can be created by creating all directories to which it belongs to synchronously, e.g., `ensurePath('~/path/to/wrote.data')` will attempt to create `~/path/to` directory recursively. | ||
* @param {string} path The path to the file. | ||
* @throws {Error} When the first folder in the path is non-executable. | ||
*/ | ||
function ensurePathSync(path) { | ||
const dir = dirname(path) | ||
try { | ||
makeSync(dir) | ||
return path | ||
} catch (err) { | ||
if (/EEXIST/.test(err.message) && err.message.indexOf(dir) != -1) { | ||
return path | ||
} | ||
throw err | ||
} | ||
} | ||
module.exports = ensurePath | ||
/** | ||
* Recursive promise-based mkdir. | ||
* @param {string} dir Path to the directory to be created | ||
*/ | ||
function makeSync(dir) { | ||
try { | ||
mkdirSync(dir) | ||
} catch (err) { | ||
if (err.code == 'ENOENT') { | ||
const parentDir = dirname(dir) | ||
makeSync(parentDir) | ||
makeSync(dir) | ||
} else if (err.code != 'EEXIST') { // created in parallel | ||
throw err | ||
} | ||
} | ||
} | ||
module.exports = ensurePath | ||
module.exports.ensurePathSync = ensurePathSync |
@@ -0,1 +1,8 @@ | ||
## 3 July 2019 | ||
### [1.1.0](https://github.com/wrote/ensure-path/compare/v1.0.8...v1.1.0) | ||
- [feature] Add the `sync` version of the function. | ||
- [build] Compiler with newer _ÀLaMode_ for tidier code. | ||
## 18 April 2019 | ||
@@ -2,0 +9,0 @@ |
{ | ||
"name": "@wrote/ensure-path", | ||
"version": "1.0.8", | ||
"version": "1.1.0", | ||
"description": "Create all directories on the way to the path.", | ||
@@ -41,2 +41,4 @@ "main": "build/index.js", | ||
"file", | ||
"create", | ||
"mkdirp", | ||
"mkdir" | ||
@@ -51,9 +53,10 @@ ], | ||
"devDependencies": { | ||
"alamode": "^2.3.4", | ||
"catchment": "^3.3.0", | ||
"documentary": "^1.23.4", | ||
"documentary": "^1.27.4", | ||
"eslint-config-artdeco": "1.0.1", | ||
"spawncommand": "^2.1.2", | ||
"spawncommand": "^2.2.0", | ||
"temp-context": "^2.1.3", | ||
"yarn-s": "1.1.0", | ||
"zoroaster": "^3.11.6" | ||
"zoroaster": "^4.1.1-alpha" | ||
}, | ||
@@ -60,0 +63,0 @@ "dependencies": { |
@@ -8,5 +8,7 @@ # @wrote/ensure-path | ||
```sh | ||
yarn add -E @wrote/ensure-path | ||
yarn add @wrote/ensure-path | ||
``` | ||
<p align="center"><a href="#table-of-contents"><img src="/.documentary/section-breaks/0.svg?sanitize=true"></a></p> | ||
## Table Of Contents | ||
@@ -17,12 +19,17 @@ | ||
* [`async ensurePath(path: string): string`](#async-ensurepathpath-string-string) | ||
* [`ensurePathSync(path: string): string`](#ensurepathsyncpath-string-string) | ||
- [Copyright](#copyright) | ||
<p align="center"><a href="#table-of-contents"><img src="/.documentary/section-breaks/1.svg?sanitize=true"></a></p> | ||
## API | ||
The package is available by importing its default function: | ||
The package is available by importing its default and named function: | ||
```js | ||
import ensurePath from '@wrote/ensure-path' | ||
import ensurePath, { ensurePathSync } from '@wrote/ensure-path' | ||
``` | ||
<p align="center"><a href="#table-of-contents"><img src="/.documentary/section-breaks/2.svg?sanitize=true"></a></p> | ||
### `async ensurePath(`<br/> `path: string,`<br/>`): string` | ||
@@ -33,3 +40,2 @@ | ||
```js | ||
/* yarn example */ | ||
import { resolve } from 'path' | ||
@@ -49,6 +55,48 @@ import ensurePath from '@wrote/ensure-path' | ||
<p align="center"><a href="#table-of-contents"><img src="/.documentary/section-breaks/3.svg?sanitize=true"></a></p> | ||
### `ensurePathSync(`<br/> `path: string,`<br/>`): string` | ||
Same as `ensurePath`, but performed synchronously. | ||
```js | ||
import { resolve } from 'path' | ||
import { ensurePathSync } from '@wrote/ensure-path' | ||
const path = 'example/path/to/temp/file.data' | ||
ensurePathSync(path) | ||
// path/to/temp is created in the cwd | ||
const absolutePath = resolve('example/path/to/temp/file.data') | ||
ensurePathSync(absolutePath) | ||
// $(pwd)/path/to/temp/file.data is created | ||
``` | ||
<p align="center"><a href="#table-of-contents"><img src="/.documentary/section-breaks/4.svg?sanitize=true"></a></p> | ||
## Copyright | ||
(c) [Wrote][1] 2019 | ||
<table> | ||
<tr> | ||
<th> | ||
<a href="https://artd.eco"> | ||
<img src="https://raw.githubusercontent.com/wrote/wrote/master/images/artdeco.png" alt="Art Deco"> | ||
</a> | ||
</th> | ||
<th>© <a href="https://artd.eco">Art Deco</a> for <a href="https://wrote.cc">Wrote</a> 2019</th> | ||
<th> | ||
<a href="https://wrote.cc"> | ||
<img src="https://avatars3.githubusercontent.com/u/40831417?s=100" width="100" alt="Wrote"> | ||
</a> | ||
</th> | ||
<th> | ||
<a href="https://www.technation.sucks" title="Tech Nation Visa"> | ||
<img src="https://raw.githubusercontent.com/artdecoweb/www.technation.sucks/master/anim.gif" | ||
alt="Tech Nation Visa"> | ||
</a> | ||
</th> | ||
<th><a href="https://www.technation.sucks">Tech Nation Visa Sucks</a></th> | ||
</tr> | ||
</table> | ||
[1]: https://wrote.cc | ||
<p align="center"><a href="#table-of-contents"><img src="/.documentary/section-breaks/-1.svg?sanitize=true"></a></p> |
@@ -1,2 +0,2 @@ | ||
import { mkdir } from 'fs' | ||
import { mkdir, mkdirSync } from 'fs' | ||
import makePromise from 'makepromise' | ||
@@ -6,5 +6,5 @@ import { dirname } from 'path' | ||
/** | ||
* Makes sure that a file can be created by creating all directories to which it belongs, e.g., `ensurePath('~/path/to/wrote.data')` will attempt to create `~/path/to` directory recursively. | ||
* @param {string} path Path to the file | ||
* @throws {Error} When the first folder in the path is non-executable | ||
* Makes sure that a file can be created by creating all directories to which it belongs to, e.g., `ensurePath('~/path/to/wrote.data')` will attempt to create `~/path/to` directory recursively. | ||
* @param {string} path The path to the file. | ||
* @throws {Error} When the first folder in the path is non-executable. | ||
*/ | ||
@@ -41,1 +41,37 @@ export default async function ensurePath(path) { | ||
} | ||
/** | ||
* Makes sure that a file can be created by creating all directories to which it belongs to synchronously, e.g., `ensurePath('~/path/to/wrote.data')` will attempt to create `~/path/to` directory recursively. | ||
* @param {string} path The path to the file. | ||
* @throws {Error} When the first folder in the path is non-executable. | ||
*/ | ||
export function ensurePathSync(path) { | ||
const dir = dirname(path) | ||
try { | ||
makeSync(dir) | ||
return path | ||
} catch (err) { | ||
if (/EEXIST/.test(err.message) && err.message.indexOf(dir) != -1) { | ||
return path | ||
} | ||
throw err | ||
} | ||
} | ||
/** | ||
* Recursive promise-based mkdir. | ||
* @param {string} dir Path to the directory to be created | ||
*/ | ||
function makeSync(dir) { | ||
try { | ||
mkdirSync(dir) | ||
} catch (err) { | ||
if (err.code == 'ENOENT') { | ||
const parentDir = dirname(dir) | ||
makeSync(parentDir) | ||
makeSync(dir) | ||
} else if (err.code != 'EEXIST') { // created in parallel | ||
throw err | ||
} | ||
} | ||
} |
11079
142
98
8