Skip to content
This repository was archived by the owner on Jan 17, 2025. It is now read-only.

Commit 5a204ef

Browse files
committed
fix for require('fs') in compositions
Fixes #934
1 parent ba8c085 commit 5a204ef

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

lib/create-from-source.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,10 @@ exports.compileToFSM = (src, opts={}) => new Promise((resolve, reject) => {
113113
require: m => {
114114
if (m === '@ibm-functions/composer') {
115115
return openwhiskComposer
116+
} else if (m.charAt(0) !== '.') {
117+
return require(m)
116118
} else {
117-
return require(path.resolve(dir, m))
118-
119+
return require(path.resolve(m))
119120
}
120121
}
121122
}
@@ -134,7 +135,20 @@ exports.compileToFSM = (src, opts={}) => new Promise((resolve, reject) => {
134135
}
135136
const sandboxWithComposer = Object.assign(sandbox, { composer: openwhiskComposer })
136137

137-
let res = vm.runInNewContext(code, sandboxWithComposer)
138+
// we need to be in the directory of the
139+
// source, in case it does relative requires
140+
// or reads from that relative location
141+
const curdir = process.cwd()
142+
process.chdir(ui.findFile(dir))
143+
144+
let res
145+
try {
146+
res = vm.runInNewContext(code, sandboxWithComposer)
147+
} finally {
148+
// make sure we change back to where we started
149+
process.chdir(curdir)
150+
}
151+
138152
debug('res', typeof res, res)
139153

140154
if (typeof res === 'function') {

0 commit comments

Comments
 (0)