-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
155 lines (128 loc) · 3.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var _ = require('lodash');
var Shipit = require('shipit-cli');
var inquirer = require('bluebird-inquirer');
var chalk = require('chalk');
var util = require('util');
var yargs = require('yargs');
var Promise = require('bluebird');
var captain = function captain(shipitConfig, options, cb) {
var shipit;
var argv = yargs.options({
'e': {
alias: 'env'
},
'r': {
alias: 'run'
}
}).argv;
// Optional args
cb = _.isFunction(cb) ? cb : function() {};
if (_.isFunction(options)) {
cb = options;
options = {};
}
// Normalize tasks from argv
var argvRun = argv.run;
argvRun = argvRun ?
argvRun.split(',').map(Function.prototype.call, String.prototype.trim) :
false;
options = _.defaults(options || {}, {
targetEnv: argv.env || false,
availableEnvs: _.without(Object.keys(shipitConfig), 'default'),
run: argvRun || [],
confirm: true,
logItems: function(options, shipit) {
return {
'Environment': options.targetEnv,
'Branch': shipit.config.branch,
'Path': shipit.config.deployTo,
};
},
init: function(shipit) {
require('shipit-deploy')(shipit);
}
});
options.run = Array.isArray(options.run) ? options.run : [options.run];
var taskPrompt = function tasksPrompt() {
if (!options.run.length) {
return inquirer.prompt([{
type: 'list',
name: 'run',
default: 'deploy',
message: 'Run task:',
choices: _.pluck(shipit.tasks, 'name')
}]).then(function(answers) {
options.run = [answers.run];
return Promise.resolve([options.run]);
});
}
return Promise.resolve(options.run);
};
var confirmPrompt = function confirmPrompt() {
if (options.logItems) {
var logItems = options.logItems(options, shipit);
var msg = Object.keys(logItems).map(function(key) {
return chalk.bold(util.format('- %s: %s', key, chalk.blue(logItems[key])));
});
console.log('\n' + msg.join('\n') + '\n');
}
var taskStr = chalk.cyan(options.run.join(chalk.white(', ')));
if ( options.confirm ) {
return inquirer.prompt([{
type: 'confirm',
name: 'taskConfirm',
default: true,
message: util.format('Run tasks [%s]', taskStr),
}]).then(function(answers) {
return new Promise(function(resolve, reject) {
if (answers.taskConfirm) {
return resolve(shipit);
}
return reject('Shipit process aborted.');
});
});
}
return Promise.resolve(shipit);
};
var envPrompt = function envPrompt() {
if (!options.targetEnv && options.availableEnvs.length > 1) {
return inquirer.prompt([{
type: 'list',
name: 'targetEnv',
default: options.targetEnv,
message: 'Target environment:',
choices: options.availableEnvs
}]).then(function(answers) {
options.targetEnv = answers.targetEnv;
return options.targetEnv;
});
}
options.targetEnv = options.targetEnv || options.availableEnvs[0];
return Promise.resolve(options.targetEnv);
};
var initShipit = function initShipit() {
shipit = new Shipit({environment: options.targetEnv});
if (_.isFunction(options.init)) {
options.init(shipit);
if (!shipit.config) {
shipit.initConfig(shipitConfig);
}
}
};
var startShipit = function startShipit() {
shipit.initialize();
return shipit.start(options.run, function(e) {
cb(e);
return Promise.resolve(shipit);
});
};
return envPrompt()
.then(initShipit)
.then(taskPrompt)
.then(confirmPrompt)
.then(startShipit)
.catch(function(e) {
console.log(chalk.red(e));
});
};
module.exports = captain;