1 | #!/usr/bin/env node
|
2 |
|
3 | 'use strict';
|
4 |
|
5 | var Liftoff = require('liftoff');
|
6 | var chalk = require('chalk');
|
7 | var path = require('path');
|
8 | var logger = console;
|
9 |
|
10 | var cli = new Liftoff({
|
11 | name: 'cha',
|
12 | addExtensions: ['.coffee'],
|
13 | });
|
14 |
|
15 | cli.on('require', function(name) {
|
16 | logger.log('Requiring external module', chalk.magenta(name));
|
17 | });
|
18 |
|
19 | cli.on('requireFail', function(name) {
|
20 | logger.log(chalk.red('Failed to load external module'), chalk.magenta(name));
|
21 | });
|
22 |
|
23 | cli.launch(handler);
|
24 |
|
25 | function handler(env) {
|
26 |
|
27 | var argv = env.argv;
|
28 | var cliPackage = require('../package');
|
29 | var versionFlag = argv.v || argv.version;
|
30 |
|
31 | if(path.extname(env.configPath) === '.coffee'){
|
32 | require('coffee-script/register');
|
33 | }
|
34 |
|
35 | if (versionFlag) {
|
36 | logger.log('Global installed cha CLI version', chalk.green(cliPackage.version));
|
37 | if (env.modulePackage.version) {
|
38 | logger.log('Local installed cha version', chalk.green(env.modulePackage.version));
|
39 | }
|
40 | process.exit(0);
|
41 | }
|
42 |
|
43 | if (!env.configPath) {
|
44 | logger.log(chalk.red('No chafile found in'), chalk.magenta(env.cwd));
|
45 | process.exit(1);
|
46 | }
|
47 |
|
48 | if (!env.modulePath) {
|
49 | logger.log(chalk.red('No local cha installed found in'), chalk.magenta(env.cwd));
|
50 | logger.log(chalk.red('Try execute: npm install cha'));
|
51 | process.exit(1);
|
52 | }
|
53 |
|
54 | if (process.cwd() !== env.cwd) {
|
55 | process.chdir(env.cwd);
|
56 | logger.log('Working directory changed to', chalk.magenta(env.cwd));
|
57 | }
|
58 |
|
59 | logger.log('Using chafile', chalk.magenta(env.configPath));
|
60 | require(env.configPath);
|
61 |
|
62 | }
|