UNPKG

2.66 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3var Liftoff = require( 'liftoff' ),
4 interpret = require( 'interpret' ),
5 v8flags = require ( 'v8flags' ),
6 path = require( 'path' ),
7 chalk = require( 'chalk' ),
8 minimist = require( 'minimist' ),
9 findup = require( 'findup-sync' ),
10 semver = require( 'semver' ),
11 logger = require( './utils/logger' ),
12 serve = require( './serve' ),
13 watch = require( './watch' ),
14 build = require( './build' ),
15 help = require( './help' ),
16
17 command,
18 cli;
19
20// This prevents the prompt from being cluttered up with
21// the remains of a progress indicator
22process.on( 'SIGINT', function () {
23 process.stderr.write( '\n' );
24 process.exit();
25});
26
27command = minimist( process.argv.slice( 2 ), {
28 alias: {
29 p: 'port',
30 h: 'help',
31 f: 'force',
32 e: 'env',
33 v: 'version'
34 },
35 boolean: 'h help f force v version'.split( ' ' )
36});
37
38if ( command.help ) {
39 help();
40}
41
42else if ( command.version ) {
43 console.log( 'gobble-cli version ' + require( '../package.json' ).version );
44}
45
46else {
47 cli = new Liftoff({
48 name: 'gobble',
49 extensions: interpret.jsVariants,
50 nodeFlags: v8flags.fetch()
51 });
52
53 cli.on( 'require', function (name) {
54 console.log( 'Requiring external module:', chalk.cyan( name ) );
55 });
56
57 cli.on( 'requireFail', function (name) {
58 console.log( 'Failed to load external module:', chalk.red( name ) );
59 });
60
61 cli.on( 'respawn', function (flags, child) {
62 var nodeFlags = flags.join(', ');
63 var pid = String(child.pid);
64 console.log( 'Node flags detected:', chalk.cyan( nodeFlags ) );
65 console.log( 'Respawned to PID:', chalk.cyan( pid ) );
66 });
67
68 cli.launch({
69 /*
70 configPath: argv.gobblefile,
71 cwd: argv.cwd,
72 require: argv.require
73 */
74 }, function ( env ) {
75 var version, gobbledir;
76
77 if ( !env.modulePath ) {
78 logger.error({
79 code: 'MISSING_GOBBLE'
80 });
81 process.exit( 1 );
82 }
83
84 // Check that the locally installed gobble has the right version
85 version = require( path.join( findup( 'node_modules/gobble' ), 'package.json' ) ).version;
86 if ( semver.lt( version, '0.7.0' ) ) {
87 logger.warn({
88 code: 'OLD_GOBBLE',
89 version: version
90 });
91 }
92
93 if ( !env.configPath ) {
94 logger.error({
95 name: 'GobbleError',
96 code: 'MISSING_GOBBLEFILE'
97 });
98 }
99
100 if ( process.cwd() !== env.cwd ) {
101 process.chdir( env.cwd );
102 }
103
104 process.env.GOBBLE_ENV = command.env || 'production';
105
106 // Execute command
107 if ( command._[0] === 'build' ) {
108 return build( command, env.configPath );
109 }
110
111 if ( command._[0] === 'watch' ) {
112 return watch( command, env.configPath );
113 }
114
115 if ( !command._[0] || command._[0] === 'serve' ) {
116 return serve( command, env.configPath );
117 }
118
119 return help( command );
120 });
121}