Fix schema (#15962)

This commit is contained in:
fisker Cheung authored and GitHub committed 2024-01-22 00:02:31 +08:00
1 parent 2fe7f4193a
commit 91a2e2513e
6 files changed
+51 -28

No files matched your search

+2 -8
View File
@@ -1,11 +1,5 @@
#!/usr/bin/env node
import { format } from "../index.js";
import { getSupportInfo } from "../src/main/support.js";
import generateSchema from "./utils/generate-schema.js";
import { generateSchema } from "./utils/generate-schema.js";
console.log(
await format(JSON.stringify(generateSchema(getSupportInfo().options)), {
parser: "json",
}),
);
console.log(await generateSchema());
+10 -9
View File
@@ -1,8 +1,7 @@
import chalk from "chalk";
import { execa } from "execa";
import outdent from "outdent";
import { fetchText, logPromise } from "../utils.js";
import { fetchText, logPromise, writeFile } from "../utils.js";
const SCHEMA_REPO = "SchemaStore/schemastore";
const SCHEMA_PATH = "src/schemas/json/prettierrc.json";
@@ -12,26 +11,28 @@ const EDIT_URL = `https://github.com/${SCHEMA_REPO}/edit/master/${SCHEMA_PATH}`;
// Any optional or manual step can be warned in this script.
async function checkSchema() {
const { stdout: schema } = await execa("node", [
"scripts/generate-schema.js",
]);
const { generateSchema } = await import("../../utils/generate-schema.js");
const schema = await generateSchema();
const remoteSchema = await logPromise(
"Checking current schema in SchemaStore",
fetchText(RAW_URL),
);
if (schema === remoteSchema.trim()) {
if (schema.trim() === remoteSchema.trim()) {
return;
}
writeFile(
new URL("../../../.tmp/schema/prettierrc.json", import.meta.url),
schema,
);
return outdent`
${chalk.bold.underline(
"The schema in {yellow SchemaStore",
)} needs an update.}
- Open ${chalk.cyan.underline(EDIT_URL)}
- Run ${chalk.yellow(
"node scripts/generate-schema.mjs",
)} and copy the new schema
- Open ${chalk.cyan.underline("/.tmp/schema/prettierrc.json")} file and copy the content
- Paste it on GitHub interface
- Open a PR
`;
+17 -2
View File
@@ -1,5 +1,7 @@
import fs from "node:fs";
import path from "node:path";
import readline from "node:readline";
import url from "node:url";
import chalk from "chalk";
import { execa } from "execa";
@@ -98,8 +100,20 @@ function readJson(filename) {
return JSON.parse(fs.readFileSync(filename));
}
function writeJson(filename, content) {
fs.writeFileSync(filename, JSON.stringify(content, null, 2) + "\n");
function writeJson(file, content) {
writeFile(file, JSON.stringify(content, null, 2) + "\n");
}
const toPath = (urlOrPath) =>
urlOrPath instanceof URL ? url.fileURLToPath(urlOrPath) : urlOrPath;
function writeFile(file, content) {
try {
fs.mkdirSync(path.dirname(toPath(file)), { recursive: true });
} catch {
// noop
}
fs.writeFileSync(file, content);
}
function processFile(filename, fn) {
@@ -139,5 +153,6 @@ export {
runGit,
runYarn,
waitForEnter,
writeFile,
writeJson,
};
+18 -6
View File
@@ -1,12 +1,16 @@
function generateSchema(options) {
function generateSchemaData(options) {
return {
$schema: "http://json-schema.org/draft-04/schema#",
title: "Schema for .prettierrc",
$schema: "http://json-schema.org/draft-07/schema#",
$id: "https://json.schemastore.org/prettierrc.json",
definitions: {
optionsDefinition: {
type: "object",
properties: Object.fromEntries(
options.map((option) => [option.name, optionToSchema(option)]),
options
.sort(({ name: optionNameA }, { name: optionNameB }) =>
optionNameA.localeCompare(optionNameB),
)
.map((option) => [option.name, optionToSchema(option)]),
),
},
overridesDefinition: {
@@ -35,9 +39,9 @@ function generateSchema(options) {
],
},
options: {
$ref: "#/definitions/optionsDefinition",
type: "object",
description: "The options to apply for this override.",
$ref: "#/definitions/optionsDefinition",
},
},
additionalProperties: false,
@@ -58,6 +62,7 @@ function generateSchema(options) {
type: "string",
},
],
title: "Schema for .prettierrc",
};
}
@@ -113,4 +118,11 @@ function choiceToSchema(choice) {
return { enum: [choice.value], description: choice.description };
}
export default generateSchema;
async function generateSchema() {
const { format, getSupportInfo } = await import("../../src/index.js");
const supportInfo = await getSupportInfo();
const schema = generateSchemaData(supportInfo.options);
return format(JSON.stringify(schema, undefined, 2), { parser: "json" });
}
export { generateSchema, generateSchemaData };