-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
275 lines (228 loc) · 6.26 KB
/
index.js
File metadata and controls
275 lines (228 loc) · 6.26 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
`use strict`;
import {
exec,
spawn,
} from 'child_process';
import {
parse,
basename,
} from 'path';
// IMPORTANT(jeff): We must require `dotenv/config` with the `-r` parameter
// to nodejs if we do not execute the following! The downside of doing this
// is that we also lose the ability to preset the environment on the same
// line as the direct run, i.e.: RC_VAR1=false node index.js
import dotenv from 'dotenv';
let useDotEnv = !!(process.env[`USE_DOTENV`]) || false;
if(useDotEnv == true) {
dotenv.config(`.env`);
}
let DEFAULT_FILTERED_FILES = [
`Thumbs.db`,
`.DS_Store`,
`._*`,
`*un~*`,
];
let DEFAULT_FILTERED_DIRS = [
`node_modules`
];
import {
DEFAULT_FILE_OPTIONS,
noop,
assign,
pathExists,
pathExistsSync,
readFile,
readFileSync,
where,
} from './src/utils.js';
function toBool(expr) {
return !!(expr);
}
function parseBool(state, options) {}
let modeLogFileAppend = toBool(process.env[`RC_LOGFILE_APPEND`]) || false;
let useLogFile = false;
let modeLogFile = process.env[`RC_LOGFILE`] || ``;
if(modeLogFile.length > 0) {
useLogFile = true;
}
let numThreads = parseInt(process.env[`RC_THREADS`],10) || 2;
let modeDry = toBool(process.env[`RC_DRY`]) || false;
let modeVerbose = toBool(process.env[`RC_VERBOSE`]) || false;
let modeDebug = toBool(process.env[`RC_DEBUG`]) || false;
import yargs from 'yargs';
function build_ignore_list(list) {
let result = [];
if(typeof list === `string`) {
let element = list.split(`\n`);
let noWordIdx = element.indexOf(``);
element.splice(noWordIdx, 1);
element.forEach((el) => {
if(pathExistsSync(el) == true) {
result.push(`/xd ${el}`);
} else if(el && typeof el === `string`) {
result.push(`/xf ${el}`);
}
});
} else if(typeof list === `array`) {
if(pathExistsSync(el) == true) {
result.push(`/xd ${el}`);
} else if(el && typeof el === `string`) {
result.push(`/xf ${el}`);
}
}
return result;
}
let DEFAULT_PARAMS = [
`/copyall`, // requires root priv
`/e`, // copy empty paths
`/z`,
`/XA:SH`, // attributes
`/V`,
`/NP`, // no progress; performance boost
`/R:4`, // read tries
`/W:2`, // write tries
//`/MT:5`, // multi threads
//`/LOG+:${process.env['RC_LOG_FILE']}`, // open log file in APPEND mode
];
let ARGS_SEPERATOR = process.env[`RC_ARGS_SEPERATOR`] ||
`\n`;
let CRITICAL_ARRAY_EMPTY = (args) => {
return `The parameter ${args} must be a non-empty array object.`;
};
function execute(cmd, params = []) {
let args = assign([], params);
if(modeDry) {
console.debug(`${cmd} ${args.join(ARGS_SEPERATOR)}`);
}
const bat = spawn(cmd, args);
bat.stdout.on(`data`, (output) => {
if(output) {
console.log(`spawn output: ${output.toString()}`);
}
});
bat.stderr.on(`data`, (output) => {
console.error(output.toString());
});
bat.on(`exit`, (code) => {
let exitCode = code || -255;
console.debug(`The process has exited with the status code of ${exitCode}.`);
});
if(args.length < 1) {
return args;
}
return args.join(ARGS_SEPERATOR);
}
function usage_help() {
const SCRIPT_NAME = basename(process.argv[0]);
console.log(`${SCRIPT_NAME} - a source and destination path must be given as your input.`);
}
function execute_robocopy(params = []) {
let result = null;
let cmd = params && params.length > 0 && params[0] ||
`robocopy.exe`;
let args = assign(DEFAULT_PARAMS, params);
if(!args || args.length < 1) {
const errMsg = CRITICAL_EMPTY_ARRAY(args);
return console.error(errMsg);
}
if(modeDebug) {
console.debug(args);
}
result = execute(cmd, args);
return(result);
}
async function main(argc = 0, argv = []) {
// let args = [];
let filter = [];
let args = argv;
let ignoreListFile = `ignore.conf`;
if(pathExistsSync(ignoreListFile) == false) {
console.log(`ignore.conf was not found.`);
filter = [];
DEFAULT_FILTERED_FILES.forEach((el) => {
if(el) {
filter.push(el);
}
});
DEFAULT_FILTERED_DIRS.forEach((el) => {
if(el) {
filter.push(el);
}
});
} else {
console.log(`ignore.conf was found and is being consulted.`);
const parsedFilterConfig = await readFileSync(ignoreListFile);
filter = build_ignore_list(parsedFilterConfig);
}
filter.forEach((el) => {
if(el) {
args.push(el);
}
});
if(argc < 1) {
usage_help();
return process.exit(0);
}
if(numThreads > 0) {
args.push(`/MT:${numThreads}`);
}
// NOTE(jeff): First, let us do the optional arguments
// parsing!
if(args.includes(`/mir`) == true) {
args.push(`/MIR`);
}
if(args.includes(`-n`) == true) {
modeDry = true;
}
if(args.includes(`dry`) == true) {
modeDry = true;
}
if(useLogFile) {
const logFileName = process.env[`RC_LOGFILE`];
if(modeLogFileAppend == true) {
// NOTE(jeff): Append to an existing log file
args.push(`/LOG+:${logFileName}`);
} else {
// NOTE(jeff): Overwrite log file
args.push(`/LOG:${logFileName}`);
}
} else {
console.info(`Execution log file has been explicitly disabled.`);
}
console.log(args);
args.forEach((arg, idx) => {
if(!arg) {
return;
}
if(idx == 0) {
return;
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
if(arg.includes("source:") == true) {
let sourceStr = arg.substring(7,255);
console.debug(`source:${sourceStr}`);
args.push(`${sourceStr}`);
} else if(arg.includes("source:") == false) {
// console.error(`CRITICAL: A source path must be given.`);
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
if(arg.includes("dest:")) {
let destStr = arg.substring(5,255);
console.debug(`dest:${destStr}`);
args.push(`${destStr}`);
} else if(args.includes(`dest:`) == false) {
// console.error(`CRITICAL: A destination path must be given.`);
}
});
execute_robocopy(args);
process.exit(0);
}
// IMPORTANT(jeff): We are needing the right array count for **only** the end-user passed input
let args = process && process.argv || [];
if(args.length > 1) {
args.shift();
args.shift();
}
let numArgs = args.length;
main(numArgs, args);
//main(process.argv.length, process.argv);