diff --git a/src/commands/default_cmd.ts b/src/commands/default_cmd.ts deleted file mode 100644 index d77ba27f1..000000000 --- a/src/commands/default_cmd.ts +++ /dev/null @@ -1,29 +0,0 @@ -import {Commander} from "../commander"; -import {Parser} from "../parser"; -import * as predefinedVariables from "../predefined_variables"; - -exports.command = "$0 [job]"; -exports.describe = "Runs the entire pipeline or a single [job]"; -exports.builder = (y: any) => { - y.positional("job", { - describe: "Jobname to execute", - type: "string", - }); -}; -exports.handler = async(argv: any) => { - const cwd = argv.cwd as string || process.cwd(); - const pipelineIid = predefinedVariables.getPipelineIid(cwd); - const parser = new Parser(cwd, pipelineIid); - - if (argv.list !== undefined) { - await Commander.runList(parser); - return; - } - - if (argv.job) { - await Commander.runSingleJob(parser, argv.job as string); - } else { - predefinedVariables.incrementPipelineIid(cwd); - await Commander.runPipeline(parser, argv.manual as string[] || []); - } -}; diff --git a/src/commands/list_cmd.ts b/src/commands/list_cmd.ts deleted file mode 100644 index 0e6c5b275..000000000 --- a/src/commands/list_cmd.ts +++ /dev/null @@ -1,12 +0,0 @@ -import {Commander} from "../commander"; -import {Parser} from "../parser"; -import * as predefinedVariables from "../predefined_variables"; - -exports.command = "list"; -exports.describe = "Lists jobs"; -exports.handler = async(argv: any) => { - const cwd = argv.cwd as string || process.cwd(); - const pipelineIid = predefinedVariables.getPipelineIid(cwd); - const parser = new Parser(cwd, pipelineIid); - await Commander.runList(parser); -}; diff --git a/src/index.ts b/src/index.ts index 635384e56..71a622a4e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,18 @@ -import {CommandModule} from "yargs"; import * as yargs from "yargs"; +import {Commander} from "./commander"; -import * as defaultCmd from "./commands/default_cmd" import {Parser} from "./parser"; import * as predefinedVariables from "./predefined_variables"; +process.on('uncaughtException', (err) => { + process.stderr.write(`${err.stack ? err.stack : err}\n`); + process.exit(5); +}); +process.on('unhandledRejection', (reason) => { + process.stderr.write(`${reason}\n`); + process.exit(5); +}); + // Array polyfill declare global { // tslint:disable-next-line:interface-name @@ -20,22 +28,42 @@ Array.prototype.first = function() { return this[0]; }; -const a = yargs +const argv = yargs .version("4.0.0") - .command(defaultCmd as CommandModule) + .usage("\nUsage: $0 Run entire pipeline\nUsage: $0 [jobname] Run single job") .option("manual", {type: "array", description: "One or more manual jobs to run during a pipeline", requiresArg: true}) .option("list", {type: "string", description: "List jobs and job information", requiresArg: false}) .option("cwd", {type: "string", description: "Path to a gitlab-ci.yml", requiresArg: true}) - .completion('', async (current, argv) => { - const cwd = argv.cwd as string || process.cwd(); + .option("completion", {type: "string", description: "Generate bash completion script", requiresArg: false}) + .completion("completion", false, async (current, a) => { + const cwd = a.cwd as string || process.cwd(); const pipelineIid = predefinedVariables.getPipelineIid(cwd); const parser = new Parser(cwd, pipelineIid); return parser.getJobNames(); }) + .epilogue('for more information, find our manual at http://github.com/firecow/') .argv; -process.on("uncaughtException", (err) => { - // Handle the error safely - process.stderr.write(`${err.stack ? err.stack : err}\n`); - process.exit(5); -}); +(async() => { + const cwd = argv.cwd as string || process.cwd(); + const pipelineIid = predefinedVariables.getPipelineIid(cwd); + const parser = new Parser(cwd, pipelineIid); + + if (argv.completion !== undefined) { + yargs.showCompletionScript(); + return; + } + + if (argv.list !== undefined) { + await Commander.runList(parser); + return; + } + + if (argv._.length > 0) { + await Commander.runSingleJob(parser, argv._[0] as string); + } else { + predefinedVariables.incrementPipelineIid(cwd); + await Commander.runPipeline(parser, argv.manual as string[] || []); + } +})(); +