33const path = require ( 'path' ) ;
44const fs = require ( 'fs' ) ;
55const archiver = require ( 'archiver' ) ;
6- const BbPromise = require ( 'bluebird' ) ;
76
87class ServerlessPlugin {
98 constructor ( serverless , options ) {
109 this . options = options ;
1110 this . cwd = process . cwd ( ) ;
1211 this . functions = serverless . service . functions ;
1312 this . serverless = serverless ;
13+ this . log = serverless . cli . log . bind ( serverless . cli ) ;
1414
1515 this . hooks = {
1616 'after:package:cleanup' : ( ) => this . createCustomPackages ( this . functions ) ,
@@ -19,7 +19,7 @@ class ServerlessPlugin {
1919 }
2020
2121 async createCustomPackages ( functions ) {
22- console . log ( "Custom packaging lambdas..." ) ;
22+ this . log ( "Custom packaging lambdas..." ) ;
2323 const fNames = Object . keys ( functions ) ;
2424 return await
2525 Promise . all ( fNames . map ( name => this . createCustomFunctionPackage ( name ) ) ) ;
@@ -35,21 +35,30 @@ class ServerlessPlugin {
3535 }
3636
3737 async createCustomFunctionPackage ( functionName ) {
38- console . log ( "Processing " + functionName ) ;
38+ this . log ( "Processing " + functionName ) ;
3939 const functionObject = this . serverless . service . getFunction ( functionName ) ;
4040 return await
4141 Promise . resolve ( this . createPackage ( functionObject , functionName ) )
4242 }
4343
4444 async createPackage ( functionObject , functionName ) {
45- const source = this . getFnSourceDir ( functionObject ) || functionName ;
46- const target = this . getFnTarget ( functionObject ) || `${ functionName } .zip` ;
45+ const getAbsPath = inputPath => {
46+ if ( inputPath ) {
47+ return path . join ( this . cwd , inputPath ) ;
48+ }
49+ else {
50+ return inputPath
51+ }
52+ } ;
53+ const source = getAbsPath ( this . getFnSourceDir ( functionObject ) || functionName ) ;
54+ const target = getAbsPath ( this . getFnTarget ( functionObject ) || `${ functionName } .zip` ) ;
55+ const libs = getAbsPath ( this . getLibDir ( functionObject ) ) ;
4756 const globs = this . getFnIncludeGlobs ( functionObject ) ;
4857
49- const zipWithGlobs = globs => source => target => this . zipSources ( source , target , globs ) ;
58+ const zipWithGlobs = globs => libs => source => target => this . zipSources ( source , target , globs , libs ) ;
5059
5160 return await
52- zipWithGlobs ( globs ) ( path . join ( this . cwd , source ) ) ( path . join ( this . cwd , target ) ) . then ( ( artifactPath ) => {
61+ zipWithGlobs ( globs ) ( libs ) ( source ) ( target ) . then ( ( artifactPath ) => {
5362 return artifactPath ;
5463 } ) ;
5564 }
@@ -75,7 +84,14 @@ class ServerlessPlugin {
7584 return false
7685 }
7786
78- async createDirIfNotExists ( dir ) {
87+ getLibDir ( functionObject ) {
88+ if ( 'package' in functionObject && 'libs' in functionObject . package ) {
89+ return functionObject . package . libs
90+ }
91+ return false
92+ }
93+
94+ async createDirIfNotExists ( dir ) {
7995 if ( ! fs . existsSync ( dir ) ) {
8096 await
8197 new Promise ( async ( resolve , reject ) => {
@@ -92,14 +108,14 @@ class ServerlessPlugin {
92108 return true
93109 }
94110
95- async zipSources ( source , target , globs ) {
111+ async zipSources ( source , target , globs , libs ) {
96112
97113 this . createDirIfNotExists ( path . dirname ( target ) ) ;
98114
99115 const output = fs . createWriteStream ( target ) ;
100116 const archive = archiver ( 'zip' ) ;
101117
102- console . log ( "Zipping " + source + " to " + target ) ;
118+ this . log ( "Zipping " + source + " to " + target ) ;
103119
104120 output . on ( 'finish' , function ( ) {
105121 console . log ( archive . pointer ( ) + ' total bytes' ) ;
@@ -130,6 +146,11 @@ class ServerlessPlugin {
130146 addGlob ( archive ) ( globs )
131147 }
132148
149+ if ( libs ) {
150+ this . log ( "Adding libraries in: " + libs ) ;
151+ archive . glob ( "*/**" , { cwd : libs , absolute : false } )
152+ }
153+
133154 archive . finalize ( ) ;
134155
135156 return new Promise ( ( resolve , reject ) => {
0 commit comments