Skip to content

Commit 0498aa7

Browse files
Add check for msbridge config files (#26)
1 parent 4553799 commit 0498aa7

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

lib/__tests__/msbridge.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
jest.mock("fs");
2+
const { readdirSync } = require("fs");
3+
const { FatalError } = require("../errors");
4+
const hasMSBridgeConfig = require("../checks/msbridge");
5+
6+
describe("Fail if yarn.lock exists", () => {
7+
it("should fail for local config", () => {
8+
jest
9+
.mocked(readdirSync)
10+
.mockImplementationOnce(() => [
11+
"index.js",
12+
".msbridge.local.json",
13+
"package.json",
14+
]);
15+
expect(hasMSBridgeConfig()).toBeInstanceOf(FatalError);
16+
});
17+
it("should fail for amplify config", () => {
18+
jest
19+
.mocked(readdirSync)
20+
.mockImplementationOnce(() => [
21+
"index.js",
22+
".msbridge.amplify.json",
23+
"package.json",
24+
]);
25+
expect(hasMSBridgeConfig()).toBeInstanceOf(FatalError);
26+
});
27+
it("should fail for standard config", () => {
28+
jest
29+
.mocked(readdirSync)
30+
.mockImplementationOnce(() => [
31+
"index.js",
32+
".msbridge.json",
33+
"package.json",
34+
]);
35+
expect(hasMSBridgeConfig()).toBeInstanceOf(FatalError);
36+
});
37+
it("should not fail when no config", () => {
38+
jest
39+
.mocked(readdirSync)
40+
.mockImplementationOnce(() => ["index.js", "package.json"]);
41+
expect(hasMSBridgeConfig()).toBeUndefined();
42+
});
43+
});

lib/checks/msbridge.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { readdirSync } = require("fs");
2+
const { FatalError } = require("../errors");
3+
4+
function hasMSBridgeConfig() {
5+
try {
6+
const allRootFiles = readdirSync(".");
7+
for (const file of allRootFiles) {
8+
if (file.startsWith(".msbridge") && file.endsWith(".json")) {
9+
return new FatalError("Unexpected " + file + " file detected");
10+
}
11+
}
12+
} catch (e) {
13+
return undefined;
14+
}
15+
}
16+
17+
module.exports = hasMSBridgeConfig;

0 commit comments

Comments
 (0)