-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
copy.js
62 lines (46 loc) · 1.66 KB
/
copy.js
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
import fs from 'fs';
import path from 'path';
import fsExtra from 'fs-extra';
import cpy from 'cpy';
import isGlob from 'is-glob';
import pExec from '../utils/p-exec';
const fsExtraDefaultOptions = {
preserveTimestamps: true,
};
const copy = async (task, { logger }) => {
const { source, absoluteSource, destination, absoluteDestination, context, toType } = task;
logger.log(`copying from ${source} to ${destination}`);
if (isGlob(source)) {
const src = path.posix.join(context, source);
await cpy(src, absoluteDestination);
} else {
const isSourceFile = fs.lstatSync(absoluteSource).isFile();
// if source is a file and target is a directory
// create the directory and copy the file into that directory
if (isSourceFile && toType === 'dir') {
await fsExtra.ensureDir(absoluteDestination);
const sourceFileName = path.basename(absoluteSource);
const filePath = path.resolve(absoluteDestination, sourceFileName);
await fsExtra.copy(absoluteSource, filePath, fsExtraDefaultOptions);
return;
}
await fsExtra.copy(absoluteSource, absoluteDestination, fsExtraDefaultOptions);
}
logger.info(`copied "${source}" to "${destination}`);
};
const copyAction = async (tasks, options) => {
const { runTasksInSeries, logger } = options;
const taskOptions = {
logger,
};
logger.debug(`processing copy tasks. tasks: ${tasks}`);
await pExec(runTasksInSeries, tasks, async (task) => {
try {
await copy(task, taskOptions);
} catch (err) {
logger.error(`error while copying. task ${err}`);
}
});
logger.debug(`copy tasks complete. tasks: ${tasks}`);
};
export default copyAction;