node-automator 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -0
- package/assets/alert.png +0 -0
- package/assets/error.png +0 -0
- package/assets/success.png +0 -0
- package/commands/abort.js +11 -0
- package/commands/alert.js +19 -0
- package/commands/assemble.js +56 -0
- package/commands/assert.js +18 -0
- package/commands/base.js +363 -0
- package/commands/break.js +11 -0
- package/commands/change_filter.js +34 -0
- package/commands/cheerio.js +15 -0
- package/commands/clear_screen.js +14 -0
- package/commands/commit.js +31 -0
- package/commands/compare.js +23 -0
- package/commands/confirm.js +16 -0
- package/commands/continue.js +11 -0
- package/commands/copy.js +14 -0
- package/commands/cursor_up.js +12 -0
- package/commands/date.js +25 -0
- package/commands/debugger.js +11 -0
- package/commands/dingtalk.js +110 -0
- package/commands/disassemble.js +36 -0
- package/commands/divination.js +15 -0
- package/commands/download.js +12 -0
- package/commands/edit.js +83 -0
- package/commands/edit_excel.js +57 -0
- package/commands/env_optional.js +57 -0
- package/commands/eval.js +27 -0
- package/commands/exec_from_file.js +31 -0
- package/commands/export_diff_head.js +31 -0
- package/commands/file_info.js +41 -0
- package/commands/file_size.js +12 -0
- package/commands/files.js +14 -0
- package/commands/first_exist.js +18 -0
- package/commands/flatten.js +18 -0
- package/commands/ftp.js +161 -0
- package/commands/git_commit.js +31 -0
- package/commands/git_export_diff_head.js +13 -0
- package/commands/git_is_dirty.js +13 -0
- package/commands/hash.js +21 -0
- package/commands/http_request.js +12 -0
- package/commands/image_crop.js +19 -0
- package/commands/input.js +37 -0
- package/commands/interval.js +60 -0
- package/commands/is_dirty.js +31 -0
- package/commands/jwt_decode.js +22 -0
- package/commands/list2dict.js +32 -0
- package/commands/log.js +72 -0
- package/commands/loop.js +115 -0
- package/commands/loop_forever.js +66 -0
- package/commands/merge_files.js +39 -0
- package/commands/mgr.js +476 -0
- package/commands/modify.js +44 -0
- package/commands/move.js +14 -0
- package/commands/move_local.js +14 -0
- package/commands/multi_select.js +16 -0
- package/commands/mysql_connect.js +29 -0
- package/commands/mysql_end.js +13 -0
- package/commands/mysql_query.js +19 -0
- package/commands/notify.js +13 -0
- package/commands/open.js +30 -0
- package/commands/parse.js +18 -0
- package/commands/pause.js +15 -0
- package/commands/print.js +47 -0
- package/commands/read_cache.js +12 -0
- package/commands/read_cfg.js +13 -0
- package/commands/read_clipboard.js +19 -0
- package/commands/read_excel.js +17 -0
- package/commands/read_only_excel.js +40 -0
- package/commands/read_plain.js +14 -0
- package/commands/remove.js +14 -0
- package/commands/retrieve.js +17 -0
- package/commands/reveal.js +33 -0
- package/commands/revision_info.js +31 -0
- package/commands/schedule.js +80 -0
- package/commands/select.js +17 -0
- package/commands/send_email.js +25 -0
- package/commands/share_data.js +745 -0
- package/commands/shell.js +13 -0
- package/commands/sleep.js +17 -0
- package/commands/store.js +13 -0
- package/commands/store_optional.js +33 -0
- package/commands/stream.js +16 -0
- package/commands/stringify.js +24 -0
- package/commands/svn_add.js +26 -0
- package/commands/svn_commit.js +49 -0
- package/commands/svn_export_diff_head.js +13 -0
- package/commands/svn_is_dirty.js +13 -0
- package/commands/svn_update.js +27 -0
- package/commands/switch.js +18 -0
- package/commands/temp.js +12 -0
- package/commands/tencent_cos.js +323 -0
- package/commands/test.js +14 -0
- package/commands/title.js +18 -0
- package/commands/tmpdir.js +21 -0
- package/commands/translate.js +58 -0
- package/commands/traversal.js +38 -0
- package/commands/truncate.js +31 -0
- package/commands/truncate_all.js +62 -0
- package/commands/ts_sort.js +18 -0
- package/commands/unzip.js +50 -0
- package/commands/updating_revision.js +28 -0
- package/commands/valid_required.js +31 -0
- package/commands/void.js +10 -0
- package/commands/watch_mail.js +94 -0
- package/commands/web_socket.js +233 -0
- package/commands/which.js +60 -0
- package/commands/write_cache.js +16 -0
- package/commands/write_cfg.js +18 -0
- package/commands/write_clipboard.js +23 -0
- package/commands/write_excel.js +17 -0
- package/commands/write_plain.js +18 -0
- package/commands/zip.js +78 -0
- package/commands/zip_data.js +64 -0
- package/examples/print_env.yml +3 -0
- package/index.js +109 -0
- package/package.json +63 -0
- package/scratch/.gitkeep +0 -0
- package/utils/alert_tool.js +133 -0
- package/utils/base64_tool.js +13 -0
- package/utils/cache/local_cache.js +79 -0
- package/utils/cache/raw_cache.js +47 -0
- package/utils/cache/simple_local_cache.js +33 -0
- package/utils/cache/simple_sqlite_cache.js +43 -0
- package/utils/cache/simple_svn_cache.js +49 -0
- package/utils/cache/svn_cache.js +40 -0
- package/utils/cache_tool.js +53 -0
- package/utils/display_tool.js +85 -0
- package/utils/divination_util.js +555 -0
- package/utils/file_tool.js +529 -0
- package/utils/hash_tool.js +8 -0
- package/utils/interaction_tool.js +228 -0
- package/utils/log_tool.js +190 -0
- package/utils/parse_directive.js +194 -0
- package/utils/parse_tool.js +236 -0
- package/utils/request_tool.js +156 -0
- package/utils/scm_tool.js +108 -0
- package/utils/selection_tool.js +593 -0
- package/utils/shell_tool.js +162 -0
- package/utils/svn_diff/diff_file.js +44 -0
- package/utils/svn_diff/diff_parser.js +55 -0
- package/utils/transform_tool.js +520 -0
|
@@ -0,0 +1,529 @@
|
|
|
1
|
+
const glob = require("glob");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const display_tool = require("./display_tool");
|
|
5
|
+
const {
|
|
6
|
+
info,
|
|
7
|
+
whisper,
|
|
8
|
+
warn,
|
|
9
|
+
setLastError
|
|
10
|
+
} = require("../utils/log_tool");
|
|
11
|
+
const tmps = {
|
|
12
|
+
arr: [],
|
|
13
|
+
};
|
|
14
|
+
const iconv = require('iconv-lite');
|
|
15
|
+
// const chardet = require("jschardet");
|
|
16
|
+
const chardet = require("chardet");
|
|
17
|
+
const { get_full_path } = require("../commands/share_data");
|
|
18
|
+
const { parse, stringify } = require("./parse_tool");
|
|
19
|
+
/**
|
|
20
|
+
*
|
|
21
|
+
* @param {Array | string} src
|
|
22
|
+
*/
|
|
23
|
+
function get_file_list(src, noGlob = false, onlyFile = false, real = false) {
|
|
24
|
+
src = tmps.arr.concat(src);
|
|
25
|
+
let files = [];
|
|
26
|
+
src.forEach(v => {
|
|
27
|
+
v = get_full_path(v);
|
|
28
|
+
if (!noGlob) {
|
|
29
|
+
try {
|
|
30
|
+
!fs.lstatSync(v);
|
|
31
|
+
files.push(v);
|
|
32
|
+
} catch (error) {
|
|
33
|
+
Array.prototype.push.apply(files, glob.sync(v));
|
|
34
|
+
}
|
|
35
|
+
} else {
|
|
36
|
+
files.push(v);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
if (onlyFile) {
|
|
40
|
+
files = files.filter(v => {
|
|
41
|
+
return !fs.lstatSync(v).isDirectory();
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
if (real) {
|
|
45
|
+
files = files.map(v => v && fs.realpathSync(v));
|
|
46
|
+
}
|
|
47
|
+
return files;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function get_fst_file(src, real, noGlob) {
|
|
51
|
+
let files = get_file_list(src, noGlob, undefined, real);
|
|
52
|
+
let file = files[0];
|
|
53
|
+
return file;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function get_file_size(src) {
|
|
57
|
+
let files = get_file_list(src);
|
|
58
|
+
return get_files_size(files);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function get_files_size(files) {
|
|
62
|
+
return files.reduce((prev, file) => {
|
|
63
|
+
let stat = fs.statSync(file);
|
|
64
|
+
return stat.size += prev;
|
|
65
|
+
}, 0);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function format_bytes(size) {
|
|
69
|
+
if (size < 2 ** 10) {
|
|
70
|
+
return `${size}B`;
|
|
71
|
+
}
|
|
72
|
+
if (size < 2 ** 20) {
|
|
73
|
+
return `${+(size / 2 ** 10).toFixed(2)}KB`;
|
|
74
|
+
}
|
|
75
|
+
if (size < 2 ** 30) {
|
|
76
|
+
return `${+(size / 2 ** 20).toFixed(2)}MB`;
|
|
77
|
+
}
|
|
78
|
+
if (size < 2 ** 40) {
|
|
79
|
+
return `${+(size / 2 ** 30).toFixed(2)}GB`;
|
|
80
|
+
}
|
|
81
|
+
if (size < 2 ** 50) {
|
|
82
|
+
return `${+(size / 2 ** 40).toFixed(2)}TB`;
|
|
83
|
+
}
|
|
84
|
+
if (size < 2 ** 60) {
|
|
85
|
+
return `${+(size / 2 ** 50).toFixed(2)}PB`;
|
|
86
|
+
}
|
|
87
|
+
if (size < 2 ** 70) {
|
|
88
|
+
return `${+(size / 2 ** 60).toFixed(2)}EB`;
|
|
89
|
+
}
|
|
90
|
+
if (size < 2 ** 80) {
|
|
91
|
+
return `${+(size / 2 ** 70).toFixed(2)}ZB`;
|
|
92
|
+
}
|
|
93
|
+
return `${+(size / 2 ** 80).toFixed(2)}YB`;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function read_plain(src, options) {
|
|
97
|
+
try {
|
|
98
|
+
let fst_src = get_fst_file(src);
|
|
99
|
+
if (!fst_src) {
|
|
100
|
+
setLastError(`${src} 未找到`);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
let value = fs.readFileSync(fst_src, {
|
|
104
|
+
flag: "r"
|
|
105
|
+
});
|
|
106
|
+
if (!options?.raw) {
|
|
107
|
+
let encoding = options && options.encoding;
|
|
108
|
+
let auto_encoding = options && options.auto_encoding;
|
|
109
|
+
if (auto_encoding) {
|
|
110
|
+
// encoding = chardet.detect(value).encoding;
|
|
111
|
+
encoding = chardet.detect(value);
|
|
112
|
+
} else {
|
|
113
|
+
encoding = encoding || "utf-8";
|
|
114
|
+
}
|
|
115
|
+
value = iconv.decode(value, encoding);
|
|
116
|
+
}
|
|
117
|
+
return value;
|
|
118
|
+
} catch (err) {
|
|
119
|
+
throw (`读取文本文件 ${err}`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function write_plain(dst, data, options) {
|
|
124
|
+
try {
|
|
125
|
+
dst = get_full_path(dst);
|
|
126
|
+
options = Object.assign({}, {
|
|
127
|
+
encoding: "utf-8",
|
|
128
|
+
flag: "w"
|
|
129
|
+
}, options);
|
|
130
|
+
// 创建不存在的目录
|
|
131
|
+
let dstDir = path.dirname(dst);
|
|
132
|
+
if (!fs.existsSync(dstDir)) {
|
|
133
|
+
fs.mkdirSync(dstDir, {
|
|
134
|
+
recursive: true
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
let value;
|
|
138
|
+
if (options.raw) {
|
|
139
|
+
value = fs.writeFileSync(dst, data);
|
|
140
|
+
} else {
|
|
141
|
+
value = fs.writeFileSync(dst, data, options);
|
|
142
|
+
}
|
|
143
|
+
return value;
|
|
144
|
+
} catch (err) {
|
|
145
|
+
throw (`保存文本文件 ${err}`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async function write_with_type(data, dst, type, options) {
|
|
150
|
+
if (Buffer.isBuffer(data) || typeof data === "string") {
|
|
151
|
+
write_plain(dst, data);
|
|
152
|
+
} else {
|
|
153
|
+
if (!type) {
|
|
154
|
+
const extname = path.extname(dst);
|
|
155
|
+
switch (extname) {
|
|
156
|
+
case ".yaml":
|
|
157
|
+
case ".yml": {
|
|
158
|
+
type = "yaml";
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
case ".json": {
|
|
162
|
+
type = "json";
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
switch (options?.mode) {
|
|
168
|
+
case "merge": {
|
|
169
|
+
data = {...(await read_cfg(dst, type)), ...data};
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
case "concat": {
|
|
173
|
+
data = [...(await read_cfg(dst, type) || []), ...data];
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
let content = await stringify(data, type, options);
|
|
178
|
+
write_plain(dst, content);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async function read_cfg(src, type, options) {
|
|
183
|
+
src = get_fst_file(src);
|
|
184
|
+
if (!src) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (!type) {
|
|
188
|
+
const extname = path.extname(src);
|
|
189
|
+
switch (extname) {
|
|
190
|
+
case ".yaml":
|
|
191
|
+
case ".yml": {
|
|
192
|
+
type = "yaml";
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
case ".xml": {
|
|
196
|
+
type = "xml";
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
case ".lua": {
|
|
200
|
+
type = "lua";
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
default: {
|
|
204
|
+
type = "json";
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
let content = await read_plain(src, options);
|
|
210
|
+
if (content == null) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
return parse(content, type, options);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function processRename(fullpath, params) {
|
|
217
|
+
if (!params) return fullpath;
|
|
218
|
+
let dirname = path.dirname(fullpath);
|
|
219
|
+
switch (typeof params) {
|
|
220
|
+
case "object": {
|
|
221
|
+
let extname = path.extname(fullpath);
|
|
222
|
+
let basename = path.basename(fullpath, extname);
|
|
223
|
+
if (params.base_trim_left) {
|
|
224
|
+
if (basename.indexOf(params.base_trim_left) == 0) {
|
|
225
|
+
basename = basename.substr(params.base_trim_left.length);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (params.base_trim_right) {
|
|
229
|
+
if (basename.indexOf(params.base_trim_right) == basename.length - params.base_trim_right.length) {
|
|
230
|
+
basename = basename.substr(0, basename.length - params.base_trim_right.length);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (params.dir_trim_left) {
|
|
234
|
+
if (dirname.indexOf(params.dir_trim_left) == 0) {
|
|
235
|
+
dirname = dirname.substr(params.dir_trim_left.length);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (params.dir_trim_right) {
|
|
239
|
+
if (dirname.indexOf(params.dir_trim_right) == dirname.length - params.dir_trim_right.length) {
|
|
240
|
+
dirname = dirname.substr(0, dirname.length - params.dir_trim_right.length);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
if (params.dir_prefix) dirname = params.dir_prefix + dirname;
|
|
244
|
+
if (params.dir_suffix) dirname = dirname + params.dir_suffix;
|
|
245
|
+
if (params.base_prefix) basename = params.base_prefix + basename;
|
|
246
|
+
if (params.base_suffix) basename = basename + params.base_suffix;
|
|
247
|
+
return path.join(dirname, basename + extname);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return path.join(dirname, params);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
async function copy(src, dst, base, options) {
|
|
254
|
+
srcs = get_file_list(src);
|
|
255
|
+
dsts = get_file_list(dst, true);
|
|
256
|
+
if (base) {
|
|
257
|
+
base = get_fst_file(base);
|
|
258
|
+
if (!base) {
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
options = Object.assign({
|
|
263
|
+
overwrite: true
|
|
264
|
+
}, options);
|
|
265
|
+
let filesize = get_files_size(srcs);
|
|
266
|
+
info("");
|
|
267
|
+
whisper(`复制 ${srcs[0]} 等 ${srcs.length} 个文件(夹) ${format_bytes(filesize)} ${options.overwrite ? "[覆盖现有文件]" : "[不覆盖现有文件]"} 到 ${dsts} options: ${JSON.stringify(options)}`, undefined, true);
|
|
268
|
+
let dir_num = 0;
|
|
269
|
+
let actual_files = [];
|
|
270
|
+
for (let srcIndex = 0; srcIndex < srcs.length; srcIndex++) {
|
|
271
|
+
const src = srcs[srcIndex];
|
|
272
|
+
let show_src = !base ? src : path.relative(base, src);
|
|
273
|
+
if (fs.lstatSync(src).isDirectory()) {
|
|
274
|
+
++dir_num;
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
display_tool.progress(srcIndex + 1, srcs.length, {
|
|
278
|
+
desc: `Copy ${show_src}`,
|
|
279
|
+
depth: 0,
|
|
280
|
+
color: "green",
|
|
281
|
+
align: true,
|
|
282
|
+
});
|
|
283
|
+
for (let dstIndex = 0; dstIndex < dsts.length; dstIndex++) {
|
|
284
|
+
const dst = dsts[dstIndex];
|
|
285
|
+
// 完整的目标文件路径
|
|
286
|
+
let dstFile;
|
|
287
|
+
if (!base) {
|
|
288
|
+
dstFile = path.join(dst, path.basename(src));
|
|
289
|
+
} else {
|
|
290
|
+
dstFile = path.join(dst, path.relative(base, src));
|
|
291
|
+
}
|
|
292
|
+
dstFile = processRename(dstFile, options.rename);
|
|
293
|
+
let dstDir = path.dirname(dstFile);
|
|
294
|
+
if (!fs.existsSync(dstDir) || !fs.lstatSync(dstDir).isDirectory()) {
|
|
295
|
+
try {
|
|
296
|
+
fs.mkdirSync(dstDir, {
|
|
297
|
+
recursive: true
|
|
298
|
+
});
|
|
299
|
+
} catch (err) {
|
|
300
|
+
throw (err);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
if (fs.existsSync(dstFile) && !options.overwrite) {
|
|
304
|
+
continue;
|
|
305
|
+
} else {
|
|
306
|
+
if (!actual_files.includes(src)) {
|
|
307
|
+
actual_files.push(src);
|
|
308
|
+
}
|
|
309
|
+
await new Promise(resolve => {
|
|
310
|
+
// !options.overwrite ? fs.constants.COPYFILE_EXCL : 0
|
|
311
|
+
fs.copyFile(src, dstFile, 0, (err) => {
|
|
312
|
+
if (err) {
|
|
313
|
+
throw (err);
|
|
314
|
+
}
|
|
315
|
+
resolve();
|
|
316
|
+
});
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
info("");
|
|
323
|
+
whisper(`复制完成 (文件: ${actual_files.length}, 目录: ${dir_num}, 总计: ${srcs.length})`, undefined, true);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
async function move(src, dst, base, options) {
|
|
327
|
+
srcs = get_file_list(src);
|
|
328
|
+
dsts = get_file_list(dst, true);
|
|
329
|
+
if (base) {
|
|
330
|
+
base = get_fst_file(base);
|
|
331
|
+
if (!base) {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
options = Object.assign({
|
|
336
|
+
overwrite: true
|
|
337
|
+
}, options);
|
|
338
|
+
let filesize = get_files_size(srcs);
|
|
339
|
+
info("");
|
|
340
|
+
whisper(`移动 ${srcs[0]} 等 ${srcs.length} 个文件(夹) ${format_bytes(filesize)} ${options.overwrite ? "[覆盖现有文件]" : "[不覆盖现有文件]"} 到 ${dsts} options: ${JSON.stringify(options)}`, undefined, true);
|
|
341
|
+
let actual_files = [];
|
|
342
|
+
let dir_num = 0;
|
|
343
|
+
for (let srcIndex = 0; srcIndex < srcs.length; srcIndex++) {
|
|
344
|
+
const src = srcs[srcIndex];
|
|
345
|
+
let show_src = !base ? src : path.relative(base, src);
|
|
346
|
+
if (fs.lstatSync(src).isDirectory()) {
|
|
347
|
+
++dir_num;
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
display_tool.progress(srcIndex + 1, srcs.length, {
|
|
351
|
+
desc: `Move ${show_src}`,
|
|
352
|
+
depth: 0,
|
|
353
|
+
color: "cyan",
|
|
354
|
+
align: true,
|
|
355
|
+
});
|
|
356
|
+
// 先复制
|
|
357
|
+
for (let dstIndex = 0; dstIndex < dsts.length; dstIndex++) {
|
|
358
|
+
const dst = dsts[dstIndex];
|
|
359
|
+
// 完整的目标文件路径
|
|
360
|
+
let dstFile;
|
|
361
|
+
if (!base) {
|
|
362
|
+
dstFile = path.join(dst, path.basename(src));
|
|
363
|
+
} else {
|
|
364
|
+
dstFile = path.join(dst, path.relative(base, src));
|
|
365
|
+
}
|
|
366
|
+
dstFile = processRename(dstFile, options.rename);
|
|
367
|
+
let dstDir = path.dirname(dstFile);
|
|
368
|
+
if (!fs.existsSync(dstDir) || !fs.lstatSync(dstDir).isDirectory()) {
|
|
369
|
+
fs.mkdirSync(dstDir, {
|
|
370
|
+
recursive: true
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
if (fs.existsSync(dstFile) && !options.overwrite) {
|
|
374
|
+
continue;
|
|
375
|
+
} else {
|
|
376
|
+
if (!actual_files.includes(src)) {
|
|
377
|
+
actual_files.push(src);
|
|
378
|
+
}
|
|
379
|
+
await new Promise(resolve => {
|
|
380
|
+
if (!options.overwrite && fs.existsSync(dstFile)) {
|
|
381
|
+
resolve();
|
|
382
|
+
} else {
|
|
383
|
+
fs.copyFile(src, dstFile, 0, (err) => {
|
|
384
|
+
if (err) {
|
|
385
|
+
throw (err);
|
|
386
|
+
}
|
|
387
|
+
resolve();
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
// 再删除
|
|
394
|
+
fs.unlinkSync(src);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
info("");
|
|
398
|
+
whisper(`移动完成 (文件: ${actual_files.length}, 目录: ${dir_num}, 总计: ${srcs.length})`, undefined, true);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
async function move_local(src, dst, base, options) {
|
|
402
|
+
srcs = get_file_list(src);
|
|
403
|
+
dsts = get_file_list(dst, true);
|
|
404
|
+
if (base) {
|
|
405
|
+
base = get_fst_file(base);
|
|
406
|
+
if (!base) {
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
options = Object.assign({
|
|
411
|
+
overwrite: true
|
|
412
|
+
}, options);
|
|
413
|
+
let filesize = get_files_size(srcs);
|
|
414
|
+
info("");
|
|
415
|
+
whisper(`移动 ${srcs[0]} 等 ${srcs.length} 个文件(夹) ${format_bytes(filesize)} ${options.overwrite ? "[覆盖现有文件]" : "[不覆盖现有文件]"} 到 ${dsts} options: ${JSON.stringify(options)}`, undefined, true);
|
|
416
|
+
let actual_files = [];
|
|
417
|
+
let dir_num = 0;
|
|
418
|
+
for (let srcIndex = 0; srcIndex < srcs.length; srcIndex++) {
|
|
419
|
+
const src = srcs[srcIndex];
|
|
420
|
+
let show_src = !base ? src : path.relative(base, src);
|
|
421
|
+
if (fs.lstatSync(src).isDirectory()) {
|
|
422
|
+
++dir_num;
|
|
423
|
+
}
|
|
424
|
+
display_tool.progress(srcIndex + 1, srcs.length, {
|
|
425
|
+
desc: `Rename ${show_src}`,
|
|
426
|
+
depth: 0,
|
|
427
|
+
color: "cyan",
|
|
428
|
+
align: true,
|
|
429
|
+
});
|
|
430
|
+
for (let dstIndex = 0; dstIndex < dsts.length; dstIndex++) {
|
|
431
|
+
const dst = dsts[dstIndex];
|
|
432
|
+
// 完整的目标文件路径
|
|
433
|
+
let dstFile;
|
|
434
|
+
if (!base) {
|
|
435
|
+
dstFile = path.join(dst, path.basename(src));
|
|
436
|
+
} else {
|
|
437
|
+
dstFile = path.join(dst, path.relative(base, src));
|
|
438
|
+
}
|
|
439
|
+
dstFile = processRename(dstFile, options.rename);
|
|
440
|
+
let dstDir = path.dirname(dstFile);
|
|
441
|
+
if (!fs.existsSync(dstDir) || !fs.lstatSync(dstDir).isDirectory()) {
|
|
442
|
+
fs.mkdirSync(dstDir, {
|
|
443
|
+
recursive: true
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
if (fs.existsSync(dstFile) && !options.overwrite) {
|
|
447
|
+
continue;
|
|
448
|
+
} else {
|
|
449
|
+
if (!actual_files.includes(src)) {
|
|
450
|
+
actual_files.push(src);
|
|
451
|
+
}
|
|
452
|
+
skip = false;
|
|
453
|
+
await new Promise(resolve => {
|
|
454
|
+
if (!options.overwrite && fs.existsSync(dstFile)) {
|
|
455
|
+
resolve();
|
|
456
|
+
} else {
|
|
457
|
+
fs.rename(src, dstFile, (err) => {
|
|
458
|
+
if (err) {
|
|
459
|
+
throw (err);
|
|
460
|
+
}
|
|
461
|
+
resolve();
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
info("");
|
|
470
|
+
whisper(`移动完成 (文件: ${actual_files.length}, 目录: ${dir_num}, 总计: ${srcs.length})`, undefined, true);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
async function remove(src, options) {
|
|
474
|
+
srcs = get_file_list(src);
|
|
475
|
+
options = Object.assign({ recursive: true }, options);
|
|
476
|
+
let filesize = get_files_size(srcs);
|
|
477
|
+
info("");
|
|
478
|
+
whisper(`删除 ${srcs[0]} 等 ${srcs.length} 个文件(夹) ${format_bytes(filesize)} options: ${JSON.stringify(options)}`, undefined, true);
|
|
479
|
+
let actual_files = [];
|
|
480
|
+
let dir_num = 0;
|
|
481
|
+
for (let srcIndex = 0; srcIndex < srcs.length; srcIndex++) {
|
|
482
|
+
const src = srcs[srcIndex];
|
|
483
|
+
let show_src = src;
|
|
484
|
+
try {
|
|
485
|
+
display_tool.progress(srcIndex + 1, srcs.length, {
|
|
486
|
+
desc: `Remove ${show_src}`,
|
|
487
|
+
depth: 0,
|
|
488
|
+
color: "cyan",
|
|
489
|
+
align: true,
|
|
490
|
+
});
|
|
491
|
+
if (fs.lstatSync(src).isDirectory()) {
|
|
492
|
+
fs.rmdirSync(src, options);
|
|
493
|
+
++dir_num;
|
|
494
|
+
} else {
|
|
495
|
+
if (fs.existsSync(src)) {
|
|
496
|
+
if (!actual_files.includes(src)) {
|
|
497
|
+
actual_files.push(src);
|
|
498
|
+
}
|
|
499
|
+
fs.unlinkSync(src);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
} catch (err) {
|
|
503
|
+
warn(err);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
info("");
|
|
508
|
+
whisper(`删除完成 (文件: ${actual_files.length}, 目录: ${dir_num}, 总计: ${srcs.length})`, undefined, true);
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
module.exports = {
|
|
512
|
+
get_file_list,
|
|
513
|
+
get_fst_file,
|
|
514
|
+
get_full_path,
|
|
515
|
+
read_cfg,
|
|
516
|
+
read_plain,
|
|
517
|
+
write_plain,
|
|
518
|
+
write_with_type,
|
|
519
|
+
copy,
|
|
520
|
+
move,
|
|
521
|
+
remove,
|
|
522
|
+
parse,
|
|
523
|
+
stringify,
|
|
524
|
+
move_local,
|
|
525
|
+
get_file_size,
|
|
526
|
+
get_files_size,
|
|
527
|
+
format_bytes,
|
|
528
|
+
processRename,
|
|
529
|
+
};
|