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
54 changes: 23 additions & 31 deletions lib/packageModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
const BbPromise = require('bluebird');
const _ = require('lodash');
const path = require('path');
const archiver = require('archiver');
const fs = require('fs');
const bestzip = require('bestzip');
const glob = require('glob');
const semver = require('semver');
const fs = require('fs');

function setArtifactPath(funcName, func, artifactPath) {
const version = this.serverless.getVersion();
Expand All @@ -26,14 +26,7 @@ function setArtifactPath(funcName, func, artifactPath) {
}

function zip(directory, name) {
const zip = archiver.create('zip');
// Create artifact in temp path and move it to the package path (if any) later
// This allows us to persist the webpackOutputPath and re-use the compiled output
const artifactFilePath = path.join(this.webpackOutputPath, name);
this.serverless.utils.writeFileDir(artifactFilePath);

const output = fs.createWriteStream(artifactFilePath);

// Check that files exist to be zipped
let files = glob.sync('**', {
cwd: directory,
dot: true,
Expand All @@ -50,29 +43,28 @@ function zip(directory, name) {
return BbPromise.reject(error);
}

output.on('open', () => {
zip.pipe(output);

_.forEach(files, filePath => {
const fullPath = path.resolve(directory, filePath);

const stats = fs.statSync(fullPath);

if (!stats.isDirectory(fullPath)) {
zip.append(fs.readFileSync(fullPath), {
name: filePath,
mode: stats.mode,
date: new Date(0) // necessary to get the same hash when zipping the same content
});
}
});

zip.finalize();
});
// Create artifact in temp path and move it to the package path (if any) later
// This allows us to persist the webpackOutputPath and re-use the compiled output
const artifactFilePath = path.join(this.webpackOutputPath, name);
this.serverless.utils.writeFileDir(artifactFilePath);

const cwd = directory;
const source = '*';
const destination = path.relative(cwd, artifactFilePath);
const zipArgs = {
source,
cwd,
destination
};
return new BbPromise((resolve, reject) => {
output.on('close', () => resolve(artifactFilePath));
zip.on('error', err => reject(err));
bestzip(zipArgs)
.then(() => {
resolve(artifactFilePath);
return null;
})
.catch(err => {
reject(err);
});
});
}

Expand Down
8 changes: 4 additions & 4 deletions lib/packagers/yarn.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ describe('yarn', () => {
it('should transform yarn trees to npm dependencies', () => {
const testYarnResult =
'{"type":"activityStart","data":{"id":0}}\n' +
'{"type":"activityTick","data":{"id":0,"name":"archiver@^2.1.1"}}\n' +
'{"type":"activityTick","data":{"id":0,"name":"bestzip@^2.1.5"}}\n' +
'{"type":"activityTick","data":{"id":0,"name":"bluebird@^3.5.1"}}\n' +
'{"type":"activityTick","data":{"id":0,"name":"fs-extra@^4.0.3"}}\n' +
'{"type":"activityTick","data":{"id":0,"name":"mkdirp@^0.5.1"}}\n' +
'{"type":"activityTick","data":{"id":0,"name":"minimist@^0.0.8"}}\n' +
'{"type":"activityTick","data":{"id":0,"name":"@sls/webpack@^1.0.0"}}\n' +
'{"type":"tree","data":{"type":"list","trees":[' +
'{"name":"archiver@2.1.1","children":[],"hint":null,"color":"bold",' +
'{"name":"bestzip@2.1.5","children":[],"hint":null,"color":"bold",' +
'"depth":0},{"name":"[email protected]","children":[],"hint":null,"color":' +
'"bold","depth":0},{"name":"[email protected]","children":[],"hint":null,' +
'"color":"bold","depth":0},{"name":"[email protected]","children":[{"name":' +
Expand All @@ -80,8 +80,8 @@ describe('yarn', () => {
const expectedResult = {
problems: [],
dependencies: {
archiver: {
version: '2.1.1',
bestzip: {
version: '2.1.5',
dependencies: {}
},
bluebird: {
Expand Down
Loading