Skip to content

Commit f261606

Browse files
authored
Update catalog handling (#385)
1 parent 4150c0a commit f261606

File tree

16 files changed

+500
-240
lines changed

16 files changed

+500
-240
lines changed

.vscode/settings.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@
1515
"*.json5": "jsonc",
1616
"api-extractor*.json": "jsonc"
1717
},
18-
"typescript.tsdk": "node_modules/typescript/lib"
18+
"typescript.tsdk": "node_modules/typescript/lib",
19+
"jest.runMode": "on-demand",
20+
"jest.jestCommandLine": "yarn test",
21+
// For now, only support tests in workspace-tools package
22+
"jest.rootPath": "packages/workspace-tools"
1923
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"type": "patch",
5+
"comment": "Update handling of \"catalog:\" vs \"catalog:default\" and add catalogsToYaml utility",
6+
"packageName": "workspace-tools",
7+
"email": "[email protected]",
8+
"dependentChangeType": "patch"
9+
}
10+
]
11+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"lage": "^2.6.2",
4242
"prettier": "^3.0.0",
4343
"tmp": "^0.2.1",
44+
"ts-dedent": "^2.2.0",
4445
"ts-jest": "^29.1.0",
4546
"typedoc": "^0.25.2",
4647
"typescript": "~5.2.2"

packages/workspace-tools/etc/workspace-tools.api.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ export interface Catalogs {
3737
named?: NamedCatalogs;
3838
}
3939

40+
// @public
41+
export function catalogsToYaml(catalogs: Catalogs, options?: {
42+
indent?: number | string;
43+
}): string;
44+
4045
// @public
4146
export function clearGitObservers(): void;
4247

packages/workspace-tools/src/__tests__/workspaces/catalogs.test.ts

Lines changed: 0 additions & 216 deletions
This file was deleted.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { describe, it, expect } from "@jest/globals";
2+
import { catalogsToYaml } from "../../workspaces/catalogsToYaml";
3+
import type { Catalogs } from "../../types/Catalogs";
4+
import dedent from "ts-dedent";
5+
6+
describe("catalogsToYaml", () => {
7+
it("returns empty string for empty catalogs", () => {
8+
const catalogs: Catalogs = {};
9+
10+
const result = catalogsToYaml(catalogs);
11+
expect(result).toBe("");
12+
});
13+
14+
it("converts default catalog to yaml format", () => {
15+
const catalogs: Catalogs = {
16+
default: {
17+
react: "^18.0.0",
18+
typescript: "~5.0.0",
19+
},
20+
};
21+
22+
const result = catalogsToYaml(catalogs);
23+
expect(result).toEqual(dedent`
24+
catalog:
25+
react: ^18.0.0
26+
typescript: ~5.0.0`);
27+
});
28+
29+
it("converts named catalogs to yaml format", () => {
30+
const catalogs: Catalogs = {
31+
named: {
32+
react18: {
33+
react: "^18.0.0",
34+
"react-dom": "^18.0.0",
35+
},
36+
react17: {
37+
react: "^17.0.0",
38+
"react-dom": "^17.0.0",
39+
},
40+
},
41+
};
42+
43+
const result = catalogsToYaml(catalogs);
44+
expect(result).toEqual(dedent`
45+
catalogs:
46+
react18:
47+
react: ^18.0.0
48+
react-dom: ^18.0.0
49+
react17:
50+
react: ^17.0.0
51+
react-dom: ^17.0.0`);
52+
});
53+
54+
it("converts both default and named catalogs", () => {
55+
const catalogs: Catalogs = {
56+
default: {
57+
typescript: "~5.0.0",
58+
},
59+
named: {
60+
testing: {
61+
jest: "^29.0.0",
62+
},
63+
},
64+
};
65+
66+
const result = catalogsToYaml(catalogs);
67+
expect(result).toEqual(dedent`
68+
catalog:
69+
typescript: ~5.0.0
70+
catalogs:
71+
testing:
72+
jest: ^29.0.0`);
73+
});
74+
75+
it("respects indent with tab", () => {
76+
const catalogs: Catalogs = {
77+
named: { test: { pkg: "1.0.0" } },
78+
};
79+
80+
const result = catalogsToYaml(catalogs, { indent: "\t" });
81+
expect(result).toEqual(dedent`
82+
catalogs:
83+
test:
84+
pkg: 1.0.0`);
85+
});
86+
87+
it("respects indent with 4 spaces", () => {
88+
const catalogs: Catalogs = {
89+
named: { test: { pkg: "1.0.0" } },
90+
};
91+
92+
const result = catalogsToYaml(catalogs, { indent: 4 });
93+
expect(result).toEqual(dedent`
94+
catalogs:
95+
test:
96+
pkg: 1.0.0`);
97+
});
98+
});

0 commit comments

Comments
 (0)