1 | #!/usr/bin/env node
|
2 |
|
3 | var 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 |
|
21 |
|
22 | process.on( 'SIGINT', function () {
|
23 | process.stderr.write( '\n' );
|
24 | process.exit();
|
25 | });
|
26 |
|
27 | command = 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 |
|
38 | if ( command.help ) {
|
39 | help();
|
40 | }
|
41 |
|
42 | else if ( command.version ) {
|
43 | console.log( 'gobble-cli version ' + require( '../package.json' ).version );
|
44 | }
|
45 |
|
46 | else {
|
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 |
|
71 |
|
72 |
|
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 |
|
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 |
|
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 | }
|