Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support .mjs #701

Merged
merged 2 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
54 changes: 51 additions & 3 deletions test/node-lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ describe('bin/node-lambda', () => {
})
})

describe('node-lambda run (Multiple events))', () => {
describe('node-lambda run (Multiple events)', () => {
const eventObj = [{
asyncTest: false,
callbackWaitsForEmptyEventLoop: true,
Expand Down Expand Up @@ -217,7 +217,7 @@ describe('bin/node-lambda', () => {
})
})

describe('node-lambda run (disable Multiple events))', () => {
describe('node-lambda run (disable Multiple events)', () => {
const eventObj = [{
asyncTest: false,
callbackWaitsForEmptyEventLoop: true,
Expand Down Expand Up @@ -264,7 +264,7 @@ describe('bin/node-lambda', () => {
})
})

describe('node-lambda run (API Gateway events))', () => {
describe('node-lambda run (API Gateway events)', () => {
const eventObj = {
asyncTest: false,
callbackWaitsForEmptyEventLoop: true,
Expand Down 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