Skip to content

Commit 207982b

Browse files
Merge pull request #260 from technote-space/release/next-v1.2.2
feat: release v1.3.0
2 parents 63e621f + dd8e984 commit 207982b

File tree

4 files changed

+125
-99
lines changed

4 files changed

+125
-99
lines changed

__tests__/utils2.test.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import { testEnv } from '@technote-space/github-action-test-helper';
33
import { Utils } from '../src';
44

5-
const {generateNewPatchVersion, generateNewMinorVersion, generateNewMajorVersion, arrayChunk, versionCompare, mask} = Utils;
6-
const {isBranch, isTagRef, normalizeRef, trimRef, getTag, getRefspec, getRemoteRefspec, getLocalRefspec, getOctokit} = Utils;
5+
const {generateNewPatchVersion, generateNewMinorVersion, generateNewMajorVersion, arrayChunk, versionCompare, mask} = Utils;
6+
const {isBranch, isTagRef, normalizeRef, trimRef, getTag, getRefspec, getRemoteRefspec, getLocalRefspec, getOctokit, replaceVariables} = Utils;
77

88
jest.useFakeTimers();
99

@@ -251,3 +251,15 @@ describe('mask', () => {
251251
});
252252
});
253253
});
254+
255+
describe('replaceVariables', () => {
256+
it('should replace variables', async() => {
257+
expect(await replaceVariables('', [])).toBe('');
258+
expect(await replaceVariables('${test1}/${test2}/${test3}/${test4}', [
259+
{key: 'test1', replace: '1'},
260+
{key: 'test2', replace: (): string => '2'},
261+
{key: 'test3', replace: (): Promise<string> => Promise.resolve('3')},
262+
{key: 'test5', replace: '5'},
263+
])).toBe('1/2/3/${test4}');
264+
});
265+
});

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@technote-space/github-action-helper",
3-
"version": "1.2.1",
3+
"version": "1.3.0",
44
"description": "Helper to filter GitHub Action.",
55
"author": {
66
"name": "Technote",
@@ -26,7 +26,7 @@
2626
"dist"
2727
],
2828
"dependencies": {
29-
"@actions/core": "^1.2.2",
29+
"@actions/core": "^1.2.3",
3030
"@actions/github": "^2.1.1",
3131
"shell-escape": "^0.2.0",
3232
"sprintf-js": "^1.1.2"
@@ -36,15 +36,15 @@
3636
"@commitlint/config-conventional": "^8.3.4",
3737
"@technote-space/github-action-test-helper": "^0.2.5",
3838
"@types/jest": "^25.1.3",
39-
"@types/node": "^13.7.7",
40-
"@typescript-eslint/eslint-plugin": "^2.21.0",
41-
"@typescript-eslint/parser": "^2.21.0",
39+
"@types/node": "^13.9.0",
40+
"@typescript-eslint/eslint-plugin": "^2.22.0",
41+
"@typescript-eslint/parser": "^2.22.0",
4242
"eslint": "^6.8.0",
4343
"husky": "^4.2.3",
4444
"jest": "^25.1.0",
4545
"jest-circus": "^25.1.0",
4646
"lint-staged": "^10.0.8",
47-
"nock": "^12.0.1",
47+
"nock": "^12.0.2",
4848
"ts-jest": "^25.2.1",
4949
"typescript": "^3.8.3"
5050
},

src/utils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,18 @@ export const mask = (value: object, target = 'token'): object => {
186186

187187
return value;
188188
};
189+
190+
export const replaceVariables = async(string: string, variables: { key: string; replace: (() => Promise<string> | string) | string }[]): Promise<string> => {
191+
let replaced = string;
192+
for (const variable of variables) {
193+
if (getRegExp(`\${${variable.key}}`).test(replaced)) {
194+
if (typeof variable.replace === 'string') {
195+
replaced = replaceAll(replaced, `\${${variable.key}}`, variable.replace);
196+
} else {
197+
replaced = replaceAll(replaced, `\${${variable.key}}`, await variable.replace());
198+
}
199+
}
200+
}
201+
202+
return replaced;
203+
};

0 commit comments

Comments
 (0)