Skip to content

Commit 50bfd88

Browse files
authored
Add file option, to specify custom .gitlab-ci.yml locations inside cwd (#84)
1 parent 04db673 commit 50bfd88

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

src/default-cmd.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import * as state from "./state";
77
import {ExitError} from "./types/exit-error";
88
import {assert} from "./asserts";
99

10-
const checkFolderAndFile = (cwd: string) => {
10+
const checkFolderAndFile = (cwd: string, file?: string) => {
1111
assert(fs.pathExistsSync(cwd), `${cwd} is not a directory`);
12-
assert(fs.existsSync(`${cwd}/.gitlab-ci.yml`), `${cwd} does not contain .gitlab-ci.yml`);
12+
13+
const gitlabFilePath = file ? `${cwd}/${file}` : `${cwd}/.gitlab-ci.yml`;
14+
assert(fs.existsSync(gitlabFilePath), `${cwd} does not contain ${file ?? ".gitlab-ci.yml"}`);
1315
};
1416

1517
exports.command = "$0 [job]";
@@ -27,20 +29,20 @@ export async function handler(argv: any) {
2729
if (argv.completion != null) {
2830
yargs.showCompletionScript();
2931
} else if (argv.list != null) {
30-
checkFolderAndFile(cwd);
32+
checkFolderAndFile(cwd, argv.file);
3133
const pipelineIid = await state.getPipelineIid(cwd);
32-
const parser = await Parser.create(cwd, pipelineIid);
34+
const parser = await Parser.create(cwd, pipelineIid, false, argv.file);
3335
Commander.runList(parser);
3436
} else if (argv.job) {
35-
checkFolderAndFile(cwd);
37+
checkFolderAndFile(cwd, argv.file);
3638
const pipelineIid = await state.getPipelineIid(cwd);
37-
const parser = await Parser.create(cwd, pipelineIid);
39+
const parser = await Parser.create(cwd, pipelineIid, false, argv.file);
3840
await Commander.runSingleJob(parser, argv.job, argv.needs, argv.privileged);
3941
} else {
40-
checkFolderAndFile(cwd);
42+
checkFolderAndFile(cwd, argv.file);
4143
await state.incrementPipelineIid(cwd);
4244
const pipelineIid = await state.getPipelineIid(cwd);
43-
const parser = await Parser.create(cwd, pipelineIid);
45+
const parser = await Parser.create(cwd, pipelineIid, false, argv.file);
4446
await Commander.runPipeline(parser, argv.manual || [], argv.privileged);
4547
}
4648
}

src/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ process.on('unhandledRejection', e => {
4444
})
4545
.option("cwd", {
4646
type: "string",
47-
description: "Path to a gitlab-ci.yml",
47+
description: "Path to a current working directory",
4848
requiresArg: true
4949
})
5050
.option("completion", {
@@ -57,6 +57,11 @@ process.on('unhandledRejection', e => {
5757
description: "Run needed jobs, when executing a single job",
5858
requiresArg: false
5959
})
60+
.option("file", {
61+
type: "string",
62+
description: "Specify custom location of the .gitlab-ci.yml. Relative to cwd, eg. (gitlab/.gitlab-ci.yml)",
63+
requiresArg: false
64+
})
6065
.option("privileged", {
6166
type: "boolean",
6267
default: false,
@@ -67,7 +72,7 @@ process.on('unhandledRejection', e => {
6772
try {
6873
const cwd = yargsArgv.cwd || process.cwd();
6974
const pipelineIid = await state.getPipelineIid(cwd);
70-
const parser = await Parser.create(cwd, pipelineIid, true);
75+
const parser = await Parser.create(cwd, pipelineIid, true, yargsArgv.file);
7176
return parser.getJobNames();
7277
} catch (e) {
7378
return ["Parser-Failed!"];

src/parser.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export class Parser {
2020
private readonly jobs: Map<string, Job> = new Map();
2121
private readonly stages: Map<string, Stage> = new Map();
2222
private readonly cwd: string;
23+
private readonly file?: string;
2324
private readonly pipelineIid: number;
2425

2526
private gitRemote: GitRemote | null = null;
@@ -29,14 +30,15 @@ export class Parser {
2930
private maxJobNameLength = 0;
3031
private readonly tabCompletionPhase: boolean;
3132

32-
private constructor(cwd: string, pipelineIid: number, tabCompletionPhase: boolean) {
33+
private constructor(cwd: string, pipelineIid: number, tabCompletionPhase: boolean, file?: string) {
3334
this.cwd = cwd;
3435
this.pipelineIid = pipelineIid;
3536
this.tabCompletionPhase = tabCompletionPhase;
37+
this.file = file;
3638
}
3739

38-
static async create(cwd: string, pipelineIid: number, tabCompletionPhase = false) {
39-
const parser = new Parser(cwd, pipelineIid, tabCompletionPhase);
40+
static async create(cwd: string, pipelineIid: number, tabCompletionPhase: boolean, file: string) {
41+
const parser = new Parser(cwd, pipelineIid, tabCompletionPhase, file);
4042

4143
const time = process.hrtime();
4244
await parser.init();
@@ -131,7 +133,7 @@ export class Parser {
131133
this.userVariables = await Parser.initUserVariables(cwd, this.gitRemote, process.env.HOME);
132134

133135
let ymlPath, yamlDataList: any[] = [];
134-
ymlPath = `${cwd}/.gitlab-ci.yml`;
136+
ymlPath = this.file ? `${cwd}/${this.file}` : `${cwd}/.gitlab-ci.yml`;
135137
const gitlabCiData = await Parser.loadYaml(ymlPath);
136138
yamlDataList = yamlDataList.concat(await Parser.prepareIncludes(gitlabCiData, cwd, this.gitRemote, this.tabCompletionPhase));
137139

0 commit comments

Comments
 (0)