From afdf89fb120c17bc1f82959be83882c16e9a2d6d Mon Sep 17 00:00:00 2001 From: garronej Date: Fri, 25 Oct 2024 00:20:35 +0000 Subject: [PATCH] Try to run format on behaf of the user when generating new files with the CLI --- src/bin/add-story.ts | 5 +++ src/bin/eject-page.ts | 5 +++ src/bin/tools/runFormat.ts | 71 ++++++++++++++++++++++++++++++++++++++ src/bin/update-kc-gen.ts | 14 +++----- 4 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 src/bin/tools/runFormat.ts diff --git a/src/bin/add-story.ts b/src/bin/add-story.ts index b2be6d65..3b72e5b0 100644 --- a/src/bin/add-story.ts +++ b/src/bin/add-story.ts @@ -15,6 +15,7 @@ import { kebabCaseToCamelCase } from "./tools/kebabCaseToSnakeCase"; import { assert, Equals } from "tsafe/assert"; import type { BuildContext } from "./shared/buildContext"; import chalk from "chalk"; +import { runFormat } from "./tools/runFormat"; export async function command(params: { buildContext: BuildContext }) { const { buildContext } = params; @@ -121,6 +122,10 @@ export async function command(params: { buildContext: BuildContext }) { fs.writeFileSync(targetFilePath, Buffer.from(componentCode, "utf8")); + runFormat({ + packageJsonFilePath: buildContext.packageJsonFilePath + }); + console.log( [ `${chalk.green("✓")} ${chalk.bold( diff --git a/src/bin/eject-page.ts b/src/bin/eject-page.ts index e982c2f7..2cb2d9af 100644 --- a/src/bin/eject-page.ts +++ b/src/bin/eject-page.ts @@ -23,6 +23,7 @@ import { assert, Equals } from "tsafe/assert"; import type { BuildContext } from "./shared/buildContext"; import chalk from "chalk"; import { maybeDelegateCommandToCustomHandler } from "./shared/customHandler_delegate"; +import { runFormat } from "./tools/runFormat"; export async function command(params: { buildContext: BuildContext }) { const { buildContext } = params; @@ -243,6 +244,10 @@ export async function command(params: { buildContext: BuildContext }) { fs.writeFileSync(targetFilePath, Buffer.from(componentCode, "utf8")); + runFormat({ + packageJsonFilePath: buildContext.packageJsonFilePath + }); + console.log( `${chalk.green("✓")} ${chalk.bold( pathJoin(".", pathRelative(process.cwd(), targetFilePath)) diff --git a/src/bin/tools/runFormat.ts b/src/bin/tools/runFormat.ts new file mode 100644 index 00000000..b7a4d787 --- /dev/null +++ b/src/bin/tools/runFormat.ts @@ -0,0 +1,71 @@ +import * as fs from "fs"; +import { dirname as pathDirname } from "path"; +import { assert, Equals } from "tsafe/assert"; +import chalk from "chalk"; +import { id } from "tsafe/id"; +import { z } from "zod"; +import { is } from "tsafe/is"; +import * as child_process from "child_process"; + +export function runFormat(params: { packageJsonFilePath: string }) { + const { packageJsonFilePath } = params; + + const parsedPackageJson = (() => { + type ParsedPackageJson = { + scripts?: Record; + }; + + const zParsedPackageJson = (() => { + type TargetType = ParsedPackageJson; + + const zTargetType = z.object({ + scripts: z.record(z.string()).optional() + }); + + assert, TargetType>>(); + + return id>(zTargetType); + })(); + + const parsedPackageJson = JSON.parse( + fs.readFileSync(packageJsonFilePath).toString("utf8") + ); + + zParsedPackageJson.parse(parsedPackageJson); + + assert(is(parsedPackageJson)); + + return parsedPackageJson; + })(); + + const { scripts } = parsedPackageJson; + + if (scripts === undefined) { + return; + } + + for (const scriptName of ["format", "lint"]) { + if (!(scriptName in scripts)) { + continue; + } + + const command = `npm run ${scriptName}`; + + console.log(chalk.grey(`$ ${command}`)); + + try { + child_process.execSync(`npm run ${scriptName}`, { + stdio: "inherit", + cwd: pathDirname(packageJsonFilePath) + }); + } catch { + console.log( + chalk.yellow( + `\`${command}\` failed, it does not matter, please format your code manually, continuing...` + ) + ); + } + + return; + } +} diff --git a/src/bin/update-kc-gen.ts b/src/bin/update-kc-gen.ts index e1366fd4..2c758458 100644 --- a/src/bin/update-kc-gen.ts +++ b/src/bin/update-kc-gen.ts @@ -3,6 +3,7 @@ import * as fs from "fs/promises"; import { join as pathJoin } from "path"; import { existsAsync } from "./tools/fs.existsAsync"; import { maybeDelegateCommandToCustomHandler } from "./shared/customHandler_delegate"; +import { runFormat } from "./tools/runFormat"; export async function command(params: { buildContext: BuildContext }) { const { buildContext } = params; @@ -27,15 +28,8 @@ export async function command(params: { buildContext: BuildContext }) { const newContent = Buffer.from( [ - `/* prettier-ignore-start */`, ``, - `/* eslint-disable */`, - ``, - `// @ts-nocheck`, - ``, - `// noinspection JSUnusedGlobalSymbols`, - ``, - `// This file is auto-generated by Keycloakify`, + `// This file is auto-generated by Keycloakify, do not modify it manually.`, ``, `import { lazy, Suspense, type ReactNode } from "react";`, ``, @@ -93,8 +87,6 @@ export async function command(params: { buildContext: BuildContext }) { ` `, ` );`, `}`, - ``, - `/* prettier-ignore-end */`, `` ] .filter(item => typeof item === "string") @@ -108,6 +100,8 @@ export async function command(params: { buildContext: BuildContext }) { await fs.writeFile(filePath, newContent); + runFormat({ packageJsonFilePath: buildContext.packageJsonFilePath }); + delete_legacy_file: { const legacyFilePath = filePath.replace(/tsx$/, "ts");