feat(cli): Enhance CLI for TypeScript folder compilation ordering based on rank and predefined rules.

This commit is contained in:
2024-11-05 01:48:49 +01:00
parent 5369e8d931
commit 59c6e72187
7 changed files with 2728 additions and 1931 deletions

View File

@ -37,24 +37,52 @@ export const runCli = async () => {
*/
tsbuildCli.addCommand('tsfolders').subscribe(async (argvArg) => {
const tsFolders = await plugins.smartfile.fs.listFolders(paths.cwd, /^ts/);
// lets make sure shared is always transpiled first
const indexShared = tsFolders.indexOf('ts_shared');
if (indexShared > -1) {
tsFolders.splice(indexShared, 1);
tsFolders.unshift('ts_shared');
// Now tsFolders contains all other folders except 'ts_shared' and 'ts_interfaces'
// We've established a base order. Now let's look at tspublish.json based ranking.
const tsPublishInstance = new plugins.tspublish.TsPublish();
const tsPublishModules = await tsPublishInstance.getModuleSubDirs(paths.cwd);
// tsPublishModules is an object: { [folderName]: tspublishJsonData }
// Create an array with folder names and their ranks
const foldersWithOrder = [];
for (const folder of tsFolders) {
let rank = Infinity; // Default rank if not specified
if (tsPublishModules[folder] && tsPublishModules[folder].order !== undefined) {
rank = tsPublishModules[folder].order;
}
foldersWithOrder.push({ folder, rank });
}
// Sort the folders based on rank
foldersWithOrder.sort((a, b) => a.rank - b.rank);
// lets make sure interfaces are always transpiled first
const indexInterfaces = tsFolders.indexOf('ts_interfaces');
if (indexInterfaces > -1) {
tsFolders.splice(indexInterfaces, 1);
tsFolders.unshift('ts_interfaces');
// Construct the sorted list of folders
const sortedTsFolders = [];
// Add the rest of the folders in sorted order
for (const item of foldersWithOrder) {
sortedTsFolders.push(item.folder);
}
// Let's make sure 'ts_shared' is always transpiled first
const ensurePosition = (folderNameArg: string, ensuredPosition: number) => {
if (tsFolders.indexOf(folderNameArg) > -1 && Object.keys(tsPublishModules).indexOf(folderNameArg) === -1) {
sortedTsFolders.splice(tsFolders.indexOf(folderNameArg), 1);
sortedTsFolders.splice(ensuredPosition, 0, folderNameArg);
}
}
ensurePosition('ts_interfaces', 0);
ensurePosition('ts_shared', 1);
const compilationCommandObject: { [key: string]: string } = {};
console.log(`compiling in this order:`);
console.log(tsFolders);
for (const tsFolder of tsFolders) {
console.log(sortedTsFolders);
for (const tsFolder of sortedTsFolders) {
compilationCommandObject[`./${tsFolder}/**/*.ts`] = `./dist_${tsFolder}`;
}
await tsbuild.compileGlobStringObject(compilationCommandObject, {}, process.cwd(), argvArg);