Skip to content

Commit

Permalink
Bugfixed that mode of file changes when zip is created (#335)
Browse files Browse the repository at this point in the history
* Add archiver.js

* Fix to use `archiver` instead of `node-zip`

We want to keep file mode.

* Add test of file mode

* Fix to test mode in non-Windows

* Modify the file used for testing

* Modify the file used for testing

* Modify archive.files's key

archive.files's key is a slash delimiter regardless of platform.

* Modify archive.files's name

archive.files's name is a slash delimiter regardless of platform.

* Modify path delimiter

archive.files's name is a slash delimiter regardless of platform.

* Modify to Arrow function and modify of variable declaration

* Fix to get mode from file
  • Loading branch information
abetomo authored and DeviaVir committed Jun 26, 2017
1 parent e640633 commit 1c12a15
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 38 deletions.
41 changes: 30 additions & 11 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict'

const path = require('path')
const os = require('os')
const aws = require('aws-sdk')
const exec = require('child_process').exec
const execSync = require('child_process').execSync
const execFile = require('child_process').execFile
const fs = require('fs-extra')
const packageJson = require(path.join(__dirname, '..', 'package.json'))
const minimatch = require('minimatch')
const zip = new (require('node-zip'))()
const archiver = require('archiver')
const dotenv = require('dotenv')
const proxy = require('proxy-agent')
const ScheduleEvents = require(path.join(__dirname, 'schedule_events'))
Expand Down Expand Up @@ -373,23 +374,41 @@ Lambda.prototype._postInstallScript = (program, codeDirectory) => {
}

Lambda.prototype._zip = (program, codeDirectory) => {
const options = {
type: 'nodebuffer',
compression: 'DEFLATE'
}

console.log('=> Zipping repo. This might take up to 30 seconds')

const tmpZipFile = path.join(os.tmpdir(), +(new Date()) + '.zip')
const output = fs.createWriteStream(tmpZipFile)
const archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
})
return new Promise((resolve) => {
output.on('close', () => {
const contents = fs.readFileSync(tmpZipFile)
fs.unlinkSync(tmpZipFile)
resolve(contents)
})
archive.pipe(output)
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)
if (file.stats.isDirectory()) return

const filePath = file.path.replace(path.join(codeDirectory, path.sep), '')
if (file.stats.isSymbolicLink()) {
// # archiver.js
// Implementation supporting symlink has been done,
// but it seems that release has not been done yet
}

archive.append(
fs.createReadStream(file.path),
{
name: filePath,
stats: file.stats
}
)
})
.on('end', () => {
resolve(zip.generate(options))
archive.finalize()
})
})
}
Expand Down
78 changes: 76 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"standard": "^10.0.2"
},
"dependencies": {
"archiver": "^1.3.0",
"aws-sdk": "^2.76.0",
"commander": "^2.10.0",
"dotenv": "^0.4.0",
Expand Down
45 changes: 28 additions & 17 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,20 +522,33 @@ describe('lib/main', function () {
})
})

it('zips the file and has an index.js file', function () {
it('Compress the file. `index.js` and `bin/node-lambda` are included and the permission is also preserved.', function () {
_timeout({ this: this, sec: 30 }) // give it time to zip

return lambda._zip(program, codeDirectory).then((data) => {
const indexJsStat = fs.lstatSync('index.js')
const binNodeLambdaStat = fs.lstatSync(path.join('bin', 'node-lambda'))

const archive = new Zip(data)
const contents = Object.keys(archive.files).map((k) => {
return archive.files[k].name.toString()
})
assert.include(contents, 'index.js')
assert.include(archive.files['index.js'].name, 'index.js')
assert.include(archive.files['bin/node-lambda'].name, 'bin/node-lambda')

if (process.platform !== 'win32') {
assert.equal(
archive.files['index.js'].unixPermissions,
indexJsStat.mode
)
assert.equal(
archive.files['bin/node-lambda'].unixPermissions,
binNodeLambdaStat.mode
)
}
})
})
})

describe('_archive', () => {
// archive.files's name is a slash delimiter regardless of platform.
it('installs and zips with an index.js file and node_modules/aws-sdk', function (done) {
_timeout({ this: this, sec: 30 }) // give it time to zip

Expand All @@ -546,17 +559,15 @@ describe('lib/main', function () {
return archive.files[k].name.toString()
})
assert.include(contents, 'index.js')
assert.include(contents, path.join('node_modules', 'aws-sdk', 'lib', 'aws.js'))
assert.include(contents, 'node_modules/aws-sdk/lib/aws.js')
done()
})
})

it('packages a prebuilt module without installing', function (done) {
_timeout({ this: this, sec: 30 }) // give it time to zip
var buildDir = '.build_' + Date.now()
after(function () {
fs.removeSync(buildDir)
})
let buildDir = '.build_' + Date.now()
after(() => fs.removeSync(buildDir))

fs.mkdirSync(buildDir)
fs.mkdirSync(path.join(buildDir, 'd'))
Expand All @@ -566,17 +577,17 @@ describe('lib/main', function () {
fs.writeFileSync(path.join(buildDir, 'd', 'testb'), '...')

program.prebuiltDirectory = buildDir
lambda._archive(program, function (err, data) {
lambda._archive(program, (err, data) => {
assert.isNull(err)
var archive = new Zip(data)
var contents = Object.keys(archive.files).map(function (k) {
const archive = new Zip(data)
const contents = Object.keys(archive.files).map((k) => {
return archive.files[k].name.toString()
});
[
'testa',
path.join('d', 'testb'),
path.join('node_modules', 'a')
].forEach(function (needle) {
'd/testb',
'node_modules/a'
].forEach((needle) => {
assert.include(contents, needle, `Target: "${needle}"`)
})
done()
Expand Down Expand Up @@ -640,7 +651,7 @@ describe('lib/main', function () {
return archive.files[k].name.toString()
})
assert.include(contents, 'index.js')
assert.include(contents, path.join('node_modules', 'aws-sdk', 'lib', 'aws.js'))
assert.include(contents, 'node_modules/aws-sdk/lib/aws.js')
done()
})
})
Expand Down
Loading

0 comments on commit 1c12a15

Please sign in to comment.