-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·47 lines (42 loc) · 1.33 KB
/
index.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
#!/usr/bin/env node
const { program } = require('commander');
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const { DateTime } = require('luxon');
require('any-date-parser');
function pathnameToIso(filename) {
if (path.dirname(filename) !== '.') {
return path.join(path.dirname(filename), pathnameToIso(path.basename(filename)));
}
if (path.extname(filename)) {
const ext = path.extname(filename);
return pathnameToIso(path.basename(filename, ext)) + path.extname(filename);
}
const dt = DateTime.fromJSDate(Date.fromString(filename));
return dt.invalid ? filename : dt.toISODate();
}
function main(dirpath, { dryRun, verbose }) {
let count = 0;
for (let filename of glob.sync(path.join(dirpath, '**/*'))) {
const newname = pathnameToIso(filename);
const changed = filename !== newname;
if (changed) {
console.log(`Rename ${filename} -> ${newname}`);
count++;
if (!dryRun) {
fs.renameSync(filename, newname);
}
} else if (verbose) {
console.log('Skipping', filename);
}
}
console.log(`Renamed ${count} files`);
}
program
.description("Rename files with date names to ISO standard")
.argument('<dir>', 'directory name')
.option('-n --dry-run', 'dry run')
.option('-v, --verbose', 'verbose')
.action(main)
program.parse();