Skip to content

Commit

Permalink
Merge pull request #7 from architect/6
Browse files Browse the repository at this point in the history
Fixes #6
  • Loading branch information
ryanblock authored Aug 8, 2023
2 parents 8d59f15 + 5e8a82b commit e00402f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
9 changes: 9 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

---

## [1.1.0] 2023-08-07

### Added

- Added support from mocks in `sandbox-invoke-mocks.cjs`, `sandbox-invoke-mocks.mjs` filenames, and importing as ES modules from `sandbox-invoke-mocks.js` (and `.mjs`, of course); fixes #6, thanks @danactive!
- Note: per [Node.js #2806](https://github.com/nodejs/help/issues/2806), ES module data may be cached in Sandbox, which is intended to be a semi-long-lived process. If using ES modules, you may need to restart Sandbox when updating your mocks.

---

## [1.0.1] 2023-07-31

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@architect/plugin-lambda-invoker",
"version": "1.0.1",
"version": "1.1.0-RC.0",
"description": "Interactively invoke Lambdas in Architect Sandbox with arbitrary events",
"main": "src/index.js",
"scripts": {
Expand Down
48 changes: 44 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module.exports = {
let { cwd, preferences } = inv._project
let jsonMocks = join(cwd, 'sandbox-invoke-mocks.json')
let jsMocks = join(cwd, 'sandbox-invoke-mocks.js')
let cjsMocks = join(cwd, 'sandbox-invoke-mocks.cjs')
let mjsMocks = join(cwd, 'sandbox-invoke-mocks.mjs')

let pragmas = [ 'customLambdas', 'events', 'queues', 'scheduled', 'tables-streams' ]
let prefs = preferences?.sandbox?.invoker
Expand Down Expand Up @@ -65,14 +67,18 @@ module.exports = {
let pragma, name, mocks, skipSelection

// Load invocation mocks
if (existsSync(jsonMocks)) {
/**/ if (existsSync(jsonMocks)) {
mocks = JSON.parse(readFileSync(jsonMocks))
}
else if (existsSync(jsMocks)) {
// Make sure changes to mocks are always reflected
delete require.cache[require.resolve(jsMocks)]
mocks = await getMod(jsMocks)
}
else if (existsSync(cjsMocks)) {
// eslint-disable-next-line
mocks = require(jsMocks)
mocks = require(cjsMocks)
}
else if (existsSync(mjsMocks)) {
mocks = await getMod(mjsMocks)
}

try {
Expand Down Expand Up @@ -178,3 +184,37 @@ function end () {
process.stdin.pause()
}
}


let esmErrors = [
'Cannot use import statement outside a module',
`Unexpected token 'export'`,
'require() of ES Module',
'Must use import to load ES Module',
]
let hasEsmError = err => esmErrors.some(msg => err.message.includes(msg))
async function getMod (filepath) {
let mod

// Best effort to ensure changes to mocks are always reflected
delete require.cache[require.resolve(filepath)]

try {
// eslint-disable-next-line
mod = require(filepath)
}
catch (err) {
if (hasEsmError(err)) {
let path = process.platform.startsWith('win')
? 'file://' + filepath
: filepath
let imported = await import(path)
mod = imported.default ? imported.default : imported
}
else {
throw err
}
}

return mod
}

0 comments on commit e00402f

Please sign in to comment.