-
Notifications
You must be signed in to change notification settings - Fork 676
Expand file tree
/
Copy pathverify-public-surface.mjs
More file actions
210 lines (179 loc) · 5.6 KB
/
Copy pathverify-public-surface.mjs
File metadata and controls
210 lines (179 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { readFile } from "node:fs/promises";
import path from "node:path";
import process from "node:process";
const repoRoot = process.cwd();
const expectedRootExports = [
"AbstractChart",
"BarChart",
"ContributionGraph",
"LineChart",
"PieChart",
"ProgressChart",
"StackedBarChart"
];
const expectedV2ValueExports = [
"AreaChart",
"BarChart",
"CalendarHeatmap",
"ChartKitProvider",
"ContributionGraph",
"DonutChart",
"LineChart",
"PieChart",
"ProgressChart",
"ProgressRing",
"StackedBarChart",
"builtInCartesianChartPresets",
"createChartPreset",
"getBarChartAccessibilitySummary",
"getBarChartDataTable",
"getContributionGraphAccessibilitySummary",
"getContributionGraphDataTable",
"getLineChartAccessibilitySummary",
"getLineChartDataTable",
"getPieChartAccessibilitySummary",
"getPieChartDataTable",
"getProgressChartAccessibilitySummary",
"getProgressChartDataTable",
"resolveCartesianChartThemeConfig",
"useChartKitTheme"
];
const expectedV2TypeExports = [
"BarChartProps",
"ChartKitProviderProps",
"ContributionGraphProps",
"LineChartProps",
"LineChartViewportConfig",
"PieChartProps",
"ProgressChartProps",
"StackedBarChartProps"
];
const readRepoFile = (relativePath) =>
readFile(path.join(repoRoot, relativePath), "utf8");
const extractRootExports = (source) => {
const exportBlock = source.match(/export\s*\{(?<body>[\s\S]*?)\};?/);
const body = exportBlock?.groups?.body ?? "";
return body
.split(",")
.map((item) => item.trim())
.filter(Boolean);
};
const extractNamedExports = (source, exportKind = "value") => {
const exports = new Set();
const keyword = exportKind === "type" ? "export type" : "export";
const exportBlockPattern = new RegExp(
`${keyword}\\s*\\{(?<body>[\\s\\S]*?)\\}\\s*from`,
"g"
);
let match;
while ((match = exportBlockPattern.exec(source)) !== null) {
const body = match.groups?.body ?? "";
body
.split(",")
.map((item) => item.trim())
.filter(Boolean)
.forEach((item) => exports.add(item.split(/\s+as\s+/)[0] ?? item));
}
return exports;
};
const assertMissing = ({ actual, expected, label }) => {
const missing = expected.filter((name) => !actual.has(name));
if (missing.length > 0) {
throw new Error(`${label} missing exports: ${missing.join(", ")}`);
}
};
const packageJson = JSON.parse(await readRepoFile("package.json"));
const corePackageJson = JSON.parse(
await readRepoFile("packages/core/package.json")
);
const svgPackageJson = JSON.parse(
await readRepoFile("packages/svg-renderer/package.json")
);
const v2PackageJson = JSON.parse(
await readRepoFile("packages/react-native/package.json")
);
const rootSource = await readRepoFile("src/index.ts");
const v2Source = await readRepoFile("packages/react-native/src/index.ts");
if (packageJson.name !== "react-native-chart-kit") {
throw new Error(`Unexpected package name: ${packageJson.name}`);
}
if (!packageJson.exports?.["./v2"]) {
throw new Error("react-native-chart-kit must expose ./v2");
}
if (packageJson.exports["./v2"].default !== "./v2/index.js") {
throw new Error(
"react-native-chart-kit ./v2 must expose the physical v2 shim"
);
}
const rootExportPaths = Object.keys(packageJson.exports ?? {}).sort();
const expectedRootExportPaths = [".", "./package.json", "./v2"];
if (
JSON.stringify(rootExportPaths) !== JSON.stringify(expectedRootExportPaths)
) {
throw new Error(
`react-native-chart-kit public exports must be ${expectedRootExportPaths.join(
", "
)}; got ${rootExportPaths.join(", ")}`
);
}
if (v2PackageJson.name !== "@chart-kit/react-native") {
throw new Error(`Unexpected v2 package name: ${v2PackageJson.name}`);
}
for (const workspacePackageJson of [
corePackageJson,
svgPackageJson,
v2PackageJson
]) {
if (
!workspacePackageJson.exports?.["."] ||
!workspacePackageJson.exports?.["./package.json"]
) {
throw new Error(
`${workspacePackageJson.name} must expose explicit workspace package paths`
);
}
}
for (const publicWorkspacePackageJson of [corePackageJson, svgPackageJson]) {
if (publicWorkspacePackageJson.private === true) {
throw new Error(`${publicWorkspacePackageJson.name} must be publishable`);
}
if (publicWorkspacePackageJson.license !== "MIT") {
throw new Error(`${publicWorkspacePackageJson.name} must remain MIT`);
}
if (publicWorkspacePackageJson.publishConfig?.access !== "public") {
throw new Error(
`${publicWorkspacePackageJson.name} must publish as public`
);
}
}
if (v2PackageJson.private !== true) {
throw new Error(`${v2PackageJson.name} must remain private`);
}
if (v2PackageJson.publishConfig) {
throw new Error(`${v2PackageJson.name} must not define publishConfig`);
}
if (packageJson.main !== "./dist/index.js") {
throw new Error(`Unexpected package main: ${packageJson.main}`);
}
if (packageJson.types !== "./dist/index.d.ts") {
throw new Error(`Unexpected package types: ${packageJson.types}`);
}
assertMissing({
actual: new Set(extractRootExports(rootSource)),
expected: expectedRootExports,
label: "Root compatibility surface"
});
assertMissing({
actual: extractNamedExports(v2Source),
expected: expectedV2ValueExports,
label: "Modern v2 value surface"
});
assertMissing({
actual: extractNamedExports(v2Source, "type"),
expected: expectedV2TypeExports,
label: "Modern v2 type surface"
});
console.log("Public surface check passed.");
console.log(`Root compatibility exports: ${expectedRootExports.length}`);
console.log(`Modern v2 value exports: ${expectedV2ValueExports.length}`);
console.log(`Modern v2 type exports: ${expectedV2TypeExports.length}`);