Skip to content

Commit

Permalink
feat(tasks): adds move task
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Feb 17, 2021
1 parent 1a6da16 commit 192ed1c
Show file tree
Hide file tree
Showing 2 changed files with 48 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 @@ -2,6 +2,7 @@ export * from './clear';
export * from './copy';
export * from './exec';
export * from './log';
export * from './move';
export * from './print';
export * from './raises';
export * from './sleep';
47 changes: 47 additions & 0 deletions src/tasks/create/move.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Task, Context } from '../../definitions';
import { getPathPairs, usePair } from '../../helpers/paths';
import { isCancelled } from '../../utils';
import { log } from './log';
import { into } from 'pipettes';
import fs from 'fs-extra';

export interface MoveOptions {
glob?: boolean;
single?: boolean;
strict?: boolean;
exists?: 'error' | 'ignore' | 'overwrite';
}

export function move(
paths: string | string[],
destination: string,
options?: MoveOptions
): Task.Async {
return async (ctx: Context): Promise<void> => {
into(ctx, log('debug', 'Move', paths, 'to', destination));

const opts = Object.assign(
{ glob: false, single: false, strict: false, exists: 'error' },
options
);
const pairs = await getPathPairs(paths, destination, ctx, {
glob: opts.glob,
single: opts.single,
strict: opts.strict
});

for (const pair of pairs) {
if (await isCancelled(ctx)) return;
await usePair(
pair,
ctx,
{ strict: opts.strict, exists: opts.exists },
([src, dest]) => {
return fs.move(src, dest, {
overwrite: opts.exists === 'overwrite'
});
}
);
}
};
}

0 comments on commit 192ed1c

Please sign in to comment.