diff --git a/__tests__/index.test.js b/__tests__/index.test.js index 6057715cda..014ccc0fc3 100644 --- a/__tests__/index.test.js +++ b/__tests__/index.test.js @@ -41,7 +41,7 @@ describe("ServerlessNextJsPlugin", () => { ${"before:offline:start"} | ${"buildNextPages"} ${"before:package:initialize"} | ${"buildNextPages"} ${"before:deploy:function:initialize"} | ${"buildNextPages"} - ${"before:package:createDeploymentArtifacts"} | ${"addStaticAssetsBucket"} + ${"before:package:createDeploymentArtifacts"} | ${"addAssetsBucketForDeployment"} ${"after:aws:deploy:deploy:uploadArtifacts"} | ${"uploadStaticAssets"} ${"after:aws:info:displayStackOutputs"} | ${"printStackOutput"} ${"after:package:createDeploymentArtifacts"} | ${"removePluginBuildDir"} @@ -162,110 +162,6 @@ describe("ServerlessNextJsPlugin", () => { }); }); - describe("#addStaticAssetsBucket", () => { - const mockCFWithBucket = { - Resources: { - NextStaticAssetsBucket: {} - } - }; - - beforeEach(() => { - addS3BucketToResources.mockResolvedValue(mockCFWithBucket); - }); - - it("should not call addS3BucketToResources if a staticAssetsBucket is not available", () => { - expect.assertions(1); - parseNextConfiguration.mockReturnValueOnce( - parsedNextConfigurationFactory({}, null) - ); - - const plugin = new ServerlessPluginBuilder().build(); - - return plugin.addStaticAssetsBucket().then(() => { - expect(addS3BucketToResources).not.toBeCalled(); - }); - }); - - it("should log when a bucket is going to be provisioned from parsed assetPrefix", () => { - expect.assertions(1); - - parseNextConfiguration.mockReturnValueOnce( - parsedNextConfigurationFactory() - ); - - const plugin = new ServerlessPluginBuilder().build(); - - return plugin.addStaticAssetsBucket().then(() => { - expect(logger.log).toBeCalledWith( - expect.stringContaining(`Found bucket "my-bucket"`) - ); - }); - }); - - it("should update coreCloudFormationTemplate with static assets bucket", () => { - expect.assertions(2); - - parseNextConfiguration.mockReturnValueOnce( - parsedNextConfigurationFactory() - ); - - const initialCoreCF = { - Resources: { bar: "baz" } - }; - - const plugin = new ServerlessPluginBuilder() - .withService({ - provider: { - coreCloudFormationTemplate: initialCoreCF - } - }) - .build(); - - return plugin.addStaticAssetsBucket().then(() => { - const { - coreCloudFormationTemplate - } = plugin.serverless.service.provider; - - expect(addS3BucketToResources).toBeCalledWith( - "my-bucket", - initialCoreCF - ); - expect(coreCloudFormationTemplate).toEqual(mockCFWithBucket); - }); - }); - - it("should update compiledCloudFormation with static assets bucket", () => { - expect.assertions(2); - - parseNextConfiguration.mockReturnValueOnce( - parsedNextConfigurationFactory() - ); - - const initialCompiledCF = { - Resources: { foo: "bar" } - }; - - const plugin = new ServerlessPluginBuilder() - .withService({ - provider: { - compiledCloudFormationTemplate: initialCompiledCF - } - }) - .build(); - - return plugin.addStaticAssetsBucket().then(() => { - const { - compiledCloudFormationTemplate - } = plugin.serverless.service.provider; - expect(addS3BucketToResources).toBeCalledWith( - "my-bucket", - initialCompiledCF - ); - expect(compiledCloudFormationTemplate).toEqual(mockCFWithBucket); - }); - }); - }); - describe("#uploadStaticAssets", () => { it("should NOT call uploadStaticAssetsToS3 when there isn't a bucket available", () => { parseNextConfiguration.mockReturnValueOnce( diff --git a/index.js b/index.js index 3576ebb10d..20cdc5c46c 100644 --- a/index.js +++ b/index.js @@ -8,6 +8,7 @@ const parseNextConfiguration = require("./lib/parseNextConfiguration"); const build = require("./lib/build"); const logger = require("./utils/logger"); const PluginBuildDir = require("./classes/PluginBuildDir"); +const addAssetsBucketForDeployment = require("./lib/addAssetsBucketForDeployment"); class ServerlessNextJsPlugin { constructor(serverless, options) { @@ -19,7 +20,7 @@ class ServerlessNextJsPlugin { this.providerRequest = this.provider.request.bind(this.provider); this.pluginBuildDir = new PluginBuildDir(this.nextConfigDir); - this.addStaticAssetsBucket = this.addStaticAssetsBucket.bind(this); + this.addAssetsBucketForDeployment = addAssetsBucketForDeployment.bind(this); this.uploadStaticAssets = this.uploadStaticAssets.bind(this); this.printStackOutput = this.printStackOutput.bind(this); this.buildNextPages = this.buildNextPages.bind(this); @@ -29,7 +30,8 @@ class ServerlessNextJsPlugin { "before:offline:start": this.buildNextPages, "before:package:initialize": this.buildNextPages, "before:deploy:function:initialize": this.buildNextPages, - "before:package:createDeploymentArtifacts": this.addStaticAssetsBucket, + "before:package:createDeploymentArtifacts": this + .addAssetsBucketForDeployment, "after:package:createDeploymentArtifacts": this.removePluginBuildDir, "after:aws:deploy:deploy:uploadArtifacts": this.uploadStaticAssets, "after:aws:info:displayStackOutputs": this.printStackOutput @@ -72,37 +74,6 @@ class ServerlessNextJsPlugin { this.serverless.service.setFunctionNames(); } - getCFTemplatesWithBucket(staticAssetsBucket) { - return Promise.all([ - addS3BucketToResources( - staticAssetsBucket, - this.serverless.service.provider.compiledCloudFormationTemplate - ), - addS3BucketToResources( - staticAssetsBucket, - this.serverless.service.provider.coreCloudFormationTemplate - ) - ]); - } - - async addStaticAssetsBucket() { - const { staticAssetsBucket } = this.configuration; - - if (!staticAssetsBucket) { - return; - } - - logger.log(`Found bucket "${staticAssetsBucket}" in assetPrefix!`); - - const [ - compiledCfWithBucket, - coreCfWithBucket - ] = await this.getCFTemplatesWithBucket(staticAssetsBucket); - - this.serverless.service.provider.compiledCloudFormationTemplate = compiledCfWithBucket; - this.serverless.service.provider.coreCloudFormationTemplate = coreCfWithBucket; - } - uploadStaticAssets() { const { nextConfiguration, staticAssetsBucket } = this.configuration; diff --git a/lib/__tests__/addAssetsBucketForDeployment.test.js b/lib/__tests__/addAssetsBucketForDeployment.test.js new file mode 100644 index 0000000000..5348ee41cb --- /dev/null +++ b/lib/__tests__/addAssetsBucketForDeployment.test.js @@ -0,0 +1,132 @@ +const addS3BucketToResources = require("../addS3BucketToResources"); +const parseNextConfiguration = require("../parseNextConfiguration"); +const parsedNextConfigurationFactory = require("../../utils/test/parsedNextConfigurationFactory"); +const ServerlessPluginBuilder = require("../../utils/test/ServerlessPluginBuilder"); +const addAssetsBucketForDeployment = require("../addAssetsBucketForDeployment"); +const logger = require("../../utils/logger"); + +jest.mock("../addS3BucketToResources"); +jest.mock("../parseNextConfiguration"); +jest.mock("../../utils/logger"); + +describe("addAssetsBucketForDeployment", () => { + const mockCFWithBucket = { + Resources: { + NextStaticAssetsBucket: {} + } + }; + + let plugin; + + beforeEach(() => { + plugin = new ServerlessPluginBuilder().build(); + addS3BucketToResources.mockResolvedValue(mockCFWithBucket); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it("should not call addS3BucketToResources if a staticAssetsBucket is not available", () => { + expect.assertions(1); + parseNextConfiguration.mockReturnValueOnce( + parsedNextConfigurationFactory({}, null) + ); + + return addAssetsBucketForDeployment.call(plugin).then(() => { + expect(addS3BucketToResources).not.toBeCalled(); + }); + }); + + it("should call parseNextConfiguration with nextConfigDir", () => { + expect.assertions(1); + + const nextConfigDir = "./"; + + plugin = new ServerlessPluginBuilder() + .withNextCustomConfig({ + nextConfigDir + }) + .build(); + + parseNextConfiguration.mockReturnValueOnce( + parsedNextConfigurationFactory() + ); + + return addAssetsBucketForDeployment.call(plugin).then(() => { + expect(parseNextConfiguration).toBeCalledWith(nextConfigDir); + }); + }); + + it("should log when a bucket is going to be provisioned from parsed assetPrefix", () => { + expect.assertions(1); + + parseNextConfiguration.mockReturnValueOnce( + parsedNextConfigurationFactory() + ); + + return addAssetsBucketForDeployment.call(plugin).then(() => { + expect(logger.log).toBeCalledWith( + expect.stringContaining(`Found bucket "my-bucket"`) + ); + }); + }); + + it("should update coreCloudFormationTemplate with static assets bucket", () => { + expect.assertions(2); + + parseNextConfiguration.mockReturnValueOnce( + parsedNextConfigurationFactory() + ); + + const initialCoreCF = { + Resources: { bar: "baz" } + }; + + plugin = new ServerlessPluginBuilder() + .withService({ + provider: { + coreCloudFormationTemplate: initialCoreCF + } + }) + .build(); + + return addAssetsBucketForDeployment.call(plugin).then(() => { + const { coreCloudFormationTemplate } = plugin.serverless.service.provider; + + expect(addS3BucketToResources).toBeCalledWith("my-bucket", initialCoreCF); + expect(coreCloudFormationTemplate).toEqual(mockCFWithBucket); + }); + }); + + it("should update compiledCloudFormation with static assets bucket", () => { + expect.assertions(2); + + parseNextConfiguration.mockReturnValueOnce( + parsedNextConfigurationFactory() + ); + + const initialCompiledCF = { + Resources: { foo: "bar" } + }; + + const plugin = new ServerlessPluginBuilder() + .withService({ + provider: { + compiledCloudFormationTemplate: initialCompiledCF + } + }) + .build(); + + return addAssetsBucketForDeployment.call(plugin).then(() => { + const { + compiledCloudFormationTemplate + } = plugin.serverless.service.provider; + expect(addS3BucketToResources).toBeCalledWith( + "my-bucket", + initialCompiledCF + ); + expect(compiledCloudFormationTemplate).toEqual(mockCFWithBucket); + }); + }); +}); diff --git a/lib/addAssetsBucketForDeployment.js b/lib/addAssetsBucketForDeployment.js new file mode 100644 index 0000000000..5312d13dba --- /dev/null +++ b/lib/addAssetsBucketForDeployment.js @@ -0,0 +1,38 @@ +const parseNextConfiguration = require("./parseNextConfiguration"); +const logger = require("../utils/logger"); +const addS3BucketToResources = require("./addS3BucketToResources"); + +const getCFTemplatesWithBucket = async function(bucketName) { + return Promise.all([ + addS3BucketToResources( + bucketName, + this.serverless.service.provider.compiledCloudFormationTemplate + ), + addS3BucketToResources( + bucketName, + this.serverless.service.provider.coreCloudFormationTemplate + ) + ]); +}; + +module.exports = async function() { + const { staticAssetsBucket } = parseNextConfiguration( + this.getPluginConfigValue("nextConfigDir") + ); + + if (!staticAssetsBucket) { + return; + } + + logger.log(`Found bucket "${staticAssetsBucket}" in assetPrefix!`); + + const [ + compiledCfWithBucket, + coreCfWithBucket + ] = await getCFTemplatesWithBucket.call(this, staticAssetsBucket); + + this.serverless.service.provider.compiledCloudFormationTemplate = compiledCfWithBucket; + this.serverless.service.provider.coreCloudFormationTemplate = coreCfWithBucket; + + return Promise.resolve(); +};