Skip to content

Commit

Permalink
Support .mjs
Browse files Browse the repository at this point in the history
  • Loading branch information
abetomo committed Dec 27, 2023
1 parent 171be69 commit 7aed3d2
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
9 changes: 9 additions & 0 deletions index_mjs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// For development/testing purposes
export const handler = (event, context, callback) => {
console.log('Running index.handler (mjs)')
console.log('==================================')
console.log('event', event)
console.log('==================================')
console.log('Stopping index.handler (mjs)')
callback(null)
}
13 changes: 12 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,18 @@ event_sources.json and ${program.eventFile} files as needed.`)

this._createSampleFile(program.eventFile, 'event.json')
const splitHandler = program.handler.split('.')
const filename = splitHandler[0] + '.js'
const filename = (() => {
for (const extension of ['.js', '.mjs']) {
if (fs.existsSync(splitHandler[0] + extension)) {
return splitHandler[0] + extension
}
}
})()
if (filename == null) {
console.error('Handler file not found.')
process.exitCode = 255
return
}
const handlername = splitHandler[1]

// Set custom environment variables if program.configFile is defined
Expand Down
48 changes: 48 additions & 0 deletions test/node-lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,54 @@ describe('bin/node-lambda', () => {
})
})
})

describe('node-lambda run (by *.mjs)', () => {
it('`node-lambda run` by index.mjs', function (done) {
const run = spawn('node', [
nodeLambdaPath, 'run',
'--handler', 'index_mjs.handler'
])
let stdoutString = ''
run.stdout.on('data', (data) => {
stdoutString += data.toString().replace(/\r|\n|\s/g, '')
})

run.on('exit', (code) => {
const expected = 'Runningindex.handler(mjs)' +
'==================================' +
'event{asyncTest:false,callbackWaitsForEmptyEventLoop:true,callbackCode:\'callback(null);\'}' +
'==================================' +
'Stoppingindex.handler(mjs)Success:'
assert.equal(stdoutString, expected)
assert.equal(code, 0)
done()
})
})
})

describe('node-lambda run (handler file not found)', () => {
it('`node-lambda run` Invalid handler specification.', function (done) {
const run = spawn('node', [
nodeLambdaPath, 'run',
'--handler', 'not_found.handler'
])
let stdoutString = ''
run.stdout.on('data', (data) => {
stdoutString += data.toString().replace(/\r|\n|\s/g, '')
})
let stderrString = ''
run.stderr.on('data', (data) => {
stderrString += data.toString()
})

run.on('exit', (code) => {
assert.equal(stdoutString, '')
assert.match(stderrString, /Handler file not found\./)
assert.equal(code, 255)
done()
})
})
})
})

describe('node-lambda duplicate check of short option', () => {
Expand Down

0 comments on commit 7aed3d2

Please sign in to comment.