tsbuild/ts/tsbuild.cli.ts

93 lines
3.2 KiB
TypeScript
Raw Normal View History

2024-05-10 13:55:25 +00:00
import * as plugins from './plugins.js';
import * as paths from './paths.js';
2022-03-12 18:05:46 +00:00
import * as tsbuild from './tsbuild.exports.js';
2020-04-30 09:02:43 +00:00
export const runCli = async () => {
const tsbuildCli = new plugins.smartcli.Smartcli();
2020-04-30 09:02:43 +00:00
/**
* the standard task compiles anything in ts/ directory to dist directory
*/
2022-08-03 17:34:51 +00:00
tsbuildCli.standardCommand().subscribe(async (argvArg) => {
tsbuild.compileGlobStringObject(
{
2020-04-30 09:02:43 +00:00
'./ts/**/*.ts': './dist_ts',
},
{},
process.cwd(),
argvArg
);
2020-04-30 09:02:43 +00:00
});
2018-12-05 22:32:27 +00:00
2020-04-30 09:02:43 +00:00
/**
* the custom command compiles any customDir to dist_customDir
*/
tsbuildCli.addCommand('custom').subscribe(async (argvArg) => {
const listedDirectories = argvArg._;
2024-05-10 13:55:25 +00:00
listedDirectories.shift(); // removes the first element that is "custom"
2020-04-30 09:02:43 +00:00
const compilationCommandObject: { [key: string]: string } = {};
for (const directory of listedDirectories) {
compilationCommandObject[`./${directory}/**/*.ts`] = `./dist_${directory}`;
}
await tsbuild.compileGlobStringObject(compilationCommandObject, {}, process.cwd(), argvArg);
});
2020-03-09 15:14:06 +00:00
2024-05-10 13:55:25 +00:00
/**
* the custom command compiles any customDir to dist_customDir
*/
tsbuildCli.addCommand('tsfolders').subscribe(async (argvArg) => {
2024-05-13 23:20:49 +00:00
const tsFolders = await plugins.smartfile.fs.listFolders(paths.cwd, /^ts/);
// 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);
// Construct the sorted list of folders
const sortedTsFolders = [];
2024-05-13 23:20:49 +00:00
// Add the rest of the folders in sorted order
for (const item of foldersWithOrder) {
sortedTsFolders.push(item.folder);
2024-05-13 23:20:49 +00:00
}
// 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);
2024-05-10 13:55:25 +00:00
const compilationCommandObject: { [key: string]: string } = {};
console.log(`compiling in this order:`);
console.log(sortedTsFolders);
for (const tsFolder of sortedTsFolders) {
2024-05-10 13:55:25 +00:00
compilationCommandObject[`./${tsFolder}/**/*.ts`] = `./dist_${tsFolder}`;
}
await tsbuild.compileGlobStringObject(compilationCommandObject, {}, process.cwd(), argvArg);
2020-04-30 09:02:43 +00:00
});
2019-01-27 01:42:58 +00:00
2020-04-30 09:02:43 +00:00
tsbuildCli.startParse();
};