From 91a2e2513e11df4c89bca14199868b2e1b8f6777 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Mon, 22 Jan 2024 00:02:31 +0800 Subject: [PATCH] Fix schema (#15962) --- scripts/generate-schema.js | 10 ++------ scripts/release/steps/post-publish-steps.js | 19 ++++++++------- scripts/release/utils.js | 19 +++++++++++++-- scripts/utils/generate-schema.js | 24 ++++++++++++++----- .../__tests__/__snapshots__/schema.js.snap | 3 ++- tests/integration/__tests__/schema.js | 4 ++-- 6 files changed, 51 insertions(+), 28 deletions(-) diff --git a/scripts/generate-schema.js b/scripts/generate-schema.js index 019e6644d08d..fac2f42b747d 100644 --- a/scripts/generate-schema.js +++ b/scripts/generate-schema.js @@ -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()); diff --git a/scripts/release/steps/post-publish-steps.js b/scripts/release/steps/post-publish-steps.js index fc272b5bebbc..d25283321abd 100644 --- a/scripts/release/steps/post-publish-steps.js +++ b/scripts/release/steps/post-publish-steps.js @@ -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 `; diff --git a/scripts/release/utils.js b/scripts/release/utils.js index 25cff98da38f..4b6c43e8b7ee 100644 --- a/scripts/release/utils.js +++ b/scripts/release/utils.js @@ -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, }; diff --git a/scripts/utils/generate-schema.js b/scripts/utils/generate-schema.js index ad4aa3031cb7..c624bb942b39 100644 --- a/scripts/utils/generate-schema.js +++ b/scripts/utils/generate-schema.js @@ -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 }; diff --git a/tests/integration/__tests__/__snapshots__/schema.js.snap b/tests/integration/__tests__/__snapshots__/schema.js.snap index cb5d0bd91e33..2bea69951a50 100644 --- a/tests/integration/__tests__/__snapshots__/schema.js.snap +++ b/tests/integration/__tests__/__snapshots__/schema.js.snap @@ -2,7 +2,8 @@ exports[`schema 1`] = ` { - "$schema": "http://json-schema.org/draft-04/schema#", + "$id": "https://json.schemastore.org/prettierrc.json", + "$schema": "http://json-schema.org/draft-07/schema#", "definitions": { "optionsDefinition": { "properties": { diff --git a/tests/integration/__tests__/schema.js b/tests/integration/__tests__/schema.js index b3a520647a9f..31f8b51dbdec 100644 --- a/tests/integration/__tests__/schema.js +++ b/tests/integration/__tests__/schema.js @@ -1,9 +1,9 @@ -import generateSchema from "../../../scripts/utils/generate-schema.js"; +import { generateSchemaData } from "../../../scripts/utils/generate-schema.js"; import prettier from "../../config/prettier-entry.js"; test("schema", async () => { const { options } = await prettier.getSupportInfo(); - const schema = generateSchema(options); + const schema = generateSchemaData(options); expect(schema).toMatchSnapshot(); });