-
Notifications
You must be signed in to change notification settings - Fork 40
/
nodecc.js
132 lines (115 loc) · 4.06 KB
/
nodecc.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* js | css files Compo and Merge tool used nodejs
* @author AlloyTeam.Horizon
*/
var fs = require('fs'),
util = require('util'),
path = require('path'),
child_process = require('child_process'),
nodecc = {};
nodecc.configFile = './config.json';
nodecc.googleCompilerPath = './tools/compiler.jar';
nodecc.yuiCompressorPath = './tools/yuicompressor-2.4.6.jar';
;(function ($) {
var exec = child_process.exec,
spawn = child_process.spawn,
dashed = '------------------------\n';
// mkdir -p
function mkdirpSync (pathes, mode) {
mode = mode || 0777;
var dirs = pathes.trim().split('/');
if (dirs[0] == '.') {
// ./aaa
dirs.shift();
}
if (dirs[0] == '..') {
// ../aaa
dirs.splice(0, 2, dirs[0] + '/' + dirs[1]);
}
dirs.length && mkdir(dirs.shift());
// mkdir
function mkdir (d) {
if (!path.existsSync(d)) {
fs.mkdirSync(d, mode);
}
dirs.length && mkdir(d + '/' + dirs.shift());
}
}
function compressFile (from, to, licence) {
to = to || from;
// check path
var dirpath = to.substring(0, to.lastIndexOf('/'));
!path.existsSync(dirpath) && mkdirpSync(dirpath);
// java -jar yuicompressor-x.y.z.jar myfile.js -o myfile-min.js --charset utf-8
util.print(from + ' Compressed Begin!\n');
var runJar = spawn('java', ['-jar', $.yuiCompressorPath, from, '-o', to, '--charset', 'utf-8']);
runJar.stdout.on('data', function (data) {
console.log('stdout: \n' + data + '\n');
});
runJar.stderr.on('data', function (data) {
console.log('stderr: \n' + data + '\n');
});
runJar.on('exit', function (code) {
util.print(dashed);
util.print(from + ' Compressed Finished! ==> ' + to + '\n');
util.print('YUI Compressor exit with code: ' + code + '\n');
if (licence && path.existsSync(licence)) {
var newCon = fs.readFileSync(licence, 'utf8') + '\n' + fs.readFileSync(to, 'utf8');
fs.writeFileSync(to, newCon, 'utf8');
}
});
}
// get 'source' and 'target' path;
function getPath (k) {
var obj = $.configContent[k],
sPath = obj.source,
licence = obj.licence,
tPath = obj.target;
if (typeof sPath == 'string') {
// normal compress
sPath = sPath.trim();
if (sPath[sPath.length-1] === '/') {
// dir
var files = fs.readdirSync(sPath);
files.forEach(function (file) {
compressFile(sPath + file, tPath + file, licence);
});
} else {
// file
compressFile(sPath, tPath, licence);
}
} else if (typeof sPath == 'object' && sPath.forEach) {
path.existsSync(tPath) && fs.unlinkSync(tPath);
// merge and compress
util.print(k + 'merging begin! \n');
var fd = fs.openSync(tPath, 'a', 0666);
sPath.forEach(function (p) {
fs.writeSync(fd, '\n' + fs.readFileSync(p, 'utf8').toString(), null, 'utf8');
});
fs.closeSync(fd);
util.print(k + 'merged finish!\n');
// compress
compressFile(tPath, null, licence);
}
}
function readConfig () {
if (!path.existsSync($.configFile)) {
throw 'config.json missed!'
}
var content = fs.readFileSync($.configFile, 'utf8').toString();
try {
content = JSON.parse(content);
} catch (e) {
console.log(e.message);
}
$.configContent = content;
Object.keys(content).forEach(function (key) {
getPath(key);
});
}
$.init = function () {
readConfig();
};
})(nodecc);
// start
nodecc.init();