Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/oxfmt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"devDependencies": {
"@types/node": "catalog:",
"execa": "^9.6.0",
"quicktype-core": "catalog:",
"tsdown": "catalog:",
"typescript": "catalog:",
"vitest": "catalog:"
Expand Down
38 changes: 37 additions & 1 deletion apps/oxfmt/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { execSync } from "node:child_process";
import { copyFileSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";

import { quicktype, InputData, JSONSchemaInput, FetchingJSONSchemaStore } from "quicktype-core";

const oxfmtDirPath = join(import.meta.dirname, ".."),
distDirPath = join(oxfmtDirPath, "dist");
distDirPath = join(oxfmtDirPath, "dist"),
jsonSchemaPath = join(oxfmtDirPath, "..", "..", "npm/oxfmt/configuration_schema.json");

// Modify `bindings.js` to use correct package names
console.log("Modifying bindings.js...");
Expand All @@ -28,6 +31,14 @@ for (const filename of readdirSync(join(oxfmtDirPath, "src-js"))) {
if (!filename.endsWith(".node")) continue;
copyFile(join(oxfmtDirPath, "src-js", filename), join(distDirPath, filename));
}
try {
const { lines } = await quicktypeJSONSchema("OxfmtConfig", readFileSync(jsonSchemaPath, "utf8"));
writeFileSync(join(distDirPath, "config.d.ts"), lines.join("\n"));
console.log("Translated oxfmt config JSON schema into TypeScript");
} catch (error) {
console.error("Translating oxfmt config JSON schema into TypeScript failed:", error);
process.exit(1);
}

console.log("Build complete!");

Expand All @@ -42,3 +53,28 @@ function copyFile(srcPath, destPath) {
copyFileSync(srcPath, destPath);
console.log(`- Copied ${srcPath.split("/").pop()}`);
}

/**
* Quicktype a JSON schema into TypeScript.
* @param {string} typeName - The name of the type to quicktype.
* @param {string} jsonSchemaString - The JSON schema string to quicktype.
* @returns {Promise<import('quicktype-core').SerializedRenderResult>} The quicktyped code.
*/
async function quicktypeJSONSchema(typeName, jsonSchemaString) {
const schemaInput = new JSONSchemaInput(new FetchingJSONSchemaStore());

// We could add multiple schemas for multiple types,
// but here we're just making one type from JSON schema.
await schemaInput.addSource({ name: typeName, schema: jsonSchemaString });

const inputData = new InputData();
inputData.addInput(schemaInput);

return await quicktype({
inputData,
lang: "typescript",
rendererOptions: {
"prefer-unions": true,
},
});
}
1 change: 1 addition & 0 deletions apps/oxlint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"jiti": "^2.6.0",
"json-stable-stringify-without-jsonify": "^1.0.1",
"oxc-parser": "^0.99.0",
"quicktype-core": "catalog:",
"rolldown": "catalog:",
"tsdown": "catalog:",
"type-fest": "^5.2.0",
Expand Down
33 changes: 32 additions & 1 deletion apps/oxlint/scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import { execSync } from "node:child_process";
import { copyFileSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { join } from "node:path";

import { quicktype, InputData, JSONSchemaInput, FetchingJSONSchemaStore } from "quicktype-core";

const oxlintDirPath = join(import.meta.dirname, ".."),
srcDirPath = join(oxlintDirPath, "src-js"),
distDirPath = join(oxlintDirPath, "dist");
distDirPath = join(oxlintDirPath, "dist"),
jsonSchemaPath = join(oxlintDirPath, "..", "..", "npm/oxlint/configuration_schema.json");

// Modify `bindings.js` to use correct package names
console.log("Modifying bindings.js...");
Expand Down Expand Up @@ -34,4 +37,32 @@ for (const filename of readdirSync(srcDirPath)) {
copyFileSync(srcPath, join(distDirPath, filename));
}

try {
const { lines } = await quicktypeJSONSchema("OxlintConfig", readFileSync(jsonSchemaPath, "utf8"));
writeFileSync(join(distDirPath, "config.d.ts"), lines.join("\n"));
console.log("Translated oxlint config JSON schema into TypeScript");
} catch (error) {
console.error("Translating oxlint config JSON schema into TypeScript failed:", error);
process.exit(1);
}

console.log("Build complete!");

async function quicktypeJSONSchema(typeName: string, jsonSchemaString: string) {
const schemaInput = new JSONSchemaInput(new FetchingJSONSchemaStore());

// We could add multiple schemas for multiple types,
// but here we're just making one type from JSON schema.
await schemaInput.addSource({ name: typeName, schema: jsonSchemaString });

const inputData = new InputData();
inputData.addInput(schemaInput);

return await quicktype({
inputData,
lang: "typescript",
rendererOptions: {
"prefer-unions": true,
},
});
}
Loading
Loading