Skip to content

Commit

Permalink
[code-infra] Fix prettier in scripts (#3382)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot authored Apr 11, 2024
1 parent 25ff3ba commit 2d4256d
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 96 deletions.
91 changes: 20 additions & 71 deletions docs/schemas/v1/definitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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."
Expand All @@ -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" },
Expand All @@ -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" },
Expand Down Expand Up @@ -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": {
Expand All @@ -245,9 +226,7 @@
"content": {
"anyOf": [
{ "type": "string" },
{
"$ref": "#/definitions/JsExpressionBinding"
},
{ "$ref": "#/definitions/JsExpressionBinding" },
{ "$ref": "#/definitions/EnvBinding" }
]
},
Expand All @@ -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"],
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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": {
Expand Down Expand Up @@ -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" },
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions scripts/docs/buildComponentsReference.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
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 =
'ATTENTION: DO NOT EDIT! This file has been auto-generated using `pnpm docs:build:api`.';

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}`);
Expand All @@ -33,7 +33,6 @@ export default function Page() {
return <MarkdownDocs {...pageProps} />;
}
`,
prettierConfigPath,
);
}

Expand Down Expand Up @@ -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 = `<span class="prop-name">${escapeCell(argName)}</span>`;
const defaultValue = argType.default;
const formattedType = `<span class="prop-type">${escapeCell(argType.type)}</span>`;
Expand All @@ -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);
}
Expand All @@ -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 = {
Expand All @@ -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));
}
6 changes: 2 additions & 4 deletions scripts/docs/generateJsonSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/');
Expand All @@ -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) => {
Expand Down
20 changes: 6 additions & 14 deletions scripts/docs/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,14 @@ export function escapeCell(value: string): string {
.replace(/\r?\n/g, '<br />');
}

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' });
}
1 change: 1 addition & 0 deletions scripts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"esModuleInterop": true,
"moduleResolution": "node",
"allowImportingTsExtensions": true,
"strict": true,
"jsx": "react"
}
}

0 comments on commit 2d4256d

Please sign in to comment.