Skip to content

Commit

Permalink
Upgrade @qetza/replacetokens to 1.2.0 (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
qetza authored Mar 11, 2024
1 parent 92d5797 commit 22e8e80
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 56 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Add support for YAML variable files (`.yml` or `.yaml`)
- Fix log level (`info`) for groups
- Add anonymous telemetry usage
- Upgrade package `@qetza/replacetokens` to `1.2.0`

## v1.0.0
- Initial Release of the ReplaceTokens action
64 changes: 24 additions & 40 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14901,7 +14901,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
};
var _a, _b;
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.replaceTokens = exports.readTextFile = exports.merge = exports.Counter = exports.Defaults = exports.Escapes = exports.MissingVariables = exports.TokenPatterns = exports.Encodings = void 0;
exports.replaceTokens = exports.readTextFile = exports.flattenAndMerge = exports.Counter = exports.Defaults = exports.Escapes = exports.MissingVariables = exports.TokenPatterns = exports.Encodings = void 0;
const fs = __importStar(__nccwpck_require__(3292));
const iconv = __importStar(__nccwpck_require__(9032));
const jschardet = __importStar(__nccwpck_require__(9783));
Expand Down Expand Up @@ -14961,29 +14961,25 @@ class Counter {
}
}
exports.Counter = Counter;
function merge(...objects) {
function flattenAndMerge(separator, ...objects) {
return objects.reduce((result, current) => {
if (Array.isArray(current))
return current;
for (const key of Object.keys(current)) {
if (!result[key]) {
// key doesn't already exists, copy current
result[key] =
current[key] && typeof current[key] === 'object' ? JSON.parse(JSON.stringify(current[key])) : current[key];
}
else if (typeof result[key] === 'object' && typeof current[key] === 'object') {
// merge object values
result[key] = merge(result[key], current[key]);
}
else {
// update scalar value
result[key] = current[key];
}
}
return result;
return Object.assign(Object.assign({}, result), flatten(current, separator));
}, {});
}
exports.merge = merge;
exports.flattenAndMerge = flattenAndMerge;
function flatten(object, separator, parentKey) {
let result = {};
Object.keys(object).forEach((key) => {
var _a;
const value = object[key];
const flattenKey = parentKey ? `${parentKey}${separator}${key}` : key;
if (value && typeof value === 'object')
result = Object.assign(Object.assign({}, result), flatten(value, separator, flattenKey));
else
result[flattenKey] = (_a = value === null || value === void 0 ? void 0 : value.toString()) !== null && _a !== void 0 ? _a : '';
});
return result;
}
function readTextFile(path, encoding = Encodings.Auto) {
return __awaiter(this, void 0, void 0, function* () {
encoding = encoding !== null && encoding !== void 0 ? encoding : Encodings.Auto;
Expand Down Expand Up @@ -15130,19 +15126,7 @@ function loadVariables(variables, options) {
console.group('loading variables');
try {
// parse, flatten and stringify json variables
const data = (function flatten(obj, parentKey) {
let result = {};
Object.keys(obj).forEach((key) => {
var _a;
const value = obj[key];
const flattenKey = parentKey ? `${parentKey}${options.separator}${key}` : key;
if (value && typeof value === 'object')
result = Object.assign(Object.assign({}, result), flatten(value, flattenKey));
else
result[flattenKey] = (_a = value === null || value === void 0 ? void 0 : value.toString()) !== null && _a !== void 0 ? _a : '';
});
return result;
})(variables !== null && variables !== void 0 ? variables : {});
const data = flatten(variables !== null && variables !== void 0 ? variables : {}, options.separator);
// log variables
let count = 0;
for (const key of Object.keys(data)) {
Expand Down Expand Up @@ -63304,7 +63288,7 @@ async function run() {
}
};
const sources = core.getMultilineInput('sources', { required: true, trimWhitespace: true });
const variables = await parseVariables(core.getInput('variables', { required: true, trimWhitespace: true }), options.root || process.cwd());
const variables = await parseVariables(core.getInput('variables', { required: true, trimWhitespace: true }), options.root || process.cwd(), options.separator);
const ifNoFilesFound = getChoiceInput('if-no-files-found', ['ignore', 'warn', 'error']) || 'ignore';
const logLevelStr = getChoiceInput('log-level', ['debug', 'info', 'warn', 'error']) || 'info';
// set telemetry attributes
Expand Down Expand Up @@ -63409,14 +63393,14 @@ function getChoiceInput(name, choices, options) {
}
var variablesEnvCount = 0;
var inlineVariablesCount = 0;
async function parseVariables(input, root) {
async function parseVariables(input, root, separator) {
input = input || '{}';
const variables = JSON.parse((0, strip_json_comments_1.default)(input));
let load = async (v) => {
if (typeof v === 'string') {
switch (v[0]) {
case '@': // single string referencing a file
return await loadVariablesFromFile(v.substring(1), root);
return await loadVariablesFromFile(v.substring(1), root, separator);
case '$': // single string referencing environment variable
core.debug(`loading variables from environment '${v.substring(1)}'`);
++variablesEnvCount;
Expand All @@ -63434,12 +63418,12 @@ async function parseVariables(input, root) {
for (let v of variables) {
vars.push(await load(v));
}
return (0, replacetokens_1.merge)(...vars);
return (0, replacetokens_1.flattenAndMerge)(separator, ...vars);
}
return await load(variables);
}
var variableFilesCount = 0;
async function loadVariablesFromFile(name, root) {
async function loadVariablesFromFile(name, root, separator) {
var files = await fg.glob(name.split(';').map(v => v.trim()), {
absolute: true,
cwd: root,
Expand All @@ -63460,7 +63444,7 @@ async function loadVariablesFromFile(name, root) {
}
++variableFilesCount;
}
return (0, replacetokens_1.merge)(...vars);
return (0, replacetokens_1.flattenAndMerge)(separator, ...vars);
}
var LogLevel;
(function (LogLevel) {
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"@actions/core": "^1.10.1",
"@opentelemetry/api": "^1.8.0",
"@opentelemetry/sdk-trace-base": "^1.22.0",
"@qetza/replacetokens": "^1.1.0",
"@qetza/replacetokens": "^1.2.0",
"axios": "^1.6.7",
"fast-glob": "^3.3.2",
"js-yaml": "^4.1.0"
Expand Down
15 changes: 8 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as core from '@actions/core';
import {
readTextFile,
merge,
flattenAndMerge,
replaceTokens,
Defaults,
Encodings,
Expand Down Expand Up @@ -95,7 +95,8 @@ export async function run(): Promise<void> {
const sources = core.getMultilineInput('sources', { required: true, trimWhitespace: true });
const variables = await parseVariables(
core.getInput('variables', { required: true, trimWhitespace: true }),
options.root || process.cwd()
options.root || process.cwd(),
options.separator!
);

const ifNoFilesFound = getChoiceInput('if-no-files-found', ['ignore', 'warn', 'error']) || 'ignore';
Expand Down Expand Up @@ -207,15 +208,15 @@ function getChoiceInput(name: string, choices: string[], options?: core.InputOpt

var variablesEnvCount = 0;
var inlineVariablesCount = 0;
async function parseVariables(input: string, root: string): Promise<{ [key: string]: any }> {
async function parseVariables(input: string, root: string, separator: string): Promise<{ [key: string]: any }> {
input = input || '{}';
const variables = JSON.parse(stripJsonComments(input));

let load = async (v: any) => {
if (typeof v === 'string') {
switch (v[0]) {
case '@': // single string referencing a file
return await loadVariablesFromFile(v.substring(1), root);
return await loadVariablesFromFile(v.substring(1), root, separator);

case '$': // single string referencing environment variable
core.debug(`loading variables from environment '${v.substring(1)}'`);
Expand Down Expand Up @@ -243,14 +244,14 @@ async function parseVariables(input: string, root: string): Promise<{ [key: stri
vars.push(await load(v));
}

return merge(...vars);
return flattenAndMerge(separator, ...vars);
}

return await load(variables);
}

var variableFilesCount = 0;
async function loadVariablesFromFile(name: string, root: string): Promise<{ [key: string]: any }> {
async function loadVariablesFromFile(name: string, root: string, separator: string): Promise<{ [key: string]: any }> {
var files = await fg.glob(
name.split(';').map(v => v.trim()),
{
Expand Down Expand Up @@ -278,7 +279,7 @@ async function loadVariablesFromFile(name: string, root: string): Promise<{ [key
++variableFilesCount;
}

return merge(...vars);
return flattenAndMerge(separator, ...vars);
}

enum LogLevel {
Expand Down
11 changes: 10 additions & 1 deletion tests/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ describe('run', () => {
case 'variables':
return JSON.stringify([
{ VAR1: 'value1', VAR2: 'value2', VAR3: 'value3' },
[1, true, { VAR6: 'value6' }],
'$ENV_VARS',
`@${path.join(__dirname, 'data/vars.jsonc').replace(/\\/g, '/')}`,
'@**/vars.(yml|yaml)'
Expand All @@ -409,7 +410,15 @@ describe('run', () => {

expect(replaceTokenSpy).toHaveBeenCalledWith(
expect.anything(),
{ VAR1: 'value1', VAR2: 'env_value2', VAR3: 'file_value3', VAR5: 'file_value5' },
{
VAR1: 'value1',
VAR2: 'env_value2',
VAR3: 'file_value3',
VAR5: 'file_value5',
'0': '1',
'1': 'true',
'2.VAR6': 'value6'
},
expect.anything()
);
});
Expand Down

0 comments on commit 22e8e80

Please sign in to comment.