Try to run format on behaf of the user when generating new files with the CLI
This commit is contained in:
parent
26a87b8eaa
commit
afdf89fb12
@ -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(
|
||||
|
@ -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))
|
||||
|
71
src/bin/tools/runFormat.ts
Normal file
71
src/bin/tools/runFormat.ts
Normal file
@ -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<string, string>;
|
||||
};
|
||||
|
||||
const zParsedPackageJson = (() => {
|
||||
type TargetType = ParsedPackageJson;
|
||||
|
||||
const zTargetType = z.object({
|
||||
scripts: z.record(z.string()).optional()
|
||||
});
|
||||
|
||||
assert<Equals<z.infer<typeof zTargetType>, TargetType>>();
|
||||
|
||||
return id<z.ZodType<TargetType>>(zTargetType);
|
||||
})();
|
||||
|
||||
const parsedPackageJson = JSON.parse(
|
||||
fs.readFileSync(packageJsonFilePath).toString("utf8")
|
||||
);
|
||||
|
||||
zParsedPackageJson.parse(parsedPackageJson);
|
||||
|
||||
assert(is<ParsedPackageJson>(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;
|
||||
}
|
||||
}
|
@ -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 }) {
|
||||
` </Suspense>`,
|
||||
` );`,
|
||||
`}`,
|
||||
``,
|
||||
`/* 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");
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user