-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_cli.js
812 lines (766 loc) · 23.4 KB
/
audio_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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
#!/usr/bin/env node
const util = require("util")
const { lookpath } = require("lookpath")
const fsWalk = require("@nodelib/fs.walk")
const path = require("path")
const chalk = require("chalk")
const fs = require("fs-extra")
const inquirer = require("inquirer")
const workerpool = require("workerpool")
const cpuCount = require("os").cpus().length
const h = require("./lib/helper")
const log = require("./lib/debug")
const un = require("./lib/unicode")
const cue = require("./lib/cue")
const { boolean, option } = require("yargs")
const metadata = require("music-metadata")
// debug and logging config
const prettyError = require("pretty-error").start()
prettyError.skipNodeFiles()
// https://www.exiftool.org/index.html#supported
// https://exiftool.org/TagNames/ID3.html
//////////////////////////////////////////////////////////////////////
// COMMAND LINE ARGS PARSE AND SETUP BEGIN
//////////////////////////////////////////////////////////////////////
const clamp = (num, min, max) => num > max ? max : num < min ? min : num
const configCli = (argv) => {
// log.setName("AudioCli");
log.setLevel(argv.verbose)
log.debug(argv)
}
const yargs = require("yargs/yargs")(process.argv.slice(2))
.command(
["test", "$0"],
"Test ffmpeg executable exists",
(yargs) => { },
(argv) => {
cmdTest()
}
)
.command(
["parse <input> [options]", "ps"],
"Parse id3 metadata for audio files and save to database",
(yargs) => {
return yargs
.positional("input", {
describe: "Input folder that contains audio files",
type: "string",
})
.option("save", {
alias: "s",
type: "boolean",
describe: "Save parsed audio tags to database",
})
},
(argv) => {
cmdParse(argv)
}
)
.command(
["split <input> [options]", "split", "sc"],
"Split audio files by cue sheet and convert to m4a(aac)",
(yargs) => {
yargs
.positional("input", {
describe: "Input folder that contains audio files",
type: "string",
})
.option("libfdk", {
alias: ["fdk", "f"],
type: "boolean",
default: true,
describe: "Use libfdk_aac encoder in ffmpeg command",
})
},
(argv) => {
cmdSplit(argv)
}
)
.command(
// format and name is important!
// <> means required
// [] means optional
// <source> is argument name
["convert <input> [options]", "ct"],
"Convert audio files to m4a(aac) format in input dir",
(yargs) => {
yargs
.positional("input", {
describe: "Input folder that contains audio files",
type: "string",
})
.option("output", {
alias: "o",
describe: "Output folder that store audio files",
type: "string",
})
.option("extensions", {
alias: "e",
type: "string",
describe: "include files by extensions (eg. .wav|.flac)",
})
.option("libfdk", {
alias: ["fdk", "f"],
type: "boolean",
default: true,
describe: "Use libfdk_aac encoder in ffmpeg command",
})
.option("suffix", {
type: "boolean",
describe: "add bitrate suffix to filename",
})
.option("all", {
alias: "a",
type: "boolean",
describe: "handle all files, default loseless audio",
})
.option("quality", {
alias: "q",
type: "string",
default: "0",
describe: "audio quality, bitrate, eg. 128/192/256/320",
})
.option("jobs", {
alias: "j",
describe: "multi jobs running parallelly",
type: "number",
})
},
(argv) => {
cmdConvert(argv)
}
)
.command(
["move <input> [options]", "mv"],
"Move audio files by language in input dir",
(yargs) => {
yargs
.positional("input", {
describe: "Input folder that contains audio files",
type: "string",
normalize: true,
})
.option("lng", {
alias: "l",
type: "array",
default: [],
describe: "Audio language that should be move (cn,ja,kr,en)",
})
.option("unknown", {
alias: "u",
type: boolean,
describe: "Move unclassified audio files to xx folder",
})
},
(argv) => {
cmdMove(argv)
}
)
.count("verbose")
.alias("v", "verbose")
.alias("h", "help")
.usage("Usage: $0 <command> <input> [options]")
.epilog("Move/Convert/Split audio files\nCopyright 2021 @ Zhang Xiaoke")
.demandCommand(1, chalk.red("Missing command you want to execute!"))
.showHelpOnFail()
.help()
.middleware([configCli])
// this line is required to parse args
yargs.argv //==yargs.parse()
//////////////////////////////////////////////////////////////////////
// COMMAND LINE ARGS PARSE AND SETUP END
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// AUDIO CLI COMMON FUNCTIONS BEGIN
//////////////////////////////////////////////////////////////////////
async function listFiles(root, options = {}) {
const startMs = Date.now()
log.info("listFiles: Root", root, options)
// https://www.npmjs.com/package/@nodelib/fs.walk
// walk 31245 files in 31 seconds
const files = await util.promisify(fsWalk.walk)(
root,
Object.assign(
{
stats: true,
concurrency: 4 * cpuCount,
followSymbolicLinks: false,
throwErrorOnBrokenSymbolicLink: false,
errorFilter: (error) => error.code == "ENOENT",
entryFilter: (entry) => entry.stats.isFile(),
},
options || {}
)
)
// https://www.npmjs.com/package/readdirp
// walk 31245 files in 30 seconds
// const files = await readdirp.promise(root, {
// fileFilter: options.fileFilter || options.entryFilter || Boolean,
// type: "files",
// alwaysStat: true,
// });
for (const [i, f] of files.entries()) {
log.debug("listFiles: Item", i + 1, h.ps(f.path), h.fz(f.stats.size))
}
log.info(
"listFiles: Result",
`total ${files.length} files found in ${h.ht(startMs)}`
)
return files
}
async function listAudio(root, loslessOnly = true) {
return listFiles(root, { entryFilter: (entry) => entry.stats.isFile() && (loslessOnly ? h.isLosslessAudio(entry.path) : h.isAudioFile(entry.path)) })
}
function selectAudioTag(mt) {
if (!mt.format.tagTypes || mt.format.tagTypes.length == 0) {
return
}
for (const type of mt.format.tagTypes) {
if (type !== "ID3v1") {
return [type, mt.native[type]]
}
}
}
async function parseTags(files) {
log.show("parseTags", `processing ${files.length} files`)
const start = Date.now()
const results = []
for (const [i, f] of files.entries()) {
let mt
try {
mt = await metadata.parseFile(f.path, { skipCovers: true })
if (mt?.format.tagTypes && mt.format.tagTypes.length > 0) {
log.info(
"parseTags",
i,
files.length,
h.ps(f.path),
mt.common.artist,
mt.common.title,
mt.format.bitrate,
mt.format.lossless
)
} else {
log.info("parseTags", i, "no tags found", h.ps(f.path))
}
} catch (error) {
log.info("parseTags", i, "no tags found", h.ps(f.path), error.message)
if (log.getLevel() >= 2) {
console.error(i, error, f.path)
}
}
f.tags = mt?.common
f.format = mt?.format
f.format && results.push(f)
}
const elapsed = Date.now() - start
log.show(
"parseTags",
`${results.length}/${files.length} files have tags ${elapsed}ms`
)
return results
}
//////////////////////////////////////////////////////////////////////
// AUDIO CLI COMMON FUNCTIONS END
//////////////////////////////////////////////////////////////////////
async function cmdTest(argv) {
const ffmpegBin = "ffmpeg"
const p = await lookpath(ffmpegBin)
if (p) {
log.showGreen(`FFMPEG FOUND: ${p}`)
} else {
log.error(
`You must have "${ffmpegBin}" in you PATH to use split and convert command!`
)
}
}
async function cmdParse(argv) {
const input = path.resolve(argv.input)
log.show("cmdParse Input:", input)
let stats
try {
stats = await fs.stat(input)
} catch (error) {
yargs.showHelp()
log.error("cmdParse", `Invalid Input:`, input)
return
}
let startMs = Date.now()
let files
if (stats.isFile()) {
files = [{ path: input }].filter((f) => h.isAudioFile(f.path))
} else if (stats.isDirectory()) {
files = await listAudio(input)
} else {
files = []
}
const fileCount = files.length
log.show(
"cmdParse",
`found ${fileCount} files in "${input}" ${h.ht(startMs)}`
)
startMs = Date.now()
// maybe very slow over network
files = await parseTags(files)
log.show("cmdParse", `found tags for ${files.length} files ${h.ht(startMs)}`)
argv.save && (await dbSaveTags(files))
}
async function cmdSplit(argv) {
const root = path.resolve(argv.input)
log.show("cmdSplit Input:", root)
if (!root || !(await fs.pathExists(root))) {
yargs.showHelp()
log.error("cmdSplit", "Invalid Input:", input)
return
}
const startMs = Date.now()
let files = await listFiles(root, {
entryFilter: (f) => h.ext(f.path, true) == ".cue",
})
files = await Promise.all(
files.map(async (f) => {
return await cue.findAudioFile(f.path)
})
)
files = files.filter((f) => f.audio)
for (const f of files) {
log.show(`cmdSplit CUE`, f.audio, `(${path.basename(f.path)})`)
}
log.show(
"cmdSplit",
`found ${files.length} cue with audio files ${h.ht(startMs)}`
)
const answer = await inquirer.prompt([
{
type: "confirm",
name: "yes",
default: false,
message: chalk.bold.red(
`Are you sure to split ${files.length} audio files by cue sheet?`
),
},
])
if (answer.yes) {
const results = await splitAllCue(files, argv.libfdk)
for (const r of results) {
if (r.failed && r.failed.length > 0) {
for (const fd of r.failed) {
log.warn("cmdSplit", fd.error, fd.file)
}
} else {
log.showGreen(
"cmdSplit",
"All done",
r.file.audio,
path.basename(r.file.path)
)
}
}
log.showGreen(
"cmdSplit",
`Total ${results.length} audio files splitted by cue sheet.`
)
} else {
log.showYellow("cmdSplit", "Will do nothing, aborted by user.")
}
}
async function splitAllCue(files, useLibfdkAAC) {
log.info("splitAllCue", `Adding ${files.length} tasks`)
const pool = workerpool.pool(__dirname + "/audio_workers.js", {
maxWorkers: cpuCount / 2,
workerType: "process",
})
const startMs = Date.now()
const options = { logLevel: log.getLevel(), useLibfdkAAC: useLibfdkAAC }
const results = await Promise.all(
files.map(async (f, i) => {
return await pool.exec("splitTracks", [f, i + 1, options])
})
)
await pool.terminate()
log.info(
"splitAllCue",
`${results.length} cue files splitted to tracks in ${h.ht(startMs)}.`
)
return results
}
async function cmdConvert(argv) {
log.show("cmdConvert", argv)
const root = path.resolve(argv.input)
log.show("cmdConvert input:", root)
log.show("cmdConvert output:", argv.output)
if (!root || !await fs.pathExists(root)) {
yargs.showHelp()
log.error("cmdConvert", `Invalid Input: '${root}'`)
return
}
const startMs = Date.now()
// list all files in dir recursilly
// keep only non-m4a audio files
// todo add check to ensure is audio file
let files = await listFiles(root)
files = files.sort((a, b) => a.path.localeCompare(b.path))
const extensions = (argv.extensions || "").toLowerCase()
if (extensions?.length >= 3) {
files = files.filter(entry => extensions.includes(h.ext(entry.path)))
} else {
files = files.filter((entry) => argv.all ? h.isAudioFile(entry.path) : h.isLosslessAudio(entry.path))
}
let fileCount = files.length
log.show(
"cmdConvert",
`${files.length} audio files found in ${root} ${h.ht(startMs)}`
)
// caution: slow on network drives
// files = await exif.readAllTags(files);
// files = files.filter((f) => h.isAudioFile(f.path));
// saveAudioDBTags(files);
// use cached file with tags database
if (files?.length == 0) {
log.warn("cmdConvert", "Nothing to do, exit now.")
return
}
const taggedFiles = await parseTags(files)
if (taggedFiles.length < fileCount) {
log.warn(
"cmdConvert",
`${fileCount - taggedFiles.length
} files have no cached tags finally`
)
} else {
files = taggedFiles
}
files = await checkFiles(files, argv)
files.forEach((f, i) => {
f.index = i,
f.total = fileCount,
f.root = root
})
fileCount = files.length
for (const file of files.slice(-100)) {
log.show(`File(${file.index}): ${file.path}`)
}
log.show(
"cmdConvert",
`Prepared ${files.length} valid audio files in ${h.ht(startMs)}`
)
const skipCount = fileCount - files.length
if (skipCount > 0) {
log.show("cmdConvert", `after check ${skipCount} audio files are skipped`)
}
if (files.length == 0) {
log.warn("Nothing to do, exit now.")
return
}
log.show(
"cmdConvert",
`There are ${files.length} audio files ready to convert`
)
log.show('cmdConvert', 'extensions:', extensions)
log.show('......')
const jobCount = clamp(Math.round(argv.jobs || cpuCount / 4), 1, 16)
const answer = await inquirer.prompt([
{
type: "confirm",
name: "yes",
default: false,
message: chalk.bold.red(
`Are you sure to convert ${files.length} files to AAC format?`
),
},
])
if (answer.yes) {
const results = await convertAll(files, argv.libfdk || true, jobCount)
log.showGreen(
"cmdConvert",
`All ${results.length} audio files are converted to AAC format.`
)
} else {
log.showYellow("cmdConvert", "Will do nothing, aborted by user.")
}
}
const QUALITY_LIST = ["128", "192", "256", "320"]
function checkOneFile(file, argv) {
if (file.format) {
file.bitrate = file.format.bitrate
file.lossless = file.format.lossless
} else if (h.isLosslessAudio(file.path)) {
file.bitrate = 1000
file.lossless = true
} else {
file.bitrate = 320
file.lossless = false
}
let quality
if (QUALITY_LIST.includes(argv.quality)) {
quality = `${argv.quality}k`
} else if (file.lossless || file.bitrate > 320 || h.isLosslessAudio(file.path)) {
quality = "320k"
} else {
quality = file?.bitrate > 256 ? "256k" : "192k"
}
file.quality = quality
return quality
}
async function checkFiles(files, argv) {
const logTag = "Check"
log.info(logTag, `before, files count:`, files.length)
const results = await Promise.all(
// true means keep
// false mean skip
files = files.map(async (f, i) => {
const quality = checkOneFile(f, argv)
const index = i + 1
const [dir, base, ext] = h.pathSplit(f.path)
// 如果没有指定输出目录,直接输出在原文件同目录;否则使用指定输出目录
const dstDir = argv.output ? h.pathRewrite(dir, argv.output || "output") : path.resolve(dir)
const dstNameBase = argv.suffix ? `${base} [${quality}]` : `${base}`
const fileDst = path.join(dstDir, `${dstNameBase}.m4a`)
const fileDstTemp = path.join(dstDir, `TMP_${dstNameBase}.m4a`)
const fileDstSameDir = path.join(dir, `${dstNameBase}.m4a`)
log.info(logTag, `SRC (${index}): ${h.ps(f.path)}`)
log.info(logTag, `DST (${index}): ${h.ps(fileDst)}`)
f.dstDir = dstDir
f.fileDst = fileDst
f.fileDstTemp = fileDstTemp
f.fileDstSameDir = fileDstSameDir
if (await fs.pathExists(fileDst)) {
log.showGray(
logTag,
`E1: ${h.ps(fileDst)}`, index
)
return false
}
if (await fs.pathExists(fileDstSameDir)) {
log.showGray(
logTag,
`E2: ${h.ps(fileDstSameDir)}`, index
)
return false
}
const cuefile = path.join(dir, `${base}.cue`)
if (await fs.pathExists(cuefile)) {
log.showGray(
logTag,
`SkipCUE ${h.ps(f.path)}`, index
)
return false
}
log.info(logTag, `OK (${index}): ${h.ps(fileDst)}`)
return f
})
)
files = results.filter(Boolean)
log.info("checkFiles", `after, files count:`, files.length)
return files
}
async function convertAll(files, useLibfdk, jobCount) {
log.info("convertAll", `Adding ${files.length} converting tasks fdk=${useLibfdk}`)
const pool = workerpool.pool(`${__dirname}/audio_workers.js`, {
maxWorkers: jobCount,
workerType: "process",
})
log.debug("convertAll", pool)
const startMs = Date.now()
const options = { logLevel: log.getLevel(), useLibfdkAAC: useLibfdk, startMs: Date.now() }
const results = await Promise.all(
files.map(async (f, i) => {
return await pool.exec("convertAudio", [f, i + 1, files.length, options])
})
)
await pool.terminate()
log.info(
"convertAll",
`Result: ${results.length} files converted in ${h.ht(startMs)}.`
)
return results
}
async function cmdMove(argv) {
log.debug(`cmdMove:`, argv)
const root = path.resolve(argv.input)
const lng = argv.lng || []
if (!root || !(await fs.pathExists(root))) {
log.error("cmdMove", `Invalid Input: '${root}'`)
yargs.showHelp()
return
}
if (lng.length == 0) {
log.error("cmdMove", `Language list is empty, abort!`)
yargs.showHelp()
return
}
if (argv.unknown) {
lng.push("xx")
}
let outputs = {}
lng.forEach((x) => {
outputs[x] = {
id: x,
input: [],
output: path.join(path.dirname(root), `${path.basename(root)}_${x}`),
}
})
log.debug("cmdMove", outputs)
const startMs = Date.now()
let files = await listAudio(root)
log.show(`cmdMove: files count`, files.length)
files = await parseTags(files)
files = files.filter((f) => f.format && f.tags)
log.show(`cmdMove: ${files.length} files have valid tags`)
// let files = await readTagsFromDatabase(root);
const fileCount = files.length
files.forEach((f, i) => {
const t = f.tags
const title = t.title
const artist = t.artist
const name = path.basename(f.path)
if (title && artist) {
if (un.strHasHiraKana(name + title + artist)) {
log.info(chalk.yellow(`JA: ${name} ${artist}-${title}`))
outputs["ja"] &&
outputs["ja"].input.push([
f.path,
path.join(outputs["ja"].output, name),
])
} else if (un.strHasHangul(name + title + artist)) {
log.info(chalk.cyan(`KR: ${name} ${artist}-${title}`))
outputs["kr"] &&
outputs["kr"].input.push([
f.path,
path.join(outputs["kr"].output, name),
])
} else if (un.strHasHanyu(name + title + artist)) {
log.info(chalk.green(`CN: ${name} ${artist}-${title}`))
outputs["cn"] &&
outputs["cn"].input.push([
f.path,
path.join(outputs["cn"].output, name),
])
} else if (un.strOnlyASCII(name + title + artist)) {
// only ascii = english
log.info(chalk.gray(`EN: ${name} ${artist}-${title}`))
outputs.en &&
outputs["en"].input.push([
f.path,
path.join(outputs["en"].output, name),
])
} else {
log.info(chalk.gray(`MISC: ${name} ${artist}-${title}`))
outputs["xx"] &&
outputs["xx"].input.push([
f.path,
path.join(outputs["xx"].output, name),
])
}
} else {
log.info("cmdMove", `no valid tags: ${h.ps(f.path)}`)
}
})
log.info("cmdMove", `Input: ${root} lng=${lng}`)
let taskCount = 0
for (const [k, v] of Object.entries(outputs)) {
taskCount += v.input.length
v.input.length > 0 &&
log.showGreen(
"cmdMove",
`Prepared: [${v.id.toUpperCase()}] ${v.input.length
} files will be moved to "${v.output}"`
)
}
if (taskCount == 0) {
log.warn("cmdMove", `No files need to be processed, abort.`)
return
}
const answer = await inquirer.prompt([
{
type: "confirm",
name: "yes",
default: false,
message: chalk.bold.red(`Are you sure to move there files?`),
},
])
if (answer.yes) {
const dout = path.join(path.dirname(root), "duplicate")
if (!fs.pathExists(dout)) {
await fs.mkdir(dout)
}
async function ensureMove(src, dst) {
log.debug(`Move: ${h.ps(src)} => ${h.ps(dst)}`)
if (src == dst) {
log.debug(`Skip:${src}`)
return
}
if (!(await fs.pathExists(src))) {
log.info(`NotExists:${src}`)
return
}
try {
if (await fs.pathExists(dst)) {
log.debug(`Duplicate:${src}`)
await fs.move(src, path.join(dout, path.basename(src)))
} else {
log.debug(`Moving to ${dst}`)
await fs.move(src, dst)
log.info(`Moved to ${dst}`)
}
} catch (error) {
log.error("Move", error)
}
}
// https://zellwk.com/blog/async-await-in-loops/
// https://techbrij.com/javascript-async-await-parallel-sequence
// paralell execute
// outputs = await Promise.all(
// Object.entries(outputs).map(async ([k, v]) => {
// if (!fs.pathExistsSync(v.output)) {
// fs.mkdirSync(v.output);
// }
// if (v.input.length == 0) {
// return v;
// }
// v.results = await Promise.all(
// v.input.map(async (a) => {
// const [src, dst] = a;
// await ensureMove(src, dst);
// return dst;
// })
// );
// d.L(`Progress: ${v.results.length} ${v.id} files moved to ${v.output}`);
// return v;
// })
// );
// sequential execute
for (const [k, v] of Object.entries(outputs)) {
if (!fs.pathExistsSync(v.output)) {
fs.mkdirSync(v.output)
}
if (v.input.length == 0) {
continue
}
v.results = await Promise.all(
v.input.map(async (a) => {
const [src, dst] = a
await ensureMove(src, dst)
return dst
})
)
log.showGreen(
"cmdMove",
`Progress: ${v.results.length} ${v.id} files moved to ${v.output}`
)
}
for (const [k, v] of Object.entries(outputs)) {
v.results &&
log.showGreen(
"cmdMove",
`Result: ${v.results.length} ${v.id} files moved to "${v.output}"`
)
}
log.showGreen(
"cmdMove",
`Total ${fileCount} files processed in ${h.ht(startMs)}`
)
} else {
log.warn("cmdMove", "Will do nothing, aborted by user.")
}
}