From f70f530d698926202180ef21816145f88a80197c Mon Sep 17 00:00:00 2001 From: ANGkeith Date: Wed, 2 Oct 2024 13:53:37 +0800 Subject: [PATCH] feat: make project file variable file configurable --- src/argv.ts | 7 +++++++ src/index.ts | 6 ++++++ src/variables-from-files.ts | 2 +- .../project-variables-file/.custom-local-var-file | 1 + .../project-variables-file/.gitlab-ci-custom.yml | 5 +++++ .../integration.project-variables-file.test.ts | 14 ++++++++++++++ 6 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 tests/test-cases/project-variables-file/.custom-local-var-file create mode 100644 tests/test-cases/project-variables-file/.gitlab-ci-custom.yml diff --git a/src/argv.ts b/src/argv.ts index 931c79423..c6b901ddf 100644 --- a/src/argv.ts +++ b/src/argv.ts @@ -22,6 +22,9 @@ async function gitRootPath () { } export class Argv { + static readonly default = { + "variablesFile": ".gitlab-ci-local-variables.yml", + }; private map: Map = new Map(); private writeStreams: WriteStreams | undefined; @@ -82,6 +85,10 @@ export class Argv { return cwd; } + get variablesFile (): string { + return this.map.get("variablesFile") ?? Argv.default.variablesFile; + } + get file (): string { return this.map.get("file") ?? ".gitlab-ci.yml"; } diff --git a/src/index.ts b/src/index.ts index 5266b3966..4991482e1 100755 --- a/src/index.ts +++ b/src/index.ts @@ -109,6 +109,12 @@ process.on("SIGUSR2", async () => await cleanupJobResources(jobs)); description: "Path to a current working directory", requiresArg: true, }) + .option("variables-file", { + type: "string", + description: "Path to the project file variables", + requiresArg: false, + default: Argv.default.variablesFile, + }) .option("completion", { type: "boolean", description: "Generate tab completion script", diff --git a/src/variables-from-files.ts b/src/variables-from-files.ts index cc5d472ca..418570c3b 100644 --- a/src/variables-from-files.ts +++ b/src/variables-from-files.ts @@ -112,7 +112,7 @@ export class VariablesFromFiles { await addVariableFileToVariables(remoteFileData, 0); await addVariableFileToVariables(homeFileData, 10); - const projectVariablesFile = `${argv.cwd}/.gitlab-ci-local-variables.yml`; + const projectVariablesFile = `${argv.cwd}/${argv.variablesFile}`; if (fs.existsSync(projectVariablesFile)) { const projectVariablesFileData: any = yaml.load(await fs.readFile(projectVariablesFile, "utf8"), {schema: yaml.FAILSAFE_SCHEMA}) ?? {}; assert(projectVariablesFileData != null, "projectEntries cannot be null/undefined"); diff --git a/tests/test-cases/project-variables-file/.custom-local-var-file b/tests/test-cases/project-variables-file/.custom-local-var-file new file mode 100644 index 000000000..9477dcce4 --- /dev/null +++ b/tests/test-cases/project-variables-file/.custom-local-var-file @@ -0,0 +1 @@ +SECRET: "firecow" diff --git a/tests/test-cases/project-variables-file/.gitlab-ci-custom.yml b/tests/test-cases/project-variables-file/.gitlab-ci-custom.yml new file mode 100644 index 000000000..be2cad7c5 --- /dev/null +++ b/tests/test-cases/project-variables-file/.gitlab-ci-custom.yml @@ -0,0 +1,5 @@ +--- +job: + image: busybox + script: + - echo $SECRET diff --git a/tests/test-cases/project-variables-file/integration.project-variables-file.test.ts b/tests/test-cases/project-variables-file/integration.project-variables-file.test.ts index 1ee6f17c3..050303475 100644 --- a/tests/test-cases/project-variables-file/integration.project-variables-file.test.ts +++ b/tests/test-cases/project-variables-file/integration.project-variables-file.test.ts @@ -34,3 +34,17 @@ test.concurrent("project-variables-file ", async () => { ]; expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining(expected)); }); + +test.concurrent("project-variables-file custom-path", async () => { + const writeStreams = new WriteStreamsMock(); + await handler({ + cwd: "tests/test-cases/project-variables-file", + file: ".gitlab-ci-custom.yml", + variablesFile: ".custom-local-var-file", + }, writeStreams); + + const expected = [ + chalk`{blueBright job} {greenBright >} firecow`, + ]; + expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining(expected)); +});