Skip to content

Commit

Permalink
refactor: move assets bucket provisioning to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcondemarin committed Mar 24, 2019
1 parent 7059534 commit 7b7f9f3
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 138 deletions.
106 changes: 1 addition & 105 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down Expand Up @@ -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(
Expand Down
37 changes: 4 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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;

Expand Down
132 changes: 132 additions & 0 deletions lib/__tests__/addAssetsBucketForDeployment.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
38 changes: 38 additions & 0 deletions lib/addAssetsBucketForDeployment.js
Original file line number Diff line number Diff line change
@@ -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();
};

0 comments on commit 7b7f9f3

Please sign in to comment.