@mui/internal-docs-infra
Version:
MUI Infra - internal documentation creation tools.
99 lines • 3.94 kB
JavaScript
// eslint-disable-next-line n/prefer-node-protocol
import { parentPort } from 'worker_threads';
// eslint-disable-next-line n/prefer-node-protocol
import { readFile } from 'fs/promises';
import { unified } from 'unified';
import remarkParse from 'remark-parse';
import remarkMdx from 'remark-mdx';
import { transformMarkdownMetadata } from "../pipeline/transformMarkdownMetadata/transformMarkdownMetadata.mjs";
import { parseCreateFactoryCall } from "../pipeline/parseCreateFactoryCall/parseCreateFactoryCall.mjs";
import { syncTypes } from "../pipeline/syncTypes/syncTypes.mjs";
function collectPerfEntries() {
const entries = performance.getEntriesByType('measure').map(entry => ({
name: entry.name,
duration: entry.duration
}));
performance.clearMeasures();
return entries;
}
if (parentPort) {
// Serialize task processing so performance.clearMeasures() in collectPerfEntries()
// cannot clear measures belonging to a concurrent in-flight task.
let taskQueue = Promise.resolve();
parentPort.on('message', task => {
taskQueue = taskQueue.then(async () => {
if (task.type === 'index') {
try {
const processor = unified().use(remarkParse).use(remarkMdx).use(transformMarkdownMetadata, {
extractToIndex: task.processorOptions
});
const content = await readFile(task.filePath, 'utf-8');
const vfile = {
path: task.filePath,
value: content
};
await processor.run(processor.parse(vfile), vfile);
parentPort.postMessage({
type: 'index',
taskId: task.taskId,
success: true,
perfEntries: task.perf ? collectPerfEntries() : undefined
});
} catch (error) {
parentPort.postMessage({
type: 'index',
taskId: task.taskId,
success: false,
perfEntries: task.perf ? collectPerfEntries() : undefined,
error: String(error)
});
}
} else if (task.type === 'types') {
try {
const content = await readFile(task.filePath, 'utf-8');
const typesMetaCall = await parseCreateFactoryCall(content, task.filePath, {
allowExternalVariants: true
});
if (!typesMetaCall) {
parentPort.postMessage({
type: 'types',
taskId: task.taskId,
success: true,
updated: false,
perfEntries: task.perf ? collectPerfEntries() : undefined
});
return;
}
const typesMarkdownPath = task.filePath.replace(/\.ts$/, '.md');
const excludeFromIndex = Boolean(typesMetaCall.structuredOptions?.excludeFromIndex);
const result = await syncTypes({
typesMarkdownPath,
rootContext: task.rootContext,
variants: typesMetaCall.variants,
watchSourceDirectly: Boolean(typesMetaCall.structuredOptions?.watchSourceDirectly),
updateParentIndex: excludeFromIndex ? undefined : task.syncTypesOptions.updateParentIndex,
ordering: task.syncTypesOptions.ordering,
descriptionReplacements: task.syncTypesOptions.descriptionReplacements,
socketDir: task.syncTypesOptions.socketDir
});
parentPort.postMessage({
type: 'types',
taskId: task.taskId,
success: true,
updated: result.updated,
updatedPath: result.updated ? typesMarkdownPath : undefined,
perfEntries: task.perf ? collectPerfEntries() : undefined
});
} catch (error) {
parentPort.postMessage({
type: 'types',
taskId: task.taskId,
success: false,
perfEntries: task.perf ? collectPerfEntries() : undefined,
error: String(error)
});
}
}
});
});
}