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

Modify from exec to execFile with _npmInstall #303

Merged
merged 8 commits into from
May 31, 2017
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
58 changes: 48 additions & 10 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var path = require('path')
var aws = require('aws-sdk')
var exec = require('child_process').exec
var execFile = require('child_process').execFile
var fs = require('fs-extra')
var packageJson = require(path.join(__dirname, '..', 'package.json'))
var minimatch = require('minimatch')
Expand Down Expand Up @@ -285,18 +286,55 @@ Lambda.prototype._rsync = function (program, src, dest, excludeNodeModules, call
})
}

Lambda.prototype._npmInstall = function (program, codeDirectory, callback) {
const installOptions = [
`--prefix ${codeDirectory}`,
process.platform === 'win32' ? `--cwd ${codeDirectory}` : null
].join(' ')
var command = program.dockerImage
? 'docker run --rm -v ' + codeDirectory + ':/var/task ' + program.dockerImage + ' npm -s install --production'
: `npm -s install --production ${installOptions}`
exec(command, {
Lambda.prototype._npmInstall = (program, codeDirectory, callback) => {
const dockerBaseOptions = [
'run', '--rm', '-v', `${codeDirectory}:/var/task`,
program.dockerImage,
'npm', '-s', 'install', '--production'
]
const npmInstallBaseOptions = [
'-s',
'install',
'--production',
'--prefix', codeDirectory
]

const params = (() => {
// reference: https://nodejs.org/api/child_process.html#child_process_spawning_bat_and_cmd_files_on_windows

// with docker
if (program.dockerImage) {
if (process.platform === 'win32') {
return {
command: 'cmd.exe',
options: ['/c', 'docker'].concat(dockerBaseOptions)
}
}
return {
command: 'docker',
options: dockerBaseOptions
}
}

// simple npm install
if (process.platform === 'win32') {
return {
command: 'cmd.exe',
options: ['/c', 'npm']
.concat(npmInstallBaseOptions)
.concat(['--cwd', codeDirectory])
}
}
return {
command: 'npm',
options: npmInstallBaseOptions
}
})()

execFile(params.command, params.options, {
maxBuffer: maxBufferSize,
env: process.env
}, function (err) {
}, (err) => {
if (err) {
return callback(err)
}
Expand Down
50 changes: 44 additions & 6 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,14 @@ describe('lib/main', function () {
describe('_rsync', function () { rsyncTests('_rsync') })
}

describe('_npmInstall', function () {
beforeEach(function (done) {
lambda._cleanDirectory(codeDirectory, function (err) {
describe('_npmInstall', () => {
beforeEach((done) => {
lambda._cleanDirectory(codeDirectory, (err) => {
if (err) {
return done(err)
}

lambda._fileCopy(program, '.', codeDirectory, true, function (err) {
lambda._fileCopy(program, '.', codeDirectory, true, (err) => {
if (err) {
return done(err)
}
Expand All @@ -340,9 +340,47 @@ describe('lib/main', function () {
it('_npm adds node_modules', function (done) {
_timeout({ this: this, sec: 30 }) // give it time to build the node modules

lambda._npmInstall(program, codeDirectory, function (err, result) {
lambda._npmInstall(program, codeDirectory, (err, result) => {
assert.isNull(err)
var contents = fs.readdirSync(codeDirectory)
const contents = fs.readdirSync(codeDirectory)
assert.include(contents, 'node_modules')
done()
})
})
})

describe('_npmInstall (When codeDirectory contains characters to be escaped)', () => {
beforeEach((done) => {
// Since '\' can not be included in the file or directory name in Windows
const directoryName = process.platform === 'win32'
? 'hoge fuga\' piyo'
: 'hoge "fuga\' \\piyo'
codeDirectory = path.join(os.tmpdir(), directoryName)
lambda._cleanDirectory(codeDirectory, (err) => {
if (err) {
return done(err)
}

lambda._fileCopy(program, '.', codeDirectory, true, (err) => {
if (err) {
return done(err)
}
done()
})
})
})

afterEach(() => {
fs.removeSync(codeDirectory)
codeDirectory = lambda._codeDirectory()
})

it('_npm adds node_modules', function (done) {
_timeout({ this: this, sec: 30 }) // give it time to build the node modules

lambda._npmInstall(program, codeDirectory, (err, result) => {
assert.isNull(err)
const contents = fs.readdirSync(codeDirectory)
assert.include(contents, 'node_modules')
done()
})
Expand Down