Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit b8a2aa7

Browse files
authored
Set default ring in spk (#366)
* Set default ring * Add function to set default ring * Updates based on feedback * Remove default ring property * Update based on feedback Co-authored-by: Michael Tarng <20913254+mtarng@users.noreply.github.com>
1 parent 033037f commit b8a2aa7

File tree

4 files changed

+81
-9
lines changed

4 files changed

+81
-9
lines changed

src/commands/ring/set-default.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ describe("test execute function and logic", () => {
3939
rings: {
4040
master: {
4141
isDefault: true
42-
}
42+
},
43+
prod: {}
4344
},
4445
services: {},
4546
variableGroups: ["testvg"]
4647
});
47-
await execute("ring", tmpDir, exitFn);
48+
await execute("prod", tmpDir, exitFn);
4849

4950
expect(exitFn).toBeCalledTimes(1);
5051
expect(exitFn.mock.calls).toEqual([[0]]);

src/commands/ring/set-default.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import commander from "commander";
2-
import { fileInfo as bedrockFileInfo } from "../../lib/bedrockYaml";
2+
import {
3+
fileInfo as bedrockFileInfo,
4+
read as loadBedrockFile,
5+
setDefaultRing
6+
} from "../../lib/bedrockYaml";
37
import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder";
48
import { PROJECT_INIT_DEPENDENCY_ERROR_MESSAGE } from "../../lib/constants";
59
import { hasValue } from "../../lib/validator";
610
import { logger } from "../../logger";
7-
import { IBedrockFileInfo } from "../../types";
8-
11+
import { IBedrockFile, IBedrockFileInfo } from "../../types";
912
import decorator from "./set-default.decorator.json";
1013

1114
/**
@@ -29,9 +32,9 @@ export const execute = async (
2932

3033
checkDependencies(projectPath);
3134

32-
// Check if ring exists in bedrock.yaml, if not, warn and exit.
33-
// Check if ring is already default, if so, warn and exit.
34-
// Set ring as default in bedrock.yaml
35+
// Get bedrock.yaml
36+
const bedrockFile = loadBedrockFile(projectPath);
37+
setDefaultRing(bedrockFile, ringName, projectPath);
3538

3639
logger.info(`Successfully set default ring: ${ringName} for this project!`);
3740
await exitFn(0);

src/lib/bedrockYaml.test.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
DEFAULT_CONTENT,
1010
fileInfo,
1111
isExists,
12-
read
12+
read,
13+
setDefaultRing
1314
} from "./bedrockYaml";
1415

1516
describe("Creation and Existence test on bedrock.yaml", () => {
@@ -156,3 +157,38 @@ describe("Bedrock file info", () => {
156157
expect(file.hasVariableGroups).toEqual(true);
157158
});
158159
});
160+
161+
describe("Set default ring", () => {
162+
it("Should set the default ring", () => {
163+
const dir = createTempDir();
164+
const data = {
165+
rings: {
166+
master: { isDefault: false },
167+
prod: {}
168+
},
169+
services: {},
170+
variableGroups: [uuid()]
171+
};
172+
create(dir, data);
173+
setDefaultRing(data, "master", dir);
174+
const result = read(dir);
175+
expect(result.rings.master.isDefault).toBe(true);
176+
expect(result.rings.prod.isDefault).toBe(undefined);
177+
});
178+
it("Should change the default ring", () => {
179+
const dir = createTempDir();
180+
const data = {
181+
rings: {
182+
master: { isDefault: false },
183+
prod: { isDefault: true }
184+
},
185+
services: {},
186+
variableGroups: [uuid()]
187+
};
188+
create(dir, data);
189+
setDefaultRing(data, "master", dir);
190+
const result = read(dir);
191+
expect(result.rings.master.isDefault).toBe(true);
192+
expect(result.rings.prod.isDefault).toBe(undefined);
193+
});
194+
});

src/lib/bedrockYaml.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,38 @@ export const addNewService = (
105105
fs.writeFileSync(path.join(absPath, YAML_NAME), asYaml);
106106
};
107107

108+
/**
109+
* Sets the default ring in bedrock.yaml
110+
* @param bedrockFile The bedrock.yaml file
111+
* @param ringName The name of the ring
112+
*/
113+
export const setDefaultRing = (
114+
bedrockFile: IBedrockFile,
115+
ringName: string,
116+
dir: string
117+
): void => {
118+
const rings = Object.keys(bedrockFile.rings);
119+
if (!rings.includes(ringName)) {
120+
throw new Error(`The ring '${ringName}' is not defined in ${YAML_NAME}`);
121+
}
122+
123+
for (const [name, value] of Object.entries(bedrockFile.rings)) {
124+
if (value === null) {
125+
bedrockFile.rings[name] = {};
126+
}
127+
const ring = bedrockFile.rings[name];
128+
129+
if (name === ringName) {
130+
ring.isDefault = true;
131+
} else {
132+
if (typeof ring.isDefault !== "undefined") {
133+
delete ring.isDefault;
134+
}
135+
}
136+
}
137+
138+
create(dir, bedrockFile);
139+
};
108140
/**
109141
* Update bedrock.yaml with new ring
110142
*

0 commit comments

Comments
 (0)