Skip to content

(WIP) Add option to package individually without moving module content to root folder (i.e. like base serverless) #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class ServerlessPythonRequirements {
useDownloadCache: true,
cacheLocation: false,
staticCacheMaxVersions: 0,
IndividuallyMoveUpModules: true,
pipCmdExtraArgs: [],
noDeploy: [],
vendor: ''
Expand Down
37 changes: 22 additions & 15 deletions lib/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,17 @@ function injectRequirements(requirementsPath, packagePath, options) {
* @param {string} module module to keep
* @return {Promise} the JSZip object written out.
*/
function moveModuleUp(source, target, module) {
function injectSourceCode(source, target, module) {
const targetZip = new JSZip();

return fse
.readFileAsync(source)
.then(buffer => JSZip.loadAsync(buffer))
.then(sourceZip => sourceZip.filter(file => file.startsWith(module + '/')))
.map(srcZipObj =>
zipFile(
targetZip,
srcZipObj.name.replace(module + '/', ''),
srcZipObj.async('nodebuffer')
)
)
.then(sourceZip => sourceZip.filter(() => true))
.map(srcZipObj => {
let targetName = srcZipObj.name.replace(module + '/', '');
return zipFile(targetZip, targetName, srcZipObj.async('nodebuffer'));
})
.then(() => writeZip(targetZip, target));
}

Expand Down Expand Up @@ -99,14 +96,24 @@ function injectAllRequirements(funcArtifact) {
return func;
})
.map(func => {
if (func.module !== '.') {
if (
this.options.IndividuallyMoveUpModules === true ||
this.options.IndividuallyMoveUpModules === 'true'
) {
const artifact = func.package ? func.package.artifact : funcArtifact;
const newArtifact = path.join(
'.serverless',
`${func.module}-${func.name}.zip`
);

let newArtifact;
if (func.module === '.') {
newArtifact = path.join('.serverless', `root-${func.name}.zip`);
} else {
newArtifact = path.join(
'.serverless',
`${func.module}-${func.name}.zip`
);
}

func.package.artifact = newArtifact;
return moveModuleUp(artifact, newArtifact, func.module).then(
return injectSourceCode(artifact, newArtifact, func.module).then(
() => func
);
} else {
Expand Down
Loading