Skip to content

Commit

Permalink
test: refactor replacement token (#568)
Browse files Browse the repository at this point in the history
  • Loading branch information
hung-cybo authored Nov 14, 2023
1 parent ca8b71e commit 8ee4244
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions features/ultils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ export const execCliKintoneSync = (
) => {
const response = spawnSync(
getCliKintoneBinary(),
replaceTokenWithEnvVars(args).split(/\s+/),
replaceTokenWithEnvVars(args, options?.env).split(/\s+/),
{
encoding: "utf-8",
shell: true,
env: options?.env ?? {},
cwd: options?.cwd ?? process.cwd(),
},
Expand All @@ -36,18 +35,38 @@ export const getCliKintoneBinary = (): string => {
}
};

export const replaceTokenWithEnvVars = (input: string) =>
input.replace(/\$\$[a-zA-Z0-9_]+/g, replacer);
const replaceTokenWithEnvVars = (
input: string,
envVars: { [key: string]: string } | undefined,
) =>
input
.replace(/\$\$[a-zA-Z0-9_]+/g, processEnvReplacer)
.replace(/\$[a-zA-Z0-9_]+/g, inputEnvReplacer(envVars));

const replacer = (substring: string) => {
const processEnvReplacer = (substring: string) => {
const key = substring.replace("$$", "");
const value = process.env[key];
if (value === undefined) {
throw new Error(`The env variable is missing: ${key}`);
throw new Error(`The env variable in process.env is missing: ${key}`);
}
return value;
};

const inputEnvReplacer = (envVars: { [key: string]: string } | undefined) => {
return (substring: string) => {
if (!envVars) {
return substring;
}

const key = substring.replace("$", "");
const value = envVars[key];
if (value === undefined) {
throw new Error(`The env variable in input parameter is missing: ${key}`);
}
return value;
};
};

export const createCsvFile = async (
inputCsvObject: string[][],
options: { baseDir?: string; destFilePath?: string },
Expand Down

0 comments on commit 8ee4244

Please sign in to comment.