forked from auth0/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.js
49 lines (36 loc) · 1.06 KB
/
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
44
45
46
47
48
49
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 <oldUrl> <newUrl>')
.action(function (oldUrl, newUrl) {
if (oldUrl[0] !== '/') {
oldUrl = '/' + oldUrl;
}
if (newUrl[0] !== '/') {
newUrl = '/' + newUrl;
}
var oldPath = 'articles' + oldUrl + '.md';
var newPath = 'articles' + newUrl + '.md';
fs.rename(oldPath, newPath, function(err) {
if (err) { return console.error(err); }
addRedirect(oldUrl, newUrl, function(err) {
if (err) { return console.error(err); }
console.log('File renamed');
});
});
});
program.parse(process.argv);