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

Add keepNodeModules option in package #472

Merged
merged 2 commits into from
Nov 27, 2018
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ Options:
-e, --environment [development] Choose environment {dev, staging, production}
-x, --excludeGlobs [event.json] Space-separated glob pattern(s) for additional exclude files (e.g. "event.json dotenv.sample")
-D, --prebuiltDirectory [] Prebuilt directory
-m, --keepNodeModules [false] Keep the current node_modules and skip the npm install step
-v, --dockerVolumes [] Additional docker volumes to mount. Each volume definition has to be separated by a space (e.g. "$HOME/.gitconfig:/etc/gitconfig $HOME/.ssh:/root/.ssh"
```

Expand Down
2 changes: 2 additions & 0 deletions bin/node-lambda
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const AWS_DLQ_TARGET_ARN = (() => {
})()
const PROXY = process.env.PROXY || process.env.http_proxy || ''
const ENABLE_RUN_MULTIPLE_EVENTS = true
const KEEP_NODE_MODULES = process.env.KEEP_NODE_MODULES || false
const DOCKER_VOLUMES = process.env.DOCKER_VOLUMES || ''

program
Expand Down Expand Up @@ -120,6 +121,7 @@ program
.option('-x, --excludeGlobs [EXCLUDE_GLOBS]',
'Space-separated glob pattern(s) for additional exclude files (e.g. "event.json dotenv.sample")', EXCLUDE_GLOBS)
.option('-D, --prebuiltDirectory [PREBUILT_DIRECTORY]', 'Prebuilt directory', PREBUILT_DIRECTORY)
.option('-m, --keepNodeModules [KEEP_NODE_MODULES]', 'Keep the current node_modules directory.', KEEP_NODE_MODULES)
.option('-v, --dockerVolumes [DOCKER_VOLUMES]', 'Additional docker volumes to mount. Each volume definition has to be separated by a space (e.g. "$HOME/.gitconfig:/etc/gitconfig $HOME/.ssh:/root/.ssh")', DOCKER_VOLUMES)
.action((prg) => lambda.package(prg))

Expand Down
42 changes: 31 additions & 11 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,20 +472,36 @@ so you can easily test run multiple events.
return path.join(os.tmpdir(), `${path.basename(path.resolve('.'))}-lambda`)
}

_cleanDirectory (codeDirectory) {
return new Promise((resolve, reject) => {
fs.remove(codeDirectory, (err) => {
if (err) return reject(err)
resolve()
})
}).then(() => {
_cleanDirectory (codeDirectory, keepNodeModules) {
if (!fs.existsSync(codeDirectory)) {
return new Promise((resolve, reject) => {
fs.mkdirs(codeDirectory, (err) => {
if (err) return reject(err)
resolve()
})
})
})
} else {
return new Promise((resolve, reject) => {
fs.readdir(codeDirectory, (err, files) => {
if (err) return reject(err)

Promise.all(files.map(file => {
return new Promise((resolve, reject) => {
if (keepNodeModules && file === 'node_modules') {
resolve()
} else {
fs.remove(path.join(codeDirectory, file), err => {
if (err) return reject(err)
resolve()
})
}
})
})).then(() => {
resolve()
})
})
})
}
}

_setRunTimeEnvironmentVars (program) {
Expand Down Expand Up @@ -600,13 +616,17 @@ they may not work as expected in the Lambda environment.
const lambdaSrcDirectory = program.sourceDirectory ? program.sourceDirectory.replace(/\/$/, '') : '.'

return Promise.resolve().then(() => {
return this._cleanDirectory(codeDirectory)
return this._cleanDirectory(codeDirectory, program.keepNodeModules)
}).then(() => {
console.log('=> Moving files to temporary directory')
return this._fileCopy(program, lambdaSrcDirectory, codeDirectory, true)
}).then(() => {
console.log('=> Running npm install --production')
return this._npmInstall(program, codeDirectory)
if (program.keepNodeModules) {
return Promise.resolve()
} else {
console.log('=> Running npm install --production')
return this._npmInstall(program, codeDirectory)
}
}).then(() => {
return this._postInstallScript(program, codeDirectory)
}).then(() => {
Expand Down