UNPKG

4.41 kBPlain TextView Raw
1#!/usr/bin/env node
2var program = require('commander');
3var path = require('path');
4var fs = require('fs');
5var root = path.join(path.dirname(fs.realpathSync(__filename)), '..');
6
7/*
8#var complete = require('complete');
9
10#complete.list = ['install','compile','build','server','test','watch'];
11
12#complete({
13# program:"compy",
14# commands:{
15# server:function(words, prev, cur){
16# complete.output(cur, ['watch']);
17# },
18# },
19# options:{
20# '--help':{}
21# }
22#})
23#complete.init();
24*/
25
26var packageJson = require(root + '/package.json');
27var isComponent;
28
29if(fs.existsSync(process.cwd() + '/package.json')){
30 var projectJson = require(process.cwd() + '/package.json');
31 if(projectJson.compy) isComponent = projectJson.compy.publishto;
32};
33
34program
35 .version(packageJson.version)
36 .usage('<command> [options]')
37 .option('-d, --dir <path>', 'project source path. Must contain package.json')
38 .option('-o, --output <path>', 'output directory for build/compile')
39 .option('-l, --library', 'generate standalone library')
40 .option('-c, --component', 'generate component')
41 .option('-a, --application', 'generate application [default]')
42 .option('-v, --verbose', 'verbosity')
43 .option('-f, --force', 'force installation of packages')
44 .option('-s, --staticServer <path>', 'custom server that serves static with compy middleware')
45 .option('--dev', 'install dev dependencies')
46
47program.on('--help', function(){
48 console.log(' Commands:');
49 console.log();
50 console.log(' install [name ...] install dependencies or component');
51 console.log(' compile compile app (in dist folder by default)');
52 console.log(' build build the app (compile and minify sources)');
53 console.log(' server [watch] run static server. If "watch" option enabled - watch changes, recompile and push livereload');
54 console.log(' test run karma tests');
55 console.log(' watch watch and rebuild assets on change');
56 console.log(' plate [appname] generate boilerplate package.json');
57 if(isComponent) console.log(' publish [version] publish version as a package');
58 console.log();
59 console.log();
60}).parse(process.argv);
61
62var cmd = program.args[0];
63if (!cmd) {
64 process.stdout.write(program.helpInformation());
65 program.emit('--help');
66 process.exit();
67}
68
69var options = {
70 cwd : program.dir && path.resolve(program.dir) || process.cwd(),
71 destination : program.output && path.resolve(program.output) || './dist',
72 verbose : !!program.verbose,
73 server : program.staticServer && path.resolve(program.staticServer),
74 isStandaloneLib: program.library,
75 isComponentjs: program.component,
76 isApplication: program.application,
77 force: !!program.force,
78 devInstall : !!program.dev
79}
80var argv = process.argv.slice(0,2);
81argv.push(program.args.map(function(a){return a.toString()}).join(":"));
82
83process.argv = argv;
84process.program = program;
85
86
87// execute `plate` command BEFORE Grunt.js context
88if (cmd == "plate") {
89 var name = program.args.length > 1 ? program.args[1] : "test";
90 if (fs.existsSync(options.cwd + "/package.json")) {
91 console.log("File packages.json already exist");
92 return;
93 }
94 console.log("Creating boilerplate 'package.json' with app name '" + name + "' ...");
95
96 fs.readFile(root + "/package.json.tpl", function (err, data) {
97 if (err) throw err;
98 fs.open(options.cwd + "/package.json", "w", function (err, fd) {
99 fs.write(fd, data.toString().replace(/\$name/g, name));
100 fs.close(fd);
101 console.log("You can start building your app with appstart.js file");
102 })
103 });
104 return;
105}
106
107if(!fs.existsSync(options.cwd + '/package.json')){
108 console.log("Please, create package.json in current dir");
109 console.log("package.json file with compy.main defined is required");
110 return;
111};
112
113var projectJson = require(options.cwd + '/package.json');
114
115if(!projectJson.compy){
116 console.log("Please define main file for compy in package.json (compy.main)");
117 console.log("package.json file with compy.main defined is required");
118 return;
119}
120
121// execute grunt task
122require('grunt').cli({gruntfile: root + "/Gruntfile.js",
123 cmd:cmd,
124 targetBase: options.cwd,
125 base: options.cwd,
126 destination: options.destination,
127 verbose : options.verbose,
128 server: options.server,
129 force: options.force,
130 devInstall: options.devInstall});