Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit ca4ce9b

Browse files
refactor: replace hardcoded values of 'sls-next-build' with var
1 parent 4970af1 commit ca4ce9b

File tree

10 files changed

+58
-35
lines changed

10 files changed

+58
-35
lines changed

__tests__/index.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe("ServerlessNextJsPlugin", () => {
6464

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

8181
return plugin.buildNextPages().then(() => {
8282
expect(plugin.serverless.service.package.include).toContain(
83-
`${nextConfigDir}/sls-next-build/**`
83+
`${nextConfigDir}/${PluginBuildDir.BUILD_DIR_NAME}/**`
8484
);
8585
});
8686
});

classes/NextPage.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require("path");
22
const merge = require("lodash.merge");
33
const toPosix = require("../utils/pathToPosix");
4+
const PluginBuildDir = require("./PluginBuildDir");
45

56
class NextPage {
67
constructor(pagePath, serverlessFunctionOverrides) {
@@ -52,7 +53,9 @@ class NextPage {
5253
// sls-next-build/categories/fridge/index.js
5354
// app/sls-next-build/index.js
5455
const pathSegments = this.pagePath.split(path.sep);
55-
const buildDirIndex = pathSegments.indexOf("sls-next-build");
56+
const buildDirIndex = pathSegments.indexOf(
57+
PluginBuildDir.BUILD_DIR_NAME
58+
);
5659

5760
const routeSegments = pathSegments
5861
.slice(buildDirIndex + 1, pathSegments.length - 1)

classes/PluginBuildDir.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@ const path = require("path");
22
const fs = require("fs-extra");
33
const logger = require("../utils/logger");
44

5-
const BUILD_DIR_NAME = "sls-next-build";
6-
75
class PluginBuildDir {
86
constructor(nextConfigDir) {
97
this.nextConfigDir = nextConfigDir;
108
}
119

1210
get buildDir() {
13-
return path.join(this.nextConfigDir, BUILD_DIR_NAME);
11+
return path.join(this.nextConfigDir, PluginBuildDir.BUILD_DIR_NAME);
1412
}
1513

1614
get posixBuildDir() {
17-
return path.posix.join(this.nextConfigDir, BUILD_DIR_NAME);
15+
return path.posix.join(this.nextConfigDir, PluginBuildDir.BUILD_DIR_NAME);
16+
}
17+
18+
static get BUILD_DIR_NAME() {
19+
return "sls-next-build";
1820
}
1921

2022
setupBuildDir() {

classes/__tests__/NextPage.test.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
const path = require("path");
22
const NextPage = require("../NextPage");
3+
const PluginBuildDir = require("../PluginBuildDir");
34

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

1011
expect(page.pagePath).toEqual(pagePath);
1112
});
1213
});
1314

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

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

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

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

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

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

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

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

9293
it("should return posix pageHandler", () => {
93-
expect(page.pageHandler).toEqual("sls-next-build/admin.render");
94+
expect(page.pageHandler).toEqual(
95+
`${PluginBuildDir.BUILD_DIR_NAME}/admin.render`
96+
);
9497
});
9598
});
9699

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

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

106109
it("should return pageHandler", () => {
107-
expect(page.pageHandler).toEqual("app/sls-next-build/admin.render");
110+
expect(page.pageHandler).toEqual(
111+
`app/${PluginBuildDir.BUILD_DIR_NAME}/admin.render`
112+
);
108113
});
109114

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

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

@@ -142,7 +147,9 @@ describe("NextPage", () => {
142147
});
143148

144149
it("should return pageHandler", () => {
145-
expect(page.pageHandler).toEqual("sls-next-build/admin.render");
150+
expect(page.pageHandler).toEqual(
151+
`${PluginBuildDir.BUILD_DIR_NAME}/admin.render`
152+
);
146153
});
147154

148155
it("should return pageFunctionName", () => {

classes/__tests__/PluginBuildDir.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ describe("PluginBuildDir", () => {
2727

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

3434
it("should have posixBuildDir regardless the platform", () => {
3535
expect(pluginBuildDir.posixBuildDir).toEqual(
36-
"path/to/nextApp/sls-next-build"
36+
`path/to/nextApp/${PluginBuildDir.BUILD_DIR_NAME}`
3737
);
3838
});
3939
});

integration/__tests__/package.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ const path = require("path");
22
const AdmZip = require("adm-zip");
33
const fs = require("fs");
44
const packageTestService = require("../../utils/test/packageTestService");
5+
const PluginBuildDir = require("../../classes/PluginBuildDir");
56

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

1011
describe.each`
1112
appDir | appBuildDir
12-
${"../basic-app"} | ${"sls-next-build"}
13-
${"../basic-app-with-nested-next-config"} | ${"app/sls-next-build"}
13+
${"../basic-app"} | ${PluginBuildDir.BUILD_DIR_NAME}
14+
${"../basic-app-with-nested-next-config"} | ${`app/${PluginBuildDir.BUILD_DIR_NAME}`}
1415
`("$appDir - package tests", ({ appDir, appBuildDir }) => {
1516
const appServerlessDir = `${appDir}/.serverless`;
1617

lib/__tests__/copyBuildFiles.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const path = require("path");
22
const fse = require("fs-extra");
33
const logger = require("../../utils/logger");
44
const copyBuildFiles = require("../copyBuildFiles");
5+
const PluginBuildDir = require("../../classes/PluginBuildDir");
56

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

1415
describe("when page files are copied correctly", () => {
1516
let pluginBuildDirObj;
16-
const pluginBuildDir = path.normalize("path/to/sls-next-build");
17+
const pluginBuildDir = path.normalize(
18+
`path/to/${PluginBuildDir.BUILD_DIR_NAME}`
19+
);
1720
const nextBuildDir = path.normalize("path/to/.next");
1821

1922
beforeEach(() => {

lib/__tests__/getCompatLayerCode.test.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
11
const path = require("path");
22
const getCompatLayerCode = require("../getCompatLayerCode");
3+
const PluginBuildDir = require("../../classes/PluginBuildDir");
34

45
describe("getCompatLayerCode", () => {
56
it("should require compatLayer", () => {
67
const compatHandlerContent = getCompatLayerCode(
7-
path.join("sls-next-build", "my-page.js")
8+
path.join(PluginBuildDir.BUILD_DIR_NAME, "my-page.js")
89
);
910
expect(compatHandlerContent).toContain('require("./compatLayer")');
1011
});
1112

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

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

2627
it("should require next page provided", () => {
2728
const compatHandlerContent = getCompatLayerCode(
28-
path.join("sls-next-build", "my-page.js")
29+
path.join(PluginBuildDir.BUILD_DIR_NAME, "my-page.js")
2930
);
3031
expect(compatHandlerContent).toContain('require("./my-page.original.js")');
3132
});
3233

3334
it("should export render method", () => {
3435
const compatHandlerContent = getCompatLayerCode(
35-
path.join("sls-next-build", "my-page.js")
36+
path.join(PluginBuildDir.BUILD_DIR_NAME, "my-page.js")
3637
);
3738
expect(compatHandlerContent).toContain("module.exports.render");
3839
});

lib/__tests__/getNextPagesFromBuildDir.test.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const stream = require("stream");
44
const path = require("path");
55
const getNextPagesFromBuildDir = require("../getNextPagesFromBuildDir");
66
const logger = require("../../utils/logger");
7+
const PluginBuildDir = require("../../classes/PluginBuildDir");
78

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

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

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

45-
const buildDir = "sls-next-build";
46+
const buildDir = PluginBuildDir.BUILD_DIR_NAME;
4647
const resolvedBuildDir = path.resolve(buildDir);
4748

4849
const promise = getNextPagesFromBuildDir(buildDir).then(nextPages => {
@@ -75,7 +76,9 @@ describe("getNextPagesFromBuildDir", () => {
7576
about: aboutPageConfigOverride
7677
};
7778

78-
const buildDir = path.normalize("/path/to/sls-next-build");
79+
const buildDir = path.normalize(
80+
`/path/to/${PluginBuildDir.BUILD_DIR_NAME}`
81+
);
7982

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

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

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

139-
const buildDir = path.normalize("/path/to/sls-next-build");
142+
const buildDir = path.normalize(
143+
`/path/to/${PluginBuildDir.BUILD_DIR_NAME}`
144+
);
140145

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

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

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

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

lib/getCompatLayerCode.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const path = require("path");
2+
const PluginBuildDir = require("../classes/PluginBuildDir");
23

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

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

0 commit comments

Comments
 (0)