/
copy.ts
63 lines (58 loc) · 1.79 KB
/
copy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { shallow } from 'merge-strategies';
import fs from 'fs-extra';
import { Task, Context } from '../../definitions';
import { getPathPairs, usePair } from '../../helpers/paths';
import { isCancelled } from '../../utils/is-cancelled';
import { series } from '../aggregate/series';
import { log } from '../stdio/log';
export interface CopyOptions {
/** Parse globs in paths */
glob?: boolean;
/** Whether to treat the destination as an exact path for a single source */
single?: boolean;
/** Disallows non existent paths and an empty set of paths */
strict?: boolean;
/** Whether to error, ignore, or overwrite existing files */
exists?: 'error' | 'ignore' | 'overwrite';
}
/**
* Copies files or directories from `paths` to `destination`.
* Treats `destination` as a folder unless `options.single` is `true`.
* @returns Task
*/
export function copy(
paths: string | string[],
destination: string,
options?: CopyOptions
): Task.Async {
return series(
log('debug', 'Copy', paths, 'to', destination),
async (ctx: Context): Promise<void> => {
const opts = shallow(
{ glob: false, single: false, strict: false, exists: 'error' },
options || undefined
);
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.copy(src, dest, {
overwrite: true,
errorOnExist: true,
dereference: false,
preserveTimestamps: false
});
}
);
}
}
);
}