diff --git a/src/tasks/create/index.ts b/src/tasks/create/index.ts index 65dcf90..154a5e9 100644 --- a/src/tasks/create/index.ts +++ b/src/tasks/create/index.ts @@ -6,4 +6,5 @@ export * from './mkdir'; export * from './move'; export * from './print'; export * from './raises'; +export * from './remove'; export * from './sleep'; diff --git a/src/tasks/create/remove.ts b/src/tasks/create/remove.ts new file mode 100644 index 0000000..4b885d0 --- /dev/null +++ b/src/tasks/create/remove.ts @@ -0,0 +1,43 @@ +import { Task, Context } from '../../definitions'; +import { getPaths, useSource } from '../../helpers/paths'; +import { isCancelled } from '../../utils'; +import { log } from './log'; +import { into } from 'pipettes'; +import fs from 'fs-extra'; + +export interface RemoveOptions { + glob?: boolean; + strict?: boolean; + recursive?: boolean; +} + +export function remove( + paths: string | string[], + options?: RemoveOptions +): Task.Async { + return async (ctx: Context): Promise => { + into(ctx, log('debug', 'Remove:', paths)); + + const opts = Object.assign( + { glob: false, strict: false, exists: 'error' }, + options + ); + const sources = await getPaths(paths, ctx, { + glob: opts.glob, + strict: opts.strict + }); + + for (const source of sources) { + if (await isCancelled(ctx)) return; + + await useSource(source, ctx, { strict: opts.strict }, (source) => { + return opts.recursive + ? fs.remove(source) + : fs + .stat(source) + .then((x) => x.isDirectory()) + .then((is) => (is ? fs.rmdir(source) : fs.unlink(source))); + }); + } + }; +}