UNPKG

1.81 kBJavaScriptView Raw
1"use strict";
2const fs = require("fs-extra");
3const glob_1 = require("glob");
4const _ = require("lodash");
5const path_1 = require("path");
6const addConfig_1 = require("../fns/add-cmd/addConfig");
7const cmdName_1 = require("../fns/cmdName");
8const flattenGlob_1 = require("../fns/flattenGlob");
9const command = cmdName_1.cmdName(__filename);
10const 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};
54module.exports = cmd;