Skip to content

Commit

Permalink
feat(exposed/fs, exposed/tags): adds mkdir; uses mkdir on ensure
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 28, 2019
1 parent 8706280 commit 6109bf8
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/exposed/fs/index.ts
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
79 changes: 79 additions & 0 deletions src/exposed/fs/mkdir.ts
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('", "')}"`);
};
}
13 changes: 3 additions & 10 deletions src/exposed/tags/ensure.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import fs from 'fs-extra';
import core from '~/core';
import asTag from '~/utils/as-tag';
import { rejects } from 'errorish';
import expose, { TExposedOverload } from '~/utils/expose';
import { absolute } from '~/utils/file';
import mkdir from '../fs/mkdir';

export default expose(ensure) as TExposedOverload<
typeof ensure,
Expand All @@ -22,11 +19,7 @@ function ensure(
*/
function ensure(...args: any[]): () => Promise<void> {
return async () => {
const directory = absolute({
path: asTag(args.shift(), ...args),
cwd: await core.cwd()
});

await fs.ensureDir(directory).catch(rejects);
const path = asTag(args.shift(), ...args);
return mkdir.fn(path, { confirm: false, fail: false });
};
}

0 comments on commit 6109bf8

Please sign in to comment.