forked from searble/lwot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
472 lines (389 loc) · 17.4 KB
/
app.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
'use strict';
module.exports = (()=> {
const clc = require("cli-color");
const path = require("path");
const fs = require("fs");
const fsext = require('fs-extra');
const fsTracer = require('./libs/fs-tracer');
const utility = require('./libs/utility');
const PROJECT_ROOT = path.resolve('.');
const LWOT_FILE = path.resolve(PROJECT_ROOT, 'lwot.json');
const SOURCE_ROOT = path.resolve(PROJECT_ROOT, 'src');
const CONTROLLER_ROOT = path.resolve(PROJECT_ROOT, 'controller');
const PLUGIN_ROOT = path.resolve(PROJECT_ROOT, 'plugins');
let messageBroker = function () {
if (arguments.length <= 2) return;
let color = arguments[0];
let cmd = arguments[1];
let message = '';
for (let i = 2; i < arguments.length; i++)
message = message + arguments[i] + ' ';
console.log(clc[color]('[' + cmd + ']'), message);
};
let loadPlugins = ()=> {
let lib = {};
if (fs.existsSync(PLUGIN_ROOT) === false)
return lib;
let pluginList = fs.readdirSync(PLUGIN_ROOT);
for (let i = 0; i < pluginList.length; i++) {
let PLUGIN_DIR = path.resolve(PLUGIN_ROOT, pluginList[i]);
let PLUGIN_NAME = pluginList[i];
if (fs.lstatSync(PLUGIN_DIR).isDirectory() === false)
continue;
lib[PLUGIN_NAME] = {};
let files = fs.readdirSync(PLUGIN_DIR);
for (let i = 0; i < files.length; i++) {
let platformPath = path.resolve(PLUGIN_DIR, files[i]);
if (fs.lstatSync(platformPath).isDirectory() === false) continue;
let MODULE_NAME = files[i];
try {
lib[PLUGIN_NAME][MODULE_NAME] = require(platformPath);
} catch (e) {
}
}
}
return lib;
};
// [module] load plugin
let plugins = loadPlugins();
// [module] platform dependent functions
let platformFunction = (fn, platforms)=> new Promise((callback)=> {
let st = new Date();
let platform = platforms.splice(0, 1)[0];
if (!fn) {
messageBroker('yellow', platform, `use platform functions. eg) 'lwot ${platform} run'`);
return;
}
if (!platform) {
callback('help');
return;
}
if (!plugins.platform || !plugins.platform[platform]) {
messageBroker('red', fn, `${platform} not exists. please "lwot install platform ${platform}"`);
callback();
return;
}
if (!plugins.platform[platform][fn]) {
messageBroker('yellow', fn, `${platform} not support ${fn}.`);
callback();
return;
}
messageBroker('blue', fn, `${fn} start '${platform}'`);
plugins.platform[platform][fn](platforms).then(()=> {
let duetime = new Date().getTime() - st.getTime();
messageBroker('blue', fn, `${fn} finished '${platform}' (${duetime}ms)`);
callback();
});
});
// [lib]
let lib = {};
// [lib] create: create project
lib.create = (args)=> new Promise((callback)=> {
let st = new Date();
if (!args || args.length == 0) {
callback('help');
return;
}
let libPath = __dirname;
let initPath = path.resolve(libPath, 'res', 'init');
let destPath = path.resolve('.', args[0]);
if (fs.existsSync(destPath)) {
messageBroker('red', 'create', '"' + destPath + '"', 'already exists');
callback();
return;
}
fsext.copySync(initPath, destPath);
fsext.copySync(path.resolve(libPath, 'res', 'idea', 'project.iml'), path.resolve(destPath, '.idea', args[0] + '.iml'));
fsext.copySync(path.resolve(libPath, 'res', 'idea', 'misc.xml'), path.resolve(destPath, '.idea', 'misc.xml'));
fsext.copySync(path.resolve(libPath, 'res', 'idea', 'jsLibraryMappings.xml'), path.resolve(destPath, '.idea', 'jsLibraryMappings.xml'));
fs.writeFileSync(path.resolve(destPath, '.idea', 'modules.xml'), fs.readFileSync(path.resolve(libPath, 'res', 'idea', 'modules.xml'), 'utf-8').replace(/PRJNAME/gim, args[0]));
let lwotjson = JSON.parse(fs.readFileSync(path.resolve(destPath, 'lwot.json'), 'utf-8'));
lwotjson.name = args[0];
fs.writeFileSync(path.resolve(destPath, 'lwot.json'), JSON.stringify(lwotjson, null, 4));
utility.bower(destPath).then(()=> {
let duetime = new Date().getTime() - st.getTime();
messageBroker('blue', 'create', `created at '${destPath}' (${duetime}ms)`);
callback();
});
});
// [lib] install, i: install plugins
lib.install = (cmds)=> new Promise((callback)=> {
let st = new Date();
let lwotConfig = JSON.parse(fs.readFileSync(LWOT_FILE, 'utf-8'));
let finalize = (plugin, packageName)=> new Promise((resolve)=> {
plugins = loadPlugins();
if (plugins[plugin][packageName] && plugins[plugin][packageName].install) {
plugins[plugin][packageName].install().then(()=> {
let duetime = new Date().getTime() - st.getTime();
messageBroker('blue', 'install', `${plugin} "${packageName}" installed (${duetime}ms)`);
resolve();
});
} else {
let duetime = new Date().getTime() - st.getTime();
messageBroker('blue', 'install', `${plugin} "${packageName}" installed (${duetime}ms)`);
resolve();
}
});
if (!cmds || cmds.length === 0) {
// copy webstorm settings
if (!fs.existsSync(path.resolve('.', '.idea'))) {
fsext.copySync(path.resolve(__dirname, 'res', 'idea', 'project.iml'), path.resolve('.', '.idea', lwotConfig.name + '.iml'));
fsext.copySync(path.resolve(__dirname, 'res', 'idea', 'misc.xml'), path.resolve('.', '.idea', 'misc.xml'));
fsext.copySync(path.resolve(__dirname, 'res', 'idea', 'jsLibraryMappings.xml'), path.resolve('.', '.idea', 'jsLibraryMappings.xml'));
fs.writeFileSync(path.resolve('.', '.idea', 'modules.xml'), fs.readFileSync(path.resolve(__dirname, 'res', 'idea', 'modules.xml'), 'utf-8').replace(/PRJNAME/gim, lwotConfig.name));
}
if (!lwotConfig.dependencies) {
return;
}
let installList = [];
for (let pluginName in lwotConfig.dependencies) {
for (let plugin in lwotConfig.dependencies[pluginName]) {
let pluginInfo = lwotConfig.dependencies[pluginName][plugin];
installList.push({name: plugin, plugin: pluginName, uri: pluginInfo});
}
}
let idx = 0;
let autoInstall = ()=> {
let pinfo = installList[idx++];
if (!pinfo) {
let duetime = new Date().getTime() - st.getTime();
messageBroker('blue', 'install', `installed (${duetime}ms)`);
callback();
return;
}
let then = (status)=> {
if (status.error)
messageBroker('red', 'install', `${pinfo.plugin} "${pinfo.name}" ${status.message} error in install.`);
autoInstall();
};
utility.plugins[pinfo.plugin](pinfo.uri, pinfo.name).then((status)=> {
finalize(pinfo.plugin, pinfo.name).then(()=> {
then(status);
});
});
};
messageBroker('blue', 'install', 'auto install');
autoInstall();
return;
}
let plugin = cmds[0];
let pluginUrl = cmds[1];
if (!pluginUrl) {
plugin = 'auto';
pluginUrl = cmds[0];
} else if (!utility.plugins[plugin]) {
return callback('help');
}
messageBroker('blue', 'install', plugin);
utility.plugins[plugin](pluginUrl).then((status)=> {
if (status.error) {
messageBroker('red', 'install', status.message);
return;
}
let packageName = status.lwot.name;
let packageVersion = status.lwot.version;
plugin = status.lwot.plugin;
if (!lwotConfig.dependencies) lwotConfig.dependencies = {};
if (!lwotConfig.dependencies[plugin]) lwotConfig.dependencies[plugin] = {};
lwotConfig.dependencies[plugin][packageName] = status.uri;
fs.writeFileSync(LWOT_FILE, JSON.stringify(lwotConfig, null, 4));
finalize(plugin, packageName).then(callback);
});
});
lib.i = lib.install;
// [lib] remove, rm: remove plugins
lib.remove = (cmds)=> new Promise((callback)=> {
let plugin = cmds[0];
let packageName = cmds[1];
if (!plugin || !utility.plugins[plugin] || !packageName) {
callback('help');
return;
}
if (!utility.plugins[plugin]) {
callback('help');
return;
}
let REMOVE_PATH = path.resolve(PLUGIN_ROOT, plugin, packageName);
if (!fs.existsSync(REMOVE_PATH)) {
messageBroker('yellow', 'remove', `${plugin} "${packageName}" does not installed.`);
callback();
return;
}
fsext.removeSync(REMOVE_PATH);
let lwotConfig = JSON.parse(fs.readFileSync(LWOT_FILE, 'utf-8'));
delete lwotConfig.dependencies[plugin][packageName];
fs.writeFileSync(LWOT_FILE, JSON.stringify(lwotConfig, null, 4));
messageBroker('blue', 'remove', `${plugin} "${packageName}" removed.`);
});
lib.rm = lib.remove;
// TODO [lib] publish, pub: publish to http://lwot.org repo.
lib.publish = (args)=> new Promise((callback)=> {
});
lib.pub = lib.publish;
// [lib] clean: clean plugins
lib.clean = ()=> new Promise((callback)=> {
if (!fs.existsSync(PLUGIN_ROOT)) {
messageBroker('yellow', 'clean', `any plugins does not installed.`);
callback();
return;
}
fsext.removeFileSync(PLUGIN_ROOT);
let lwotConfig = JSON.parse(fs.readFileSync(LWOT_FILE, 'utf-8'));
delete lwotConfig.dependencies;
fs.writeFileSync(LWOT_FILE, JSON.stringify(lwotConfig, null, 4));
messageBroker('yellow', 'clean', `project clean`);
});
// [lib] bower: lwot bower install
lib.bower = (args)=> new Promise((callback)=> {
if (!args) args = [];
args.push('--save');
utility.terminal('bower', args, {cwd: PROJECT_ROOT}).then(callback);
});
// [lib] npm: lwot npm [platform] [npm-cmd] [node_modules] ...
lib.npm = (args)=> new Promise((callback)=> {
let platfromName = args.splice(0, 1)[0];
let ncmd = args.splice(0, 1)[0];
if (!platfromName || !ncmd) {
callback('help');
return;
}
let APP_ROOT = path.resolve(PLUGIN_ROOT, 'platform', platfromName, 'app');
let APP_PACKAGE_FILE = path.resolve(APP_ROOT, 'package.json');
let CTRL_PACKAGE_FILE = path.resolve(CONTROLLER_ROOT, platfromName, 'package.json');
if (!fs.existsSync(APP_ROOT)) {
messageBroker('yellow', 'npm', `"${platfromName}" does not installed.`);
callback();
return;
}
if (!fs.existsSync(APP_PACKAGE_FILE)) {
messageBroker('yellow', 'npm', `"${platfromName}" does not support npm.`);
callback();
return;
}
let params = JSON.parse(JSON.stringify(args));
params.unshift('--save');
params.unshift(ncmd);
utility.terminal('npm', params, {cwd: APP_ROOT}).then(()=> {
if (!fs.existsSync(CTRL_PACKAGE_FILE)) {
fsext.copySync(APP_PACKAGE_FILE, CTRL_PACKAGE_FILE);
} else {
let cpf = JSON.parse(fs.readFileSync(CTRL_PACKAGE_FILE, 'utf-8'));
let apf = JSON.parse(fs.readFileSync(APP_PACKAGE_FILE, 'utf-8'));
cpf.dependencies = apf.dependencies;
fs.writeFileSync(CTRL_PACKAGE_FILE, JSON.stringify(cpf, null, 4));
}
messageBroker('blue', 'npm', `installed to "${platfromName}"`);
callback();
});
});
// [lib] build
lib.build = (platforms)=> new Promise((callback)=> {
let dest = [];
let PLATFORM_ROOT = path.resolve(PLUGIN_ROOT, 'platform');
if (platforms.length == 0)
platforms = fs.readdirSync(PLATFORM_ROOT);
// find platforms
for (let i = 0; i < platforms.length; i++)
if (fs.lstatSync(path.resolve(PLATFORM_ROOT, platforms[i])).isDirectory())
dest.push({name: platforms[i], dest: path.resolve(PLATFORM_ROOT, platforms[i], 'app')});
// Generation of jadeIncludeRelationTree.json
utility.createJadeIncludeRelationTree(SOURCE_ROOT);
let idx = 0;
let compileLoop = ()=> {
let destPoint = dest[idx++];
if (!destPoint) {
callback();
return;
}
let st = new Date();
let CTRL_SRC = path.resolve(CONTROLLER_ROOT, destPoint.name);
let CTRL_DEST = path.resolve(destPoint.dest, 'controller');
let WWW_SRC = SOURCE_ROOT;
let WWW_DEST = path.resolve(destPoint.dest, 'www');
let BOWER_SRC = path.resolve(PROJECT_ROOT, 'bower_components');
let BOWER_DEST = path.resolve(WWW_DEST, 'libs');
let compilerFn = require('./libs/compiler');
if (plugins.platform[destPoint.name].compile && typeof plugins.platform[destPoint.name].compile == 'object')
compilerFn = plugins.platform[destPoint.name].compile;
if (fs.existsSync(CTRL_DEST)) {
fsext.removeSync(CTRL_DEST);
}
if (!fs.existsSync(WWW_DEST)) {
fsext.removeSync(WWW_DEST);
}
fsext.mkdirsSync(CTRL_DEST);
fsext.mkdirsSync(WWW_DEST);
// compile ui components
fsTracer.compile(compilerFn, WWW_SRC, WWW_DEST, true, true)
.then(()=> fsTracer.compile(null, CTRL_SRC, CTRL_DEST, true, true))
.then(()=> new Promise((next)=> {
if (fs.existsSync(path.resolve(CTRL_SRC, 'package.json')))
fsext.copySync(path.resolve(CTRL_SRC, 'package.json'), path.resolve(destPoint.dest, 'package.json'));
try {
fsext.mkdirsSync(BOWER_DEST);
} catch (e) {
}
try {
fsext.copySync(BOWER_SRC, BOWER_DEST);
} catch (e) {
}
next();
}))
.then(()=> utility.npm(destPoint.dest))
.then(()=> {
let duetime = new Date().getTime() - st.getTime();
messageBroker('blue', 'build', destPoint.name, '(' + duetime + 'ms)');
compileLoop();
});
};
compileLoop();
});
// [lib] watch
lib.watch = (platforms)=> new Promise((callback)=> {
lib.build(JSON.parse(JSON.stringify(platforms))).then(()=> {
let SRC_WATCH = [];
let CTRL_WATCH = [];
let PLATFORM_ROOT = path.resolve(PLUGIN_ROOT, 'platform');
if (platforms.length == 0)
platforms = fs.readdirSync(PLATFORM_ROOT);
for (let i = 0; i < platforms.length; i++) {
if (fs.lstatSync(path.resolve(PLATFORM_ROOT, platforms[i])).isDirectory()) {
let compilerFn = require('./libs/compiler');
if (plugins.platform[platforms[i]].compile && typeof plugins.platform[platforms[i]].compile == 'object')
compilerFn = plugins.platform[platforms[i]].compile;
SRC_WATCH.push({
compiler: compilerFn,
dest: path.resolve(PLATFORM_ROOT, platforms[i], 'app', 'www')
});
CTRL_WATCH.push({
compiler: null,
dest: path.resolve(PLATFORM_ROOT, platforms[i], 'app', 'controller')
});
}
}
messageBroker('blue', 'watch', 'now watching src folder');
fsTracer.watch(SOURCE_ROOT, SRC_WATCH);
fsTracer.watch(CONTROLLER_ROOT, CTRL_WATCH);
callback();
});
});
// TODO [lib] struct
lib.struct = ()=> new Promise((callback)=> {
});
// TODO [lib] template
lib.template = ()=> new Promise((callback)=> {
});
lib.v = lib['-v'] = lib.version = ()=> new Promise((callback)=> {
let version = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'), 'utf-8')).version;
console.log(`v${version}`);
});
// [lib] bind platform function
for (let platform in plugins.platform)
if (!lib[platform])
lib[platform] = (args) => new Promise((next)=> {
let fn = args.splice(0, 1);
args.unshift(platform);
platformFunction(fn, args).then(next);
});
return lib;
})();