From b8d4daf4c12dc648d2c32ace6d6e33d1a74ab4ee Mon Sep 17 00:00:00 2001 From: Joseph Garrone Date: Sun, 17 Nov 2024 03:38:38 +0100 Subject: [PATCH] Temporarely restore runFormat (for merge conflicts) --- src/bin/tools/runFormat.ts | 76 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/bin/tools/runFormat.ts diff --git a/src/bin/tools/runFormat.ts b/src/bin/tools/runFormat.ts new file mode 100644 index 00000000..fd2eb108 --- /dev/null +++ b/src/bin/tools/runFormat.ts @@ -0,0 +1,76 @@ +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; + } + + const scriptKeys = Object.keys(scripts); + const scriptNames = scriptKeys.filter(scriptKey => + scriptKey.trim().match(/^(lint|format)/) + ); + + for (const scriptName of scriptNames) { + 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; + } +}