-
Notifications
You must be signed in to change notification settings - Fork 293
/
Copy pathzip.js
166 lines (156 loc) Β· 5.28 KB
/
zip.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const fse = require('fs-extra');
const path = require('path');
const get = require('lodash.get');
const set = require('set-value');
const uniqBy = require('lodash.uniqby');
const BbPromise = require('bluebird');
const JSZip = require('jszip');
const { addTree, writeZip } = require('./zipTree');
BbPromise.promisifyAll(fse);
/**
* Add the vendor helper to the current service tree.
* @return {Promise}
*/
function addVendorHelper() {
if (this.options.zip) {
if (this.serverless.service.package.individually) {
return BbPromise.resolve(this.targetFuncs)
.map((f) => {
if (!get(f, 'package.patterns')) {
set(f, ['package', 'patterns'], []);
}
if (!get(f, 'module')) {
set(f, ['module'], '.');
}
f.package.patterns.push('unzip_requirements.py');
return f;
})
.then((functions) => uniqBy(functions, (func) => func.module))
.map((f) => {
if (this.log) {
this.log.info(`Adding Python requirements helper to ${f.module}`);
} else {
this.serverless.cli.log(
`Adding Python requirements helper to ${f.module}...`
);
}
return fse.copyAsync(
path.resolve(__dirname, '../unzip_requirements.py'),
path.join(this.servicePath, f.module, 'unzip_requirements.py')
);
});
} else {
if (this.log) {
this.log.info('Adding Python requirements helper');
} else {
this.serverless.cli.log('Adding Python requirements helper...');
}
if (!get(this.serverless.service, 'package.patterns')) {
set(this.serverless.service, ['package', 'patterns'], []);
}
this.serverless.service.package.patterns.push('unzip_requirements.py');
return fse.copyAsync(
path.resolve(__dirname, '../unzip_requirements.py'),
path.join(this.servicePath, 'unzip_requirements.py')
);
}
}
}
/**
* Remove the vendor helper from the current service tree.
* @return {Promise} the promise to remove the vendor helper.
*/
function removeVendorHelper() {
if (this.options.zip && this.options.cleanupZipHelper) {
if (this.serverless.service.package.individually) {
return BbPromise.resolve(this.targetFuncs)
.map((f) => {
if (!get(f, 'module')) {
set(f, ['module'], '.');
}
return f;
})
.then((funcs) => uniqBy(funcs, (f) => f.module))
.map((f) => {
if (this.log) {
this.log.info(
`Removing Python requirements helper from ${f.module}`
);
} else {
this.serverless.cli.log(
`Removing Python requirements helper from ${f.module}...`
);
}
return fse.removeAsync(
path.join(this.servicePath, f.module, 'unzip_requirements.py')
);
});
} else {
if (this.log) {
this.log.info('Removing Python requirements helper');
} else {
this.serverless.cli.log('Removing Python requirements helper...');
}
return fse.removeAsync(
path.join(this.servicePath, 'unzip_requirements.py')
);
}
}
}
/**
* Zip up .serverless/requirements or .serverless/[MODULE]/requirements.
* @return {Promise} the promise to pack requirements.
*/
function packRequirements() {
if (this.options.zip) {
if (this.serverless.service.package.individually) {
return BbPromise.resolve(this.targetFuncs)
.filter((func) => {
return (
func.runtime || this.serverless.service.provider.runtime
).match(/^python.*/);
})
.map((f) => {
if (!get(f, 'module')) {
set(f, ['module'], '.');
}
return f;
})
.then((funcs) => uniqBy(funcs, (f) => f.module))
.map((f) => {
let packProgress;
if (this.progress && this.log) {
packProgress = this.progress.get(
`python-pack-requirements-${f.module}`
);
packProgress.update(
`Zipping required Python packages for ${f.module}`
);
this.log.info(`Zipping required Python packages for ${f.module}`);
} else {
this.serverless.cli.log(
`Zipping required Python packages for ${f.module}...`
);
}
f.package.patterns.push(`${f.module}/.requirements.zip`);
return addTree(new JSZip(), `.serverless/${f.module}/requirements`)
.then((zip) => writeZip(zip, `${f.module}/.requirements.zip`))
.finally(() => packProgress && packProgress.remove());
});
} else {
let packProgress;
if (this.progress) {
packProgress = this.progress.get(`python-pack-requirements`);
} else {
this.serverless.cli.log('Zipping required Python packages...');
}
this.serverless.service.package.patterns.push('.requirements.zip');
return addTree(new JSZip(), '.serverless/requirements')
.then((zip) =>
writeZip(zip, path.join(this.servicePath, '.requirements.zip'))
)
.finally(() => packProgress && packProgress.remove());
}
}
}
module.exports = { addVendorHelper, removeVendorHelper, packRequirements };