From 192ed1c63725cac3b9aab7afbc043f25505d2cd0 Mon Sep 17 00:00:00 2001 From: Rafa Mel Date: Wed, 17 Feb 2021 14:10:57 +0100 Subject: [PATCH] feat(tasks): adds move task --- src/tasks/create/index.ts | 1 + src/tasks/create/move.ts | 47 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/tasks/create/move.ts diff --git a/src/tasks/create/index.ts b/src/tasks/create/index.ts index 3ad2254..52b6ca5 100644 --- a/src/tasks/create/index.ts +++ b/src/tasks/create/index.ts @@ -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'; diff --git a/src/tasks/create/move.ts b/src/tasks/create/move.ts new file mode 100644 index 0000000..14ddd9e --- /dev/null +++ b/src/tasks/create/move.ts @@ -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 => { + 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' + }); + } + ); + } + }; +}