| 22 | }; |
| 23 | |
| 24 | export async function handler (args: any, writeStreams: WriteStreams, jobs: Job[] = [], childPipelineDepth = 0) { |
| 25 | assert(childPipelineDepth <= 2, "Parent and child pipelines have a maximum depth of two levels of child pipelines."); |
| 26 | const argv = await Argv.build({...args, childPipelineDepth: childPipelineDepth}, writeStreams); |
| 27 | const cwd = argv.cwd; |
| 28 | const stateDir = argv.stateDir; |
| 29 | const file = argv.file; |
| 30 | let parser: Parser; |
| 31 | |
| 32 | if (argv.completion) { |
| 33 | yargs(process.argv.slice(2)).scriptName("gitlab-ci-local").showCompletionScript(); |
| 34 | return []; |
| 35 | } |
| 36 | |
| 37 | assert(fs.existsSync(`${cwd}/${file}`), `${path.resolve(cwd)}/${file} could not be found`); |
| 38 | |
| 39 | if (argv.preview) { |
| 40 | const pipelineIid = await state.getPipelineIid(cwd, stateDir); |
| 41 | parser = await Parser.create(argv, writeStreams, pipelineIid, jobs, false); |
| 42 | const gitlabData = parser.gitlabData; |
| 43 | for (const jobName of Object.keys(gitlabData)) { |
| 44 | if (jobName === "stages") { |
| 45 | continue; |
| 46 | } |
| 47 | if (jobName.startsWith(".") || ["include", "after_script", "before_script", "default"].includes(jobName)) { |
| 48 | // Remove since these are redundant info which are already "extended" in the jobs |
| 49 | delete gitlabData[jobName]; |
| 50 | } |
| 51 | } |
| 52 | writeStreams.stdout(`---\n${yaml.dump(gitlabData, {lineWidth: 160})}`); |
| 53 | } else if (argv.list || argv.listAll) { |
| 54 | const pipelineIid = await state.getPipelineIid(cwd, stateDir); |
| 55 | parser = await Parser.create(argv, writeStreams, pipelineIid, jobs); |
| 56 | Commander.runList(parser, writeStreams, argv.listAll); |
| 57 | } else if (argv.validateDependencyChain) { |
| 58 | const pipelineIid = await state.getPipelineIid(cwd, stateDir); |
| 59 | parser = await Parser.create(argv, writeStreams, pipelineIid, jobs); |
| 60 | Commander.validateDependencyChain(parser); |
| 61 | writeStreams.stdout(chalk`{green ✓ All job dependencies are valid}\n`); |
| 62 | } else if (argv.listJson) { |
| 63 | const pipelineIid = await state.getPipelineIid(cwd, stateDir); |
| 64 | parser = await Parser.create(argv, writeStreams, pipelineIid, jobs); |
| 65 | Commander.runJson(parser, writeStreams); |
| 66 | } else if (argv.listCsv || argv.listCsvAll) { |
| 67 | const pipelineIid = await state.getPipelineIid(cwd, stateDir); |
| 68 | parser = await Parser.create(argv, writeStreams, pipelineIid, jobs); |
| 69 | Commander.runCsv(parser, writeStreams, argv.listCsvAll); |
| 70 | } else if (argv.job.length > 0) { |
| 71 | assert(argv.stage === null, "You cannot use --stage when starting individual jobs"); |
| 72 | if (argv.registry) { |
| 73 | await Utils.startDockerRegistry(argv); |
| 74 | } |
| 75 | generateGitIgnore(cwd, stateDir); |
| 76 | const time = process.hrtime(); |
| 77 | let pipelineIid: number; |
| 78 | if (argv.needs || argv.onlyNeeds) { |
| 79 | pipelineIid = await state.incrementPipelineIid(cwd, stateDir); |
| 80 | } else { |
| 81 | pipelineIid = await state.getPipelineIid(cwd, stateDir); |