Skip to content

Commit 1aab175

Browse files
committed
Zip the same way as serverless itself does
1 parent 2562dad commit 1aab175

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

index.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const _ = require('lodash');
66
const path = require('path');
77
const fse = require('fs-extra');
88
const child_process = require('child_process');
9-
const Zip = require('adm-zip');
9+
const {zipDirectory} = require('./zipService');
1010

1111
BbPromise.promisifyAll(fse);
1212

@@ -60,15 +60,8 @@ class ServerlessPythonRequirements {
6060

6161
packRequirements() {
6262
return this.installRequirements().then(() => {
63-
return new BbPromise((resolve, reject) => {
64-
if (this.custom.zipImport) {
65-
this.serverless.cli.log('Zipping required Python packages...');
66-
const zip = new Zip();
67-
zip.addLocalFolder('.requirements', '');
68-
zip.writeZip('.requirements.zip');
69-
fse.remove('.requirements', (err) => err?reject():resolve());
70-
} else resolve();
71-
});
63+
if (this.custom.zipImport)
64+
return zipDirectory('.requirements', '.requirements.zip');
7265
});
7366
}
7467

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-python-requirements",
3-
"version": "2.0.0-beta.2",
3+
"version": "2.0.0-beta.3",
44
"engines": {
55
"node": ">=4.0"
66
},
@@ -29,6 +29,7 @@
2929
],
3030
"files": [
3131
"index.js",
32+
"zipService.js",
3233
"LICENSE",
3334
"package.json",
3435
"requirements.py",
@@ -39,9 +40,10 @@
3940
"scripts": {},
4041
"devDependencies": {},
4142
"dependencies": {
43+
"archiver": "^1.3.0",
4244
"bluebird": "^3.0.6",
43-
"adm-zip": "0.4.7",
4445
"fs-extra": "^0.26.7",
46+
"glob-all": "^3.1.0",
4547
"lodash": "^4.13.1"
4648
}
4749
}

zipService.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
// ripped from serverless and adapted for simpler use
3+
// https://github.com/serverless/serverless/blob/b0df37673bd3a1fac11ccbfa3eb48e3626c0acb1/lib/plugins/package/lib/zipService.js
4+
5+
const archiver = require('archiver');
6+
const BbPromise = require('bluebird');
7+
const path = require('path');
8+
const fs = require('fs');
9+
const glob = require('glob-all');
10+
11+
module.exports = {
12+
zipDirectory(dirPath, artifactFilePath) {
13+
const patterns = ['**'];
14+
15+
const zip = archiver.create('zip');
16+
17+
const output = fs.createWriteStream(artifactFilePath);
18+
19+
output.on('open', () => {
20+
zip.pipe(output);
21+
22+
const files = glob.sync(patterns, {
23+
cwd: dirPath,
24+
dot: true,
25+
silent: true,
26+
follow: true,
27+
});
28+
29+
files.forEach((filePath) => {
30+
const fullPath = path.resolve(
31+
dirPath,
32+
filePath
33+
);
34+
35+
const stats = fs.statSync(fullPath);
36+
37+
if (!stats.isDirectory(fullPath)) {
38+
zip.append(fs.createReadStream(fullPath), {
39+
name: filePath,
40+
mode: stats.mode,
41+
});
42+
}
43+
});
44+
45+
zip.finalize();
46+
});
47+
48+
return new BbPromise((resolve, reject) => {
49+
output.on('close', () => resolve(artifactFilePath));
50+
zip.on('error', (err) => reject(err));
51+
});
52+
},
53+
};

0 commit comments

Comments
 (0)