From c65ed64854d2f6f0763c3967e2e8725c53f5d5b3 Mon Sep 17 00:00:00 2001 From: Radek Mariowski Date: Sat, 13 Apr 2024 15:54:49 +0200 Subject: [PATCH 1/2] Add missingVarDefault and missingVarLog inputs --- README.md | 2 + __tests__/main.test.ts | 87 ++++++++++++++++++++++++++++++++++++------ action.yml | 6 +++ dist/index.js | 66 ++++++++++++++++++++++++++++++-- src/main.ts | 12 +++++- src/missingVarLog.ts | 5 +++ src/replace.ts | 21 ++++++++-- 7 files changed, 178 insertions(+), 21 deletions(-) create mode 100644 src/missingVarLog.ts diff --git a/README.md b/README.md index b799bbf..85827b1 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,8 @@ Simple GitHub Action to replace tokens in files. - `files` - Glob expression, file name or array of glob/file name - `tokenPrefix` - Prefix to use when matching tokens, defaults to `#{` - `tokenSuffix` - Suffix to use when matching tokens, defaults to `}#` +- `missingVarDefault` - The default value to use when a variable is not found, defaults to `""` +- `missingVarLog` - The level to log variable not found messages. Accepted values `off`, `warn`, `error`, defaults to `off` ## Example diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 186e813..c5082aa 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -1,9 +1,19 @@ import { promises as fs } from "fs"; -import * as process from 'process' +import * as process from 'process'; +import * as core from '@actions/core'; import { replaceTokens } from "../src/replace"; +import { MissingVarLog } from "../src/missingVarLog"; + +let warningMock: jest.SpiedFunction +let errorMock: jest.SpiedFunction describe("basic functionality", () => { beforeEach(async () => { + jest.clearAllMocks(); + + errorMock = jest.spyOn(core, 'error').mockImplementation(); + warningMock = jest.spyOn(core, 'warning').mockImplementation(); + await fs.writeFile("test.txt", "hello #{ACTOR}#", "utf8"); await fs.writeFile("test2.txt", "#{GREETING}# #{ACTOR}#", "utf8"); }) @@ -15,34 +25,34 @@ describe("basic functionality", () => { test("replaces single token in file", async () => { process.env["ACTOR"] = "world"; - await replaceTokens("#{", "}#", ["test.txt"]); + await replaceTokens("#{", "}#", ["test.txt"], "", MissingVarLog.Off); const content = await fs.readFile('test.txt', 'utf8'); - expect(content).toBe("hello world") + expect(content).toBe("hello world"); }); test("replaces single token in file specified with glob", async () => { process.env["ACTOR"] = "world"; - await replaceTokens("#{", "}#", ["*.txt"]); + await replaceTokens("#{", "}#", ["*.txt"], "", MissingVarLog.Off); const content = await fs.readFile('test.txt', 'utf8'); - expect(content).toBe("hello world") + expect(content).toBe("hello world"); const content2 = await fs.readFile('test2.txt', 'utf8'); - expect(content2).toBe(" world") + expect(content2).toBe(" world"); }); test("replaces multiple token in file", async () => { process.env["GREETING"] = "hallo"; process.env["ACTOR"] = "welt"; - await replaceTokens("#{", "}#", ["test2.txt"]); + await replaceTokens("#{", "}#", ["test2.txt"], "", MissingVarLog.Off); const content = await fs.readFile('test2.txt', 'utf8'); - expect(content).toBe("hallo welt") + expect(content).toBe("hallo welt"); }); test("returns list of changed files", async () => { - const result = await replaceTokens("#{", "}#", ["*.txt"]); + const result = await replaceTokens("#{", "}#", ["*.txt"], "", MissingVarLog.Off); expect(result).toEqual([ "test.txt", "test2.txt" @@ -50,7 +60,7 @@ describe("basic functionality", () => { }); test("returns only list of changed files", async () => { - const result = await replaceTokens("#{", "}#", ["test.txt"]); + const result = await replaceTokens("#{", "}#", ["test.txt"], "", MissingVarLog.Off); expect(result).toEqual([ "test.txt" @@ -58,8 +68,61 @@ describe("basic functionality", () => { }); test("does not throw when no match", async () => { - const result = await replaceTokens("#{", "}#", [""]); + const result = await replaceTokens("#{", "}#", [""], "", MissingVarLog.Off); expect(result).toEqual([]); }); -}); \ No newline at end of file + + test("does not log missing variable to console when missingVarLog is 'off'", async () => { + delete process.env["GREETING"]; + process.env["ACTOR"] = "world"; + await replaceTokens("#{", "}#", ["test2.txt"], "", MissingVarLog.Off); + + const content = await fs.readFile('test2.txt', 'utf8'); + expect(content).toBe(" world"); + expect(warningMock).not.toHaveBeenCalled(); + expect(errorMock).not.toHaveBeenCalled(); + }); + + test("logs missing variable warning to console when missingVarLog is 'warn'", async () => { + delete process.env["GREETING"]; + process.env["ACTOR"] = "world"; + await replaceTokens("#{", "}#", ["test2.txt"], "", MissingVarLog.Warn); + + const content = await fs.readFile('test2.txt', 'utf8'); + expect(content).toBe(" world"); + expect(warningMock).toHaveBeenCalled(); + expect(errorMock).not.toHaveBeenCalled(); + }); + + test("logs missing variable error to console when missingVarLog is 'error'", async () => { + delete process.env["GREETING"]; + process.env["ACTOR"] = "world"; + await replaceTokens("#{", "}#", ["test2.txt"], "", MissingVarLog.Error); + + const content = await fs.readFile('test2.txt', 'utf8'); + expect(content).toBe(" world"); + expect(warningMock).not.toHaveBeenCalled(); + expect(errorMock).toHaveBeenCalled(); + }); + + test("does not log missing variable to console when missingVarLog is incorrect", async () => { + delete process.env["GREETING"]; + process.env["ACTOR"] = "world"; + await replaceTokens("#{", "}#", ["test2.txt"], "", 'NONE' as MissingVarLog); + + const content = await fs.readFile('test2.txt', 'utf8'); + expect(content).toBe(" world"); + expect(warningMock).not.toHaveBeenCalled(); + expect(errorMock).not.toHaveBeenCalled(); + }); + + test("replaces token with value from missingVarDefault", async () => { + delete process.env["GREETING"]; + process.env["ACTOR"] = "world"; + await replaceTokens("#{", "}#", ["test2.txt"], "[MISSING_VALUE]", MissingVarLog.Off); + + const content = await fs.readFile('test2.txt', 'utf8'); + expect(content).toBe("[MISSING_VALUE] world"); + }); +}); diff --git a/action.yml b/action.yml index 8367e7b..7535be3 100644 --- a/action.yml +++ b/action.yml @@ -2,6 +2,12 @@ name: "Replace tokens" description: "Replace tokens in files" author: "cschleiden" inputs: + missingVarDefault: + description: "" + default: "" + missingVarLog: + description: "" + default: "off" tokenPrefix: description: "" default: "#{" diff --git a/dist/index.js b/dist/index.js index 2852681..39896b4 100644 --- a/dist/index.js +++ b/dist/index.js @@ -8711,6 +8711,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge Object.defineProperty(exports, "__esModule", ({ value: true })); const core = __importStar(__nccwpck_require__(2186)); const replace_1 = __nccwpck_require__(5287); +const missingVarLog_1 = __nccwpck_require__(1367); function getFiles() { let files = core.getInput("files", { required: true, @@ -8721,13 +8722,19 @@ function getFiles() { } return [files]; } +function getMissingVarLog() { + const value = core.getInput("missingVarLog"); + return Object.values(missingVarLog_1.MissingVarLog).includes(value) ? value : missingVarLog_1.MissingVarLog.Off; +} function run() { return __awaiter(this, void 0, void 0, function* () { try { const tokenPrefix = core.getInput("tokenPrefix") || "#{"; const tokenSuffix = core.getInput("tokenSuffix") || "}#"; const files = getFiles(); - const result = yield (0, replace_1.replaceTokens)(tokenPrefix, tokenSuffix, Array.isArray(files) ? files : [files]); + const missingVarDefault = core.getInput("missingVarDefault") || ""; + const missingVarLog = getMissingVarLog(); + const result = yield (0, replace_1.replaceTokens)(tokenPrefix, tokenSuffix, Array.isArray(files) ? files : [files], missingVarDefault, missingVarLog); console.log(`Replaced tokens in files: ${result}`); } catch (error) { @@ -8739,6 +8746,23 @@ function run() { run(); +/***/ }), + +/***/ 1367: +/***/ ((__unused_webpack_module, exports) => { + +"use strict"; + +Object.defineProperty(exports, "__esModule", ({ value: true })); +exports.MissingVarLog = void 0; +var MissingVarLog; +(function (MissingVarLog) { + MissingVarLog["Off"] = "off"; + MissingVarLog["Warn"] = "warn"; + MissingVarLog["Error"] = "error"; +})(MissingVarLog || (exports.MissingVarLog = MissingVarLog = {})); + + /***/ }), /***/ 5287: @@ -8746,6 +8770,29 @@ run(); "use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { @@ -8757,8 +8804,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }; Object.defineProperty(exports, "__esModule", ({ value: true })); exports.replaceTokens = void 0; +const core = __importStar(__nccwpck_require__(2186)); const replace_in_file_1 = __nccwpck_require__(5983); -function replaceTokens(tokenPrefix, tokenSuffix, files) { +const missingVarLog_1 = __nccwpck_require__(1367); +function replaceTokens(tokenPrefix, tokenSuffix, files, missingVarDefault, missingVarLog) { return __awaiter(this, void 0, void 0, function* () { const fromRegEx = new RegExp(`${escapeDelimiter(tokenPrefix)}(.+?)${escapeDelimiter(tokenSuffix)}`, "gm"); const matchRegEx = new RegExp(`${escapeDelimiter(tokenPrefix)}(.+?)${escapeDelimiter(tokenSuffix)}`); @@ -8770,9 +8819,18 @@ function replaceTokens(tokenPrefix, tokenSuffix, files) { const m = match.match(matchRegEx); if (m) { const tokenName = m[1]; - return process.env[tokenName] || ""; + const value = process.env[tokenName]; + if (!!value) { + return value; + } + if (missingVarLog === missingVarLog_1.MissingVarLog.Error) { + core.error(`Variable not found: ${tokenName}`); + } + else if (missingVarLog == missingVarLog_1.MissingVarLog.Warn) { + core.warning(`Variable not found: ${tokenName}`); + } } - return ""; + return missingVarDefault; } }); return result.filter(r => r.hasChanged).map(r => r.file); diff --git a/src/main.ts b/src/main.ts index 8ba0119..ceb9aed 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,5 +1,6 @@ import * as core from "@actions/core"; import { replaceTokens } from "./replace"; +import { MissingVarLog } from "./missingVarLog"; function getFiles(): string[] { let files = @@ -14,15 +15,24 @@ function getFiles(): string[] { return [files]; } +function getMissingVarLog(): MissingVarLog { + const value = core.getInput("missingVarLog") as MissingVarLog; + return Object.values(MissingVarLog).includes(value) ? value : MissingVarLog.Off; +} + async function run() { try { const tokenPrefix = core.getInput("tokenPrefix") || "#{"; const tokenSuffix = core.getInput("tokenSuffix") || "}#"; const files = getFiles(); + const missingVarDefault = core.getInput("missingVarDefault") || ""; + const missingVarLog = getMissingVarLog(); const result = await replaceTokens( tokenPrefix, tokenSuffix, - Array.isArray(files) ? files : [files] + Array.isArray(files) ? files : [files], + missingVarDefault, + missingVarLog ); console.log(`Replaced tokens in files: ${result}`); } catch (error) { diff --git a/src/missingVarLog.ts b/src/missingVarLog.ts new file mode 100644 index 0000000..88865d2 --- /dev/null +++ b/src/missingVarLog.ts @@ -0,0 +1,5 @@ +export enum MissingVarLog { + Off = "off", + Warn = "warn", + Error = "error" +} diff --git a/src/replace.ts b/src/replace.ts index 430d899..f98ad15 100644 --- a/src/replace.ts +++ b/src/replace.ts @@ -1,9 +1,13 @@ +import * as core from "@actions/core"; import { replaceInFile } from "replace-in-file"; +import { MissingVarLog } from "./missingVarLog"; export async function replaceTokens( tokenPrefix: string, tokenSuffix: string, - files: string[] + files: string[], + missingVarDefault: string, + missingVarLog: MissingVarLog ) { const fromRegEx = new RegExp( `${escapeDelimiter(tokenPrefix)}(.+?)${escapeDelimiter(tokenSuffix)}`, @@ -12,7 +16,7 @@ export async function replaceTokens( const matchRegEx = new RegExp( `${escapeDelimiter(tokenPrefix)}(.+?)${escapeDelimiter(tokenSuffix)}` ); - + const result = await replaceInFile({ files, allowEmptyPaths: true, @@ -21,10 +25,19 @@ export async function replaceTokens( const m = match.match(matchRegEx); if (m) { const tokenName = m[1]; - return process.env[tokenName] || ""; + const value = process.env[tokenName]; + if (!!value) { + return value; + } + + if (missingVarLog === MissingVarLog.Error) { + core.error(`Variable not found: ${tokenName}`); + } else if (missingVarLog == MissingVarLog.Warn) { + core.warning(`Variable not found: ${tokenName}`); + } } - return ""; + return missingVarDefault; } }); From eaf4ecbd4db9b4ea7526f5f633c515fd6aa00ec7 Mon Sep 17 00:00:00 2001 From: Radek Mariowski Date: Thu, 18 Apr 2024 19:53:36 +0200 Subject: [PATCH 2/2] Add additionalVariables inputs --- README.md | 3 ++- __tests__/main.test.ts | 31 ++++++++++++++++++++----------- action.yml | 3 +++ dist/index.js | 11 ++++++++--- src/main.ts | 4 +++- src/replace.ts | 11 ++++++++--- 6 files changed, 44 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 85827b1..95b8d6e 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ Simple GitHub Action to replace tokens in files. - `tokenSuffix` - Suffix to use when matching tokens, defaults to `}#` - `missingVarDefault` - The default value to use when a variable is not found, defaults to `""` - `missingVarLog` - The level to log variable not found messages. Accepted values `off`, `warn`, `error`, defaults to `off` +- `additionalVariables` - A JSON formatted string containing additional variable values, defaults to `"{}"` ## Example @@ -38,4 +39,4 @@ If you want to use a different token format, you can specify a custom token pref # Acknowledgements - Inspired by the excellent https://marketplace.visualstudio.com/items?itemName=qetza.replacetokens Azure Pipelines task. -- Uses [replace-in-file](https://github.com/adamreisnz/replace-in-file) to do the actual replacement \ No newline at end of file +- Uses [replace-in-file](https://github.com/adamreisnz/replace-in-file) to do the actual replacement diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index c5082aa..661577b 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -25,7 +25,7 @@ describe("basic functionality", () => { test("replaces single token in file", async () => { process.env["ACTOR"] = "world"; - await replaceTokens("#{", "}#", ["test.txt"], "", MissingVarLog.Off); + await replaceTokens("#{", "}#", ["test.txt"], "", MissingVarLog.Off, "{}"); const content = await fs.readFile('test.txt', 'utf8'); expect(content).toBe("hello world"); @@ -33,7 +33,7 @@ describe("basic functionality", () => { test("replaces single token in file specified with glob", async () => { process.env["ACTOR"] = "world"; - await replaceTokens("#{", "}#", ["*.txt"], "", MissingVarLog.Off); + await replaceTokens("#{", "}#", ["*.txt"], "", MissingVarLog.Off, "{}"); const content = await fs.readFile('test.txt', 'utf8'); expect(content).toBe("hello world"); @@ -45,14 +45,14 @@ describe("basic functionality", () => { test("replaces multiple token in file", async () => { process.env["GREETING"] = "hallo"; process.env["ACTOR"] = "welt"; - await replaceTokens("#{", "}#", ["test2.txt"], "", MissingVarLog.Off); + await replaceTokens("#{", "}#", ["test2.txt"], "", MissingVarLog.Off, "{}"); const content = await fs.readFile('test2.txt', 'utf8'); expect(content).toBe("hallo welt"); }); test("returns list of changed files", async () => { - const result = await replaceTokens("#{", "}#", ["*.txt"], "", MissingVarLog.Off); + const result = await replaceTokens("#{", "}#", ["*.txt"], "", MissingVarLog.Off, "{}"); expect(result).toEqual([ "test.txt", "test2.txt" @@ -60,7 +60,7 @@ describe("basic functionality", () => { }); test("returns only list of changed files", async () => { - const result = await replaceTokens("#{", "}#", ["test.txt"], "", MissingVarLog.Off); + const result = await replaceTokens("#{", "}#", ["test.txt"], "", MissingVarLog.Off, "{}"); expect(result).toEqual([ "test.txt" @@ -68,7 +68,7 @@ describe("basic functionality", () => { }); test("does not throw when no match", async () => { - const result = await replaceTokens("#{", "}#", [""], "", MissingVarLog.Off); + const result = await replaceTokens("#{", "}#", [""], "", MissingVarLog.Off, "{}"); expect(result).toEqual([]); }); @@ -76,7 +76,7 @@ describe("basic functionality", () => { test("does not log missing variable to console when missingVarLog is 'off'", async () => { delete process.env["GREETING"]; process.env["ACTOR"] = "world"; - await replaceTokens("#{", "}#", ["test2.txt"], "", MissingVarLog.Off); + await replaceTokens("#{", "}#", ["test2.txt"], "", MissingVarLog.Off, "{}"); const content = await fs.readFile('test2.txt', 'utf8'); expect(content).toBe(" world"); @@ -87,7 +87,7 @@ describe("basic functionality", () => { test("logs missing variable warning to console when missingVarLog is 'warn'", async () => { delete process.env["GREETING"]; process.env["ACTOR"] = "world"; - await replaceTokens("#{", "}#", ["test2.txt"], "", MissingVarLog.Warn); + await replaceTokens("#{", "}#", ["test2.txt"], "", MissingVarLog.Warn, "{}"); const content = await fs.readFile('test2.txt', 'utf8'); expect(content).toBe(" world"); @@ -98,7 +98,7 @@ describe("basic functionality", () => { test("logs missing variable error to console when missingVarLog is 'error'", async () => { delete process.env["GREETING"]; process.env["ACTOR"] = "world"; - await replaceTokens("#{", "}#", ["test2.txt"], "", MissingVarLog.Error); + await replaceTokens("#{", "}#", ["test2.txt"], "", MissingVarLog.Error, "{}"); const content = await fs.readFile('test2.txt', 'utf8'); expect(content).toBe(" world"); @@ -109,7 +109,7 @@ describe("basic functionality", () => { test("does not log missing variable to console when missingVarLog is incorrect", async () => { delete process.env["GREETING"]; process.env["ACTOR"] = "world"; - await replaceTokens("#{", "}#", ["test2.txt"], "", 'NONE' as MissingVarLog); + await replaceTokens("#{", "}#", ["test2.txt"], "", 'NONE' as MissingVarLog, "{}"); const content = await fs.readFile('test2.txt', 'utf8'); expect(content).toBe(" world"); @@ -120,9 +120,18 @@ describe("basic functionality", () => { test("replaces token with value from missingVarDefault", async () => { delete process.env["GREETING"]; process.env["ACTOR"] = "world"; - await replaceTokens("#{", "}#", ["test2.txt"], "[MISSING_VALUE]", MissingVarLog.Off); + await replaceTokens("#{", "}#", ["test2.txt"], "[MISSING_VALUE]", MissingVarLog.Off, "{}"); const content = await fs.readFile('test2.txt', 'utf8'); expect(content).toBe("[MISSING_VALUE] world"); }); + + test("replaces token with value from additionalVariables", async () => { + const additionalVariables = { GREETING: "Hello" }; + process.env["ACTOR"] = "world"; + await replaceTokens("#{", "}#", ["test2.txt"], "", MissingVarLog.Off, additionalVariables); + + const content = await fs.readFile('test2.txt', 'utf8'); + expect(content).toBe("Hello world"); + }); }); diff --git a/action.yml b/action.yml index 7535be3..e29bc37 100644 --- a/action.yml +++ b/action.yml @@ -2,6 +2,9 @@ name: "Replace tokens" description: "Replace tokens in files" author: "cschleiden" inputs: + additionalVariables: + description: "" + default: "" missingVarDefault: description: "" default: "" diff --git a/dist/index.js b/dist/index.js index 39896b4..ec7c034 100644 --- a/dist/index.js +++ b/dist/index.js @@ -8734,7 +8734,8 @@ function run() { const files = getFiles(); const missingVarDefault = core.getInput("missingVarDefault") || ""; const missingVarLog = getMissingVarLog(); - const result = yield (0, replace_1.replaceTokens)(tokenPrefix, tokenSuffix, Array.isArray(files) ? files : [files], missingVarDefault, missingVarLog); + const additionalVariables = JSON.parse(core.getInput("additionalVariables") || "{}"); + const result = yield (0, replace_1.replaceTokens)(tokenPrefix, tokenSuffix, Array.isArray(files) ? files : [files], missingVarDefault, missingVarLog, additionalVariables); console.log(`Replaced tokens in files: ${result}`); } catch (error) { @@ -8807,10 +8808,14 @@ exports.replaceTokens = void 0; const core = __importStar(__nccwpck_require__(2186)); const replace_in_file_1 = __nccwpck_require__(5983); const missingVarLog_1 = __nccwpck_require__(1367); -function replaceTokens(tokenPrefix, tokenSuffix, files, missingVarDefault, missingVarLog) { +function replaceTokens(tokenPrefix, tokenSuffix, files, missingVarDefault, missingVarLog, additionalVariables) { return __awaiter(this, void 0, void 0, function* () { const fromRegEx = new RegExp(`${escapeDelimiter(tokenPrefix)}(.+?)${escapeDelimiter(tokenSuffix)}`, "gm"); const matchRegEx = new RegExp(`${escapeDelimiter(tokenPrefix)}(.+?)${escapeDelimiter(tokenSuffix)}`); + const getVariable = (tokenName) => { + var _a; + return (_a = additionalVariables[tokenName]) !== null && _a !== void 0 ? _a : process.env[tokenName]; + }; const result = yield (0, replace_in_file_1.replaceInFile)({ files, allowEmptyPaths: true, @@ -8819,7 +8824,7 @@ function replaceTokens(tokenPrefix, tokenSuffix, files, missingVarDefault, missi const m = match.match(matchRegEx); if (m) { const tokenName = m[1]; - const value = process.env[tokenName]; + const value = getVariable(tokenName); if (!!value) { return value; } diff --git a/src/main.ts b/src/main.ts index ceb9aed..e6db2a6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -27,12 +27,14 @@ async function run() { const files = getFiles(); const missingVarDefault = core.getInput("missingVarDefault") || ""; const missingVarLog = getMissingVarLog(); + const additionalVariables = JSON.parse(core.getInput("additionalVariables") || "{}"); const result = await replaceTokens( tokenPrefix, tokenSuffix, Array.isArray(files) ? files : [files], missingVarDefault, - missingVarLog + missingVarLog, + additionalVariables ); console.log(`Replaced tokens in files: ${result}`); } catch (error) { diff --git a/src/replace.ts b/src/replace.ts index f98ad15..454abe8 100644 --- a/src/replace.ts +++ b/src/replace.ts @@ -7,7 +7,8 @@ export async function replaceTokens( tokenSuffix: string, files: string[], missingVarDefault: string, - missingVarLog: MissingVarLog + missingVarLog: MissingVarLog, + additionalVariables: any ) { const fromRegEx = new RegExp( `${escapeDelimiter(tokenPrefix)}(.+?)${escapeDelimiter(tokenSuffix)}`, @@ -16,7 +17,11 @@ export async function replaceTokens( const matchRegEx = new RegExp( `${escapeDelimiter(tokenPrefix)}(.+?)${escapeDelimiter(tokenSuffix)}` ); - + + const getVariable = (tokenName: string): string | undefined => { + return additionalVariables[tokenName] ?? process.env[tokenName] + } + const result = await replaceInFile({ files, allowEmptyPaths: true, @@ -25,7 +30,7 @@ export async function replaceTokens( const m = match.match(matchRegEx); if (m) { const tokenName = m[1]; - const value = process.env[tokenName]; + const value = getVariable(tokenName); if (!!value) { return value; }