Skip to content

Commit f27d1c2

Browse files
kikarHyperBrain
authored andcommitted
Fixed package individually:true (#163)
* Fixed package individually:true * Fixed issue with SLS <1.18. Updated unit tests. * Removed debug logging
1 parent 22a460d commit f27d1c2

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

lib/cleanup.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const BbPromise = require('bluebird');
44
const path = require('path');
55
const fse = require('fs-extra');
6+
const _ = require('lodash');
67

78
module.exports = {
89
cleanup() {
@@ -20,11 +21,18 @@ module.exports = {
2021
} else {
2122
if (this.serverless.service.package.individually) {
2223
const functionNames = this.serverless.service.getAllFunctions();
23-
functionNames.forEach(name => {
24-
this.serverless.service.functions[name].artifact = path.join(
24+
25+
_.forEach(functionNames, name => {
26+
const func = this.serverless.service.getFunction(name);
27+
// The location of the artifact property changed from Serverless
28+
// 1.17 to 1.18 (damn, the guys should use SemVer!). We have to
29+
// use the new one if available here.
30+
const artifactParent = func.artifact ? func : func.package;
31+
32+
artifactParent.artifact = path.join(
2533
this.serverless.config.servicePath,
2634
'.serverless',
27-
path.basename(this.serverless.service.functions[name].artifact)
35+
path.basename(artifactParent.artifact)
2836
);
2937
});
3038
resolve();

tests/cleanup.test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,65 @@ describe('cleanup', () => {
122122
fseMock.copy.yields(null, {});
123123
serverless.service.package.individually = true;
124124

125+
const testFunctionsConfig = {
126+
func1: {
127+
handler: 'module1.func1handler',
128+
package: {
129+
artifact: 'artifact-func1.zip',
130+
},
131+
events: [{
132+
http: {
133+
method: 'get',
134+
path: 'func1path',
135+
},
136+
}],
137+
},
138+
func2: {
139+
handler: 'module2.func2handler',
140+
package: {
141+
artifact: 'artifact-func2.zip',
142+
},
143+
events: [{
144+
http: {
145+
method: 'POST',
146+
path: 'func2path',
147+
},
148+
}, {
149+
nonhttp: 'non-http',
150+
}],
151+
},
152+
func3: {
153+
handler: 'module2.func3handler',
154+
package: {
155+
artifact: 'artifact-func3.zip',
156+
},
157+
events: [{
158+
nonhttp: 'non-http',
159+
}],
160+
},
161+
};
162+
serverless.service.functions = testFunctionsConfig;
163+
164+
return expect(module.cleanup()).to.be.fulfilled
165+
.then(() => {
166+
expect(serverless.config.servicePath).to.equal('my/Original/Service/Path');
167+
expect(fseMock.copy).to.have.been.calledOnce;
168+
expect(fseMock.copy).to.have.been
169+
.calledWith('my/Output/Path/.serverless', 'my/Original/Service/Path/.serverless');
170+
_.forEach(['func1', 'func2', 'func3'], funcName => {
171+
expect(serverless.service.functions[funcName].package).to.have.a.property('artifact')
172+
.that.equals(`my/Original/Service/Path/.serverless/artifact-${funcName}.zip`);
173+
});
174+
});
175+
});
176+
177+
it('should call copy with the right parameters with individual packaging for old Serverless versions (<=1.17)', () => {
178+
dirExistsSyncStub.returns(true);
179+
module.originalServicePath = 'my/Original/Service/Path';
180+
fseMock.copy.reset();
181+
fseMock.copy.yields(null, {});
182+
serverless.service.package.individually = true;
183+
125184
const testFunctionsConfig = {
126185
func1: {
127186
handler: 'module1.func1handler',

0 commit comments

Comments
 (0)