Skip to content

Properly supporting sls deploy function command and zip option #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const BbPromise = require('bluebird');
const fse = require('fs-extra');
const values = require('lodash.values');
const {
addVendorHelper,
removeVendorHelper,
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/clean.js
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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'));
});
Expand Down
25 changes: 11 additions & 14 deletions lib/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -75,12 +74,8 @@ 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(values(this.serverless.service.functions))
return BbPromise.resolve(this.targetFuncs)
.filter(func =>
(func.runtime || this.serverless.service.provider.runtime).match(
/^python.*/
Expand All @@ -107,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,
Expand Down
3 changes: 1 addition & 2 deletions lib/pip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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.*/
Expand Down
7 changes: 3 additions & 4 deletions lib/zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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'], []);
Expand Down Expand Up @@ -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'], '.');
Expand Down Expand Up @@ -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'], '.');
Expand Down