From 2d4256d783621c916cd11a7e99b0c4559f712548 Mon Sep 17 00:00:00 2001 From: Jan Potoms <2109932+Janpot@users.noreply.github.com> Date: Thu, 11 Apr 2024 12:22:21 +0200 Subject: [PATCH] [code-infra] Fix prettier in scripts (#3382) --- docs/schemas/v1/definitions.json | 91 ++++++------------------ package.json | 3 + pnpm-lock.yaml | 9 +++ scripts/docs/buildComponentsReference.ts | 15 ++-- scripts/docs/generateJsonSchemas.ts | 6 +- scripts/docs/utils.ts | 20 ++---- scripts/tsconfig.json | 1 + 7 files changed, 49 insertions(+), 96 deletions(-) diff --git a/docs/schemas/v1/definitions.json b/docs/schemas/v1/definitions.json index 137061fcf63..7322bc2101f 100644 --- a/docs/schemas/v1/definitions.json +++ b/docs/schemas/v1/definitions.json @@ -81,10 +81,7 @@ { "type": "object", "properties": { - "name": { - "type": "string", - "description": "The name of the role." - }, + "name": { "type": "string", "description": "The name of the role." }, "description": { "type": "string", "description": "A description of the role." @@ -125,10 +122,7 @@ "spec": { "type": "object", "properties": { - "displayName": { - "type": "string", - "description": "Page name to display in the UI." - }, + "displayName": { "type": "string", "description": "Page name to display in the UI." }, "id": { "type": "string", "description": "Serves as a canonical id of the page. Deprecated: use an alias instead." @@ -138,10 +132,7 @@ "items": { "type": "string" }, "description": "Page name aliases." }, - "title": { - "type": "string", - "description": "Title for this page." - }, + "title": { "type": "string", "description": "Title for this page." }, "parameters": { "type": "array", "items": { "$ref": "#/definitions/NameStringValuePair" }, @@ -152,10 +143,7 @@ "items": { "type": "object", "properties": { - "name": { - "type": "string", - "description": "A name for the query" - }, + "name": { "type": "string", "description": "A name for the query" }, "enabled": { "anyOf": [ { "type": "boolean" }, @@ -218,22 +206,15 @@ ], "description": "The URL of the request" }, - "method": { - "type": "string", - "description": "The request method." - }, + "method": { "type": "string", "description": "The request method." }, "headers": { "type": "array", - "items": { - "$ref": "#/definitions/BindableNameStringValue" - }, + "items": { "$ref": "#/definitions/BindableNameStringValue" }, "description": "Extra request headers." }, "searchParams": { "type": "array", - "items": { - "$ref": "#/definitions/BindableNameStringValue" - }, + "items": { "$ref": "#/definitions/BindableNameStringValue" }, "description": "Extra url query parameters." }, "body": { @@ -245,9 +226,7 @@ "content": { "anyOf": [ { "type": "string" }, - { - "$ref": "#/definitions/JsExpressionBinding" - }, + { "$ref": "#/definitions/JsExpressionBinding" }, { "$ref": "#/definitions/EnvBinding" } ] }, @@ -259,15 +238,10 @@ { "type": "object", "properties": { - "kind": { - "type": "string", - "const": "urlEncoded" - }, + "kind": { "type": "string", "const": "urlEncoded" }, "content": { "type": "array", - "items": { - "$ref": "#/definitions/BindableNameStringValue" - } + "items": { "$ref": "#/definitions/BindableNameStringValue" } } }, "required": ["kind", "content"], @@ -288,18 +262,14 @@ "anyOf": [ { "type": "object", - "properties": { - "kind": { "type": "string", "const": "raw" } - }, + "properties": { "kind": { "type": "string", "const": "raw" } }, "required": ["kind"], "additionalProperties": false, "description": "Don't interpret this body at all." }, { "type": "object", - "properties": { - "kind": { "type": "string", "const": "json" } - }, + "properties": { "kind": { "type": "string", "const": "json" } }, "required": ["kind"], "additionalProperties": false, "description": "Interpret the fetch response as JSON" @@ -319,9 +289,7 @@ }, { "type": "object", - "properties": { - "kind": { "type": "string", "const": "xml" } - }, + "properties": { "kind": { "type": "string", "const": "xml" } }, "required": ["kind"], "additionalProperties": false, "description": "Interpret the fetch response as XML" @@ -445,12 +413,8 @@ "type": "object", "properties": { "mode": { "type": "string", "enum": ["light", "dark"] }, - "primary": { - "$ref": "#/definitions/SimplePaletteColorOptions" - }, - "secondary": { - "$ref": "#/definitions/SimplePaletteColorOptions" - } + "primary": { "$ref": "#/definitions/SimplePaletteColorOptions" }, + "secondary": { "$ref": "#/definitions/SimplePaletteColorOptions" } }, "additionalProperties": true } @@ -473,10 +437,7 @@ "JsExpressionBinding": { "type": "object", "properties": { - "$$jsExpression": { - "type": "string", - "description": "The expression to be evaluated." - } + "$$jsExpression": { "type": "string", "description": "The expression to be evaluated." } }, "required": ["$$jsExpression"], "additionalProperties": false, @@ -485,10 +446,7 @@ "EnvBinding": { "type": "object", "properties": { - "$$env": { - "type": "string", - "description": "The name of an environment variable." - } + "$$env": { "type": "string", "description": "The name of an environment variable." } }, "required": ["$$env"], "additionalProperties": false, @@ -497,10 +455,7 @@ "JsExpressionAction": { "type": "object", "properties": { - "$$jsExpressionAction": { - "type": "string", - "description": "The code to be executed." - } + "$$jsExpressionAction": { "type": "string", "description": "The code to be executed." } }, "required": ["$$jsExpressionAction"], "additionalProperties": false, @@ -512,10 +467,7 @@ "$$navigationAction": { "type": "object", "properties": { - "page": { - "type": "string", - "description": "The page that is being navigated to" - }, + "page": { "type": "string", "description": "The page that is being navigated to" }, "parameters": { "type": "object", "additionalProperties": { @@ -543,10 +495,7 @@ { "type": "boolean" }, { "type": "null" }, { "type": "array", "items": { "$ref": "#/definitions/BindableProp" } }, - { - "type": "object", - "additionalProperties": { "$ref": "#/definitions/BindableProp" } - }, + { "type": "object", "additionalProperties": { "$ref": "#/definitions/BindableProp" } }, { "$ref": "#/definitions/JsExpressionBinding" }, { "$ref": "#/definitions/EnvBinding" }, { "$ref": "#/definitions/JsExpressionAction" }, diff --git a/package.json b/package.json index 7ef7689230f..301be774c37 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "@testing-library/react": "14.2.2", "@types/archiver": "6.0.2", "@types/gtag.js": "0.0.19", + "@types/invariant": "2.2.37", "@types/node": "20.12.5", "@typescript-eslint/eslint-plugin": "7.6.0", "@typescript-eslint/parser": "7.6.0", @@ -80,11 +81,13 @@ "vitest-fail-on-console": "0.6.3" }, "dependencies": { + "@types/lodash": "4.17.0", "archiver": "7.0.1", "cross-env": "7.0.3", "dotenv-cli": "7.4.1", "eslint-import-resolver-exports": "1.0.0-beta.5", "inquirer": "9.2.17", + "invariant": "2.2.4", "lodash": "4.17.21", "semver": "7.6.0", "tsup": "8.0.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e119b2efa1f..f89065a2cc2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,6 +11,9 @@ importers: .: dependencies: + '@types/lodash': + specifier: 4.17.0 + version: 4.17.0 archiver: specifier: 7.0.1 version: 7.0.1 @@ -26,6 +29,9 @@ importers: inquirer: specifier: 9.2.17 version: 9.2.17 + invariant: + specifier: 2.2.4 + version: 2.2.4 lodash: specifier: 4.17.21 version: 4.17.21 @@ -75,6 +81,9 @@ importers: '@types/gtag.js': specifier: 0.0.19 version: 0.0.19 + '@types/invariant': + specifier: 2.2.37 + version: 2.2.37 '@types/node': specifier: 20.12.5 version: 20.12.5 diff --git a/scripts/docs/buildComponentsReference.ts b/scripts/docs/buildComponentsReference.ts index ba6473946ab..bc9991063ce 100644 --- a/scripts/docs/buildComponentsReference.ts +++ b/scripts/docs/buildComponentsReference.ts @@ -1,9 +1,10 @@ import * as path from 'path'; import * as fs from 'fs/promises'; -import { kebabCase } from 'lodash'; +import kebabCase from 'lodash/kebabCase'; import type { ComponentConfig } from '@toolpad/studio-runtime'; import * as builtins from '@toolpad/studio-components'; import * as toolpadCore from '@toolpad/studio-runtime'; +import invariant from 'invariant'; import { escapeCell, writePrettifiedFile } from './utils'; const AUTO_GENERATED_WARNING = @@ -11,7 +12,6 @@ const AUTO_GENERATED_WARNING = const currentDirectory = __dirname; const projectRoot = path.resolve(currentDirectory, '..', '..'); -const prettierConfigPath = path.resolve(projectRoot, 'prettier.config.js'); const docsRoot = path.resolve(projectRoot, 'docs'); const absolutePathRoot = '/toolpad/studio/reference/components'; const componentDocsRoot = path.resolve(docsRoot, `data${absolutePathRoot}`); @@ -33,7 +33,6 @@ export default function Page() { return ; } `, - prettierConfigPath, ); } @@ -68,7 +67,9 @@ export async function buildComponentsReference() { '', '| Name | Type | Default | Description |', '|:-----|:-----|:--------|:------------|', - ...Object.entries(config.argTypes).map(([argName, argType]) => { + ...Object.entries(config.argTypes ?? {}).map(([argName, argType]) => { + invariant(argType, 'argType not found'); + invariant(argType.type, 'argType type not found'); const formattedName = `${escapeCell(argName)}`; const defaultValue = argType.default; const formattedType = `${escapeCell(argType.type)}`; @@ -88,7 +89,7 @@ export async function buildComponentsReference() { ].join('\n'); const mdFilePath = path.resolve(componentDocsRoot, `${slug}.md`); - await writePrettifiedFile(mdFilePath, md, prettierConfigPath); + await writePrettifiedFile(mdFilePath, md); await writePageFile(mdFilePath); } @@ -108,7 +109,7 @@ export async function buildComponentsReference() { ].join('\n'); const indexMdFilePath = path.resolve(componentDocsRoot, `index.md`); - await writePrettifiedFile(indexMdFilePath, indexMd, prettierConfigPath); + await writePrettifiedFile(indexMdFilePath, indexMd); await writePageFile(indexMdFilePath); const manifest = { @@ -127,5 +128,5 @@ export async function buildComponentsReference() { }; const configJsonPath = path.resolve(componentDocsRoot, `manifest.json`); - await writePrettifiedFile(configJsonPath, JSON.stringify(manifest, null, 2), prettierConfigPath); + await writePrettifiedFile(configJsonPath, JSON.stringify(manifest, null, 2)); } diff --git a/scripts/docs/generateJsonSchemas.ts b/scripts/docs/generateJsonSchemas.ts index 198a3d74404..8da9a636727 100644 --- a/scripts/docs/generateJsonSchemas.ts +++ b/scripts/docs/generateJsonSchemas.ts @@ -2,8 +2,8 @@ import * as fs from 'fs/promises'; import * as path from 'path'; import { zodToJsonSchema } from 'zod-to-json-schema'; import * as z from 'zod'; -import * as prettier from 'prettier'; import { META } from '../../packages/toolpad-studio/src/server/schema'; +import { writePrettifiedFile } from './utils'; const currentDirectory = __dirname; const SCHEMA_DIR = path.resolve(currentDirectory, '../../docs/schemas/v1/'); @@ -15,10 +15,8 @@ async function main() { const jsonSchema = zodToJsonSchema(z.object(META.schemas), { definitions: META.definitions, }); - await prettier.resolveConfigFile(schemaFile); const jsonContent = JSON.stringify(jsonSchema); - const fileContent = await prettier.format(jsonContent, { filepath: schemaFile }); - await fs.writeFile(schemaFile, fileContent); + await writePrettifiedFile(schemaFile, jsonContent); } main().catch((err) => { diff --git a/scripts/docs/utils.ts b/scripts/docs/utils.ts index 9a80dde2c12..5908acf5273 100644 --- a/scripts/docs/utils.ts +++ b/scripts/docs/utils.ts @@ -9,22 +9,14 @@ export function escapeCell(value: string): string { .replace(/\r?\n/g, '
'); } -export async function writePrettifiedFile( - filename: string, - data: string, - prettierConfigPath: string, -) { - const prettierConfig = await prettier.resolveConfig(filename, { - config: prettierConfigPath, - }); +export async function writePrettifiedFile(filepath: string, data: string) { + const prettierConfig = await prettier.resolveConfig(filepath); - if (prettierConfig === null) { - throw new Error( - `Could not resolve config for '${filename}' using prettier config path '${prettierConfigPath}'.`, - ); + if (!prettierConfig) { + throw new Error(`Could not resolve config for '${filepath}'.`); } - const content = await prettier.format(data, { ...prettierConfig, filepath: filename }); + const content = await prettier.format(data, { ...prettierConfig, filepath }); - await fs.writeFile(filename, content, { encoding: 'utf8' }); + await fs.writeFile(filepath, content, { encoding: 'utf8' }); } diff --git a/scripts/tsconfig.json b/scripts/tsconfig.json index 23961b286c2..1db844111cc 100644 --- a/scripts/tsconfig.json +++ b/scripts/tsconfig.json @@ -19,6 +19,7 @@ "esModuleInterop": true, "moduleResolution": "node", "allowImportingTsExtensions": true, + "strict": true, "jsx": "react" } }