Skip to content

Commit

Permalink
Add relative path import support for actions directory handler (#866)
Browse files Browse the repository at this point in the history
* Add support for relative path loading for actions in directory mode

* Add support for relative path loading for actions in directory mode

---------

Co-authored-by: Will Vedder <[email protected]>
  • Loading branch information
willvedd and willvedd authored Nov 29, 2023
1 parent 5ab9ef5 commit ae243c5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/context/directory/handlers/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ function parse(context: DirectoryContext): ParsedActions {
const actionFolder = path.join(constants.ACTIONS_DIRECTORY, `${action.name}`);

if (action.code) {
const toUnixPath = (somePath) =>
somePath.replace(/[\\/]+/g, '/').replace(/^([a-zA-Z]+:|\.\/)/, '');
action.code = context.loadFile(toUnixPath(action.code), actionFolder);
const unixPath = action.code.replace(/[\\/]+/g, '/').replace(/^([a-zA-Z]+:|\.\/)/, '');
if (fs.existsSync(unixPath)) {
action.code = context.loadFile(unixPath, actionFolder);
} else {
action.code = context.loadFile(path.join(context.filePath, action.code), actionFolder);
}
}

return action;
Expand Down
39 changes: 39 additions & 0 deletions test/context/directory/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,45 @@ describe('#directory context actions', () => {
expect(context.assets.actions).to.deep.equal(actionsTarget);
});

it('should process actions when code is stored in path relative to input file', async () => {
const repoDir = path.join(testDataDir, 'directory', 'test5');
const files = {
'separate-directory': {
'action-code.js':
'/** @type {PostLoginAction} */ module.exports = async (event, context) => { console.log("test-action"); return {}; };',
},
[constants.ACTIONS_DIRECTORY]: {
'action-one.json': `{
"name": "action-one",
"code": "./separate-directory/action-code.js",
"runtime": "node12",
"dependencies": [
{
"name": "lodash",
"version": "4.17.20"
}
],
"secrets": [],
"status": "built",
"supported_triggers": [
{
"id": "post-login",
"version": "v1"
}
],
"deployed": true
}`,
},
};
createDir(repoDir, files);
const config = {
AUTH0_INPUT_FILE: repoDir,
};
const context = new Context(config, mockMgmtClient());
await context.loadAssetsFromLocal();
expect(context.assets.actions).to.deep.equal(actionsTarget);
});

it('should ignore bad actions directory', async () => {
const repoDir = path.join(testDataDir, 'directory', 'test2');
cleanThenMkdir(repoDir);
Expand Down

0 comments on commit ae243c5

Please sign in to comment.