-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathcli.js
executable file
·86 lines (78 loc) · 2.18 KB
/
cli.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env node
import process from 'node:process';
import os from 'node:os';
import meow from 'meow';
import cpy from 'cpy';
const cli = meow(`
Usage
$ cpy <source …> <destination>
Options
--no-overwrite Don't overwrite the destination
--cwd=<dir> Working directory for files
--rename=<filename> Rename all <source> filenames to <filename>. Supports string templates.
--dot Allow patterns to match entries that begin with a period (.)
--flat Flatten directory structure. All copied files will be put in the same directory.
--concurrency Number of files being copied concurrently
<source> can contain globs if quoted
Examples
Copy all .png files in src folder into dist except src/goat.png
$ cpy 'src/*.png' '!src/goat.png' dist
Copy all files inside src folder into dist and preserve path structure
$ cpy . '../dist/' --cwd=src
Copy all .png files in the src folder to dist and prefix the image filenames
$ cpy 'src/*.png' dist --cwd=src --rename=hi-{{basename}}
`, {
importMeta: import.meta,
flags: {
overwrite: {
type: 'boolean',
default: true,
},
cwd: {
type: 'string',
default: process.cwd(),
},
rename: {
type: 'string',
},
dot: {
type: 'boolean',
default: false,
},
flat: {
type: 'boolean',
default: false,
},
concurrency: {
type: 'number',
default: (os.cpus().length > 0 ? os.cpus().length : 1) * 2,
},
},
});
try {
const {rename} = cli.flags;
const stringTemplate = '{{basename}}';
if (rename?.includes(stringTemplate)) {
cli.flags.rename = basename => {
const parts = basename.split('.');
const fileExtension = parts.length > 1 ? `.${parts.pop()}` : '';
const nameWithoutExtension = parts.join('.');
return rename.replace(stringTemplate, nameWithoutExtension) + fileExtension;
};
}
await cpy(cli.input, cli.input.pop(), {
cwd: cli.flags.cwd,
rename: cli.flags.rename,
overwrite: cli.flags.overwrite,
dot: cli.flags.dot,
flat: cli.flags.flat,
concurrency: cli.flags.concurrency,
});
} catch (error) {
if (error.name === 'CpyError') {
console.error(error.message);
process.exit(1);
} else {
throw error;
}
}