Skip to content

Commit

Permalink
refactor: replace hardcoded values of 'sls-next-build' with var
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcondemarin committed Apr 3, 2019
1 parent 4970af1 commit ca4ce9b
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 35 deletions.
4 changes: 2 additions & 2 deletions __tests__/index.test.js
Expand Up @@ -64,7 +64,7 @@ describe("ServerlessNextJsPlugin", () => {

return plugin.buildNextPages().then(() => {
expect(plugin.serverless.service.package.include).toContain(
`${nextConfigDir}/sls-next-build/**`
`${nextConfigDir}/${PluginBuildDir.BUILD_DIR_NAME}/**`
);
});
});
Expand All @@ -80,7 +80,7 @@ describe("ServerlessNextJsPlugin", () => {

return plugin.buildNextPages().then(() => {
expect(plugin.serverless.service.package.include).toContain(
`${nextConfigDir}/sls-next-build/**`
`${nextConfigDir}/${PluginBuildDir.BUILD_DIR_NAME}/**`
);
});
});
Expand Down
5 changes: 4 additions & 1 deletion classes/NextPage.js
@@ -1,6 +1,7 @@
const path = require("path");
const merge = require("lodash.merge");
const toPosix = require("../utils/pathToPosix");
const PluginBuildDir = require("./PluginBuildDir");

class NextPage {
constructor(pagePath, serverlessFunctionOverrides) {
Expand Down Expand Up @@ -52,7 +53,9 @@ class NextPage {
// sls-next-build/categories/fridge/index.js
// app/sls-next-build/index.js
const pathSegments = this.pagePath.split(path.sep);
const buildDirIndex = pathSegments.indexOf("sls-next-build");
const buildDirIndex = pathSegments.indexOf(
PluginBuildDir.BUILD_DIR_NAME
);

const routeSegments = pathSegments
.slice(buildDirIndex + 1, pathSegments.length - 1)
Expand Down
10 changes: 6 additions & 4 deletions classes/PluginBuildDir.js
Expand Up @@ -2,19 +2,21 @@ const path = require("path");
const fs = require("fs-extra");
const logger = require("../utils/logger");

const BUILD_DIR_NAME = "sls-next-build";

class PluginBuildDir {
constructor(nextConfigDir) {
this.nextConfigDir = nextConfigDir;
}

get buildDir() {
return path.join(this.nextConfigDir, BUILD_DIR_NAME);
return path.join(this.nextConfigDir, PluginBuildDir.BUILD_DIR_NAME);
}

get posixBuildDir() {
return path.posix.join(this.nextConfigDir, BUILD_DIR_NAME);
return path.posix.join(this.nextConfigDir, PluginBuildDir.BUILD_DIR_NAME);
}

static get BUILD_DIR_NAME() {
return "sls-next-build";
}

setupBuildDir() {
Expand Down
27 changes: 17 additions & 10 deletions classes/__tests__/NextPage.test.js
@@ -1,18 +1,19 @@
const path = require("path");
const NextPage = require("../NextPage");
const PluginBuildDir = require("../PluginBuildDir");

describe("NextPage", () => {
describe("#constructor", () => {
it("should set a pagePath", () => {
const pagePath = "sls-next-build/home.js";
const pagePath = `${PluginBuildDir.BUILD_DIR_NAME}/home.js`;
const page = new NextPage(pagePath);

expect(page.pagePath).toEqual(pagePath);
});
});

describe("When is the index page", () => {
const buildDir = "sls-next-build";
const buildDir = PluginBuildDir.BUILD_DIR_NAME;
const pagePath = path.join(buildDir, "index.js");
let page;

Expand All @@ -33,7 +34,7 @@ describe("NextPage", () => {
});

describe("When is the _error page", () => {
const buildDir = "sls-next-build";
const buildDir = PluginBuildDir.BUILD_DIR_NAME;
const pagePath = path.join(buildDir, "_error.js");
let page;

Expand All @@ -58,7 +59,7 @@ describe("NextPage", () => {
});

describe("When is a nested page", () => {
const buildDir = "sls-next-build";
const buildDir = PluginBuildDir.BUILD_DIR_NAME;
const pagePath = path.join(buildDir, "categories/fridge/fridges.js");
let page;

Expand All @@ -81,7 +82,7 @@ describe("NextPage", () => {
});

describe("When pagePath has win format", () => {
const buildDir = "sls-next-build";
const buildDir = PluginBuildDir.BUILD_DIR_NAME;
const pagePath = `${buildDir}\\admin.js`;
let page;

Expand All @@ -90,12 +91,14 @@ describe("NextPage", () => {
});

it("should return posix pageHandler", () => {
expect(page.pageHandler).toEqual("sls-next-build/admin.render");
expect(page.pageHandler).toEqual(
`${PluginBuildDir.BUILD_DIR_NAME}/admin.render`
);
});
});

describe("When the build directory is a subdirectory", () => {
const buildDir = path.join("app", "sls-next-build");
const buildDir = path.join("app", PluginBuildDir.BUILD_DIR_NAME);
const pagePath = path.join(buildDir, "admin.js");
let page;

Expand All @@ -104,7 +107,9 @@ describe("NextPage", () => {
});

it("should return pageHandler", () => {
expect(page.pageHandler).toEqual("app/sls-next-build/admin.render");
expect(page.pageHandler).toEqual(
`app/${PluginBuildDir.BUILD_DIR_NAME}/admin.render`
);
});

it("should return pageRoute", () => {
Expand All @@ -113,7 +118,7 @@ describe("NextPage", () => {
});

describe("When a new instance is created", () => {
const buildDir = "sls-next-build";
const buildDir = PluginBuildDir.BUILD_DIR_NAME;
const pagePath = `${buildDir}/admin.js`;
let page;

Expand Down Expand Up @@ -142,7 +147,9 @@ describe("NextPage", () => {
});

it("should return pageHandler", () => {
expect(page.pageHandler).toEqual("sls-next-build/admin.render");
expect(page.pageHandler).toEqual(
`${PluginBuildDir.BUILD_DIR_NAME}/admin.render`
);
});

it("should return pageFunctionName", () => {
Expand Down
4 changes: 2 additions & 2 deletions classes/__tests__/PluginBuildDir.test.js
Expand Up @@ -27,13 +27,13 @@ describe("PluginBuildDir", () => {

it("should have buildDir at same level as next config.", () => {
expect(pluginBuildDir.buildDir).toEqual(
path.join(nextConfigDir, "sls-next-build")
path.join(nextConfigDir, PluginBuildDir.BUILD_DIR_NAME)
);
});

it("should have posixBuildDir regardless the platform", () => {
expect(pluginBuildDir.posixBuildDir).toEqual(
"path/to/nextApp/sls-next-build"
`path/to/nextApp/${PluginBuildDir.BUILD_DIR_NAME}`
);
});
});
Expand Down
5 changes: 3 additions & 2 deletions integration/__tests__/package.test.js
Expand Up @@ -2,15 +2,16 @@ const path = require("path");
const AdmZip = require("adm-zip");
const fs = require("fs");
const packageTestService = require("../../utils/test/packageTestService");
const PluginBuildDir = require("../../classes/PluginBuildDir");

const readJsonFile = filePath => {
return JSON.parse(fs.readFileSync(filePath, "utf-8"));
};

describe.each`
appDir | appBuildDir
${"../basic-app"} | ${"sls-next-build"}
${"../basic-app-with-nested-next-config"} | ${"app/sls-next-build"}
${"../basic-app"} | ${PluginBuildDir.BUILD_DIR_NAME}
${"../basic-app-with-nested-next-config"} | ${`app/${PluginBuildDir.BUILD_DIR_NAME}`}
`("$appDir - package tests", ({ appDir, appBuildDir }) => {
const appServerlessDir = `${appDir}/.serverless`;

Expand Down
5 changes: 4 additions & 1 deletion lib/__tests__/copyBuildFiles.test.js
Expand Up @@ -2,6 +2,7 @@ const path = require("path");
const fse = require("fs-extra");
const logger = require("../../utils/logger");
const copyBuildFiles = require("../copyBuildFiles");
const PluginBuildDir = require("../../classes/PluginBuildDir");

jest.mock("fs-extra");
jest.mock("../../utils/logger");
Expand All @@ -13,7 +14,9 @@ describe("copyBuildFiles", () => {

describe("when page files are copied correctly", () => {
let pluginBuildDirObj;
const pluginBuildDir = path.normalize("path/to/sls-next-build");
const pluginBuildDir = path.normalize(
`path/to/${PluginBuildDir.BUILD_DIR_NAME}`
);
const nextBuildDir = path.normalize("path/to/.next");

beforeEach(() => {
Expand Down
11 changes: 6 additions & 5 deletions lib/__tests__/getCompatLayerCode.test.js
@@ -1,38 +1,39 @@
const path = require("path");
const getCompatLayerCode = require("../getCompatLayerCode");
const PluginBuildDir = require("../../classes/PluginBuildDir");

describe("getCompatLayerCode", () => {
it("should require compatLayer", () => {
const compatHandlerContent = getCompatLayerCode(
path.join("sls-next-build", "my-page.js")
path.join(PluginBuildDir.BUILD_DIR_NAME, "my-page.js")
);
expect(compatHandlerContent).toContain('require("./compatLayer")');
});

it("should require compatLayer with correct path when page is nested", () => {
const compatHandlerContent = getCompatLayerCode(
path.join("sls-next-build", "categories/fridge/fridges.js")
path.join(PluginBuildDir.BUILD_DIR_NAME, "categories/fridge/fridges.js")
);
expect(compatHandlerContent).toContain('require("../../compatLayer")');
});

it("should require compatLayer with correct path when buildDir is nested", () => {
const compatHandlerContent = getCompatLayerCode(
path.join("app/sls-next-build", "page.js")
path.join(`app/${PluginBuildDir.BUILD_DIR_NAME}`, "page.js")
);
expect(compatHandlerContent).toContain('require("./compatLayer")');
});

it("should require next page provided", () => {
const compatHandlerContent = getCompatLayerCode(
path.join("sls-next-build", "my-page.js")
path.join(PluginBuildDir.BUILD_DIR_NAME, "my-page.js")
);
expect(compatHandlerContent).toContain('require("./my-page.original.js")');
});

it("should export render method", () => {
const compatHandlerContent = getCompatLayerCode(
path.join("sls-next-build", "my-page.js")
path.join(PluginBuildDir.BUILD_DIR_NAME, "my-page.js")
);
expect(compatHandlerContent).toContain("module.exports.render");
});
Expand Down
19 changes: 12 additions & 7 deletions lib/__tests__/getNextPagesFromBuildDir.test.js
Expand Up @@ -4,6 +4,7 @@ const stream = require("stream");
const path = require("path");
const getNextPagesFromBuildDir = require("../getNextPagesFromBuildDir");
const logger = require("../../utils/logger");
const PluginBuildDir = require("../../classes/PluginBuildDir");

jest.mock("fs");
jest.mock("klaw");
Expand All @@ -26,7 +27,7 @@ describe("getNextPagesFromBuildDir", () => {
it("should return an empty array when there are no pages", () => {
expect.assertions(1);

const buildDir = path.normalize("path/to/sls-next-build");
const buildDir = path.normalize(`path/to/${PluginBuildDir.BUILD_DIR_NAME}`);

const getPagesPromise = getNextPagesFromBuildDir(buildDir).then(
nextPages => {
Expand All @@ -42,7 +43,7 @@ describe("getNextPagesFromBuildDir", () => {
it("should return two next pages", () => {
expect.assertions(5);

const buildDir = "sls-next-build";
const buildDir = PluginBuildDir.BUILD_DIR_NAME;
const resolvedBuildDir = path.resolve(buildDir);

const promise = getNextPagesFromBuildDir(buildDir).then(nextPages => {
Expand Down Expand Up @@ -75,7 +76,9 @@ describe("getNextPagesFromBuildDir", () => {
about: aboutPageConfigOverride
};

const buildDir = path.normalize("/path/to/sls-next-build");
const buildDir = path.normalize(
`/path/to/${PluginBuildDir.BUILD_DIR_NAME}`
);

const promise = getNextPagesFromBuildDir(buildDir, pageConfig).then(
nextPages => {
Expand Down Expand Up @@ -113,7 +116,7 @@ describe("getNextPagesFromBuildDir", () => {
it("should skip _app and _document pages", () => {
expect.assertions(2);

const buildDir = path.normalize("./sls-next-build");
const buildDir = path.normalize(`./${PluginBuildDir.BUILD_DIR_NAME}`);
const resolvedBuildDir = path.resolve(buildDir);

const promise = getNextPagesFromBuildDir(buildDir).then(nextPages => {
Expand All @@ -136,7 +139,9 @@ describe("getNextPagesFromBuildDir", () => {
it("should skip compatLayer file", () => {
expect.assertions(2);

const buildDir = path.normalize("/path/to/sls-next-build");
const buildDir = path.normalize(
`/path/to/${PluginBuildDir.BUILD_DIR_NAME}`
);

const promise = getNextPagesFromBuildDir(buildDir).then(nextPages => {
expect(nextPages).toHaveLength(1);
Expand All @@ -153,7 +158,7 @@ describe("getNextPagesFromBuildDir", () => {
it("should handle nested pages", () => {
expect.assertions(5);

const buildDir = path.normalize("./sls-next-build");
const buildDir = path.normalize(`./${PluginBuildDir.BUILD_DIR_NAME}`);
const resolvedBuildDir = path.resolve(buildDir);

const promise = getNextPagesFromBuildDir(buildDir).then(nextPages => {
Expand Down Expand Up @@ -182,7 +187,7 @@ describe("getNextPagesFromBuildDir", () => {
it("should skip page directories", () => {
expect.assertions(1);

const buildDir = path.normalize("./sls-next-build");
const buildDir = path.normalize(`./${PluginBuildDir.BUILD_DIR_NAME}`);
const resolvedBuildDir = path.resolve(buildDir);
fs.lstatSync.mockReturnValue({ isDirectory: () => true });

Expand Down
3 changes: 2 additions & 1 deletion lib/getCompatLayerCode.js
@@ -1,4 +1,5 @@
const path = require("path");
const PluginBuildDir = require("../classes/PluginBuildDir");

const PAGE_BUNDLE_PATH = "/*page_bundle_path_placeholder*/";
const COMPAT_LAYER_PATH = "/*compat_layer_path_placeholder*/";
Expand All @@ -20,7 +21,7 @@ const relativePathToCompatLayerFile = jsHandlerPath => {
if (pathSegments.length > 2) {
// this is a nested page in the build directory. e.g. sls-next-build/categories/uno/dos.js
// compatLayer is in sls-next-build/compatLayer.js
const buildDirIndex = pathSegments.indexOf("sls-next-build");
const buildDirIndex = pathSegments.indexOf(PluginBuildDir.BUILD_DIR_NAME);
const noLevelsToBuildDir = pathSegments.length - buildDirIndex;

for (let index = 0; index < noLevelsToBuildDir - 2; index++) {
Expand Down

0 comments on commit ca4ce9b

Please sign in to comment.