diff --git a/index.js b/index.js index b444a9b6..dece8ba1 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,6 @@ const { const { injectAllRequirements } = require('./lib/inject'); const { layerRequirements } = require('./lib/layer'); const { installAllRequirements } = require('./lib/pip'); -const { pipfileToRequirements } = require('./lib/pipenv'); const { pyprojectTomlToRequirements } = require('./lib/poetry'); const { cleanup, cleanupCache } = require('./lib/clean'); @@ -156,7 +155,6 @@ class ServerlessPythonRequirements { return; } return BbPromise.bind(this) - .then(pipfileToRequirements) .then(pyprojectTomlToRequirements) .then(addVendorHelper) .then(installAllRequirements) diff --git a/lib/pip.js b/lib/pip.js index 14864794..34a9859f 100644 --- a/lib/pip.js +++ b/lib/pip.js @@ -13,6 +13,7 @@ const { getRequirementsWorkingPath, getUserCachePath } = require('./shared'); +const { pipfileToRequirements } = require('./pipenv'); /** * Omit empty commands. @@ -55,6 +56,7 @@ function mergeCommands(commands) { */ function generateRequirementsFile( requirementsPath, + modulePath, targetFile, serverless, servicePath, @@ -74,13 +76,10 @@ function generateRequirementsFile( ); } else if ( options.usePipenv && - fse.existsSync(path.join(servicePath, 'Pipfile')) + fse.existsSync(path.join(servicePath, modulePath, 'Pipfile')) ) { - filterRequirementsFile( - path.join(servicePath, '.serverless/requirements.txt'), - targetFile, - options - ); + pipfileToRequirements(modulePath, targetFile, serverless, options); + filterRequirementsFile(targetFile, targetFile, options); serverless.cli.log( `Parsed requirements.txt from Pipfile in ${targetFile}...` ); @@ -439,7 +438,7 @@ function copyVendors(vendorFolder, targetFolder, serverless) { * @param {Object} options * @param {string} fileName */ -function requirementsFileExists(servicePath, options, fileName) { +function requirementsFileExists(servicePath, modulePath, options) { if ( options.usePoetry && fse.existsSync(path.join(servicePath, 'pyproject.toml')) @@ -447,11 +446,14 @@ function requirementsFileExists(servicePath, options, fileName) { return true; } - if (options.usePipenv && fse.existsSync(path.join(servicePath, 'Pipfile'))) { + if ( + options.usePipenv && + fse.existsSync(path.join(servicePath, modulePath, 'Pipfile')) + ) { return true; } - if (fse.existsSync(fileName)) { + if (fse.existsSync(path.join(servicePath, modulePath, options.fileName))) { return true; } @@ -480,7 +482,7 @@ function installRequirementsIfNeeded( const fileName = path.join(servicePath, modulePath, options.fileName); // Skip requirements generation, if requirements file doesn't exist - if (!requirementsFileExists(servicePath, options, fileName)) { + if (!requirementsFileExists(servicePath, modulePath, options)) { return false; } @@ -500,6 +502,7 @@ function installRequirementsIfNeeded( generateRequirementsFile( fileName, + modulePath, slsReqsTxt, serverless, servicePath, diff --git a/lib/pipenv.js b/lib/pipenv.js index 6718844c..021250e2 100644 --- a/lib/pipenv.js +++ b/lib/pipenv.js @@ -6,23 +6,29 @@ const { EOL } = require('os'); /** * pipenv install */ -function pipfileToRequirements() { - if ( - !this.options.usePipenv || - !fse.existsSync(path.join(this.servicePath, 'Pipfile')) - ) { +function pipfileToRequirements( + modulePath, + outputRequirements, + serverless, + options +) { + const pipenvPath = path.join(modulePath, 'Pipfile'); + + // Stop if Pipfile file does not exist + if (!options.usePipenv || !fse.existsSync(pipenvPath)) { return; } - this.serverless.cli.log('Generating requirements.txt from Pipfile...'); + serverless.cli.log('Generating requirements.txt from Pipfile...'); const res = spawnSync( 'pipenv', ['lock', '--requirements', '--keep-outdated'], { - cwd: this.servicePath + cwd: modulePath } ); + if (res.error) { if (res.error.code === 'ENOENT') { throw new Error( @@ -34,9 +40,8 @@ function pipfileToRequirements() { if (res.status !== 0) { throw new Error(res.stderr); } - fse.ensureDirSync(path.join(this.servicePath, '.serverless')); fse.writeFileSync( - path.join(this.servicePath, '.serverless/requirements.txt'), + outputRequirements, removeEditableFlagFromRequirementsString(res.stdout) ); } diff --git a/test.js b/test.js index 987c862c..9a89777d 100644 --- a/test.js +++ b/test.js @@ -1620,8 +1620,11 @@ test( 'foobar has retained its executable file permissions' ); - const zipfiles_hello2 = listZipFilesWithMetaData('.serverless/module2-sls-py-req-test-indiv-dev-hello2.zip'); - const flaskPerm = statSync('.serverless/module2/requirements/bin/flask').mode; + const zipfiles_hello2 = listZipFilesWithMetaData( + '.serverless/module2-sls-py-req-test-indiv-dev-hello2.zip' + ); + const flaskPerm = statSync('.serverless/module2/requirements/bin/flask') + .mode; t.true( zipfiles_hello2['bin/flask'].unixPermissions === flaskPerm, @@ -1654,8 +1657,11 @@ test( 'foobar has retained its executable file permissions' ); - const zipfiles_hello2 = listZipFilesWithMetaData('.serverless/module2-sls-py-req-test-indiv-dev-hello2.zip'); - const flaskPerm = statSync('.serverless/module2/requirements/bin/flask').mode; + const zipfiles_hello2 = listZipFilesWithMetaData( + '.serverless/module2-sls-py-req-test-indiv-dev-hello2.zip' + ); + const flaskPerm = statSync('.serverless/module2/requirements/bin/flask') + .mode; t.true( zipfiles_hello2['bin/flask'].unixPermissions === flaskPerm,