UNPKG

2.25 kBPlain TextView Raw
1#!/usr/bin/env node
2
3var chalk = require('chalk');
4var interpret = require('interpret');
5var v8flags = require('v8flags');
6var Liftoff = require('liftoff');
7var argv = require('minimist')(process.argv.slice(2));
8var Shipit = require('../lib/shipit');
9var pkg = require('../package.json');
10var Promise = require('bluebird');
11
12// Initialize cli.
13var cli = new Liftoff({
14 name: 'shipit',
15 extensions: interpret.jsVariants,
16 v8flags: v8flags
17});
18
19// Launch cli.
20cli.launch({
21 cwd: argv.cwd,
22 configPath: argv.shipitfile,
23 require: argv.require,
24 completion: argv.completion
25}, invoke);
26
27/**
28 * Invoke CLI.
29 *
30 * @param {object} env CLI environment
31 */
32
33function invoke(env) {
34 if (argv.version) {
35 console.info('v%s', pkg.version);
36 exit(0);
37 }
38
39 if (!env.configPath) {
40 console.error(chalk.red('shipitfile not found'));
41 exit(1);
42 }
43
44 if (argv._.length === 0) {
45 console.error(chalk.red('environment not found'));
46 exit(1);
47 }
48
49 // Run the 'default' task if no task is specified
50 var tasks = argv._.slice(1);
51 if (tasks.length === 0) {
52 tasks.push('default');
53 }
54
55 try {
56 initShipit(argv._[0], env.configPath, tasks);
57 } catch(e) {
58 console.error(chalk.red(e.message));
59 exit(1);
60 }
61}
62
63/**
64 * Initialize shipit.
65 *
66 * @param {string} env Shipit environement
67 * @param {string} shipfile Shipitfile path
68 * @param {string[]} tasks Tasks
69 */
70
71function initShipit(env, shipfile, tasks) {
72
73 // Create.
74 var shipit = new Shipit({environment: env});
75
76 // Load shipfile.
77 var pendingConfig = require(shipfile)(shipit);
78
79 var done = function() {
80
81 // Initialize shipit.
82 shipit.initialize();
83
84 // Run tasks.
85 shipit.start(tasks);
86
87 shipit.on('task_err', function() {
88 exit(1);
89 });
90
91 shipit.on('task_not_found', function() {
92 exit(1);
93 });
94 }
95
96 Promise.resolve(pendingConfig)
97 .then(done)
98 .catch(function(err) {
99 console.error('Could not load async config.', err);
100 } )
101}
102
103/**
104 * Properly exit.
105 * Even on Windows.
106 *
107 * @param {number} code Exit code
108 */
109
110function exit(code) {
111 if (process.platform === 'win32' && process.stdout.bufferSize) {
112 process.stdout.once('drain', function() {
113 process.exit(code);
114 });
115 return;
116 }
117
118 process.exit(code);
119}