-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy.ts
177 lines (160 loc) · 6 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import chalk from 'chalk';
const { red } = chalk;
import { copy, CopyOptions } from 'fs-extra';
import { env } from 'process';
import * as logger from '../logger.js';
const quietDefault = env.FSE_CLI_QUIET && env.FSE_CLI_QUIET === 'true';
const copyDef = {
name: 'copy',
spec: {
'--all': Boolean,
'--keepExisting': Boolean,
'--errorOnExist': Boolean,
'--dereference': Boolean,
'--preserveTimestamps': Boolean,
'--quiet': Boolean,
'-a': '--all',
'-k': '--keepExisting',
'-e': '--errorOnExist',
'-d': '--dereference',
'-p': '--preserveTimestamps',
'-q': '--quiet'
},
'default': {
keepExisting: false,
errorOnExist: false,
dereference: false,
preserveTimestamps: false,
quiet: quietDefault
},
options: (args: { _: unknown[], [key: string]: unknown }): Record<string, unknown> => ({
askAll: args['--all'] || false,
keepExisting: args['--keepExisting'] || copyDef.default.keepExisting,
errorOnExist: args['--errorOnExist'] || copyDef.default.errorOnExist,
dereference: args['--dereference'] || copyDef.default.dereference,
preserveTimestamps: args['--preserveTimestamps'] || copyDef.default.preserveTimestamps,
quiet: args['--quiet'] || copyDef.default.quiet,
src: args._[0], // TODO a list of directories to put in the same destination?
dest: args._[1]
}),
questions: (options: { [_: string]: unknown }): Record<string, unknown>[] => {
const questions: Record<string, unknown>[] = [];
if (!options.src) {
questions.push({
type: 'input',
name: 'src',
message: "Please fill in the source to copy",
validate: (input: string) => (input && input.trim()) ? true : "A source file or directory is required"
});
}
if (!options.dest) {
questions.push({
type: 'input',
name: 'dest',
message: "Please fill in the destination of the copy",
validate: (input: string) => (input && input.trim()) ? true : "A destination directory is required"
});
}
if (!options.askAll) { return questions; }
if (!options.keepExisting) {
questions.push({
type: 'confirm',
name: 'keepExisting',
message: 'Keep existing files?',
default: copyDef.default.keepExisting
});
}
if (!options.errorOnExist) {
questions.push({
type: 'confirm',
name: 'errorOnExist',
when: (answers: { keepExisting: boolean }) => !answers.keepExisting,
message: 'When the destination exists, throw an error?',
default: copyDef.default.errorOnExist
});
}
if (!options.dereference) {
questions.push({
type: 'confirm',
name: 'dereference',
message: 'Dereference symlinks?',
default: copyDef.default.dereference
});
}
if (!options.preserveTimestamps) {
questions.push({
type: 'confirm',
name: 'preserveTimestamps',
message: 'Keep last modification and access times?',
default: copyDef.default.preserveTimestamps
});
}
if (!options.quiet) {
questions.push({
type: 'confirm',
name: 'quiet',
message: 'Toggle to quiet mode?',
default: copyDef.default.quiet
});
}
return questions;
}
};
export const def = copyDef;
interface CliCopyOptions extends CopyOptions {
askAll?: boolean;
keepExisting?: boolean;
quiet?: boolean;
}
/**
* Wrapper for node-fs-extra copy function.
* https://github.com/jprichardson/node-fs-extra/blob/master/docs/copy.md
*/
export function job({ src, dest, ...copyOptions }:
{ src: string; dest: string; copyOptions: { [_: string]: unknown } }): void {
const otherOptions = copyOptions as CliCopyOptions;
const showAll = otherOptions.askAll;
const quiet = otherOptions.quiet;
otherOptions.overwrite = !otherOptions.keepExisting;
delete otherOptions.keepExisting;
delete otherOptions.askAll;
delete otherOptions.quiet;
function info(message: string, ...params: unknown[]) {
logger.info(message, { quiet, params });
}
function error(message: string, ...params: unknown[]) {
logger.error(message, { quiet, params });
}
info(`Copying file or directory ... from '${src}' to '${dest}'${showAll ? " with options: " : "."}`);
if (showAll) {
Object.entries(otherOptions).forEach(o => {
const key = o[0];
const value = JSON.stringify(o[1]);
info(`- ${key}: ${value}`);
});
}
function mainMessageFromError(err: Error | string | { code: string; syscall: string; path: string }): string {
const msg = err.toString();
const groups = /^\s*Error\s*:\s*(.*?\s+already\s+exists\s*)$/.exec(msg);
if (!groups) {
// only if under Linux
// TODO what about other os?
const linuxError = err as { code: string; syscall: string; path: string };
if (linuxError.code === 'EISDIR' && linuxError.syscall === 'unlink') {
return `it seems you're trying to copy a file to the directory '${linuxError.path}'`
+ ', which is not allowed';
}
return undefined as unknown as string;
}
return groups[1];
}
copy(src, dest, otherOptions, err => {
if (err) {
const mainMsg = mainMessageFromError(err) || err;
error(`${red.bold('ERROR')} thrown while copying file or directory: `, mainMsg);
return;
}
info('Copy complete...');
return;
});
}