-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathtools.js
43 lines (30 loc) · 1006 Bytes
/
tools.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
var program = require('commander');
var fs = require('fs');
var redirectFilePath = './redirect-urls.json';
var addRedirect = function(oldUrl, newUrl, callback) {
fs.readFile(redirectFilePath, function(err, json) {
if (err) { return callback(err); }
var data = JSON.parse(json);
data.push({
from: oldUrl,
to: newUrl
});
json = JSON.stringify(data, null, 2);
fs.writeFile(redirectFilePath, json, callback);
});
};
program
.version('0.0.1')
.command('mv <oldPath> <newPath>')
.action(function (oldPath, newPath) {
fs.rename(oldPath, newPath, function(err) {
if (err) { return console.error(err); }
var oldUrl = oldPath.replace('articles', '').replace('.md', '');
var newUrl = newPath.replace('articles', '').replace('.md', '');
addRedirect(oldUrl, newUrl, function(err) {
if (err) { return console.error(err); }
console.log('File renamed');
});
});
});
program.parse(process.argv);