serverless-single
Advanced tools
Comparing version 0.1.0 to 0.1.1
216
index.js
@@ -19,143 +19,139 @@ 'use strict'; | ||
const path=require('path'), | ||
fs=require('fs'), | ||
fse=require('fs-extra'), | ||
BbPromise=require('bluebird'); // Serverless uses Bluebird Promises and we recommend you do to because they provide more than your average Promise :) | ||
const path = require('path'), | ||
fs = require('fs'), | ||
fse = require('fs-extra'), | ||
BbPromise = require('bluebird'); // Serverless uses Bluebird Promises and we recommend you do to because they provide more than your average Promise :) | ||
module.exports=function (S) { // Always pass in the ServerlessPlugin Class | ||
module.exports = function(S) { // Always pass in the ServerlessPlugin Class | ||
/** | ||
* Adding/Manipulating Serverless classes | ||
* - You can add or manipulate Serverless classes like this | ||
*/ | ||
S.classes.Project.newStaticMethod = function() { console.log("A new method!"); }; | ||
S.classes.Project.prototype.newMethod = function() { S.classes.Project.newStaticMethod(); }; | ||
/** | ||
* Extending the Plugin Class | ||
* - Here is how you can add custom Actions and Hooks to Serverless. | ||
* - This class is only required if you want to add Actions and Hooks. | ||
*/ | ||
class PluginBoilerplate extends S.classes.Plugin { | ||
/** | ||
* Adding/Manipulating Serverless classes | ||
* - You can add or manipulate Serverless classes like this | ||
* Constructor | ||
* - Keep this and don't touch it unless you know what you're doing. | ||
*/ | ||
S.classes.Project.newStaticMethod=function () { | ||
console.log("A new method!"); | ||
}; | ||
S.classes.Project.prototype.newMethod=function () { | ||
S.classes.Project.newStaticMethod(); | ||
}; | ||
constructor() { | ||
super(); | ||
this.name = 'myPlugin'; // Define your plugin's name | ||
} | ||
/** | ||
* Extending the Plugin Class | ||
* - Here is how you can add custom Actions and Hooks to Serverless. | ||
* - This class is only required if you want to add Actions and Hooks. | ||
* Register Hooks | ||
* - If you would like to register hooks (i.e., functions) that fire before or after a core Serverless Action or your Custom Action, include this function. | ||
* - Make sure to identify the Action you want to add a hook for and put either "pre" or "post" to describe when it should happen. | ||
*/ | ||
class PluginBoilerplate extends S.classes.Plugin { | ||
registerHooks() { | ||
/** | ||
* Constructor | ||
* - Keep this and don't touch it unless you know what you're doing. | ||
*/ | ||
S.addHook(this._hookPre.bind(this), { | ||
action: 'codePackageLambda', | ||
event: 'post' | ||
}); | ||
constructor() { | ||
super(); | ||
this.name='myPlugin'; // Define your plugin's name | ||
} | ||
return BbPromise.resolve(); | ||
} | ||
/** | ||
* Register Hooks | ||
* - If you would like to register hooks (i.e., functions) that fire before or after a core Serverless Action or your Custom Action, include this function. | ||
* - Make sure to identify the Action you want to add a hook for and put either "pre" or "post" to describe when it should happen. | ||
*/ | ||
/** | ||
* Your Custom PRE Hook | ||
* - Here is an example of a Custom PRE Hook. Include this and modify it if you would like to write your a hook that fires BEFORE an Action. | ||
* - Be sure to ALWAYS accept and return the "evt" object, or you will break the entire flow. | ||
* - The "evt" object contains Action-specific data. You can add custom data to it, but if you change any data it will affect subsequent Actions and Hooks. | ||
* - You can also access other Project-specific data @ this.S Again, if you mess with data on this object, it could break everything, so make sure you know what you're doing ;) | ||
*/ | ||
registerHooks() { | ||
_hookPre(evt) { | ||
S.addHook(this._hookPre.bind(this), { | ||
action: 'codePackageLambda', | ||
event: 'post' | ||
let _this = this; | ||
let func = S.getProject().getFunction(evt.options.name), | ||
singlize; | ||
if (func.getRuntime().getName().contains('nodejs')) { | ||
singlize = new SinglizeNodejs(S, evt, func); | ||
return singlize.singlize() | ||
.then(function(evt) { | ||
return evt; | ||
}); | ||
} | ||
return BbPromise.resolve(evt); | ||
return BbPromise.resolve(); | ||
} | ||
} | ||
} | ||
/** | ||
* Your Custom PRE Hook | ||
* - Here is an example of a Custom PRE Hook. Include this and modify it if you would like to write your a hook that fires BEFORE an Action. | ||
* - Be sure to ALWAYS accept and return the "evt" object, or you will break the entire flow. | ||
* - The "evt" object contains Action-specific data. You can add custom data to it, but if you change any data it will affect subsequent Actions and Hooks. | ||
* - You can also access other Project-specific data @ this.S Again, if you mess with data on this object, it could break everything, so make sure you know what you're doing ;) | ||
*/ | ||
class SinglizeNodejs{ | ||
_hookPre(evt) { | ||
constructor(S, evt, func) { | ||
this.evt = evt; | ||
this.function = func; | ||
} | ||
let _this=this; | ||
let func=S.getProject().getFunction(evt.options.name), | ||
singlize; | ||
singlize() { | ||
let _this = this; | ||
if (func.getRuntime().getName() === 'nodejs') { | ||
singlize=new SinglizeNodejs(S, evt, func); | ||
return singlize.singlize() | ||
.then(function (evt) { | ||
return evt; | ||
}); | ||
} | ||
return BbPromise.resolve(evt); | ||
return new BbPromise(function(resolve, reject) { | ||
if(!_this.function.custom.single){ | ||
resolve( _this.evt); | ||
} | ||
} | ||
let includeFolder = _this.function.custom.single.include; | ||
var allowRemoveFolder = function(name) { | ||
var allow=true; | ||
includeFolder.forEach(function (folder) { | ||
if (name.startsWith(folder)) { | ||
allow = false; | ||
} | ||
}); | ||
return allow; | ||
}; | ||
class SinglizeNodejs { | ||
fse.walk(_this.evt.data.pathDist) | ||
.on('data', function (item) { | ||
if (item.stats.isFile()) { | ||
let name = path.relative(_this.evt.data.pathDist, item.path); | ||
constructor(S, evt, func) { | ||
this.evt=evt; | ||
this.function=func; | ||
} | ||
singlize() { | ||
let _this=this; | ||
return new BbPromise(function (resolve, reject) { | ||
if (!_this.function.custom.single) { | ||
resolve(_this.evt); | ||
var allow = allowRemoveFolder(name); | ||
if(allow && !name.startsWith(_this.function.name)){ | ||
fse.removeSync(item.path); | ||
} | ||
let includeFolder=_this.function.custom.single.include; | ||
var allowRemoveFolder=function (name) { | ||
var allow=true; | ||
includeFolder.forEach(function (folder) { | ||
if (name.startsWith(folder)) { | ||
allow=false; | ||
} | ||
}) | ||
.on('end', function () { | ||
fse.walk(_this.evt.data.pathDist) | ||
.on('data', function (item) { | ||
if (!item.stats.isFile()) { | ||
let name=path.relative(_this.evt.data.pathDist, item.path); | ||
var allow = allowRemoveFolder(name); | ||
if(allow && !name.startsWith(_this.function.name)){ | ||
if(name){ | ||
fse.remove(item.path); | ||
} | ||
}); | ||
return allow; | ||
}; | ||
} | ||
} | ||
}) | ||
.on('end', function () { | ||
resolve( _this.evt); | ||
}); | ||
}); | ||
fse.walk(_this.evt.data.pathDist) | ||
.on('data', function (item) { | ||
if (item.stats.isFile()) { | ||
let name=path.relative(_this.evt.data.pathDist, item.path); | ||
var allow=allowRemoveFolder(name); | ||
if (allow && !name.startsWith(_this.function.name)) { | ||
fse.removeSync(item.path); | ||
} | ||
} | ||
}) | ||
.on('end', function () { | ||
fse.walk(_this.evt.data.pathDist) | ||
.on('data', function (item) { | ||
if (!item.stats.isFile()) { | ||
let name=path.relative(_this.evt.data.pathDist, item.path); | ||
var allow=allowRemoveFolder(name); | ||
if (allow && !name.startsWith(_this.function.name)) { | ||
if (name) { | ||
fse.remove(item.path); | ||
} | ||
} | ||
} | ||
}) | ||
.on('end', function () { | ||
resolve(_this.evt); | ||
}); | ||
}); | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
return PluginBoilerplate; | ||
return PluginBoilerplate; | ||
}; |
{ | ||
"name": "serverless-single", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"engines": { | ||
@@ -5,0 +5,0 @@ "node": ">=4.0" |
#Serverless Single Plugin | ||
[![serverless](http://public.serverless.com/badges/v3.svg)](http://www.serverless.com) | ||
@@ -3,0 +4,0 @@ Removes unnecessary folders, or unused lambda functions in your zip file, thus reducing zip file size. |
33
7358
126