Skip to content

Commit 927b7cb

Browse files
author
bweigel
committed
Add option to package individually without moving module content to root folder
1 parent 190df96 commit 927b7cb

File tree

10 files changed

+357
-31
lines changed

10 files changed

+357
-31
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class ServerlessPythonRequirements {
4848
useDownloadCache: false,
4949
cacheLocation: false,
5050
staticCacheMaxVersions: 0,
51+
IndividuallyMoveUpModules: true,
5152
pipCmdExtraArgs: [],
5253
noDeploy: [
5354
'boto3',

lib/inject.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,17 @@ function injectRequirements(requirementsPath, packagePath, options) {
5050
* @param {string} module module to keep
5151
* @return {Promise} the JSZip object written out.
5252
*/
53-
function moveModuleUp(source, target, module) {
53+
function injectSourceCode(source, target, module) {
5454
const targetZip = new JSZip();
5555

5656
return fse
5757
.readFileAsync(source)
5858
.then(buffer => JSZip.loadAsync(buffer))
59-
.then(sourceZip => sourceZip.filter(file => file.startsWith(module + '/')))
60-
.map(srcZipObj =>
61-
zipFile(
62-
targetZip,
63-
srcZipObj.name.replace(module + '/', ''),
64-
srcZipObj.async('nodebuffer')
65-
)
66-
)
59+
.then(sourceZip => sourceZip.filter(() => true))
60+
.map(srcZipObj => {
61+
let targetName = srcZipObj.name.replace(module + '/', '');
62+
return zipFile(targetZip, targetName, srcZipObj.async('nodebuffer'));
63+
})
6764
.then(() => writeZip(targetZip, target));
6865
}
6966

@@ -88,14 +85,24 @@ function injectAllRequirements(funcArtifact) {
8885
return func;
8986
})
9087
.map(func => {
91-
if (func.module !== '.') {
88+
if (
89+
this.options.IndividuallyMoveUpModules === true ||
90+
this.options.IndividuallyMoveUpModules === 'true'
91+
) {
9292
const artifact = func.package ? func.package.artifact : funcArtifact;
93-
const newArtifact = path.join(
94-
'.serverless',
95-
`${func.module}-${func.name}.zip`
96-
);
93+
94+
let newArtifact;
95+
if (func.module === '.') {
96+
newArtifact = path.join('.serverless', `root-${func.name}.zip`);
97+
} else {
98+
newArtifact = path.join(
99+
'.serverless',
100+
`${func.module}-${func.name}.zip`
101+
);
102+
}
103+
97104
func.package.artifact = newArtifact;
98-
return moveModuleUp(artifact, newArtifact, func.module).then(
105+
return injectSourceCode(artifact, newArtifact, func.module).then(
99106
() => func
100107
);
101108
} else {

lib/pip.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,14 +404,15 @@ function installRequirementsIfNeeded(
404404
!fse.existsSync(path.join(servicePath, 'Pipfile')) &&
405405
!fse.existsSync(fileName)
406406
) {
407-
return false;
407+
return false
408408
}
409409
} else {
410410
if (!fse.existsSync(fileName)) {
411411
return false;
412412
}
413413
}
414414

415+
415416
let requirementsTxtDirectory;
416417
// Copy our requirements to another path in .serverless (incase of individually packaged)
417418
if (modulePath && modulePath !== '.') {

0 commit comments

Comments
 (0)