@@ -3,6 +3,7 @@ const fse = require('fs-extra');
3
3
const path = require ( 'path' ) ;
4
4
const JSZip = require ( 'jszip' ) ;
5
5
const { writeZip, addTree } = require ( './zipTree' ) ;
6
+ const { sha256Path, getRequirementsLayerPath} = require ( "./shared" ) ;
6
7
7
8
BbPromise . promisifyAll ( fse ) ;
8
9
@@ -11,13 +12,33 @@ BbPromise.promisifyAll(fse);
11
12
* @return {Promise } the JSZip object constructed.
12
13
*/
13
14
function zipRequirements ( ) {
14
- const rootZip = new JSZip ( ) ;
15
15
const src = path . join ( '.serverless' , 'requirements' ) ;
16
- const runtimepath = 'python' ;
17
-
18
- return addTree ( rootZip . folder ( runtimepath ) , src ) . then ( ( ) =>
19
- writeZip ( rootZip , path . join ( '.serverless' , 'pythonRequirements.zip' ) )
16
+ const reqChecksum = sha256Path ( path . join ( '.serverless' , 'requirements.txt' ) ) ;
17
+ const targetZipPath = path . join ( '.serverless' , 'pythonRequirements.zip' ) ;
18
+ const zipCachePath = getRequirementsLayerPath (
19
+ reqChecksum ,
20
+ targetZipPath ,
21
+ this . options
20
22
) ;
23
+
24
+ const promises = [ ] ;
25
+ if ( fse . existsSync ( zipCachePath ) ) {
26
+ this . serverless . cli . log ( "Found cached Python Requirements Lambda Layer file" ) ;
27
+ } else {
28
+ const rootZip = new JSZip ( ) ;
29
+ const runtimepath = 'python' ;
30
+
31
+ promises . push (
32
+ addTree ( rootZip . folder ( runtimepath ) , src ) . then ( ( ) =>
33
+ writeZip ( rootZip , zipCachePath )
34
+ )
35
+ ) ;
36
+ }
37
+ return BbPromise . all ( promises ) . then ( ( ) => {
38
+ if ( zipCachePath !== targetZipPath ) {
39
+ fse . symlink ( zipCachePath , targetZipPath , 'file' )
40
+ }
41
+ } ) ;
21
42
}
22
43
23
44
/**
0 commit comments