Skip to content

Commit 0113376

Browse files
author
bweigel
committed
refactoring & caching bug-fix
- refactors naming of functions for easier comprehension of what its doing - refactors logic of determining requirments-path - fixes bug in caching depending on correct deterination of working path
1 parent f7d3777 commit 0113376

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

lib/pip.js

+23-24
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,19 @@ function quote_single(quoteme) {
2727
* @param {Object} options
2828
* @return {undefined}
2929
*/
30-
function installRequirementsFile(
30+
function generateRequirementsFile(
3131
requirementsPath,
3232
targetFile,
3333
serverless,
3434
servicePath,
3535
options
3636
) {
3737
if (options.usePipenv && fse.existsSync(path.join(servicePath, 'Pipfile'))) {
38-
generateRequirementsFile(
38+
filterRequirementsFile(
39+
/*
40+
TODO: this is supposed to generate a requirements.txt from Pipfile,
41+
but source and target are basicalle the same
42+
*/
3943
path.join(servicePath, '.serverless/requirements.txt'),
4044
targetFile,
4145
options
@@ -44,7 +48,7 @@ function installRequirementsFile(
4448
`Parsed requirements.txt from Pipfile in ${targetFile}...`
4549
);
4650
} else {
47-
generateRequirementsFile(requirementsPath, targetFile, options);
51+
filterRequirementsFile(requirementsPath, targetFile, options);
4852
serverless.cli.log(
4953
`Generated requirements from ${requirementsPath} in ${targetFile}...`
5054
);
@@ -300,7 +304,7 @@ function dockerPathForWin(options, path) {
300304
* @param {string} target requirements where results are written
301305
* @param {Object} options
302306
*/
303-
function generateRequirementsFile(source, target, options) {
307+
function filterRequirementsFile(source, target, options) {
304308
const noDeploy = new Set(options.noDeploy || []);
305309
const requirements = fse
306310
.readFileSync(source, { encoding: 'utf-8' })
@@ -381,11 +385,21 @@ function installRequirementsIfNeeded(
381385
// Our source requirements, under our service path, and our module path (if specified)
382386
const fileName = path.join(servicePath, modulePath, options.fileName);
383387

384-
// First, generate the requirements file to our local .serverless folder
385-
fse.ensureDirSync(path.join(servicePath, '.serverless'));
386-
const slsReqsTxt = path.join(servicePath, '.serverless', 'requirements.txt');
388+
let requirementsTxtDirectory;
389+
// Copy our requirements to another filename in .serverless (incase of individually packaged)
390+
if (modulePath && modulePath != '.') {
391+
requirementsTxtDirectory = path.join(
392+
servicePath,
393+
'.serverless',
394+
modulePath
395+
);
396+
} else {
397+
requirementsTxtDirectory = path.join(servicePath, '.serverless');
398+
}
399+
fse.ensureDirSync(requirementsTxtDirectory);
400+
const slsReqsTxt = path.join(requirementsTxtDirectory, 'requirements.txt');
387401

388-
installRequirementsFile(
402+
generateRequirementsFile(
389403
fileName,
390404
slsReqsTxt,
391405
serverless,
@@ -401,28 +415,13 @@ function installRequirementsIfNeeded(
401415
return false;
402416
}
403417

404-
// Copy our requirements to another filename in .serverless (incase of individually packaged)
405-
if (modulePath && modulePath != '.') {
406-
fse.existsSync(path.join(servicePath, '.serverless', modulePath));
407-
const destinationFile = path.join(
408-
servicePath,
409-
'.serverless',
410-
modulePath,
411-
'requirements.txt'
412-
);
413-
serverless.cli.log(
414-
`Copying from ${slsReqsTxt} into ${destinationFile} ...`
415-
);
416-
fse.copySync(slsReqsTxt, destinationFile);
417-
}
418-
419418
// Then generate our MD5 Sum of this requirements file to determine where it should "go" to and/or pull cache from
420419
const reqChecksum = md5Path(slsReqsTxt);
421420

422421
// Then figure out where this cache should be, if we're caching, if we're in a module, etc
423422
const workingReqsFolder = getRequirementsWorkingPath(
424423
reqChecksum,
425-
servicePath,
424+
requirementsTxtDirectory,
426425
options
427426
);
428427

lib/shared.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ function checkForAndDeleteMaxCacheVersions(options, serverless) {
5757
* @param {Object} options
5858
* @return {string}
5959
*/
60-
function getRequirementsWorkingPath(subfolder, servicePath, options) {
60+
function getRequirementsWorkingPath(
61+
subfolder,
62+
requirementsTxtDirectory,
63+
options
64+
) {
6165
// If we want to use the static cache
6266
if (options && options.useStaticCache) {
6367
if (subfolder) {
@@ -69,7 +73,7 @@ function getRequirementsWorkingPath(subfolder, servicePath, options) {
6973
}
7074

7175
// If we don't want to use the static cache, then fallback to the way things used to work
72-
return path.join(servicePath, '.serverless', 'requirements');
76+
return path.join(requirementsTxtDirectory, 'requirements');
7377
}
7478

7579
/**

0 commit comments

Comments
 (0)