1 | "use strict";
|
2 | const fs = require("fs-extra");
|
3 | const glob_1 = require("glob");
|
4 | const _ = require("lodash");
|
5 | const path_1 = require("path");
|
6 | const addConfig_1 = require("../fns/add-cmd/addConfig");
|
7 | const cmdName_1 = require("../fns/cmdName");
|
8 | const flattenGlob_1 = require("../fns/flattenGlob");
|
9 | const command = cmdName_1.cmdName(__filename);
|
10 | const cmd = {
|
11 | builder(argv) {
|
12 | return addConfig_1.addConfig(argv, command)
|
13 | .option('from', {
|
14 | alias: 'f',
|
15 | array: true,
|
16 | demandOption: true,
|
17 | describe: 'Glob(s) to copy'
|
18 | })
|
19 | .option('to', {
|
20 | alias: 't',
|
21 | array: true,
|
22 | demandOption: true,
|
23 | describe: 'Dir(s) to copy to'
|
24 | });
|
25 | },
|
26 | command,
|
27 | describe: 'Copy files from point A to point B',
|
28 | handler(c) {
|
29 | if (!c.from.length || !c.to.length) {
|
30 | throw new Error('At least one from/to path is required');
|
31 | }
|
32 | if (c.to.length !== 1 && c.to.length !== c.from.length) {
|
33 | throw new Error('"to" must either contain 1 path or have the same number of paths as "from"');
|
34 | }
|
35 | const opts = { cwd: process.cwd() };
|
36 | const fromTos = c.from
|
37 | .map((p, idx) => {
|
38 | return glob_1.sync(p, opts)
|
39 | .map(from => {
|
40 | return { from, to: path_1.join(c.to[idx] || c.to[0], path_1.basename(from)) };
|
41 | });
|
42 | })
|
43 | .reduce(flattenGlob_1.flattenGlob, []);
|
44 | _(fromTos)
|
45 | .map('to')
|
46 | .map(path_1.dirname)
|
47 | .uniq()
|
48 | .forEach(dir => fs.mkdirpSync(dir));
|
49 | for (const ft of fromTos) {
|
50 | fs.copySync(ft.from, ft.to, { dereference: true, preserveTimestamps: true });
|
51 | }
|
52 | }
|
53 | };
|
54 | module.exports = cmd;
|