diff --git a/index.js b/index.js index 12fa48e..42feaa1 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ const BbPromise = require('bluebird'); const _ = require('lodash'); const Fse = require('fs-extra'); +const minimatch = require('minimatch') const Path = require('path'); const ChildProcess = require('child_process'); const zipper = require('zip-local'); @@ -28,10 +29,19 @@ class PkgPyFuncs { config.buildDir ? this.buildDir = config.buildDir : this.error("No buildDir configuration specified") this.globalRequirements = config.globalRequirements || [] this.globalIncludes = config.globalIncludes || [] + this.globalExcludes = config.globalExcludes || [] config.cleanup === undefined ? this.cleanup = true : this.cleanup = config.cleanup this.useDocker = config.useDocker || false this.dockerImage = config.dockerImage || `lambci/lambda:build-${this.serverless.service.provider.runtime}` this.containerName = config.containerName || 'serverless-package-python-functions' + this.pipArgs = config.pipCmdExtraArgs || [] + + this.defaultExcludes = [ + "**/.serverless", + "**/node_modules", + "**/package.json", + "**/package-lock.json" + ] } clean(){ @@ -51,7 +61,7 @@ class PkgPyFuncs { } selectAll() { - const functions = this.serverless.service.functions + const functions = _.pickBy(this.serverless.service.functions, (value, key) => !this.options.function || key === this.options.function ) const info = _.map(functions, (target) => { return { name: target.name, @@ -75,7 +85,16 @@ class PkgPyFuncs { } let cmd = 'pip' - let args = ['install','--upgrade','-t', upath.normalize(buildPath), '-r', upath.normalize(requirementsPath)] + const out = this.runProcess(cmd, ['-V']) + const pipVersion = out.split(" ")[1] + + let args = ['install','--disable-pip-version-check','--upgrade','-t', upath.normalize(buildPath), '-r', upath.normalize(requirementsPath)] + if (pipVersion.split('.')[0] >= 10) { + args.push('--no-warn-conflicts') + } + if (this.pipArgs){ + args = _.concat(args, this.pipArgs) + } if ( this.useDocker === true ){ cmd = 'docker' args = ['exec',this.containerName, 'pip', ...args] @@ -150,12 +169,26 @@ class PkgPyFuncs { if (this.globalIncludes){ includes = _.concat(includes, this.globalIncludes) } - _.forEach(includes, (item) => { Fse.copySync(item, buildPath) } ) + let excludes = target.excludes || [] + excludes = _.concat(excludes, this.defaultExcludes) + if (this.globalExcludes){ + excludes = _.concat(excludes, this.globalExcludes) + } + + let filter = (src, dest) => { + for(var i = 0; i < excludes.length; i++) { + if (minimatch(src, excludes[i])){ + return false + } + } + return true + } + _.forEach(includes, (item) => { Fse.copySync(item, buildPath, filter) } ) // Install requirements let requirements = [requirementsPath] if (this.globalRequirements){ - requirements = _.concat(requirements, this.globalRequirements) + requirements = _.concat(this.globalRequirements, requirements) } _.forEach(requirements, (req) => { this.installRequirements(buildPath,req)}) zipper.sync.zip(buildPath).compress().save(`${buildPath}.zip`) @@ -173,7 +206,12 @@ class PkgPyFuncs { .then(this.setupDocker) .then(this.selectAll) .map(this.makePackage), - + 'deploy:function:packageFunction': () => BbPromise.bind(this) + .then(this.fetchConfig) + .then( () => { Fse.ensureDirAsync(this.buildDir) }) + .then(this.setupDocker) + .then(this.selectAll) + .map(this.makePackage), 'after:deploy:deploy': () => BbPromise.bind(this) .then(this.clean) }; diff --git a/package.json b/package.json index 244a699..60b57d2 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "bluebird": "^3.5.0", "fs-extra": "^3.0.0", "lodash": "^4.17.4", + "minimatch": "^3.0.4", "upath": "^1.0.0", "zip-local": "^0.3.4" },