Skip to content

Commit ffeca55

Browse files
author
Sebastian Schürmann
committed
refactor(vanillin): more async reads where possible
1 parent 14c9bf3 commit ffeca55

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

packages/vanillin/src/compiler.ts

+22-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createProgram, CompilerOptions, Program, CustomTransformers, getPreEmitDiagnostics, Diagnostic, EmitResult, CompilerHost, ModuleKind, ModuleResolutionKind, ScriptTarget } from "typescript";
22
import transformer from './transformer.js';
3-
import { readFileSync } from "fs";
3+
import { readFile } from "fs/promises";
44
import { createVirtualCompilerHost } from "./virtual-fs.js";
55
import { resolve, dirname } from "path";
66
import { fileURLToPath } from "url";
@@ -13,12 +13,21 @@ import { fileURLToPath } from "url";
1313
const loadTypeScriptLibFiles = async () => {
1414
const tsPath = await import.meta.resolve('typescript');
1515
const tsLibPath = resolve(dirname(fileURLToPath(tsPath)), '');
16-
const libFiles = {
17-
'lib.es2015.d.ts': readFileSync(resolve(tsLibPath, 'lib.es2015.d.ts'), 'utf-8'),
18-
'lib.dom.d.ts': readFileSync(resolve(tsLibPath, 'lib.dom.d.ts'), 'utf-8'),
19-
'lib.es5.d.ts': readFileSync(resolve(tsLibPath, 'lib.es5.d.ts'), 'utf-8'),
16+
17+
const libFilePaths = {
18+
'lib.es2015.d.ts': resolve(tsLibPath, 'lib.es2015.d.ts'),
19+
'lib.dom.d.ts': resolve(tsLibPath, 'lib.dom.d.ts'),
20+
'lib.es5.d.ts': resolve(tsLibPath, 'lib.es5.d.ts'),
2021
};
21-
return libFiles;
22+
23+
const fileContents = await Promise.all(
24+
Object.entries(libFilePaths).map(async ([fileName, filePath]) => {
25+
const content = await readFile(filePath, 'utf-8');
26+
return [fileName, content] as const;
27+
})
28+
);
29+
30+
return Object.fromEntries(fileContents);
2231
};
2332

2433
/**
@@ -130,16 +139,18 @@ export class Compiler {
130139
*/
131140
static async withVirtualFs(fileNames: string[], options: CompilerOptions): Promise<Compiler> {
132141
// Load source files
133-
const sourceFiles = fileNames.reduce((acc, fileName) => {
134-
acc[fileName] = readFileSync(fileName, 'utf-8');
135-
return acc;
136-
}, {} as { [path: string]: string });
142+
const sourceFiles = await Promise.all(
143+
fileNames.map(async (fileName) => {
144+
const content = await readFile(fileName, 'utf-8');
145+
return [fileName, content] as const;
146+
})
147+
);
137148

138149
// Load TypeScript lib files
139150
const libFiles = await loadTypeScriptLibFiles();
140151

141152
// Combine source files and lib files
142-
const files = { ...sourceFiles, ...libFiles };
153+
const files = { ...Object.fromEntries(sourceFiles), ...libFiles };
143154

144155
const host = createVirtualCompilerHost({
145156
files,

packages/vanillin/src/doc-gen.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TSDocParser, type ParserContext, type DocComment, TSDocConfiguration, TSDocTagDefinition, TSDocTagSyntaxKind } from '@microsoft/tsdoc';
22
import * as path from 'path';
3-
import * as fs from 'fs';
3+
import { readFile } from 'fs/promises';
44
import { Formatter } from './formatter';
55

66
/**
@@ -23,11 +23,11 @@ export class DocGen {
2323
* Parses a TypeScript source file to extract its documentation.
2424
*
2525
* @param doc - The path to the TypeScript source file to parse
26-
* @returns A ParserContext containing the parsed documentation
26+
* @returns A Promise that resolves to a ParserContext containing the parsed documentation
2727
*/
28-
parseDoc(doc: string): ParserContext {
28+
async parseDoc(doc: string): Promise<ParserContext> {
2929
const inputFilename: string = path.resolve(doc);
30-
const inputBuffer: string = fs.readFileSync(inputFilename).toString();
30+
const inputBuffer: string = await readFile(inputFilename, 'utf-8');
3131

3232
const customConfiguration: TSDocConfiguration = new TSDocConfiguration();
3333

@@ -58,8 +58,12 @@ export class DocGen {
5858
*
5959
* @param context - The parsed documentation context to render
6060
* @returns A formatted string containing only the custom documentation blocks
61+
* @throws Error if the context or docComment is undefined
6162
*/
62-
renderDocs(context: ParserContext) {
63+
renderDocs(context: ParserContext): string {
64+
if (!context || !context.docComment) {
65+
throw new Error('Invalid parser context: docComment is undefined');
66+
}
6367
const docComment: DocComment = context.docComment;
6468
const result = Formatter.renderDocNodes(docComment.customBlocks);
6569
return result;

packages/vanillin/test/doc-gen.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ import assert from "node:assert";
33
import { DocGen } from '../src/doc-gen';
44

55
describe('DocGen', () => {
6-
it('should parse a typescript file and return a ParserContext', () => {
6+
it('should parse a typescript file and return a ParserContext', async () => {
77
const docGen = new DocGen();
8-
const result = docGen.parseDoc('./test/fixtures/my-circle.ts');
8+
const result = await docGen.parseDoc('./test/fixtures/my-circle.ts');
99
assert.ok(result, 'ParserContext should be returned');
1010
});
1111

12-
it('returns the demo tag', () => {
12+
it('returns the demo tag', async () => {
1313
const docGen = new DocGen();
14-
const result = docGen.parseDoc('./test/fixtures/my-circle.ts');
14+
const result = await docGen.parseDoc('./test/fixtures/my-circle.ts');
1515
assert.equal(result.docComment.customBlocks.length, 1);
1616
});
1717

18-
it('rendered result contains tag', () => {
18+
it('rendered result contains tag', async () => {
1919
const docGen = new DocGen();
20-
const result = docGen.parseDoc('./test/fixtures/my-circle.ts');
20+
const result = await docGen.parseDoc('./test/fixtures/my-circle.ts');
2121
const doc = docGen.renderDocs(result);
2222
assert.match(doc, /my-circle/);
2323
});

0 commit comments

Comments
 (0)