feat(AWS Deploy): --minify-template CLI param (#11980)

This commit is contained in:
Mieszko Kycermann 2023-05-24 16:53:41 +01:00 committed by GitHub
parent 6c9cdfb873
commit 4d64730130
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 41 deletions

View File

@ -32,6 +32,10 @@ commands.set('deploy', {
'Enforces new function version by overriding descriptions across all your functions. To be used only when migrating to new hashing algorithm.',
type: 'boolean',
},
'minify-template': {
usage: 'Minify the CloudFormation template',
type: 'boolean',
},
},
lifecycleEvents: ['deploy', 'finalize'],
});
@ -270,3 +274,15 @@ for (const schema of commands.values()) {
}
for (const [name, schema] of serviceCommands) commands.set(name, schema);
const packageCommandConfig = commands.get('package');
commands.set('package', {
...packageCommandConfig,
options: {
...packageCommandConfig.options,
'minify-template': {
usage: 'Minify the CloudFormation template for AWS packages',
type: 'boolean',
},
},
});

View File

@ -1,5 +1,6 @@
'use strict';
const fs = require('fs').promises;
const path = require('path');
module.exports = {
@ -12,9 +13,12 @@ module.exports = {
compiledTemplateFileName
);
this.serverless.utils.writeFileSync(
compiledTemplateFilePath,
this.serverless.service.provider.compiledCloudFormationTemplate
);
const shouldMinify = this.options['minify-template'];
const templateContents = this.serverless.service.provider.compiledCloudFormationTemplate;
const stringTemplateContents = JSON.stringify(templateContents, null, shouldMinify ? 0 : 2);
await fs.mkdir(path.dirname(compiledTemplateFilePath), { recursive: true });
await fs.writeFile(compiledTemplateFilePath, stringTemplateContents);
},
};

View File

@ -1,46 +1,47 @@
'use strict';
const expect = require('chai').expect;
const sinon = require('sinon');
const runServerless = require('../../../../../../utils/run-serverless');
const fs = require('fs').promises;
const path = require('path');
const AwsPackage = require('../../../../../../../lib/plugins/aws/package/index');
const Serverless = require('../../../../../../../lib/serverless');
const AwsProvider = require('../../../../../../../lib/plugins/aws/provider');
describe('#saveCompiledTemplate()', () => {
let serverless;
let awsPackage;
let getCompiledTemplateFileNameStub;
let writeFileSyncStub;
beforeEach(() => {
const options = {};
serverless = new Serverless({ commands: [], options: {} });
serverless.setProvider('aws', new AwsProvider(serverless, options));
awsPackage = new AwsPackage(serverless, options);
serverless.serviceDir = 'my-service';
serverless.service = {
provider: {
compiledCloudFormationTemplate: 'compiled content',
},
};
getCompiledTemplateFileNameStub = sinon
.stub(awsPackage.provider.naming, 'getCompiledTemplateFileName')
.returns('compiled.json');
writeFileSyncStub = sinon.stub(awsPackage.serverless.utils, 'writeFileSync').returns();
});
afterEach(() => {
awsPackage.provider.naming.getCompiledTemplateFileName.restore();
awsPackage.serverless.utils.writeFileSync.restore();
});
it('should write the compiled template to disk', async () => {
const filePath = path.join(awsPackage.serverless.serviceDir, '.serverless', 'compiled.json');
return awsPackage.saveCompiledTemplate().then(() => {
expect(getCompiledTemplateFileNameStub.calledOnce).to.equal(true);
expect(writeFileSyncStub.calledWithExactly(filePath, 'compiled content')).to.equal(true);
it('should save the compiled template to disk', async () => {
const result = await runServerless({
fixture: 'aws',
command: ['package'],
});
const cfTemplateFilePath = path.join(
result.fixtureData.servicePath,
'.serverless',
'cloudformation-template-update-stack.json'
);
const cfTemplateJsonOnDisk = await fs.readFile(cfTemplateFilePath, 'utf-8');
const cfTemplateOnDisk = JSON.parse(cfTemplateJsonOnDisk);
expect(cfTemplateOnDisk).to.deep.equal(result.cfTemplate);
});
it('should minify compiled template if --minify-template is set', async () => {
const result = await runServerless({
fixture: 'aws',
command: ['package'],
options: {
'minify-template': true,
},
});
const cfTemplateFilePath = path.join(
result.fixtureData.servicePath,
'.serverless',
'cloudformation-template-update-stack.json'
);
const cfTemplateJsonOnDisk = await fs.readFile(cfTemplateFilePath, 'utf-8');
const cfMinifiedTemplateJson = JSON.stringify(result.cfTemplate, null, 0);
expect(cfTemplateJsonOnDisk).to.equal(cfMinifiedTemplateJson);
});
});