fix(core): package mode (#20580)

This commit is contained in:
Stephen Zhou
2025-11-26 20:44:18 +08:00
committed by GitHub
parent 87bb77ca4c
commit b4a2aa5e70
9 changed files with 346 additions and 29 deletions

View File

@@ -88,8 +88,58 @@ for (const namespace in namespaces) {
}
}
// Generate route paths type
const allRoutePaths: string[] = [];
for (const namespace in namespaces) {
for (const path in namespaces[namespace].routes) {
const fullPath = `/${namespace}${path}`;
// Split path into segments to handle optional parameters
const segments = fullPath.split('/');
let hasOptional = false;
// Process each segment
const processedSegments = segments.map((segment) => {
if (segment.endsWith('?')) {
hasOptional = true;
return '${string}';
} else if (segment.startsWith(':')) {
return '${string}';
}
return segment;
});
if (hasOptional) {
// Generate both versions: with and without optional parameters
// Find the index where optional parameters start
const firstOptionalIndex = segments.findIndex((s) => s.endsWith('?'));
// Version without optional parameters
const requiredPath = processedSegments.slice(0, firstOptionalIndex).join('/');
allRoutePaths.push(requiredPath);
// Version with optional parameters
const fullProcessedPath = processedSegments.join('/');
allRoutePaths.push(fullProcessedPath);
} else {
allRoutePaths.push(processedSegments.join('/'));
}
}
}
// Remove duplicates and sort
const uniquePaths = [...new Set(allRoutePaths)].toSorted();
const routePathsType = `// This file is auto-generated by scripts/workflow/build-routes.ts
// Do not edit manually
export type RoutePath =
${uniquePaths.map((path) => ` | \`${path}\``).join('\n')};
`;
fs.writeFileSync(path.join(__dirname, '../../assets/build/radar-rules.json'), JSON.stringify(radar, null, 2));
fs.writeFileSync(path.join(__dirname, '../../assets/build/radar-rules.js'), `(${toSource(radar)})`);
fs.writeFileSync(path.join(__dirname, '../../assets/build/maintainers.json'), JSON.stringify(maintainers, null, 2));
fs.writeFileSync(path.join(__dirname, '../../assets/build/routes.json'), JSON.stringify(namespaces, null, 2));
fs.writeFileSync(path.join(__dirname, '../../assets/build/routes.js'), `export default ${JSON.stringify(namespaces, null, 2)}`.replaceAll(/"module": "(.*)"\n/g, `"module": $1\n`));
fs.writeFileSync(path.join(__dirname, '../../assets/build/route-paths.ts'), routePathsType);