Skip to content

Commit 37adf9e

Browse files
chongkongdschep
authored andcommitted
Properly supporting sls deploy function command and zip option (#217)
This PR fixes two issues I encountered while using `individually: true` option. **1. Should package only the given function when invoked by `sls deploy function --function $FN_NAME`** Suppose using `individually: true`. If you'd like to deploy a single function using `sls deploy function` command you'd expect the plugin to package only the module that contains the function specified. The current implementation, however, runs packaging for every module in `serverless.yml` definition, taking as much time as deploying all applications just to deploy a single function. I simply added explicit `targetFuncs` property to filter target function if any specified in command line option. **2. `moveModulesUp` should be called regardless of `zip` option** When `individually: true`, each deployed function has its module as a root directory. This is achieved by calling `moveModulesUp`, but this method call is ignored when `zip` option is enabled. This certainly seems to be a wrong implementation (causing #203). If `injectModule` is ensured not to be called when `zip: true`, it is safe to call `moveModulesUp` whether `zip` is enabled or not. The fix seems to work well on my case, but it lacks new unit tests (I couldn't figure out how to compose one.) If you let me known how to add a test, I'll update the PR as soon as I can.
1 parent 71f36e7 commit 37adf9e

File tree

5 files changed

+24
-22
lines changed

5 files changed

+24
-22
lines changed

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const BbPromise = require('bluebird');
55
const fse = require('fs-extra');
6+
const values = require('lodash.values');
67
const {
78
addVendorHelper,
89
removeVendorHelper,
@@ -83,6 +84,13 @@ class ServerlessPythonRequirements {
8384
return options;
8485
}
8586

87+
get targetFuncs() {
88+
let inputOpt = this.serverless.processedInput.options;
89+
return inputOpt.function
90+
? [inputOpt.functionObj]
91+
: values(this.serverless.service.functions);
92+
}
93+
8694
/**
8795
* The plugin constructor
8896
* @param {Object} serverless

lib/clean.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const BbPromise = require('bluebird');
22
const fse = require('fs-extra');
33
const path = require('path');
4-
const values = require('lodash.values');
54

65
BbPromise.promisifyAll(fse);
76

@@ -13,7 +12,7 @@ function cleanup() {
1312
const artifacts = ['.requirements'];
1413
if (this.options.zip) {
1514
if (this.serverless.service.package.individually) {
16-
values(this.serverless.service.functions).forEach(f => {
15+
this.targetFuncs.forEach(f => {
1716
artifacts.push(path.join(f.module, '.requirements.zip'));
1817
artifacts.push(path.join(f.module, 'unzip_requirements.py'));
1918
});

lib/inject.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const glob = require('glob-all');
44
const get = require('lodash.get');
55
const set = require('lodash.set');
66
const path = require('path');
7-
const values = require('lodash.values');
87
const JSZip = require('jszip');
98
const { writeZip, zipFile } = require('./zipTree');
109

@@ -75,12 +74,8 @@ function moveModuleUp(source, target, module) {
7574
function injectAllRequirements(funcArtifact) {
7675
this.serverless.cli.log('Injecting required Python packages to package...');
7776

78-
if (this.options.zip) {
79-
return;
80-
}
81-
8277
if (this.serverless.service.package.individually) {
83-
return BbPromise.resolve(values(this.serverless.service.functions))
78+
return BbPromise.resolve(this.targetFuncs)
8479
.filter(func =>
8580
(func.runtime || this.serverless.service.provider.runtime).match(
8681
/^python.*/
@@ -107,14 +102,16 @@ function injectAllRequirements(funcArtifact) {
107102
return func;
108103
}
109104
})
110-
.map(func =>
111-
injectRequirements(
112-
path.join('.serverless', func.module, 'requirements'),
113-
func.package.artifact,
114-
this.options
115-
)
116-
);
117-
} else {
105+
.map(func => {
106+
return this.options.zip
107+
? func
108+
: injectRequirements(
109+
path.join('.serverless', func.module, 'requirements'),
110+
func.package.artifact,
111+
this.options
112+
);
113+
});
114+
} else if (!this.options.zip) {
118115
return injectRequirements(
119116
path.join('.serverless', 'requirements'),
120117
this.serverless.service.package.artifact || funcArtifact,

lib/pip.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const path = require('path');
44
const get = require('lodash.get');
55
const set = require('lodash.set');
66
const { spawnSync } = require('child_process');
7-
const values = require('lodash.values');
87
const { buildImage, getBindPath, getDockerUid } = require('./docker');
98
const { getSlimPackageCommands } = require('./slim');
109

@@ -210,7 +209,7 @@ function installAllRequirements() {
210209
fse.ensureDirSync(path.join(this.servicePath, '.serverless'));
211210
if (this.serverless.service.package.individually) {
212211
let doneModules = [];
213-
values(this.serverless.service.functions)
212+
this.targetFuncs
214213
.filter(func =>
215214
(func.runtime || this.serverless.service.provider.runtime).match(
216215
/^python.*/

lib/zip.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const fse = require('fs-extra');
22
const path = require('path');
33
const get = require('lodash.get');
44
const set = require('lodash.set');
5-
const values = require('lodash.values');
65
const uniqBy = require('lodash.uniqby');
76
const BbPromise = require('bluebird');
87
const JSZip = require('jszip');
@@ -17,7 +16,7 @@ BbPromise.promisifyAll(fse);
1716
function addVendorHelper() {
1817
if (this.options.zip) {
1918
if (this.serverless.service.package.individually) {
20-
return BbPromise.resolve(values(this.serverless.service.functions))
19+
return BbPromise.resolve(this.targetFuncs)
2120
.map(f => {
2221
if (!get(f, 'package.include')) {
2322
set(f, ['package', 'include'], []);
@@ -63,7 +62,7 @@ function addVendorHelper() {
6362
function removeVendorHelper() {
6463
if (this.options.zip && this.options.cleanupZipHelper) {
6564
if (this.serverless.service.package.individually) {
66-
return BbPromise.resolve(values(this.serverless.service.functions))
65+
return BbPromise.resolve(this.targetFuncs)
6766
.map(f => {
6867
if (!get(f, 'module')) {
6968
set(f, ['module'], '.');
@@ -95,7 +94,7 @@ function removeVendorHelper() {
9594
function packRequirements() {
9695
if (this.options.zip) {
9796
if (this.serverless.service.package.individually) {
98-
return BbPromise.resolve(values(this.serverless.service.functions))
97+
return BbPromise.resolve(this.targetFuncs)
9998
.map(f => {
10099
if (!get(f, 'module')) {
101100
set(f, ['module'], '.');

0 commit comments

Comments
 (0)