Skip to content

Commit

Permalink
Fix schema (#15962)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 21, 2024
1 parent 2fe7f41 commit 91a2e25
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 28 deletions.
10 changes: 2 additions & 8 deletions scripts/generate-schema.js
Original file line number Diff line number Diff line change
@@ -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());
19 changes: 10 additions & 9 deletions scripts/release/steps/post-publish-steps.js
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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
`;
Expand Down
19 changes: 17 additions & 2 deletions scripts/release/utils.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -139,5 +153,6 @@ export {
runGit,
runYarn,
waitForEnter,
writeFile,
writeJson,
};
24 changes: 18 additions & 6 deletions scripts/utils/generate-schema.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down Expand Up @@ -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,
Expand All @@ -58,6 +62,7 @@ function generateSchema(options) {
type: "string",
},
],
title: "Schema for .prettierrc",
};
}

Expand Down Expand Up @@ -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 };
3 changes: 2 additions & 1 deletion tests/integration/__tests__/__snapshots__/schema.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/__tests__/schema.js
Original file line number Diff line number Diff line change
@@ -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();
});

0 comments on commit 91a2e25

Please sign in to comment.