Skip to content

Commit ae243c5

Browse files
authored
Add relative path import support for actions directory handler (#866)
* 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]>
1 parent 5ab9ef5 commit ae243c5

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/context/directory/handlers/actions.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ function parse(context: DirectoryContext): ParsedActions {
2828
const actionFolder = path.join(constants.ACTIONS_DIRECTORY, `${action.name}`);
2929

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

3639
return action;

test/context/directory/actions.test.js

+39
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,45 @@ describe('#directory context actions', () => {
111111
expect(context.assets.actions).to.deep.equal(actionsTarget);
112112
});
113113

114+
it('should process actions when code is stored in path relative to input file', async () => {
115+
const repoDir = path.join(testDataDir, 'directory', 'test5');
116+
const files = {
117+
'separate-directory': {
118+
'action-code.js':
119+
'/** @type {PostLoginAction} */ module.exports = async (event, context) => { console.log("test-action"); return {}; };',
120+
},
121+
[constants.ACTIONS_DIRECTORY]: {
122+
'action-one.json': `{
123+
"name": "action-one",
124+
"code": "./separate-directory/action-code.js",
125+
"runtime": "node12",
126+
"dependencies": [
127+
{
128+
"name": "lodash",
129+
"version": "4.17.20"
130+
}
131+
],
132+
"secrets": [],
133+
"status": "built",
134+
"supported_triggers": [
135+
{
136+
"id": "post-login",
137+
"version": "v1"
138+
}
139+
],
140+
"deployed": true
141+
}`,
142+
},
143+
};
144+
createDir(repoDir, files);
145+
const config = {
146+
AUTH0_INPUT_FILE: repoDir,
147+
};
148+
const context = new Context(config, mockMgmtClient());
149+
await context.loadAssetsFromLocal();
150+
expect(context.assets.actions).to.deep.equal(actionsTarget);
151+
});
152+
114153
it('should ignore bad actions directory', async () => {
115154
const repoDir = path.join(testDataDir, 'directory', 'test2');
116155
cleanThenMkdir(repoDir);

0 commit comments

Comments
 (0)