mirror of
https://github.com/mermaid-js/mermaid.git
synced 2026-05-23 20:10:38 +00:00
d6f4a62733
Use `@microsoft/api-extractor` to bundle the TypeScript `.d.ts` types for `@mermaid-js/parser`. In a future commit, we want to bundle `langium`, which would need us to bundle `langium`'s types as well. Bundling reduces the size of our `dist/` folder, and makes it more obvious which of our types are external. I've made this as a `prepack` step, so that it doesn't affect the majority of mermaid developers when they run `pnpm install`. It's only when we publish the package that we'd bundle the code. This also means it will be tested by the `pnpm run test:check:tsc` test that we have.
61 lines
1.8 KiB
JavaScript
Executable File
61 lines
1.8 KiB
JavaScript
Executable File
#!/usr/bin/env -S node --experimental-strip-types
|
|
|
|
/**
|
|
* Runs `api-extractor` to create a single `dist/src/index.d.ts` bundle,
|
|
* then removes all unused `dist/*.d.ts` files.
|
|
*/
|
|
import { Extractor, ExtractorConfig } from '@microsoft/api-extractor';
|
|
import { glob, rm, rmdir } from 'node:fs/promises';
|
|
import { join } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
// Run API extractor to create new `dist/src/index.d.ts` file
|
|
let typesBundle;
|
|
{
|
|
const apiExtractorJsonPath = fileURLToPath(import.meta.resolve('../api-extractor.json'));
|
|
|
|
// Load and parse the api-extractor.json file
|
|
const extractorConfig = ExtractorConfig.loadFileAndPrepare(apiExtractorJsonPath);
|
|
|
|
// Invoke API Extractor
|
|
const extractorResult = Extractor.invoke(extractorConfig, {
|
|
localBuild: !process.env.CI,
|
|
// Equivalent to the "--verbose" command-line parameter
|
|
showVerboseMessages: true,
|
|
});
|
|
|
|
if (!extractorResult.succeeded) {
|
|
throw new Error(
|
|
`API Extractor completed with ${extractorResult.errorCount} errors` +
|
|
` and ${extractorResult.warningCount} warnings`
|
|
);
|
|
}
|
|
typesBundle = extractorResult.extractorConfig.untrimmedFilePath;
|
|
}
|
|
|
|
// Remove all other `dist/*.d.ts` files
|
|
const rootDir = fileURLToPath(import.meta.resolve('../'));
|
|
for await (const file of glob('./dist/**/*.d.ts', {
|
|
cwd: rootDir,
|
|
exclude: [typesBundle],
|
|
})) {
|
|
await rm(join(rootDir, file));
|
|
}
|
|
|
|
// @ts-expect-error -- Our tsconfig.json doesn't support `Array.fromAsync`
|
|
const directories: string[] = await Array.fromAsync(
|
|
glob('./dist/**/', {
|
|
cwd: rootDir,
|
|
})
|
|
);
|
|
// delete subdirectories before their parents
|
|
for (const dir of directories.sort().reverse()) {
|
|
try {
|
|
await rmdir(join(rootDir, dir));
|
|
} catch (err) {
|
|
if (err instanceof Error && 'code' in err && err.code !== 'ENOTEMPTY') {
|
|
throw err;
|
|
}
|
|
}
|
|
}
|