diff --git a/index.js b/index.js index dda2fe8a..33e6a3da 100644 --- a/index.js +++ b/index.js @@ -108,17 +108,36 @@ class ServerlessPythonRequirements { } }; - const before = () => - BbPromise.bind(this) + const before = () => { + if ( + arguments[1].functionObj && + arguments[1].functionObj.runtime && + !arguments[1].functionObj.runtime.startsWith('python') + ) + return; + return BbPromise.bind(this) .then(pipfileToRequirements) .then(addVendorHelper) .then(installAllRequirements) .then(packRequirements); + }; - const after = () => - BbPromise.bind(this) + const after = () => { + if ( + arguments[1].functionObj && + arguments[1].functionObj.runtime && + !arguments[1].functionObj.runtime.startsWith('python') + ) + return; + return BbPromise.bind(this) .then(removeVendorHelper) - .then(injectAllRequirements); + .then(() => + injectAllRequirements.bind(this)( + arguments[1].functionObj && + arguments[1].functionObj.package.artifact + ) + ); + }; const invalidateCaches = () => { if (this.options.invalidateCaches) { diff --git a/lib/inject.js b/lib/inject.js index 1c10a615..337813a3 100644 --- a/lib/inject.js +++ b/lib/inject.js @@ -72,7 +72,7 @@ function moveModuleUp(source, target, module) { * Inject requirements into packaged application. * @return {Promise} the combined promise for requirements injection. */ -function injectAllRequirements() { +function injectAllRequirements(funcArtifact) { this.serverless.cli.log('Injecting required Python packages to package...'); if (this.options.zip) { @@ -94,8 +94,11 @@ function injectAllRequirements() { }) .map(func => { if (func.module !== '.') { - const artifact = func.package.artifact; - const newArtifact = path.join('.serverless', `${func.module}.zip`); + const artifact = func.package ? func.package.artifact : funcArtifact; + const newArtifact = path.join( + '.serverless', + `${func.module}-${func.name}.zip` + ); func.package.artifact = newArtifact; return moveModuleUp(artifact, newArtifact, func.module).then( () => func @@ -114,7 +117,7 @@ function injectAllRequirements() { } else { return injectRequirements( path.join('.serverless', 'requirements'), - this.serverless.service.package.artifact, + this.serverless.service.package.artifact || funcArtifact, this.options ); } diff --git a/lib/pip.js b/lib/pip.js index a5647d84..fc8a08a6 100644 --- a/lib/pip.js +++ b/lib/pip.js @@ -201,29 +201,35 @@ function installAllRequirements() { fse.ensureDirSync(path.join(this.servicePath, '.serverless')); if (this.serverless.service.package.individually) { let doneModules = []; - values(this.serverless.service.functions).forEach(f => { - if (!get(f, 'module')) { - set(f, ['module'], '.'); - } - if (!doneModules.includes(f.module)) { - installRequirements( - path.join(f.module, this.options.fileName), - path.join('.serverless', f.module), - this.serverless, - this.servicePath, - this.options - ); - if (f.vendor) { - // copy vendor libraries to requirements folder - copyVendors( - f.vendor, + values(this.serverless.service.functions) + .filter(func => + (func.runtime || this.serverless.service.provider.runtime).match( + /^python.*/ + ) + ) + .map(f => { + if (!get(f, 'module')) { + set(f, ['module'], '.'); + } + if (!doneModules.includes(f.module)) { + installRequirements( + path.join(f.module, this.options.fileName), path.join('.serverless', f.module), - this.serverless + this.serverless, + this.servicePath, + this.options ); + if (f.vendor) { + // copy vendor libraries to requirements folder + copyVendors( + f.vendor, + path.join('.serverless', f.module), + this.serverless + ); + } + doneModules.push(f.module); } - doneModules.push(f.module); - } - }); + }); } else { installRequirements( this.options.fileName, diff --git a/test.bats b/test.bats index 49093fcc..b4b6a237 100755 --- a/test.bats +++ b/test.bats @@ -197,8 +197,8 @@ teardown() { cd tests/individually npm i $(npm pack ../..) sls package - unzip .serverless/module1.zip -d puck - unzip .serverless/module2.zip -d puck2 + unzip .serverless/module1-sls-py-req-test-indiv-dev-hello1.zip -d puck + unzip .serverless/module2-sls-py-req-test-indiv-dev-hello2.zip -d puck2 ls puck/handler1.py ls puck2/handler2.py ls puck/pyaml diff --git a/tests/base/package.json b/tests/base/package.json index c53d13ee..1130d4b8 100644 --- a/tests/base/package.json +++ b/tests/base/package.json @@ -9,6 +9,6 @@ "author": "", "license": "ISC", "dependencies": { - "serverless-python-requirements": "file:serverless-python-requirements-4.0.0.tgz" + "serverless-python-requirements": "file:serverless-python-requirements-4.0.2.tgz" } }