diff --git a/packages/core/src/app-middlewares/before/z-object.js b/packages/core/src/app-middlewares/before/z-object.js index 668a3032f..8f2c295ac 100644 --- a/packages/core/src/app-middlewares/before/z-object.js +++ b/packages/core/src/app-middlewares/before/z-object.js @@ -29,7 +29,10 @@ const injectZObject = (input) => { generateCallbackUrl: createCallbackHigherOrderFunction(input), hash: hashing.hashify, JSON: createJSONtool(), - require: (moduleName) => require(moduleName), + require: (moduleName) => + require(require.resolve(moduleName, { + paths: module.paths.concat([process.cwd()]), + })), stashFile: createFileStasher(input), }; diff --git a/packages/core/test/create-app.js b/packages/core/test/create-app.js index c1db93d70..80e772a7d 100644 --- a/packages/core/test/create-app.js +++ b/packages/core/test/create-app.js @@ -602,6 +602,16 @@ describe('create-app', () => { /Cannot find module 'non-existing-package'/ ); }); + + it('should be able to import from other paths using zRequire', async () => { + const input = createTestInput( + 'resources.listrequire.list.operation.perform' + ); + const appPass = createApp(appDefinition); + + const { results } = await appPass(input); + results.should.eql('www.base-url.com'); + }); }); describe('response.content parsing', () => { diff --git a/packages/core/test/userapp/constants.js b/packages/core/test/userapp/constants.js new file mode 100644 index 000000000..53f5932ad --- /dev/null +++ b/packages/core/test/userapp/constants.js @@ -0,0 +1,5 @@ +const BASE_URL = 'www.base-url.com'; + +module.exports = { + BASE_URL, +}; diff --git a/packages/core/test/userapp/index.js b/packages/core/test/userapp/index.js index 7f4dff217..cee86c212 100644 --- a/packages/core/test/userapp/index.js +++ b/packages/core/test/userapp/index.js @@ -26,6 +26,24 @@ const List = { }, }; +const ListRequire = { + key: 'listrequire', + noun: 'List', + list: { + display: { + description: 'Trigger on new thing in list.', + label: 'List', + }, + operation: { + perform: (z, bundle) => { + // in prod, process.cwd will return the app root directory + const { BASE_URL } = z.require('./test/userapp/constants.js'); + return BASE_URL; + }, + }, + }, +}; + const Contact = { key: 'contact', noun: 'Contact', @@ -386,9 +404,7 @@ const CachedCustomInputFields = { description: 'Get/Set custom input fields in zcache', }, operation: { - inputFields: [ - helpers.getCustomFields, - ], + inputFields: [helpers.getCustomFields], perform: () => {}, }, }, @@ -582,6 +598,7 @@ const App = { afterResponse: [], resources: { [List.key]: List, + [ListRequire.key]: ListRequire, [Contact.key]: Contact, [ContactError.key]: ContactError, [ContactSource.key]: ContactSource,