-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(exposed/fs, exposed/tags): adds mkdir; uses mkdir on ensure
- Loading branch information
Showing
3 changed files
with
84 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export { default as json } from './json'; | ||
export { default as remove } from './remove'; | ||
export { default as mkdir } from './mkdir'; | ||
export { default as rw } from './rw'; | ||
export { default as write } from './write'; | ||
|
||
// TODO: copy, mkdir, move | ||
// TODO: copy, move |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import path from 'path'; | ||
import fs from 'fs-extra'; | ||
import { rejects } from 'errorish'; | ||
import core from '~/core'; | ||
import { absolute, exists } from '~/utils/file'; | ||
import confirm from '../prompts/confirm'; | ||
import { parallel } from 'promist'; | ||
import logger from '~/utils/logger'; | ||
import chalk from 'chalk'; | ||
import expose from '~/utils/expose'; | ||
import { IFsReadOptions } from './types'; | ||
|
||
export default expose(mkdir); | ||
/** | ||
* Deep creates a directory or an array of them. | ||
* It is an *exposed* function: call `mkdir.fn()`, which takes the same arguments, in order to execute on call. | ||
* @param paths a path for a directory, or an array of them. | ||
* @param options an `IFsReadOptions` object. In this case, if `fail` is true, `mkdir` will fail if one of the directories already exists. | ||
* @returns An asynchronous function -hence, calling `mkdir` won't have any effect until the returned function is called. | ||
*/ | ||
function mkdir( | ||
paths: string | string[], | ||
options: IFsReadOptions = {} | ||
): () => Promise<void> { | ||
return async () => { | ||
options = Object.assign({ confirm: false, fail: true }, options); | ||
|
||
const cwd = await core.cwd(); | ||
paths = Array.isArray(paths) ? paths : [paths]; | ||
paths = paths.map((path) => absolute({ path, cwd })); | ||
|
||
const existingPaths = await parallel.filter(paths, (path) => exists(path)); | ||
const nonExistingPaths = paths.filter( | ||
(path) => !existingPaths.includes(path) | ||
); | ||
const relatives = { | ||
existing: existingPaths.map((x) => path.relative(cwd, x)), | ||
nonExisting: nonExistingPaths.map((x) => path.relative(cwd, x)) | ||
}; | ||
|
||
if (options.fail && existingPaths.length) { | ||
throw Error(`Directory already exists: ${relatives.existing[0]}`); | ||
} | ||
|
||
// eslint-disable-next-line no-console | ||
(options.confirm ? console.log : logger.debug)( | ||
chalk.bold.yellow( | ||
relatives.existing.length | ||
? 'Directories to create' | ||
: 'No directories to create' | ||
) + | ||
(relatives.existing.length | ||
? `\n Existing paths: "${relatives.existing.join('", "')}"` | ||
: '') + | ||
(relatives.nonExisting.length | ||
? `\n Non existing paths: "${relatives.nonExisting.join('", "')}"` | ||
: '') | ||
); | ||
|
||
if (!nonExistingPaths.length) return; | ||
if (options.confirm) { | ||
const action = await confirm.fn({ no: false }).then((x) => x !== false); | ||
|
||
if (!action) { | ||
if (options.fail) throw Error(`Cancelled by user`); | ||
else return; | ||
} | ||
} | ||
|
||
await parallel.each(nonExistingPaths, async (absolute, i) => { | ||
await fs.ensureDir(absolute).catch(rejects); | ||
|
||
const relative = relatives.nonExisting[i]; | ||
logger.debug(`Created: ${relative}`); | ||
}); | ||
|
||
logger.info(`Created: "${relatives.nonExisting.join('", "')}"`); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters