Skip to content

Commit 7fc9890

Browse files
committed
Emit schema.ts as a giant string
1 parent e9e8635 commit 7fc9890

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

packages/mql-typescript/out/schema-export.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/mql-typescript/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"reformat": "npm run prettier -- --write .",
4848
"extract-metaschema": "json-refs resolve mongo-php-library/generator/config/schema.json | json-schema-to-zod -n Operator -o src/metaschema.ts && npm run prettier -- --write src/metaschema.ts",
4949
"pregenerate-schema": "npm run extract-metaschema",
50-
"generate-schema": "ts-node src/cli.ts schema && npm run prettier -- --write out/schema.ts",
50+
"generate-schema": "ts-node src/cli.ts schema && npm run prettier -- --write out/*",
5151
"pregenerate-tests": "npm run extract-metaschema",
5252
"generate-tests": "ts-node src/cli.ts tests && npm run prettier -- --write tests/**/*.ts",
5353
"pregenerate-driver-schema": "npm run extract-metaschema",

packages/mql-typescript/src/generator.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { WriteStream } from 'fs';
12
import { createWriteStream } from 'fs';
23
import { StringWriter } from './utils';
34
import path from 'path';
@@ -29,7 +30,7 @@ class BsonDate extends Date {
2930

3031
export abstract class GeneratorBase {
3132
private outputBuffer: StringWriter | undefined;
32-
private outputStream?: NodeJS.WritableStream;
33+
private outputStream?: WriteStream;
3334

3435
constructor() {
3536
// The default YAML schema will represent BsonDate using the Date representation because
@@ -232,10 +233,28 @@ export abstract class GeneratorBase {
232233
}
233234
}
234235

235-
protected emitToFile(filePath: string): void {
236+
protected async emitToFile(filePath: string): Promise<void> {
237+
await this.flushFile();
238+
236239
this.outputStream = createWriteStream(filePath, { encoding: 'utf8' });
237240
}
238241

242+
private flushFile(): Promise<void> {
243+
if (this.outputStream) {
244+
return new Promise((resolve, reject) => {
245+
this.outputStream?.close((err) => {
246+
if (err) {
247+
reject(err);
248+
} else {
249+
resolve();
250+
}
251+
});
252+
});
253+
}
254+
255+
return Promise.resolve();
256+
}
257+
239258
protected emit(str: string): void {
240259
(this.outputBuffer ?? this.outputStream ?? process.stdout).write(str);
241260
}
@@ -275,7 +294,7 @@ export abstract class GeneratorBase {
275294

276295
protected abstract generateImpl(iterable: YamlFiles): Promise<void>;
277296

278-
public generate(
297+
public async generate(
279298
categoryFilter?: string,
280299
operatorFilter?: string,
281300
): Promise<void> {
@@ -287,6 +306,8 @@ export abstract class GeneratorBase {
287306
: undefined;
288307

289308
const files = this.listSourceYAMLFiles(categoryRegex, operatorRegex);
290-
return this.generateImpl(files);
309+
await this.generateImpl(files);
310+
311+
await this.flushFile();
291312
}
292313
}

packages/mql-typescript/src/schema-generator.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { YamlFiles } from './generator';
33
import { GeneratorBase } from './generator';
44
import { Operator } from './metaschema';
55
import { capitalize } from './utils';
6+
import * as fs from 'fs/promises';
67

78
type ArgType = NonNullable<
89
typeof Operator._type.arguments
@@ -13,10 +14,13 @@ type SyntheticVariables = NonNullable<
1314
>[number]['syntheticVariables'];
1415

1516
export class SchemaGenerator extends GeneratorBase {
16-
constructor() {
17-
super();
18-
this.emitToFile(path.resolve(__dirname, '..', 'out', 'schema.ts'));
19-
}
17+
private schemaOutputFile = path.resolve(__dirname, '..', 'out', 'schema.ts');
18+
private schemaExportFile = path.resolve(
19+
__dirname,
20+
'..',
21+
'out',
22+
'schema-export.js',
23+
);
2024

2125
private toTypeName(type: string): string {
2226
return this.trivialTypeMappings[type as ArgType] ?? capitalize(type);
@@ -408,6 +412,8 @@ export class SchemaGenerator extends GeneratorBase {
408412
}
409413

410414
protected override async generateImpl(yamlFiles: YamlFiles): Promise<void> {
415+
await this.emitToFile(this.schemaOutputFile);
416+
411417
this.emitHeader();
412418

413419
for await (const file of yamlFiles) {
@@ -569,5 +575,19 @@ export class SchemaGenerator extends GeneratorBase {
569575
} = ${[...new Set(interfaces)].join('|')};`,
570576
);
571577
}
578+
579+
// Generate the schema-export.js which will be used by the autocomplete package to load the types
580+
// Switch to the new file that will just export the types as a giant string:
581+
// exports.schema = "** contents of the out/schema.ts file **";
582+
await this.emitToFile(this.schemaExportFile);
583+
const schemaDefinition = await fs.readFile(this.schemaOutputFile, 'utf8');
584+
this.emit("exports.schema = '");
585+
this.emit(
586+
schemaDefinition
587+
.replace(/\\/g, '\\\\') // Escape backslashes
588+
.replace(/'/g, "\\'") // Escape double quotes
589+
.replace(/\n/g, '\\n'), // Escape newlines
590+
);
591+
this.emit("'\n");
572592
}
573593
}

0 commit comments

Comments
 (0)