UNPKG

4.5 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(' graph show all dependencies/versions installed')
59 console.log();
60 console.log();
61}).parse(process.argv);
62
63var cmd = program.args[0];
64if (!cmd) {
65 process.stdout.write(program.helpInformation());
66 program.emit('--help');
67 process.exit();
68}
69
70var options = {
71 cwd : program.dir && path.resolve(program.dir) || process.cwd(),
72 destination : program.output && path.resolve(program.output) || './dist',
73 verbose : !!program.verbose,
74 server : program.staticServer && path.resolve(program.staticServer),
75 isStandaloneLib: program.library,
76 isComponentjs: program.component,
77 isApplication: program.application,
78 force: !!program.force,
79 devInstall : !!program.dev
80}
81var argv = process.argv.slice(0,2);
82argv.push(program.args.map(function(a){return a.toString()}).join(":"));
83
84process.argv = argv;
85process.program = program;
86
87
88// execute `plate` command BEFORE Grunt.js context
89if (cmd == "plate") {
90 var name = program.args.length > 1 ? program.args[1] : "test";
91 if (fs.existsSync(options.cwd + "/package.json")) {
92 console.log("File packages.json already exist");
93 return;
94 }
95 console.log("Creating boilerplate 'package.json' with app name '" + name + "' ...");
96
97 fs.readFile(root + "/package.json.tpl", function (err, data) {
98 if (err) throw err;
99 fs.open(options.cwd + "/package.json", "w", function (err, fd) {
100 fs.write(fd, data.toString().replace(/\$name/g, name));
101 fs.close(fd);
102 console.log("You can start building your app with appstart.js file");
103 })
104 });
105 return;
106}
107
108if(!fs.existsSync(options.cwd + '/package.json')){
109 console.log("Please, create package.json in current dir");
110 console.log("package.json file with compy.main defined is required");
111 return;
112};
113
114var projectJson = require(options.cwd + '/package.json');
115
116if(!projectJson.compy){
117 console.log("Please define main file for compy in package.json (compy.main)");
118 console.log("package.json file with compy.main defined is required");
119 return;
120}
121
122// execute grunt task
123require('grunt').cli({gruntfile: root + "/Gruntfile.js",
124 cmd:cmd,
125 targetBase: options.cwd,
126 base: options.cwd,
127 destination: options.destination,
128 verbose : options.verbose,
129 server: options.server,
130 force: options.force,
131 devInstall: options.devInstall});