@@ -6,10 +6,12 @@ const set = require('lodash.set');
6
6
const { spawnSync} = require ( 'child_process' ) ;
7
7
const values = require ( 'lodash.values' ) ;
8
8
const { buildImage, getBindPath, getDockerUid} = require ( './docker' ) ;
9
- const { checkForAndDeleteMaxCacheVersions, getRequirementsWorkingPath, md5Path} = require ( './shared' ) ;
9
+ const {
10
+ checkForAndDeleteMaxCacheVersions, getRequirementsWorkingPath, md5Path,
11
+ } = require ( './shared' ) ;
10
12
11
13
/**
12
- + * Just generate the requirements file in the .serverless folder
14
+ * Just generate the requirements file in the .serverless folder
13
15
* @param {string } requirementsPath
14
16
* @param {string } targetFile
15
17
* @param {Object } serverless
@@ -44,9 +46,8 @@ function installRequirementsFile(requirementsPath, targetFile, serverless, servi
44
46
* @return {undefined }
45
47
*/
46
48
function installRequirements ( targetFolder , serverless , options ) {
47
-
48
49
const targetRequirementsTxt = path . join ( targetFolder , 'requirements.txt' ) ;
49
-
50
+
50
51
serverless . cli . log ( `Installing requirements from ${ targetRequirementsTxt } ...` ) ;
51
52
52
53
let cmd ;
@@ -77,7 +78,7 @@ function installRequirements(targetFolder, serverless, options) {
77
78
pipCmd . push ( '--system' ) ;
78
79
}
79
80
}
80
-
81
+
81
82
// If we are dockerizing pip
82
83
if ( options . dockerizePip ) {
83
84
cmd = 'docker' ;
@@ -154,8 +155,10 @@ function generateRequirementsFile(source, target, options) {
154
155
const noDeploy = new Set ( options . noDeploy || [ ] ) ;
155
156
const requirements = fse . readFileSync ( source , { encoding : 'utf-8' } ) . split ( / \r ? \n / ) ;
156
157
const filteredRequirements = requirements . filter ( ( req ) => {
157
- req = req . trim ( )
158
- if ( req . length == 0 || req [ 0 ] == '#' ) { return false ; } // Skip empty lines and comments
158
+ req = req . trim ( ) ;
159
+ if ( req . length == 0 || req [ 0 ] == '#' ) {
160
+ return false ;
161
+ }
159
162
return ! noDeploy . has ( req . split ( / [ = < > \t ] / ) [ 0 ] . trim ( ) ) ;
160
163
} ) ;
161
164
filteredRequirements . sort ( ) ; // Sort them alphabetically
@@ -187,8 +190,6 @@ function copyVendors(vendorFolder, targetFolder, serverless) {
187
190
}
188
191
189
192
190
-
191
-
192
193
/**
193
194
* This evaluates if requirements are actually needed to be installed, but fails
194
195
* gracefully if no req file is found intentionally. It also assists with code
@@ -200,13 +201,12 @@ function copyVendors(vendorFolder, targetFolder, serverless) {
200
201
* @return {string }
201
202
*/
202
203
function installRequirementsIfNeeded ( servicePath , modulePath , options , serverless ) {
203
-
204
204
// Our source requirements, under our service path, and our module path (if specified)
205
- const fileName = path . join ( servicePath , modulePath , options . fileName )
206
-
207
- // First, generate the requirements file to our local .serverless folder
205
+ const fileName = path . join ( servicePath , modulePath , options . fileName ) ;
206
+
207
+ // First, generate the requirements file to our local .serverless folder
208
208
fse . ensureDirSync ( path . join ( servicePath , '.serverless' ) ) ;
209
- const slsReqsTxt = path . join ( servicePath , '.serverless' , 'requirements.txt' )
209
+ const slsReqsTxt = path . join ( servicePath , '.serverless' , 'requirements.txt' ) ;
210
210
// Incase it's laying around from a previous package (when individually/failed packaging)
211
211
if ( fse . existsSync ( slsReqsTxt ) ) {
212
212
fse . removeSync ( slsReqsTxt ) ;
@@ -218,29 +218,30 @@ function installRequirementsIfNeeded(servicePath, modulePath, options, serverles
218
218
servicePath ,
219
219
options
220
220
) ;
221
-
221
+
222
222
// If no requirements file or an empty requirements file, then do nothing
223
223
if ( ! fse . existsSync ( slsReqsTxt ) || fse . statSync ( slsReqsTxt ) . size == 0 ) {
224
224
serverless . cli . log ( `Skipping empty output requirements.txt file from ${ slsReqsTxt } ` ) ;
225
225
return false ;
226
226
}
227
-
227
+
228
228
// Copy our requirements to another filename in .serverless (incase of individually packaged)
229
- destinationFile = path . join ( servicePath , '.serverless' , modulePath + " _requirements.txt" )
229
+ destinationFile = path . join ( servicePath , '.serverless' , modulePath + ' _requirements.txt' ) ;
230
230
serverless . cli . log ( `Copying from ${ slsReqsTxt } into ${ destinationFile } ...` ) ;
231
- fse . copySync ( slsReqsTxt , destinationFile )
232
-
231
+ fse . copySync ( slsReqsTxt , destinationFile ) ;
232
+
233
233
// Then generate our MD5 Sum of this requirements file to determine where it should "go" to and/or pull cache from
234
- const reqChecksum = md5Path ( slsReqsTxt )
235
-
234
+ const reqChecksum = md5Path ( slsReqsTxt ) ;
235
+
236
236
// Then figure out where this cache should be, if we're caching, etc
237
- const workingReqsFolder = getRequirementsWorkingPath ( reqChecksum , servicePath , options )
238
-
237
+ const workingReqsFolder = getRequirementsWorkingPath ( reqChecksum , servicePath , options ) ;
238
+
239
239
// Check if our static cache is present and is valid
240
240
if ( fse . existsSync ( workingReqsFolder ) ) {
241
241
if ( fse . existsSync ( path . join ( workingReqsFolder , '.completed_requirements' ) ) ) {
242
242
serverless . cli . log ( `Using static cache of requirements found at ${ workingReqsFolder } ...` ) ;
243
- fse . utimesSync ( workingReqsFolder , new Date ( ) , new Date ( ) ) // We'll "touch" the folder, as to bring it to the start of the FIFO cache
243
+ // We'll "touch" the folder, as to bring it to the start of the FIFO cache
244
+ fse . utimesSync ( workingReqsFolder , new Date ( ) , new Date ( ) ) ;
244
245
fse . removeSync ( slsReqsTxt ) ;
245
246
return workingReqsFolder ;
246
247
}
@@ -249,13 +250,13 @@ function installRequirementsIfNeeded(servicePath, modulePath, options, serverles
249
250
rimraf . sync ( workingReqsFolder ) ;
250
251
}
251
252
}
252
-
253
+
253
254
// Ensuring the working reqs folder exists
254
255
fse . ensureDirSync ( workingReqsFolder ) ;
255
-
256
+
256
257
// Copy our requirements.txt into our working folder...
257
258
fse . copySync ( slsReqsTxt , path . join ( workingReqsFolder , 'requirements.txt' ) ) ;
258
-
259
+
259
260
// Then install our requirements from this folder
260
261
installRequirements (
261
262
workingReqsFolder ,
@@ -270,13 +271,13 @@ function installRequirementsIfNeeded(servicePath, modulePath, options, serverles
270
271
serverless
271
272
) ;
272
273
}
273
-
274
+
274
275
// Then touch our ".completed_requirements" file so we know we can use this for static cache
275
276
if ( options . useStaticCache ) {
276
277
fse . closeSync ( fse . openSync ( path . join ( workingReqsFolder , '.completed_requirements' ) , 'w' ) ) ;
277
278
}
278
279
fse . removeSync ( slsReqsTxt ) ; // Clean up after ourselves
279
- return workingReqsFolder
280
+ return workingReqsFolder ;
280
281
}
281
282
282
283
@@ -285,9 +286,8 @@ function installRequirementsIfNeeded(servicePath, modulePath, options, serverles
285
286
* @return {undefined }
286
287
*/
287
288
function installAllRequirements ( ) {
288
-
289
289
// First, check and delete cache versions, if enabled
290
- checkForAndDeleteMaxCacheVersions ( this . options , this . serverless )
290
+ checkForAndDeleteMaxCacheVersions ( this . options , this . serverless ) ;
291
291
292
292
// Then if we're going to package functions individually...
293
293
if ( this . serverless . service . package . individually ) {
@@ -300,19 +300,26 @@ function installAllRequirements() {
300
300
}
301
301
// If we didn't already process a module (functions can re-use modules)
302
302
if ( ! doneModules . includes ( f . module ) ) {
303
- const reqsInstalledAt = installRequirementsIfNeeded ( this . servicePath , f . module , this . options , this . serverless ) ;
303
+ const reqsInstalledAt = installRequirementsIfNeeded (
304
+ this . servicePath , f . module , this . options , this . serverless
305
+ ) ;
304
306
// Add symlinks into .serverless for each module so it's easier for injecting and for users to see where reqs are
305
307
if ( reqsInstalledAt ) {
306
- fse . symlink ( reqsInstalledAt , path . join ( this . servicePath , '.serverless' , `${ f . module } _requirements` ) )
308
+ fse . symlink (
309
+ reqsInstalledAt ,
310
+ path . join ( this . servicePath , '.serverless' , `${ f . module } _requirements` )
311
+ ) ;
307
312
}
308
313
doneModules . push ( f . module ) ;
309
314
}
310
315
} ) ;
311
316
} else {
312
- const reqsInstalledAt = installRequirementsIfNeeded ( this . servicePath , '' , this . options , this . serverless ) ;
317
+ const reqsInstalledAt = installRequirementsIfNeeded (
318
+ this . servicePath , '' , this . options , this . serverless
319
+ ) ;
313
320
// Add symlinks into .serverless for so it's easier for injecting and for users to see where reqs are
314
321
if ( reqsInstalledAt ) {
315
- fse . symlink ( reqsInstalledAt , path . join ( this . servicePath , '.serverless' , `_requirements` ) )
322
+ fse . symlink ( reqsInstalledAt , path . join ( this . servicePath , '.serverless' , `_requirements` ) ) ;
316
323
}
317
324
}
318
325
} ;
0 commit comments