-
Notifications
You must be signed in to change notification settings - Fork 0
/
standard_years.mjs
executable file
·48 lines (43 loc) · 1.13 KB
/
standard_years.mjs
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
#!/usr/bin/env node
/* Rename files from YYYY-MM-DD to YYYY⁄MM⁄DD
*/
import fs from 'node:fs'
import glob from 'glob'
import yargs from 'yargs'
import path from 'node:path'
const args = (
yargs(process.argv.slice(2))
.command('* [paths...]', (
'Rename files from YYYY-MM-DD to YYYY⁄MM⁄DD in the given paths.'
))
.demandOption('paths')
.alias('h', 'help')
.help()
.showHelpOnFail(true, 'HELP!')
)
const argv = await args.argv
if(!argv.paths) {
yargs.showHelp()
process.exit(2)
}
const paths = (
Array.isArray(argv.paths) ? argv.paths : Object.values(argv.paths)
)
paths.forEach((dir) => {
glob.sync(path.join(dir, '*')).forEach((full) => {
const file = path.basename(full)
const [match, year, month, day, rest] = (
file.match(/^(\d{4})-(\d{2})-(\d{2})(.+)$/) ?? []
)
if(match) {
const newFile = `${year}⁄${month}⁄${day}${rest}`
const newFull = path.join(dir, newFile)
if(fs.existsSync(newFull)) {
console.error(`File already exists: ${newFull}`)
} else {
fs.renameSync(full, newFull)
console.debug(`${file} → ${newFile}`)
}
}
})
})