@@ -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,49 @@ 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 ,
22
+ this . serverless
20
23
) ;
24
+
25
+ const promises = [ ] ;
26
+ if ( fse . existsSync ( zipCachePath ) ) {
27
+ let layerProgress ;
28
+ if ( this . progress && this . log ) {
29
+ layerProgress = this . progress . get ( 'python-layer-requirements' ) ;
30
+ layerProgress . update (
31
+ 'Using cached Python Requirements Lambda Layer file'
32
+ ) ;
33
+ this . log . info ( 'Found cached Python Requirements Lambda Layer file' ) ;
34
+ } else {
35
+ this . serverless . cli . log (
36
+ 'Found cached Python Requirements Lambda Layer file'
37
+ ) ;
38
+ }
39
+ } else {
40
+ const rootZip = new JSZip ( ) ;
41
+ const runtimepath = 'python' ;
42
+
43
+ promises . push (
44
+ addTree ( rootZip . folder ( runtimepath ) , src ) . then ( ( ) =>
45
+ writeZip ( rootZip , zipCachePath )
46
+ )
47
+ ) ;
48
+ }
49
+ return BbPromise . all ( promises ) . then ( ( ) => {
50
+ if ( zipCachePath !== targetZipPath ) {
51
+ if ( process . platform === 'win32' ) {
52
+ fse . copySync ( zipCachePath , targetZipPath ) ;
53
+ } else {
54
+ fse . symlink ( zipCachePath , targetZipPath , 'file' ) ;
55
+ }
56
+ }
57
+ } ) ;
21
58
}
22
59
23
60
/**
0 commit comments