Skip to content

Commit

Permalink
refactor(CLI): Expose function artifact size in deploy summary
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Oct 7, 2021
1 parent c9f2227 commit 8746100
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 29 deletions.
58 changes: 32 additions & 26 deletions lib/plugins/aws/deploy/lib/uploadArtifacts.js
Expand Up @@ -29,7 +29,7 @@ module.exports = {

async uploadArtifacts() {
const artifactFilePaths = [
...this.getFunctionArtifactFilePaths(),
...(await this.getFunctionArtifactFilePaths()),
...this.getLayerArtifactFilePaths(),
];
if (artifactFilePaths.length === 1) {
Expand Down Expand Up @@ -112,35 +112,41 @@ module.exports = {
return response;
},

getFunctionArtifactFilePaths() {
async getFunctionArtifactFilePaths() {
const functionNames = this.serverless.service.getAllFunctions();
return _.uniq(
functionNames
.map((name) => {
const functionObject = this.serverless.service.getFunction(name);
if (functionObject.image) return null;
const functionArtifactFileName = this.provider.naming.getFunctionArtifactName(name);
functionObject.package = functionObject.package || {};
const artifactFilePath =
functionObject.package.artifact || this.serverless.service.package.artifact;

if (
!artifactFilePath ||
(this.serverless.service.artifact && !functionObject.package.artifact)
) {
(
await Promise.all(
functionNames.map(async (name) => {
const functionObject = this.serverless.service.getFunction(name);
if (functionObject.image) return null;
const functionArtifactFileName = this.provider.naming.getFunctionArtifactName(name);
functionObject.package = functionObject.package || {};
let artifactFilePath =
functionObject.package.artifact || this.serverless.service.package.artifact;

if (
this.serverless.service.package.individually ||
functionObject.package.individually
!artifactFilePath ||
(this.serverless.service.artifact && !functionObject.package.artifact)
) {
const artifactFileName = functionArtifactFileName;
return path.join(this.packagePath, artifactFileName);
if (
this.serverless.service.package.individually ||
functionObject.package.individually
) {
const artifactFileName = functionArtifactFileName;
artifactFilePath = path.join(this.packagePath, artifactFileName);
} else {
artifactFilePath = path.join(
this.packagePath,
this.provider.naming.getServiceArtifactName()
);
}
}
return path.join(this.packagePath, this.provider.naming.getServiceArtifactName());
}

return artifactFilePath;
})
.filter(Boolean)
functionObject.artifactSize = (await this.getFileStats(artifactFilePath)).size;
return artifactFilePath;
})
)
).filter(Boolean)
);
},

Expand Down Expand Up @@ -168,7 +174,7 @@ module.exports = {
legacy.log('Uploading artifacts...');

const artifactFilePaths = [
...this.getFunctionArtifactFilePaths(),
...(await this.getFunctionArtifactFilePaths()),
...this.getLayerArtifactFilePaths(),
];

Expand Down
9 changes: 7 additions & 2 deletions lib/plugins/aws/info/display.js
@@ -1,7 +1,8 @@
'use strict';

const chalk = require('chalk');
const { legacy, isVerboseMode } = require('@serverless/utils/log');
const filesize = require('filesize');
const { legacy, isVerboseMode, style } = require('@serverless/utils/log');

module.exports = {
displayServiceInfo() {
Expand Down Expand Up @@ -131,7 +132,11 @@ module.exports = {
const outputSectionItems = [];
info.functions.forEach((f) => {
functionsMessage += `\n ${f.name}: ${f.deployedName}`;
outputSectionItems.push(`${f.name}: ${f.deployedName}`);
outputSectionItems.push(
`${f.name}: ${f.deployedName}${
f.artifactSize ? style.aside(` (${filesize(f.artifactSize)})`) : ''
}`
);
});
this.serverless.addServiceOutputSection('functions', outputSectionItems);
} else {
Expand Down
4 changes: 3 additions & 1 deletion lib/plugins/aws/info/getStackInfo.js
Expand Up @@ -69,9 +69,11 @@ module.exports = {

// Functions
this.serverless.service.getAllFunctions().forEach((func) => {
const functionObj = this.serverless.service.getFunction(func);
const functionInfo = {};
functionInfo.name = func;
functionInfo.deployedName = this.serverless.service.getFunction(func).name;
functionInfo.deployedName = functionObj.name;
functionInfo.artifactSize = functionObj.artifactSize;
this.gatheredData.info.functions.push(functionInfo);
});

Expand Down
2 changes: 2 additions & 0 deletions test/unit/lib/plugins/aws/info/getStackInfo.test.js
Expand Up @@ -94,10 +94,12 @@ describe('#getStackInfo()', () => {
{
name: 'hello',
deployedName: 'my-service-dev-hello',
artifactSize: undefined,
},
{
name: 'world',
deployedName: 'customized',
artifactSize: undefined,
},
],
layers: [
Expand Down

0 comments on commit 8746100

Please sign in to comment.