This repository has been archived by the owner on Dec 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
cli.js
66 lines (63 loc) · 2.06 KB
/
cli.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
const execa = require('execa');
const updateNotifier = require('update-notifier');
const pkg = require('./package.json');
// Set `updateCheckInterval` to 0 to always notify users, otherwise the notification would never be triggered when running on Travis
updateNotifier({pkg, updateCheckInterval: 0}).notify();
module.exports = async () => {
const cli = require('yargs')
.command('$0 [script]', 'Run a deployment script only once in the Travis test matrix', yargs => {
yargs
.positional('script', {describe: 'The script to run once', type: 'string'})
.demandCommand(0, 0, '', 'Only one script argument is allowed')
.example('travis-deploy-once --buildLeaderId 1 "deploy-script --script-arg script-arg-value"');
})
.alias('h', 'help')
.alias('v', 'version')
.option('t', {
alias: 'github-token',
describe: 'GitHub OAuth token',
type: 'string',
})
.option('b', {
alias: 'build-leader-id',
describe: 'Define which Travis job will run the script',
type: 'number',
})
.option('p', {
alias: 'pro',
describe: 'Use Travis Pro',
type: 'boolean',
})
.option('u', {
alias: 'travis-url',
describe: 'Travis Enterprise API endpoint URL',
type: 'string',
})
.exitProcess(false);
try {
const {script, buildLeaderId, githubToken, travisUrl: enterprise, pro, help, version} = cli.argv;
if (Boolean(help) || Boolean(version)) {
process.exitCode = 0;
return;
}
if (
(await require('./lib/travis-deploy-once')({travisOpts: {pro, enterprise}, buildLeaderId, githubToken})) === true
) {
if (script) {
const shell = execa.shell(script, {reject: false});
shell.stdout.pipe(process.stdout);
shell.stderr.pipe(process.stderr);
process.exitCode = (await shell).code;
} else {
process.exitCode = 0;
}
} else if (!script) {
process.exitCode = 1;
}
} catch (error) {
if (error.name !== 'YError') {
console.error(error);
}
process.exitCode = 1;
}
};