Skip to content

Commit

Permalink
feat(tasks): adds remove task
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Feb 18, 2021
1 parent b560772 commit fb53a87
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/tasks/create/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export * from './mkdir';
export * from './move';
export * from './print';
export * from './raises';
export * from './remove';
export * from './sleep';
43 changes: 43 additions & 0 deletions src/tasks/create/remove.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
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)));
});
}
};
}

0 comments on commit fb53a87

Please sign in to comment.