@@ -4,30 +4,8 @@ var cmd = require('commander'); |
4 | 4 | var Cube = require('../index'); |
5 | 5 | var fs = require('xfs'); |
6 | 6 | var path = require('path'); |
7 | | -var JsProcessor = require('../lib/processor_js2'); |
8 | 7 | var pkg = require(path.join(__dirname, '../package')); |
9 | | -var spawn = require('child_process').spawn; |
10 | | - |
11 | | -function exec(cmd, cb) { |
12 | | - var args = cmd.split(/\s/); |
13 | | - cmd = spawn(args[0], args.slice(1), process.env); |
14 | | - cmd.stdout.on('data', function (data) { |
15 | | - console.log(data.toString()); |
16 | | - }); |
17 | | - cmd.stdout.on('error', function (data) { |
18 | | - console.log(data.toString()); |
19 | | - }); |
20 | | - |
21 | | - cmd.stderr.on('data', function (data) { |
22 | | - console.log(data.toString()); |
23 | | - }); |
24 | | - cmd.stderr.on('error', function (data) { |
25 | | - console.log(data.toString()); |
26 | | - }); |
27 | | - |
28 | | - cmd.on('close', cb); |
29 | | -} |
30 | | - |
| 8 | +var childProcess = require('child_process'); |
31 | 9 |
|
32 | 10 | function getCwd() { |
33 | 11 | return process.cwd(); |
@@ -130,42 +108,30 @@ cmd.command('install') |
130 | 108 | .description('install dependences') |
131 | 109 | .action(function (option, dir) { |
132 | 110 | dir = dir ? filePath(dir) : getCwd(); |
133 | | - var cubeCfg; |
134 | | - try { |
135 | | - cubeCfg = require(path.join(dir, 'cube.json')); |
136 | | - } catch (e) { |
137 | | - console.log('nothing need to install'); |
138 | | - return; |
| 111 | + var out = childProcess.execSync('npm3 -v').toString().trim(); |
| 112 | + var m = out.match(/(\d+)\.(\d+)\.(\d+)/); |
| 113 | + if (!m) { |
| 114 | + console.log('[Error] npm3 not installed'); |
| 115 | + console.log('using this cmd: npm install -g npm3'); |
| 116 | + process.exit(1); |
139 | 117 | } |
140 | | - var deps = cubeCfg.deps || cubeCfg.dependencies; |
141 | | - if (!deps) { |
142 | | - return; |
143 | | - } |
144 | | - var pkgList = []; |
145 | | - Object.keys(deps).forEach(function (v) { |
146 | | - pkgList.push(v + '@' + deps[v]); |
147 | | - }); |
148 | | - exec('npm install ' + pkgList.join(' '), function (code) { |
149 | | - console.log('install ended', code); |
150 | | - }); |
| 118 | + childProcess.execSync('npm3 install'); |
151 | 119 | }); |
152 | 120 |
|
153 | 121 | cmd.command('update') |
154 | 122 | .description('update cube.min.js') |
155 | | - .option('--min', 'simple cube.min.js, no other run_time inculde') |
| 123 | + .option('-a, --all', 'simple cube.min.js, no other run_time inculde') |
156 | 124 | .action(function (option) { |
157 | 125 | findCube(function (cubeFile) { |
158 | 126 | if (!cubeFile) { |
159 | 127 | return; |
160 | 128 | } |
161 | 129 | var codeCube = '//cube.min.js\n' + fs.readFileSync(sourcePath('./runtime/cube.min.js')).toString(); |
162 | | - if (!option.min) { |
| 130 | + if (option.all) { |
163 | 131 | console.log('enabled jade'); |
164 | 132 | codeCube += '\n//jade_runtime\n' + fs.readFileSync(sourcePath('./runtime/jade_runtime.min.js')); |
165 | 133 | console.log('enabled ejs'); |
166 | 134 | codeCube += '\n//ejs_runtime\n' + fs.readFileSync(sourcePath('./runtime/ejs_runtime.min.js')); |
167 | | - console.log('enabled mcss'); |
168 | | - codeCube += '\n//cube_css.min.js\n' + fs.readFileSync(sourcePath('./runtime/cube_css.min.js')); |
169 | 135 | optmizeInfo(); |
170 | 136 | } |
171 | 137 | fs.sync().save(cubeFile, codeCube); |
@@ -259,10 +225,126 @@ cmd.command('build') |
259 | 225 | } |
260 | 226 | }); |
261 | 227 |
|
| 228 | +/** |
| 229 | + * build into single file |
| 230 | + */ |
| 231 | +cmd.command('all-in-one') |
| 232 | + .usage('-p processor1,processor2 your_file') |
| 233 | + .option('-o, --output [value]', 'output file') |
| 234 | + .option('-b, --base [value]', 'set dir base') |
| 235 | + .option('-r, --resbase [value]', 'resource base') |
| 236 | + .action(function (source, args) { |
| 237 | + if (!args || !source) { |
| 238 | + return this.help(); |
| 239 | + } |
| 240 | + if (args.processors) { |
| 241 | + args.processors = args.processors.split(','); |
| 242 | + args.processors.forEach(function (v, i, a) { |
| 243 | + a[i] = v.trim(); |
| 244 | + }); |
| 245 | + } |
| 246 | + |
| 247 | + var cwd = getCwd(); |
| 248 | + var outputPath, cube, tool, root; |
| 249 | + var compress = true; |
| 250 | + var fstat; |
| 251 | + root = args.base ? args.base : ''; |
| 252 | + if (root) { |
| 253 | + root = isAbsPath(root) ? root : path.join(cwd, root); |
| 254 | + } |
| 255 | + source = isAbsPath(source) ? source : path.join(cwd, source); |
| 256 | + try { |
| 257 | + fstat = fs.statSync(source); |
| 258 | + } catch (e) { |
| 259 | + console.log('source not fould', e); |
| 260 | + this.help(); |
| 261 | + return; |
| 262 | + } |
| 263 | + if (fstat.isDirectory()) { |
| 264 | + console.log('proess file only'); |
| 265 | + return ; |
| 266 | + } |
| 267 | + outputPath = args.output ? (isAbsPath(args.output) ? args.output : path.join(cwd, args.output)) : source.replace(/\.(\w+)$/, '.release'); |
| 268 | + root = (root ? root : path.dirname(source)).replace(/(\/|\\)$/, ''); |
| 269 | + cube = new Cube({ |
| 270 | + release: true, |
| 271 | + root: root, |
| 272 | + compress: compress, |
| 273 | + middleware: false, |
| 274 | + processors: args.processors, |
| 275 | + resBase: args.resbase, |
| 276 | + remote: args.remote |
| 277 | + }); |
| 278 | + tool = Cube.getTool(); |
| 279 | + var codes = []; |
| 280 | + var styles = []; |
| 281 | + var infos = []; |
| 282 | + var codesMap = {}; |
| 283 | + var indent = 0; |
| 284 | + function _cb(err, res, i) { |
| 285 | + if (err.length) { |
| 286 | + err.forEach(function () { |
| 287 | + |
| 288 | + }); |
| 289 | + return ; |
| 290 | + } |
| 291 | + var result = res.result; |
| 292 | + if (codesMap[result.file]) { |
| 293 | + return; |
| 294 | + } |
| 295 | + codesMap[result.file] = true; |
| 296 | + if (result.type === 'style') { |
| 297 | + styles.push(result.code); |
| 298 | + } else { |
| 299 | + codes.unshift(result.type !== 'script' ? result.wraped : result.code); |
| 300 | + } |
| 301 | + infos.push(fmtInfo(i, result.file)); |
| 302 | + if (result.originalRequires) { |
| 303 | + var n = i + 1; |
| 304 | + result.originalRequires.forEach(function (m) { |
| 305 | + if (!fs.existsSync(path.join(root, m))) { |
| 306 | + return console.error('[error] missing require:', m); |
| 307 | + } |
| 308 | + if (codesMap[m]) { |
| 309 | + return; |
| 310 | + } |
| 311 | + |
| 312 | + tool.processFile(cube, path.join(root, m), null, {}, function (err, data) { |
| 313 | + _cb(err, data, n); |
| 314 | + }); |
| 315 | + }); |
| 316 | + } |
| 317 | + } |
| 318 | + tool.processFile(cube, source, null, {}, function (err, data) { |
| 319 | + _cb(err, data, indent); |
| 320 | + }); |
| 321 | + codes.unshift('var Cube = g.Cube;'); |
| 322 | + codes.unshift(fs.readFileSync(path.join(__dirname, '../runtime/cube.min.js')).toString().replace('window,null', 'g,null')); |
| 323 | + codes.unshift('!(function(host, propName){\nvar g = {};'); |
| 324 | + codes.push('Cube.use("' + infos[0] + '", function(mod) { host[propName] = mod;});'); |
| 325 | + codes.push('})(host, "cubeExports")'); |
| 326 | + fs.sync().save(path.join(outputPath, '/script.js'), codes.join('\n')); |
| 327 | + fs.sync().save(path.join(outputPath, '/style.css'), styles.join('\n')); |
| 328 | + console.log('===== deps ====='); |
| 329 | + console.log(infos.join('\n')); |
| 330 | + console.log('======'); |
| 331 | + console.log('output dir:', outputPath); |
| 332 | + console.log('======'); |
| 333 | + }); |
| 334 | + |
| 335 | +function fmtInfo(indent) { |
| 336 | + var args = [].slice.call(arguments, 1); |
| 337 | + var str = []; |
| 338 | + for (var i = 0 ; i < indent ; i ++) { |
| 339 | + str.push(' '); |
| 340 | + } |
| 341 | + args.unshift(str.join('')); |
| 342 | + return args.join(''); |
| 343 | +} |
| 344 | + |
262 | 345 | /** |
263 | 346 | * build工程目录,并进行自动合并优化 |
264 | 347 | * node_modules目录将进行选择性build |
265 | | - * |
266 | 348 | */ |
267 | 349 | cmd.command('smart-build') |
268 | 350 | /** |
@@ -340,6 +422,7 @@ cmd.command('smart-build') |
340 | 422 | } |
341 | 423 | }); |
342 | 424 |
|
| 425 | +/* |
343 | 426 | cmd.command('dag') |
344 | 427 | .usage('your_code_dir') |
345 | 428 | .description('gen DAG graph') |
@@ -397,6 +480,7 @@ cmd.command('dag') |
397 | 480 | console.log('browser open: ' + root + '/dag_out.html'); |
398 | 481 | } |
399 | 482 | }); |
| 483 | +*/ |
400 | 484 |
|
401 | 485 | cmd.command('help') |
402 | 486 | .description('help') |
|