From 1bcbc6ea963b488d3fa28a34c5aa43e945d9b0b7 Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 22 Jun 2017 19:20:56 +0900 Subject: [PATCH 1/2] Refactoring _postInstallScript --- lib/main.js | 44 +++++++++++++++++++++----------------------- test/main.js | 44 +++++++++++++++++++++++--------------------- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/lib/main.js b/lib/main.js index 9e3124d1..2c5e5534 100644 --- a/lib/main.js +++ b/lib/main.js @@ -349,26 +349,26 @@ Lambda.prototype._npmInstall = (program, codeDirectory) => { }) } -Lambda.prototype._postInstallScript = function (program, codeDirectory, callback) { - var scriptFilename = 'post_install.sh' - var cmd = path.join(codeDirectory, scriptFilename) + ' ' + program.environment +Lambda.prototype._postInstallScript = (program, codeDirectory) => { + const scriptFilename = 'post_install.sh' + const filePath = path.join(codeDirectory, scriptFilename) + if (!fs.existsSync(filePath)) return Promise.resolve() - var filePath = path.join(codeDirectory, scriptFilename) - - if (!fs.existsSync(filePath)) { - return callback(null) - } + const cmd = path.join(codeDirectory, scriptFilename) + ' ' + program.environment console.log('=> Running post install script ' + scriptFilename) - exec(cmd, { - env: process.env, - cwd: codeDirectory, - maxBuffer: maxBufferSize - }, function (error, stdout, stderr) { - if (error) { - return callback(new Error(`${error} stdout: ${stdout} stderr: ${stderr}`)) - } - console.log('\t\t' + stdout) - callback(null) + + return new Promise((resolve, reject) => { + exec(cmd, { + env: process.env, + cwd: codeDirectory, + maxBuffer: maxBufferSize + }, (error, stdout, stderr) => { + if (error) { + return reject(new Error(`${error} stdout: ${stdout} stderr: ${stderr}`)) + } + console.log('\t\t' + stdout) + resolve() + }) }) } @@ -532,13 +532,11 @@ Lambda.prototype._buildAndArchive = function (program, archiveCallback) { } console.log('=> Running npm install --production') _this._npmInstall(program, codeDirectory).then(() => { - _this._postInstallScript(program, codeDirectory, (err) => { - if (err) { - return archiveCallback(err) - } - + _this._postInstallScript(program, codeDirectory).then(() => { console.log('=> Zipping deployment package') _this._zip(program, codeDirectory, archiveCallback) + }).catch((err) => { + return archiveCallback(err) }) }).catch((err) => { return archiveCallback(err) diff --git a/test/main.js b/test/main.js index cc35875b..31df8f0c 100644 --- a/test/main.js +++ b/test/main.js @@ -436,69 +436,71 @@ describe('lib/main', function () { }) }) - describe('_postInstallScript', function () { + describe('_postInstallScript', () => { if (process.platform === 'win32') { return it('`_postInstallScript` test does not support Windows.') } const postInstallScriptPath = path.join(codeDirectory, 'post_install.sh') - var hook + let hook /** * Capture console output */ function captureStream (stream) { - var oldWrite = stream.write - var buf = '' + let oldWrite = stream.write + let buf = '' stream.write = function (chunk, encoding, callback) { buf += chunk.toString() // chunk is a String or Buffer oldWrite.apply(stream, arguments) } return { - unhook: function unhook () { + unhook: () => { stream.write = oldWrite }, - captured: function () { - return buf - } + captured: () => buf } } - beforeEach(function () { + beforeEach(() => { hook = captureStream(process.stdout) }) - afterEach(function () { + afterEach(() => { hook.unhook() if (fs.existsSync(postInstallScriptPath)) { fs.unlinkSync(postInstallScriptPath) } }) - it('should not throw any errors if no script', function (done) { - lambda._postInstallScript(program, codeDirectory, function (err) { - assert.isNull(err) - done() + it('should not throw any errors if no script', () => { + return lambda._postInstallScript(program, codeDirectory).then((dummy) => { + assert.isUndefined(dummy) }) }) - it('should throw any errors if script fails', function (done) { + it('should throw any errors if script fails', () => { fs.writeFileSync(postInstallScriptPath, '___fails___') - lambda._postInstallScript(program, codeDirectory, function (err) { + return lambda._postInstallScript(program, codeDirectory).then((dummy) => { + assert.isUndefined(dummy) + }).catch((err) => { assert.instanceOf(err, Error) assert.match(err.message, /^Error: Command failed:/) - done() }) }) - it('running script gives expected output', function (done) { - fs.writeFileSync(postInstallScriptPath, fs.readFileSync(path.join('test', 'post_install.sh'))) + it('running script gives expected output', () => { + fs.writeFileSync( + postInstallScriptPath, + fs.readFileSync(path.join('test', 'post_install.sh')) + ) fs.chmodSync(path.join(codeDirectory, 'post_install.sh'), '755') - lambda._postInstallScript(program, codeDirectory, function (err) { + return lambda._postInstallScript(program, codeDirectory).then((dummy) => { + assert.isUndefined(dummy) + }).catch((err) => { assert.isNull(err) assert.equal( `=> Running post install script post_install.sh\n\t\tYour environment is ${program.environment}\n`, hook.captured() ) - done() }) }) }) From 36c5a144a2fe76976b3bcfa501cc090e41fcceed Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 22 Jun 2017 19:45:45 +0900 Subject: [PATCH 2/2] Refactoring _zip and _buildAndArchive --- lib/main.js | 48 ++++++++++++++++++++++++++---------------------- test/main.js | 44 ++++++++++++++++++-------------------------- 2 files changed, 44 insertions(+), 48 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2c5e5534..a3770efb 100644 --- a/lib/main.js +++ b/lib/main.js @@ -372,25 +372,26 @@ Lambda.prototype._postInstallScript = (program, codeDirectory) => { }) } -Lambda.prototype._zip = function (program, codeDirectory, callback) { - var options = { +Lambda.prototype._zip = (program, codeDirectory) => { + const options = { type: 'nodebuffer', compression: 'DEFLATE' } console.log('=> Zipping repo. This might take up to 30 seconds') - fs.walk(codeDirectory) - .on('data', function (file) { - if (!file.stats.isDirectory()) { - var content = fs.readFileSync(file.path) - var filePath = file.path.replace(path.join(codeDirectory, path.sep), '') - zip.file(filePath, content) - } - }) - .on('end', function () { - var data = zip.generate(options) - return callback(null, data) - }) + return new Promise((resolve) => { + fs.walk(codeDirectory) + .on('data', (file) => { + if (!file.stats.isDirectory()) { + const content = fs.readFileSync(file.path) + const filePath = file.path.replace(path.join(codeDirectory, path.sep), '') + zip.file(filePath, content) + } + }) + .on('end', () => { + resolve(zip.generate(options)) + }) + }) } Lambda.prototype._codeDirectory = function () { @@ -500,7 +501,7 @@ Lambda.prototype._archivePrebuilt = function (program, archiveCallback) { } console.log('=> Zipping deployment package') - _this._zip(program, codeDirectory, archiveCallback) + _this._zip(program, codeDirectory).then((data) => archiveCallback(null, data)) }) } @@ -530,14 +531,17 @@ Lambda.prototype._buildAndArchive = function (program, archiveCallback) { if (err) { return archiveCallback(err) } + console.log('=> Running npm install --production') - _this._npmInstall(program, codeDirectory).then(() => { - _this._postInstallScript(program, codeDirectory).then(() => { - console.log('=> Zipping deployment package') - _this._zip(program, codeDirectory, archiveCallback) - }).catch((err) => { - return archiveCallback(err) - }) + 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) }) diff --git a/test/main.js b/test/main.js index 31df8f0c..e0e29881 100644 --- a/test/main.js +++ b/test/main.js @@ -510,9 +510,7 @@ describe('lib/main', 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) - } + if (err) return done(err) lambda._npmInstall(program, codeDirectory).then(() => { done() }).catch((err) => { @@ -524,17 +522,15 @@ describe('lib/main', function () { }) }) - it('zips the file and has an index.js file', function (done) { + it('zips the file and has an index.js file', function () { _timeout({ this: this, sec: 30 }) // give it time to zip - lambda._zip(program, codeDirectory, function (err, data) { - assert.isNull(err) - var archive = new Zip(data) - var contents = Object.keys(archive.files).map(function (k) { + return lambda._zip(program, codeDirectory).then((data) => { + const archive = new Zip(data) + const contents = Object.keys(archive.files).map((k) => { return archive.files[k].name.toString() }) assert.include(contents, 'index.js') - done() }) }) }) @@ -588,26 +584,22 @@ describe('lib/main', function () { }) }) - describe('_readArchive', function () { + describe('_readArchive', () => { const testZipFile = path.join(os.tmpdir(), 'node-lambda-test.zip') - var bufferExpected = null - before(function (done) { + let bufferExpected = null + before(function () { _timeout({ this: this, sec: 30 }) // give it time to zip - lambda._zip(program, codeDirectory, function (err, data) { - assert.isNull(err) + return lambda._zip(program, codeDirectory).then((data) => { bufferExpected = data fs.writeFileSync(testZipFile, data) - done() }) }) - after(function () { - fs.unlinkSync(testZipFile) - }) + after(() => fs.unlinkSync(testZipFile)) - it('_readArchive fails (undefined)', function (done) { - lambda._readArchive(program, function (err, data) { + it('_readArchive fails (undefined)', (done) => { + lambda._readArchive(program, (err, data) => { assert.isUndefined(data) assert.instanceOf(err, Error) assert.equal(err.message, 'No such Zipfile [undefined]') @@ -615,10 +607,10 @@ describe('lib/main', function () { }) }) - it('_readArchive fails (does not exists file)', function (done) { + it('_readArchive fails (does not exists file)', (done) => { const filePath = path.join(path.resolve('/aaaa'), 'bbbb') const _program = Object.assign({ deployZipfile: filePath }, program) - lambda._readArchive(_program, function (err, data) { + lambda._readArchive(_program, (err, data) => { assert.isUndefined(data) assert.instanceOf(err, Error) assert.equal(err.message, `No such Zipfile [${filePath}]`) @@ -626,9 +618,9 @@ describe('lib/main', function () { }) }) - it('_readArchive reads the contents of the zipfile', function (done) { + it('_readArchive reads the contents of the zipfile', (done) => { const _program = Object.assign({ deployZipfile: testZipFile }, program) - lambda._readArchive(_program, function (err, data) { + lambda._readArchive(_program, (err, data) => { assert.isNull(err) assert.deepEqual(data, bufferExpected) done() @@ -653,9 +645,9 @@ describe('lib/main', function () { }) }) - it('`deployZipfile` is a valid value._archive reads the contents of the zipfile', function (done) { + it('`deployZipfile` is a valid value._archive reads the contents of the zipfile', (done) => { const _program = Object.assign({ deployZipfile: testZipFile }, program) - lambda._archive(_program, function (err, data) { + lambda._archive(_program, (err, data) => { assert.isNull(err) assert.deepEqual(data, bufferExpected) done()