/
ninjabuild
executable file
·114 lines (108 loc) · 3.33 KB
/
ninjabuild
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
#!/usr/bin/env node
/*
Copyright 2008-2011 Jamie Hoover.
Licensed per the terms of the Apache License v2.0. See Readme.md for more details.
*/
/*globals */
/*jshint bitwise: true, browser: true, curly: true, eqeqeq: true, forin: true, immed: true, indent: 2, node: true, maxerr: 3, newcap: true, noarg: true, noempty: true, nomen: true, nonew: true, onevar: true, plusplus: false, regexp: true, strict: false, undef: true, white: true*/
var
version = '1.0.0',
copyright = '/*! Ninja UI v' + version + ' ninjaui.com | ninjaui.com/#license */\n',
help = [
'',
'Usage: ninja <path> [options]',
'',
'<path> Ninja UI directory, defaults to current',
'',
'Options:',
'',
' -h, --help Display help information',
' -o, --output <path> Output directory, if different than input',
' -v, --version Display program version',
''
].join('\n'),
options = {
input: '.',
output: '.'
},
args = process.argv.slice(2),
fs = require('fs'),
exec = require('child_process').exec,
ttfs = [],
arg,
ansiColors = {
// 'black': 30, 'yellow': 33, 'blue': 34, 'white': 37
'red': 31,
'green': 32,
'magenta': 35,
'cyan': 36
},
color = function (color, str) {
if (!color) {
return str;
}
return "\033[" + ansiColors[color] + 'm' + str + "\033[0m";
};
while (args.length) {
arg = args.shift();
switch (arg) {
case '-h':
case '--help':
case 'help':
console.log(help);
process.exit();
break;
case '-vn':
case '--versionnumber':
console.log(version);
process.exit();
break;
case '-v':
case '--version':
console.log('Ninja UI ' + version);
process.exit();
break;
case '-o':
case '--output':
if (args.length) {
options.output = args.shift();
break;
}
console.error(color('red', 'Path required after -o, --output'));
process.exit(1);
break;
default:
options.input = arg;
options.output = arg;
}
}
fs.readFile(options.input + '/src/ninjaui.js', function (err, scriptBuffer) {
console.log(color('cyan', 'Reading JavaScript source file...'));
if (err) {
throw err;
}
var scriptString = scriptBuffer.toString().replace('development', version).replace('development', version);
fs.readFile(options.input + '/src/ninjaui.css', function (err, styleBuffer) {
console.log(color('cyan', 'Reading Cascading Style Sheet source file...'));
if (err) {
throw err;
}
var
path = require('path'),
cleanCSS = require('clean-css'),
jsp = require('uglify-js').parser,
pro = require('uglify-js').uglify,
ast;
styleBuffer = new Buffer(cleanCSS.process(styleBuffer.toString()));
scriptString = scriptString.replace('../src/ninjaui.css', 'data:text/css;base64,' + styleBuffer.toString('base64'));
console.log(color('green', 'Writing JavaScript files...'));
fs.writeFileSync(options.output + '/jquery.ninjaui.js', scriptString);
ast = jsp.parse(scriptString);
ast = pro.ast_mangle(ast);
ast = pro.ast_squeeze(ast);
ast = pro.gen_code(ast);
fs.writeFileSync(options.output + '/jquery.ninjaui.min.js', copyright + ast);
console.log('Ninja UI ' + version + ' build complete.');
console.log('Test URL: ' + color('magenta', 'file://' + path.normalize(__dirname + '/../test/index.html?environment=production')));
});
});