Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add relative path import support for actions directory handler #866

Merged
merged 3 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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