forked from BTNcafe/BOOTSTRAP-BOX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PACKER.js
366 lines (277 loc) · 7.39 KB
/
PACKER.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* UPPERCASE.IO Packer.
*/
global.PACKER = function() {'use strict';
var
//IMPORT: fs
fs = require('fs'),
//IMPORT: path
path = require('path'),
// root path
rootPath = __dirname,
// box name
boxName = process.argv[2],
// browser script
browserScript = '',
// secured browser script
securedBrowserScript = '',
// common script
commonScript = '',
// server script
serverScript = '',
// log.
log = function(msg) {
console.log('PACKER: ' + msg);
},
// check is allowed folder.
checkIsAllowedFolder = function(params) {
//REQUIRED: params
//REQUIRED: params.path
//REQUIRED: params.name
var
// path
path = params.path,
// name
name = params.name;
return (
// is directory
fs.statSync(rootPath + '/' + path).isDirectory() === true &&
// hide folder
name[0] !== '.' &&
// node.js module
name !== 'node_modules' &&
// not_load
name !== 'not_load' &&
// deprecated
name !== 'deprecated' &&
// _ folder
name[0] !== '_'
);
},
// copy.
copy = function(path, to) {
if (fs.statSync(path).isDirectory() === true) {
fs.mkdirSync(to);
fs.readdirSync(path).forEach(function(name) {
if (name[0] !== '.') {
copy(path + '/' + name, to + '/' + name);
}
});
} else {
fs.createReadStream(path).pipe(fs.createWriteStream(to));
}
},
// write.
write = function(to, content) {
fs.writeFileSync(to, content);
},
// scan folder.
scanFolder = function(path, func) {
//REQUIRED: path
//REQUIRED: func
var
// folder paths
folderPaths,
// extra
i;
if (fs.existsSync(path) === true) {
folderPaths = [];
fs.readdirSync(path).forEach(function(name) {
var fullPath = path + '/' + name;
if (checkIsAllowedFolder({
path : fullPath,
name : name
}) === true) {
folderPaths.push(fullPath);
} else if (fs.statSync(rootPath + '/' + fullPath).isDirectory() !== true) {
func(fullPath);
}
});
for ( i = 0; i < folderPaths.length; i += 1) {
scanFolder(folderPaths[i], func);
}
}
},
// scan box folder.
scanBoxFolder = function(func) {
//REQUIRED: func
fs.readdirSync(boxName).forEach(function(name) {
if (name[0] !== '.' && name !== 'BROWSER' && name !== 'BROWSER_SECURED' && name !== 'COMMON' && name !== 'SERVER') {
func(boxName + '/' + name);
}
});
},
// load for browser.
loadForBrowser = function(relativePath) {
//REQUIRED: relativePath
var
// absolute path
absolutePath = rootPath + '/' + relativePath,
// extname
extname = path.extname(relativePath),
// content
content = fs.readFileSync(absolutePath);
if (extname === '.js') {
// add to browser script.
browserScript += content + '\n';
} else if (extname === '.__UPPERCASE_IO_COMPILED') {
// add to browser script.
browserScript += content + '\n';
}
},
// load for browser secured.
loadForBrowserSecured = function(relativePath) {
//REQUIRED: relativePath
var
// absolute path
absolutePath = rootPath + '/' + relativePath,
// extname
extname = path.extname(relativePath),
// content
content = fs.readFileSync(absolutePath);
if (extname === '.js') {
// add to secured browser script.
securedBrowserScript += content + '\n';
} else if (extname === '.__UPPERCASE_IO_COMPILED') {
// add to secured browser script.
securedBrowserScript += content + '\n';
}
},
// load for common.
loadForCommon = function(relativePath) {
//REQUIRED: relativePath
var
// absolute path
absolutePath = rootPath + '/' + relativePath,
// extname
extname = path.extname(relativePath),
// content
content = fs.readFileSync(absolutePath);
if (extname === '.js') {
// add to common script.
commonScript += content + '\n';
} else if (extname === '.__UPPERCASE_IO_COMPILED') {
// add to common script.
commonScript += content + '\n';
}
},
// load for sever.
loadForServer = function(relativePath) {
//REQUIRED: relativePath
var
// absolute path
absolutePath = rootPath + '/' + relativePath,
// extname
extname = path.extname(relativePath),
// content
content = fs.readFileSync(absolutePath);
if (extname === '.js') {
// add to server script.
serverScript += content + '\n';
} else if (extname === '.__UPPERCASE_IO_COMPILED') {
// add to server script.
serverScript += content + '\n';
}
},
// minify
minify = function() {
var
// uglify-js
uglifyJS = require(rootPath + '/UPPERCASE.IO/SERVER/node_modules/uglify-js');
// minify browser script.
browserScript = uglifyJS.minify(browserScript, {
fromString : true,
mangle : true
}).code;
// minify secured browser script.
securedBrowserScript = uglifyJS.minify(securedBrowserScript, {
fromString : true,
mangle : true
}).code;
// minify common script.
commonScript = uglifyJS.minify(commonScript, {
fromString : true,
mangle : true
}).code;
// minify server script.
serverScript = uglifyJS.minify(serverScript, {
fromString : true,
mangle : true
}).code;
};
// pack box.
log('PACKING BOX [' + boxName + ']...');
// create folder.
log('CREATING FOLDER...');
fs.mkdirSync('__PACK/' + boxName);
log('CREATED FOLDER!');
// load box.
log('LOADING BOX...');
// load for browser.
log('LOADING FOR BROWSER...');
scanFolder(boxName + '/BROWSER', loadForBrowser);
log('LOADED FOR BROWSER!');
// load for browser secured.
log('LOADING FOR BROWSER SECURED...');
scanFolder(boxName + '/BROWSER_SECURED', loadForBrowserSecured);
log('LOADED FOR BROWSER SECURED!');
// load for common.
log('LOADING FOR COMMON...');
scanFolder(boxName + '/COMMON', loadForCommon);
log('LOADED FOR COMMON!');
// load for server.
log('LOADING FOR SERVER...');
scanFolder(boxName + '/SERVER', loadForServer);
log('LOADED FOR SERVER!');
log('LOADED BOX!');
// minify.
log('MINIFYING...');
minify();
log('MINIFYED!');
// save box.
log('SAVING BOX...');
scanBoxFolder(function(path) {
copy(path, '__PACK/' + boxName + '/' + path.substring(boxName.length + 1));
});
// save browser script.
if (browserScript !== '') {
log('SAVING BROWSER SCRIPT...');
fs.mkdirSync('__PACK/' + boxName + '/BROWSER');
write('__PACK/' + boxName + '/BROWSER/BROWSER.js', browserScript);
log('SAVED BROWSER SCRIPT!');
}
// save browser secured script.
if (securedBrowserScript !== '') {
log('SAVING BROWSER SECURED SCRIPT...');
fs.mkdirSync('__PACK/' + boxName + '/BROWSER_SECURED');
write('__PACK/' + boxName + '/BROWSER_SECURED/BROWSER_SECURED.js', securedBrowserScript);
log('SAVED BROWSER SECURED SCRIPT!');
}
// save common script.
if (commonScript !== '') {
log('SAVING COMMON SCRIPT...');
fs.mkdirSync('__PACK/' + boxName + '/COMMON');
write('__PACK/' + boxName + '/COMMON/COMMON.js', commonScript);
log('SAVED COMMON SCRIPT!');
}
// save server script.
if (serverScript !== '') {
log('SAVING SERVER SCRIPT...');
fs.mkdirSync('__PACK/' + boxName + '/SERVER');
write('__PACK/' + boxName + '/SERVER/SERVER.js', serverScript);
log('SAVED SERVER SCRIPT!');
}
// save node module.
if (fs.existsSync(boxName + '/SERVER/node_modules') === true) {
log('SAVING NODE MODULES...');
if (fs.existsSync('__PACK/' + boxName + '/SERVER') === false) {
fs.mkdirSync('__PACK/' + boxName + '/SERVER');
}
copy(boxName + '/SERVER/node_modules', '__PACK/' + boxName + '/SERVER/node_modules');
log('SAVED NODE MODULES!');
}
log('SAVED BOX!');
// done!
log('PACKED BOX [' + boxName + ']!');
};
PACKER();