Skip to content

Commit

Permalink
Handle package (global) exclude (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
arichiardi authored and moea committed Sep 5, 2017
1 parent cff7494 commit 1dbcf50
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
45 changes: 43 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
const bluebird = require('bluebird');
const _ = require('lodash');
const path = require('path');
const jszip = require('jszip');
const fs = require('fs');
const minimatch= require('minimatch');

const munge = require('./serverless-cljs-plugin/munge');
const mkdirp = bluebird.promisify(require('mkdirp'));
const exec = bluebird.promisify(require('child_process').exec,
{multiArgs: true});

jszip.external.Promise = bluebird;

const DEFAULT_EXCLUDE = ['node_modules/serverless-cljs-plugin'];

function handler(fn) {
return `index.${munge.munge(fn.cljs)}`;
}
Expand All @@ -30,7 +37,7 @@ function edn(v) {
return '[' + v.map(edn).join(' ') + ']';
}
if (_.isPlainObject(v)) {
return '{' + _.map(v, (v, k) => ':' + k + ' ' + edn(v)).join(' ') + '}'
return '{' + _.map(v, (v, k) => ':' + k + ' ' + edn(v)).join(' ') + '}';
}
return v;
}
Expand All @@ -50,6 +57,35 @@ function basepath(config, service, opts) {
return `${config.servicePath}/.serverless/${opts.function || service.service}`;
}

const readFile = bluebird.promisify(fs.readFile);
const writeFile = bluebird.promisify(fs.writeFile);

const applyZipExclude = bluebird.coroutine(
function*(serverless, opts) {
// TODO respect exclude on individual functions
const exclude = _.uniq(_.get(serverless.service.package, "exclude", []));

if (!_.isEmpty(exclude)) {
const data = yield readFile(serverless.service.__cljsArtifact);

const newZip = jszip();

const oldZip = yield jszip.loadAsync(data);
oldZip
.filter((path, file) => !_.some(exclude, pattern => minimatch(path, pattern)))
.forEach((entry) => newZip.file(entry.name, entry.nodeStream("nodebuffer")));

const buffer = yield newZip.generateAsync({
type: "nodebuffer",
platform: process.platform,
compression: "DEFLATE",
comment: `Generated by serverless-cljs-plugin on ${new Date().toISOString()}`
});

yield writeFile(serverless.service.__cljsArtifact, buffer);
}
});

function cljsLambdaBuild(serverless, opts) {
const fns = slsToCljsLambda(serverless.service.functions, opts);
const compiler = _.get(serverless.service, 'custom.cljsCompiler');
Expand Down Expand Up @@ -78,13 +114,18 @@ const after_createDeploymentArtifacts = bluebird.coroutine(

yield cljsLambdaBuild(serverless, opts);

yield applyZipExclude(serverless, opts);

serverless.cli.log(`Returning artifact path ${serverless.service.__cljsArtifact}`);
return serverless.service.__cljsArtifact;
});

class ServerlessPlugin {
constructor(serverless, opts) {
opts.function = opts.f || opts.function;
_.update(
serverless.service, 'package.exclude', v => _.concat(v || [], DEFAULT_EXCLUDE));

opts.function = (opts.f || opts.function);

serverless.service.__cljsBasePath = (
`${basepath(serverless.config, serverless.service, opts)}`);
Expand Down
4 changes: 4 additions & 0 deletions lumo-example/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ functions:
example:
cljs: lumo-example.core/example

package:
exclude:
- node_modules/.yarn*

plugins:
- serverless-cljs-plugin
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
"dependencies": {
"archiver": "2.0.0",
"bluebird": "3.4.6",
"jszip": "3.1.4",
"lodash": "4.17.2",
"minimatch": "3.0.4",
"mkdirp": "0.5.1",
"mustache": "2.3.0"
}
Expand Down
6 changes: 3 additions & 3 deletions serverless-cljs-plugin/serverless_lumo/build.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
:files #{[\"path/to/file\" {:name \"file\"}]
\"another/file\"}}
Seqable :files entries are passed as parameters to the archiver.file
function, for details check:
https://archiverjs.com/docs/Archiver.html#file"
Seqable :files entries are passed as parameters to the archiver's
.file function, for details check:
- https://archiverjs.com/docs/Archiver.html#file"
[output-path zip-opts compiler-opts]

(let [archiver (archiver "zip" #js {:zlib {:level 9}})
Expand Down

0 comments on commit 1dbcf50

Please sign in to comment.