diff --git a/src/context/directory/handlers/actions.ts b/src/context/directory/handlers/actions.ts index e32759c18..9ae2c9889 100644 --- a/src/context/directory/handlers/actions.ts +++ b/src/context/directory/handlers/actions.ts @@ -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; diff --git a/test/context/directory/actions.test.js b/test/context/directory/actions.test.js index 97aa5ec63..fce53da85 100644 --- a/test/context/directory/actions.test.js +++ b/test/context/directory/actions.test.js @@ -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);