UNPKG

2.11 kBJavaScriptView Raw
1const execa = require('execa');
2const updateNotifier = require('update-notifier');
3const pkg = require('./package.json');
4
5// Set `updateCheckInterval` to 0 to always notify users, otherwise the notification would never be triggered when running on Travis
6updateNotifier({pkg, updateCheckInterval: 0}).notify();
7
8module.exports = async () => {
9 const cli = require('yargs')
10 .command('$0 [script]', 'Run a deployment script only once in the Travis test matrix', yargs => {
11 yargs
12 .positional('script', {describe: 'The script to run once', type: 'string'})
13 .demandCommand(0, 0, '', 'Only one script argument is allowed')
14 .example('travis-deploy-once --buildLeaderId 1 "deploy-script --script-arg script-arg-value"');
15 })
16 .alias('h', 'help')
17 .alias('v', 'version')
18 .option('t', {
19 alias: 'github-token',
20 describe: 'GitHub OAuth token',
21 type: 'string',
22 })
23 .option('b', {
24 alias: 'build-leader-id',
25 describe: 'Define which Travis job will run the script',
26 type: 'number',
27 })
28 .option('p', {
29 alias: 'pro',
30 describe: 'Use Travis Pro',
31 type: 'boolean',
32 })
33 .option('u', {
34 alias: 'travis-url',
35 describe: 'Travis Enterprise API endpoint URL',
36 type: 'string',
37 })
38 .exitProcess(false);
39
40 try {
41 const {script, buildLeaderId, githubToken, travisUrl: enterprise, pro, help, version} = cli.argv;
42 if (Boolean(help) || Boolean(version)) {
43 process.exitCode = 0;
44 return;
45 }
46 if (
47 (await require('./lib/travis-deploy-once')({travisOpts: {pro, enterprise}, buildLeaderId, githubToken})) === true
48 ) {
49 if (script) {
50 const shell = execa.shell(script, {reject: false});
51 shell.stdout.pipe(process.stdout);
52 shell.stderr.pipe(process.stderr);
53 process.exitCode = (await shell).code;
54 } else {
55 process.exitCode = 0;
56 }
57 } else if (!script) {
58 process.exitCode = 1;
59 }
60 } catch (error) {
61 if (error.name !== 'YError') {
62 console.error(error);
63 }
64 process.exitCode = 1;
65 }
66};