Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

Commit

Permalink
feat(params): add support for nested action and package params (#68)
Browse files Browse the repository at this point in the history
fixes #67
  • Loading branch information
tripodsan authored Nov 5, 2019
1 parent 4e1dbe1 commit 3d0619b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 31 deletions.
83 changes: 55 additions & 28 deletions src/action_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,35 @@ module.exports = class ActionBuilder {
cwd = path.dirname(params);
}
let data;
// first try JSON
try {
data = JSON.parse(content);
} catch (e) {
// then try env
data = dotenv.parse(content);
if (typeof params === 'object') {
data = content;
} else {
// first try JSON
try {
data = JSON.parse(content);
} catch (e) {
// then try env
data = dotenv.parse(content);
}
}

// resolve file references
Object.keys(data).forEach((key) => {
const param = `${data[key]}`;
if (param.startsWith('@') && !param.startsWith('@@')) {
const filePath = path.resolve(cwd, param.substring(1));
data[key] = `@${filePath}`;
}
});
const resolve = (obj) => {
// resolve file references
Object.keys(obj).forEach((key) => {
const value = obj[key];
if (typeof value === 'object') {
resolve(value);
} else {
const param = String(value);
if (param.startsWith('@') && !param.startsWith('@@')) {
const filePath = path.resolve(cwd, param.substring(1));
// eslint-disable-next-line no-param-reassign
obj[key] = `@${filePath}`;
}
}
});
};
resolve(data);
return data;
}

Expand All @@ -69,20 +82,34 @@ module.exports = class ActionBuilder {
* @returns the resolved object.
*/
static async resolveParams(params) {
const resolved = {};
await Promise.all(Object.keys(params).map(async (key) => {
const param = params[key];
if (!param.startsWith('@')) {
resolved[key] = param;
return;
}
if (param.startsWith('@@')) {
resolved[key] = param.substring(1);
return;
}
resolved[key] = await fse.readFile(param.substring(1), 'utf-8');
}));
return resolved;
const tasks = [];
const resolve = async (obj, key, file) => {
// eslint-disable-next-line no-param-reassign
obj[key] = await fse.readFile(file, 'utf-8');
};

const resolver = (obj) => {
Object.keys(obj).forEach((key) => {
const param = obj[key];
if (typeof param === 'object') {
resolver(param);
} else {
const value = String(param);
if (value.startsWith('@@')) {
// eslint-disable-next-line no-param-reassign
obj[key] = value.substring(1);
} else if (value.startsWith('@')) {
tasks.push(resolve(obj, key, value.substring(1)));
} else {
// eslint-disable-next-line no-param-reassign
obj[key] = value;
}
}
});
};
resolver(params);
await Promise.all(tasks);
return params;
}

/**
Expand Down
12 changes: 10 additions & 2 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,17 @@ describe('CLI Test', () => {
assert.deepEqual(builder._externals, [/.*/]);
});

it('can add params from json file', () => {
it('can add params from json file', async () => {
const file = path.resolve(__dirname, 'fixtures/test-params.json');
const builder = new CLI()
.prepare(['-f', file]);
await builder.validate();
assert.deepEqual(builder._params, {
bar: 'Hello, world.',
foo: 42,
secrets: {
key: 'my test key!\n',
},
});
});

Expand All @@ -209,13 +213,17 @@ describe('CLI Test', () => {
});
});

it('can add package params from json file', () => {
it('can add package params from json file', async () => {
const file = path.resolve(__dirname, 'fixtures/test-params.json');
const builder = new CLI()
.prepare(['--package.params-file', file]);
await builder.validate();
assert.deepEqual(builder._packageParams, {
bar: 'Hello, world.',
foo: 42,
secrets: {
key: 'my test key!\n',
},
});
});

Expand Down
5 changes: 4 additions & 1 deletion test/fixtures/test-params.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"foo": 42,
"bar": "Hello, world."
"bar": "Hello, world.",
"secrets": {
"key": "@./test-key.txt"
}
}

0 comments on commit 3d0619b

Please sign in to comment.