#!/usr/bin/env node var program = require('commander'); var path = require('path'); var fs = require('fs'); var root = path.join(path.dirname(fs.realpathSync(__filename)), '..'); /* #var complete = require('complete'); #complete.list = ['install','compile','build','server','test','watch']; #complete({ # program:"compy", # commands:{ # server:function(words, prev, cur){ # complete.output(cur, ['watch']); # }, # }, # options:{ # '--help':{} # } #}) #complete.init(); */ var packageJson = require(root + '/package.json'); var isComponent; if(fs.existsSync(process.cwd() + '/package.json')){ var projectJson = require(process.cwd() + '/package.json'); if(projectJson.compy) isComponent = projectJson.compy.publishto; }; program .version(packageJson.version) .usage(' [options]') .option('-d, --dir ', 'project source path. Must contain package.json') .option('-o, --output ', 'output directory for build/compile') // .option('-l, --library', 'generate standalone library') // .option('-c, --component', 'generate component') // .option('-a, --application', 'generate application [default]') .option('-v, --verbose', 'verbosity') .option('-f, --force', 'force installation of packages') .option('-s, --staticServer ', 'custom server that serves static with compy middleware') .option(' --dev', 'install dev dependencies') program.on('--help', function(){ console.log(' Commands:'); console.log(); console.log(' install [name ...] install dependencies or component'); console.log(' compile compile app (in dist folder by default)'); console.log(' build build the app (compile and minify sources)'); console.log(' server [watch] run static server. If "watch" option enabled - watch changes, recompile and push livereload'); console.log(' test run karma tests'); console.log(' watch watch and rebuild assets on change'); console.log(' plate [appname] generate boilerplate package.json'); if(isComponent) console.log(' publish [version] publish version as a package'); console.log(' graph show all dependencies/versions installed') console.log(); console.log(); }).parse(process.argv); var cmd = program.args[0]; if (!cmd) { process.stdout.write(program.helpInformation()); program.emit('--help'); process.exit(); } var options = { cwd : program.dir && path.resolve(program.dir) || process.cwd(), destination : program.output && path.resolve(program.output) || './dist', verbose : !!program.verbose, server : program.staticServer && path.resolve(program.staticServer), isStandaloneLib: program.library, isComponentjs: program.component, isApplication: program.application, force: !!program.force, devInstall : !!program.dev } var argv = process.argv.slice(0,2); argv.push(program.args.map(function(a){return a.toString()}).join(":")); process.argv = argv; process.program = program; // execute `plate` command BEFORE Grunt.js context if (cmd == "plate") { var name = program.args.length > 1 ? program.args[1] : "test"; if (fs.existsSync(options.cwd + "/package.json")) { console.log("File packages.json already exist"); return; } console.log("Creating boilerplate 'package.json' with app name '" + name + "' ..."); fs.readFile(root + "/package.json.tpl", function (err, data) { if (err) throw err; fs.open(options.cwd + "/package.json", "w", function (err, fd) { fs.write(fd, data.toString().replace(/\$name/g, name)); fs.close(fd); console.log("You can start building your app with appstart.js file"); }) }); return; } if(!fs.existsSync(options.cwd + '/package.json')){ console.log("Please, create package.json in current dir"); console.log("package.json file with compy.main defined is required"); return; }; var projectJson = require(options.cwd + '/package.json'); if(!projectJson.compy){ console.log("Please define main file for compy in package.json (compy.main)"); console.log("package.json file with compy.main defined is required"); return; } // execute grunt task require('grunt').cli({gruntfile: root + "/Gruntfile.js", cmd:cmd, targetBase: options.cwd, base: options.cwd, destination: options.destination, verbose : options.verbose, server: options.server, force: options.force, devInstall: options.devInstall});