Skip to content

Commit 8358673

Browse files
authored
feat: support for yarn (#581)
fix #579
1 parent 02df10f commit 8358673

File tree

3 files changed

+250
-110
lines changed

3 files changed

+250
-110
lines changed

bin/node-lambda

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const fs = require('fs')
1111
const packageJson = fs.existsSync(path.join(process.cwd(), 'package.json')) ? require(path.join(process.cwd(), 'package.json')) : {}
1212
const packageJsonName = packageJson.name || 'UnnamedFunction'
1313

14+
const PACKAGE_MANAGER = process.env.PACKAGE_MANAGER || 'npm'
1415
const AWS_ENVIRONMENT = process.env.AWS_ENVIRONMENT || ''
1516
const AWS_ENDPOINT = process.env.AWS_ENDPOINT || ''
1617
const CONFIG_FILE = process.env.CONFIG_FILE || ''
@@ -66,6 +67,7 @@ program.showSuggestionAfterError()
6667
program
6768
.command('deploy')
6869
.description('Deploy your application to Amazon Lambda')
70+
.option('--packageManager [PACKAGE_MANAGER]', 'Package manager', PACKAGE_MANAGER)
6971
.option('-e, --environment [AWS_ENVIRONMENT]', 'Choose environment {dev, staging, production}',
7072
AWS_ENVIRONMENT)
7173
.option('-E, --endpoint [AWS_ENDPOINT]', 'Choose endpoint (e.g. localstack, "http://127.0.0.1:4574")',
@@ -123,6 +125,7 @@ program
123125
.command('package')
124126
.alias('zip')
125127
.description('Create zipped package for Amazon Lambda deployment')
128+
.option('--packageManager [PACKAGE_MANAGER]', 'Package manager', PACKAGE_MANAGER)
126129
.option('-A, --packageDirectory [PACKAGE_DIRECTORY]', 'Local Package Directory', PACKAGE_DIRECTORY)
127130
.option('-I, --dockerImage [DOCKER_IMAGE]', 'Docker image for npm ci', DOCKER_IMAGE)
128131
.option('-n, --functionName [AWS_FUNCTION_NAME]', 'Lambda FunctionName', AWS_FUNCTION_NAME)

lib/main.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,38 @@ Emulate only the body of the API Gateway event.
407407
}
408408
}
409409

410+
_getYarnInstallCommand (program, codeDirectory) {
411+
const installOptions = [
412+
'-s',
413+
'install',
414+
'--production'
415+
]
416+
417+
if (program.optionalDependencies === false) {
418+
installOptions.push('--ignore-optional')
419+
}
420+
421+
if (!program.dockerImage) {
422+
installOptions.push('--cwd', codeDirectory)
423+
}
424+
425+
return {
426+
packageManager: 'yarn',
427+
installOptions
428+
}
429+
}
430+
410431
_packageInstall (program, codeDirectory) {
411432
// Run on windows:
412433
// https://nodejs.org/api/child_process.html#child_process_spawning_bat_and_cmd_files_on_windows
413434

414-
const { packageManager, installOptions } = this._getNpmInstallCommand(program, codeDirectory)
435+
const { packageManager, installOptions } = (() => {
436+
// default npm
437+
if (program.packageManager === 'yarn') {
438+
return this._getYarnInstallCommand(program, codeDirectory)
439+
}
440+
return this._getNpmInstallCommand(program, codeDirectory)
441+
})()
415442

416443
const paramsOnContainer = (() => {
417444
// with docker
@@ -459,7 +486,7 @@ Emulate only the body of the API Gateway event.
459486
env: process.env
460487
}, (err) => {
461488
if (err) return reject(err)
462-
resolve()
489+
resolve(packageManager)
463490
})
464491
})
465492
}
@@ -701,7 +728,8 @@ they may not work as expected in the Lambda environment.
701728
await this._fileCopy(program, lambdaSrcDirectory, codeDirectory, true)
702729
if (!program.keepNodeModules) {
703730
console.log('=> Running package install')
704-
await this._packageInstall(program, codeDirectory)
731+
const usedPackageManager = await this._packageInstall(program, codeDirectory)
732+
console.log(`(Package manager used was '${usedPackageManager}'.)`)
705733
}
706734
await this._postInstallScript(program, codeDirectory)
707735
console.log('=> Zipping deployment package')

0 commit comments

Comments
 (0)