Skip to content
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
93 changes: 40 additions & 53 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ Lambda.prototype._eventSourceList = function (program) {
return list
}

Lambda.prototype._fileCopy = (program, src, dest, excludeNodeModules, callback) => {
Lambda.prototype._fileCopy = (program, src, dest, excludeNodeModules) => {
const srcAbsolutePath = path.resolve(src)
const excludes = (() => {
return [
Expand Down Expand Up @@ -262,34 +262,31 @@ Lambda.prototype._fileCopy = (program, src, dest, excludeNodeModules, callback)
}).join(',') + '}'
const dirPatternRegExp = new RegExp(`(${dirBlobs.join('|')})$`)

fs.mkdirs(dest, (err) => {
if (err) {
return callback(err)
}
const options = {
dereference: true, // same meaning as `-L` of `rsync` command
filter: (src, dest) => {
if (!program.prebuiltDirectory && src === path.join(srcAbsolutePath, 'package.json')) {
// include package.json unless prebuiltDirectory is set
return true
}
return new Promise((resolve, reject) => {
fs.mkdirs(dest, (err) => {
if (err) return reject(err)
const options = {
dereference: true, // same meaning as `-L` of `rsync` command
filter: (src, dest) => {
if (!program.prebuiltDirectory && src === path.join(srcAbsolutePath, 'package.json')) {
// include package.json unless prebuiltDirectory is set
return true
}

if (!minimatch(src, pattern, { matchBase: true })) {
return true
}
// Directory check. Even if `src` is a directory it will not end with '/'.
if (!dirPatternRegExp.test(src)) {
return false
if (!minimatch(src, pattern, { matchBase: true })) {
return true
}
// Directory check. Even if `src` is a directory it will not end with '/'.
if (!dirPatternRegExp.test(src)) {
return false
}
return !fs.statSync(src).isDirectory()
}
return !fs.statSync(src).isDirectory()
}
}
fs.copy(src, dest, options, function (err) {
if (err) {
return callback(err)
}

return callback(null, true)
fs.copy(src, dest, options, (err) => {
if (err) return reject(err)
resolve()
})
})
})
}
Expand Down Expand Up @@ -514,13 +511,11 @@ Lambda.prototype._archivePrebuilt = function (program, archiveCallback) {
const codeDirectory = this._codeDirectory()
const _this = this

_this._fileCopy(program, program.prebuiltDirectory, codeDirectory, false, (err) => {
if (err) {
return archiveCallback(err)
}

_this._fileCopy(program, program.prebuiltDirectory, codeDirectory, false).then(() => {
console.log('=> Zipping deployment package')
_this._zip(program, codeDirectory).then((data) => archiveCallback(null, data))
}).catch((err) => {
return archiveCallback(err)
})
}

Expand All @@ -542,29 +537,21 @@ Lambda.prototype._buildAndArchive = function (program, archiveCallback) {
const codeDirectory = _this._codeDirectory()
const lambdaSrcDirectory = program.sourceDirectory ? program.sourceDirectory.replace(/\/$/, '') : '.'

_this._cleanDirectory(codeDirectory).then(() => {
return Promise.resolve().then(() => {
return _this._cleanDirectory(codeDirectory)
}).then(() => {
console.log('=> Moving files to temporary directory')

// Move files to tmp folder
_this._fileCopy(program, lambdaSrcDirectory, codeDirectory, true, (err) => {
if (err) {
return archiveCallback(err)
}

console.log('=> Running npm install --production')
Promise.resolve().then(() => {
return _this._npmInstall(program, codeDirectory)
}).then(() => {
return _this._postInstallScript(program, codeDirectory)
}).then(() => {
console.log('=> Zipping deployment package')
return _this._zip(program, codeDirectory)
}).then((data) => {
return archiveCallback(null, data)
}).catch((err) => {
return archiveCallback(err)
})
})
return _this._fileCopy(program, lambdaSrcDirectory, codeDirectory, true)
}).then(() => {
console.log('=> Running npm install --production')
return _this._npmInstall(program, codeDirectory)
}).then(() => {
return _this._postInstallScript(program, codeDirectory)
}).then(() => {
console.log('=> Zipping deployment package')
return _this._zip(program, codeDirectory)
}).then((data) => {
return archiveCallback(null, data)
}).catch((err) => {
return archiveCallback(err)
})
Expand Down
83 changes: 26 additions & 57 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,14 @@ describe('lib/main', function () {
})
})

it('`codeDirectory` is empty. (For `codeDirectory` where the file was present)', (done) => {
lambda._fileCopy(program, '.', codeDirectory, true, (err, result) => {
assert.isNull(err)
it('`codeDirectory` is empty. (For `codeDirectory` where the file was present)', () => {
return lambda._fileCopy(program, '.', codeDirectory, true).then(() => {
const contents = fs.readdirSync(codeDirectory)
assert.isTrue(contents.length > 0)
lambda._cleanDirectory(codeDirectory).then(() => {
return lambda._cleanDirectory(codeDirectory).then(() => {
assert.isTrue(fs.existsSync(codeDirectory))
const contents = fs.readdirSync(codeDirectory)
assert.equal(contents.length, 0)
done()
})
})
})
Expand All @@ -283,17 +281,15 @@ describe('lib/main', function () {

beforeEach(() => lambda._cleanDirectory(codeDirectory))

it('_fileCopy an index.js as well as other files', (done) => {
lambda._fileCopy(program, '.', codeDirectory, true, (err, result) => {
assert.isNull(err)
it('_fileCopy an index.js as well as other files', () => {
return lambda._fileCopy(program, '.', codeDirectory, true).then(() => {
const contents = fs.readdirSync(codeDirectory);
['index.js', 'package.json'].forEach((needle) => {
assert.include(contents, needle, `Target: "${needle}"`)
});
['node_modules', 'build'].forEach((needle) => {
assert.notInclude(contents, needle, `Target: "${needle}"`)
})
done()
})
})

Expand All @@ -311,20 +307,17 @@ describe('lib/main', function () {
done()
})

it('_fileCopy an index.js as well as other files', (done) => {
lambda._fileCopy(program, '.', codeDirectory, true, (err, result) => {
assert.isNull(err)
it('_fileCopy an index.js as well as other files', () => {
return lambda._fileCopy(program, '.', codeDirectory, true).then(() => {
const contents = fs.readdirSync(codeDirectory);
['index.js', 'package.json'].forEach((needle) => {
assert.include(contents, needle, `Target: "${needle}"`)
})
done()
})
})

it('_fileCopy excludes files matching excludeGlobs', (done) => {
lambda._fileCopy(program, '.', codeDirectory, true, (err, result) => {
assert.isNull(err)
it('_fileCopy excludes files matching excludeGlobs', () => {
return lambda._fileCopy(program, '.', codeDirectory, true).then(() => {
let contents = fs.readdirSync(codeDirectory);
['__unittest', 'fuga'].forEach((needle) => {
assert.include(contents, needle, `Target: "${needle}"`)
Expand All @@ -343,21 +336,18 @@ describe('lib/main', function () {

contents = fs.readdirSync(path.join(codeDirectory, '__unittest', 'hoge'))
assert.equal(contents.length, 0, 'directory:__unittest/hoge is empty')
done()
})
})

it('_fileCopy should not exclude package.json, even when excluded by excludeGlobs', (done) => {
it('_fileCopy should not exclude package.json, even when excluded by excludeGlobs', () => {
program.excludeGlobs = '*.json'
lambda._fileCopy(program, '.', codeDirectory, true, (err, result) => {
assert.isNull(err)
return lambda._fileCopy(program, '.', codeDirectory, true).then(() => {
const contents = fs.readdirSync(codeDirectory)
assert.include(contents, 'package.json')
done()
})
})

it('_fileCopy should not include package.json when --prebuiltDirectory is set', (done) => {
it('_fileCopy should not include package.json when --prebuiltDirectory is set', () => {
const buildDir = '.build_' + Date.now()
after(() => fs.removeSync(buildDir))

Expand All @@ -367,28 +357,19 @@ describe('lib/main', function () {

program.excludeGlobs = '*.json'
program.prebuiltDirectory = buildDir
lambda._fileCopy(program, buildDir, codeDirectory, true, (err, result) => {
assert.isNull(err)
return lambda._fileCopy(program, buildDir, codeDirectory, true).then(() => {
const contents = fs.readdirSync(codeDirectory)
assert.notInclude(contents, 'package.json', 'Target: "packages.json"')
assert.include(contents, 'testa', 'Target: "testa"')
done()
})
})
})
})

describe('_npmInstall', () => {
beforeEach((done) => {
lambda._cleanDirectory(codeDirectory).then(() => {
lambda._fileCopy(program, '.', codeDirectory, true, (err) => {
if (err) {
return done(err)
}
done()
})
}).catch((err) => {
done(err)
beforeEach(() => {
return lambda._cleanDirectory(codeDirectory).then(() => {
return lambda._fileCopy(program, '.', codeDirectory, true)
})
})

Expand All @@ -403,21 +384,14 @@ describe('lib/main', function () {
})

describe('_npmInstall (When codeDirectory contains characters to be escaped)', () => {
beforeEach((done) => {
beforeEach(() => {
// 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).then(() => {
lambda._fileCopy(program, '.', codeDirectory, true, (err) => {
if (err) {
return done(err)
}
done()
})
}).catch((err) => {
done(err)
return lambda._cleanDirectory(codeDirectory).then(() => {
return lambda._fileCopy(program, '.', codeDirectory, true)
})
})

Expand Down Expand Up @@ -506,19 +480,14 @@ describe('lib/main', function () {
})

describe('_zip', () => {
beforeEach(function (done) {
beforeEach(function () {
_timeout({ this: this, sec: 30 }) // give it time to build the node modules
lambda._cleanDirectory(codeDirectory).then(() => {
lambda._fileCopy(program, '.', codeDirectory, true, (err) => {
if (err) return done(err)
lambda._npmInstall(program, codeDirectory).then(() => {
done()
}).catch((err) => {
done(err)
})
})
}).catch((err) => {
done(err)
return Promise.resolve().then(() => {
return lambda._cleanDirectory(codeDirectory)
}).then(() => {
return lambda._fileCopy(program, '.', codeDirectory, true)
}).then(() => {
return lambda._npmInstall(program, codeDirectory)
})
})

Expand Down