@@ -3,6 +3,7 @@ const fse = require('fs-extra');
33const path = require ( 'path' ) ;
44const JSZip = require ( 'jszip' ) ;
55const { writeZip, addTree } = require ( './zipTree' ) ;
6+ const { sha256Path, getRequirementsLayerPath } = require ( './shared' ) ;
67
78BbPromise . promisifyAll ( fse ) ;
89
@@ -11,13 +12,49 @@ BbPromise.promisifyAll(fse);
1112 * @return {Promise } the JSZip object constructed.
1213 */
1314function zipRequirements ( ) {
14- const rootZip = new JSZip ( ) ;
1515 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
2023 ) ;
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+ } ) ;
2158}
2259
2360/**
0 commit comments