-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.js
83 lines (67 loc) · 2 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
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
var program = require('commander');
var fs = require('fs');
var path = require('path');
var Imagemin = require('imagemin');
var imageminOptipng = require('imagemin-optipng');
var redirectFilePath = './config/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');
program.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.command('imgs')
.action(function() {
new Imagemin()
.src('media/**/*.png')
.dest('media')
.use(imageminOptipng({optimizationLevel: 3}))
.run((err, files) => {
if (err) {
console.error(err);
}
console.log('Minified ' + files.length + ' images.');
//=> {path: 'build/images/foo.jpg', contents: <Buffer 89 50 4e ...>}
});
});
program.command('img <imgPath>')
.action(function(imgPath) {
new Imagemin()
.src(imgPath)
.dest(path.dirname(imgPath))
.use(imageminOptipng({optimizationLevel: 3}))
.run((err, files) => {
if (err) {
console.error(err);
}
console.log('Minified ' + files.length + ' images.');
//=> {path: 'build/images/foo.jpg', contents: <Buffer 89 50 4e ...>}
});
});
program.parse(process.argv);