From ab88a9c262dd786cdfcc0f54a056f3387ec98330 Mon Sep 17 00:00:00 2001 From: chongkong Date: Thu, 5 Jul 2018 18:18:18 +0900 Subject: [PATCH 1/3] Use only the given function in sls input option if any --- index.js | 8 ++++++++ lib/clean.js | 3 +-- lib/inject.js | 3 +-- lib/pip.js | 3 +-- lib/zip.js | 7 +++---- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 6e55fdb3..a2b850c1 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ const BbPromise = require('bluebird'); const fse = require('fs-extra'); +const values = require('lodash.values'); const { addVendorHelper, removeVendorHelper, @@ -83,6 +84,13 @@ class ServerlessPythonRequirements { return options; } + get targetFuncs() { + let inputOpt = this.serverless.processedInput.options + return inputOpt.function + ? [inputOpt.functionObj] + : values(this.serverless.service.functions) + } + /** * The plugin constructor * @param {Object} serverless diff --git a/lib/clean.js b/lib/clean.js index f3c4fbef..332ceb37 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -1,7 +1,6 @@ const BbPromise = require('bluebird'); const fse = require('fs-extra'); const path = require('path'); -const values = require('lodash.values'); BbPromise.promisifyAll(fse); @@ -13,7 +12,7 @@ function cleanup() { const artifacts = ['.requirements']; if (this.options.zip) { if (this.serverless.service.package.individually) { - values(this.serverless.service.functions).forEach(f => { + this.targetFuncs.forEach(f => { artifacts.push(path.join(f.module, '.requirements.zip')); artifacts.push(path.join(f.module, 'unzip_requirements.py')); }); diff --git a/lib/inject.js b/lib/inject.js index 337813a3..0e9c44de 100644 --- a/lib/inject.js +++ b/lib/inject.js @@ -4,7 +4,6 @@ const glob = require('glob-all'); const get = require('lodash.get'); const set = require('lodash.set'); const path = require('path'); -const values = require('lodash.values'); const JSZip = require('jszip'); const { writeZip, zipFile } = require('./zipTree'); @@ -80,7 +79,7 @@ function injectAllRequirements(funcArtifact) { } if (this.serverless.service.package.individually) { - return BbPromise.resolve(values(this.serverless.service.functions)) + return BbPromise.resolve(this.targetFuncs) .filter(func => (func.runtime || this.serverless.service.provider.runtime).match( /^python.*/ diff --git a/lib/pip.js b/lib/pip.js index 2b061371..7d555b31 100644 --- a/lib/pip.js +++ b/lib/pip.js @@ -4,7 +4,6 @@ const path = require('path'); const get = require('lodash.get'); const set = require('lodash.set'); const { spawnSync } = require('child_process'); -const values = require('lodash.values'); const { buildImage, getBindPath, getDockerUid } = require('./docker'); const { getSlimPackageCommands } = require('./slim'); @@ -210,7 +209,7 @@ function installAllRequirements() { fse.ensureDirSync(path.join(this.servicePath, '.serverless')); if (this.serverless.service.package.individually) { let doneModules = []; - values(this.serverless.service.functions) + this.targetFuncs .filter(func => (func.runtime || this.serverless.service.provider.runtime).match( /^python.*/ diff --git a/lib/zip.js b/lib/zip.js index eba21976..1139d0d9 100644 --- a/lib/zip.js +++ b/lib/zip.js @@ -2,7 +2,6 @@ const fse = require('fs-extra'); const path = require('path'); const get = require('lodash.get'); const set = require('lodash.set'); -const values = require('lodash.values'); const uniqBy = require('lodash.uniqby'); const BbPromise = require('bluebird'); const JSZip = require('jszip'); @@ -17,7 +16,7 @@ BbPromise.promisifyAll(fse); function addVendorHelper() { if (this.options.zip) { if (this.serverless.service.package.individually) { - return BbPromise.resolve(values(this.serverless.service.functions)) + return BbPromise.resolve(this.targetFuncs) .map(f => { if (!get(f, 'package.include')) { set(f, ['package', 'include'], []); @@ -63,7 +62,7 @@ function addVendorHelper() { function removeVendorHelper() { if (this.options.zip && this.options.cleanupZipHelper) { if (this.serverless.service.package.individually) { - return BbPromise.resolve(values(this.serverless.service.functions)) + return BbPromise.resolve(this.targetFuncs) .map(f => { if (!get(f, 'module')) { set(f, ['module'], '.'); @@ -95,7 +94,7 @@ function removeVendorHelper() { function packRequirements() { if (this.options.zip) { if (this.serverless.service.package.individually) { - return BbPromise.resolve(values(this.serverless.service.functions)) + return BbPromise.resolve(this.targetFuncs) .map(f => { if (!get(f, 'module')) { set(f, ['module'], '.'); From 83b994cca1dacf7511d4a17689ed343631578d2b Mon Sep 17 00:00:00 2001 From: chongkong Date: Thu, 5 Jul 2018 18:56:45 +0900 Subject: [PATCH 2/3] Let moveModuleUp be invoked when zip option is set --- lib/inject.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/inject.js b/lib/inject.js index 0e9c44de..46a4dbe4 100644 --- a/lib/inject.js +++ b/lib/inject.js @@ -74,10 +74,6 @@ function moveModuleUp(source, target, module) { function injectAllRequirements(funcArtifact) { this.serverless.cli.log('Injecting required Python packages to package...'); - if (this.options.zip) { - return; - } - if (this.serverless.service.package.individually) { return BbPromise.resolve(this.targetFuncs) .filter(func => @@ -106,14 +102,16 @@ function injectAllRequirements(funcArtifact) { return func; } }) - .map(func => - injectRequirements( - path.join('.serverless', func.module, 'requirements'), - func.package.artifact, - this.options - ) - ); - } else { + .map(func => { + return this.options.zip + ? func + : injectRequirements( + path.join('.serverless', func.module, 'requirements'), + func.package.artifact, + this.options + ) + }); + } else if (!this.options.zip) { return injectRequirements( path.join('.serverless', 'requirements'), this.serverless.service.package.artifact || funcArtifact, From a98f63f2ed6650615264f443386210040450473f Mon Sep 17 00:00:00 2001 From: chongkong Date: Mon, 9 Jul 2018 17:50:59 +0900 Subject: [PATCH 3/3] Fix formatting --- index.js | 4 ++-- lib/inject.js | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index a2b850c1..1857b239 100644 --- a/index.js +++ b/index.js @@ -85,10 +85,10 @@ class ServerlessPythonRequirements { } get targetFuncs() { - let inputOpt = this.serverless.processedInput.options + let inputOpt = this.serverless.processedInput.options; return inputOpt.function ? [inputOpt.functionObj] - : values(this.serverless.service.functions) + : values(this.serverless.service.functions); } /** diff --git a/lib/inject.js b/lib/inject.js index 46a4dbe4..973ba99b 100644 --- a/lib/inject.js +++ b/lib/inject.js @@ -106,10 +106,10 @@ function injectAllRequirements(funcArtifact) { return this.options.zip ? func : injectRequirements( - path.join('.serverless', func.module, 'requirements'), - func.package.artifact, - this.options - ) + path.join('.serverless', func.module, 'requirements'), + func.package.artifact, + this.options + ); }); } else if (!this.options.zip) { return injectRequirements(