From 32fb1e2f71a166ef50fd44936dc1c166ed5c539c Mon Sep 17 00:00:00 2001 From: Joseph Garrone Date: Sun, 17 Nov 2024 19:22:34 +0100 Subject: [PATCH] Fix runPrettier script --- src/bin/postinstall/uiModuleMeta.ts | 6 ++--- src/bin/tools/runPrettier.ts | 37 +++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/bin/postinstall/uiModuleMeta.ts b/src/bin/postinstall/uiModuleMeta.ts index 24cdcd33..8ad49514 100644 --- a/src/bin/postinstall/uiModuleMeta.ts +++ b/src/bin/postinstall/uiModuleMeta.ts @@ -8,7 +8,7 @@ import { is } from "tsafe/is"; import { existsAsync } from "../tools/fs.existsAsync"; import { listInstalledModules } from "../tools/listInstalledModules"; import { crawlAsync } from "../tools/crawlAsync"; -import { getIsPrettierAvailable, getPrettierAndConfig } from "../tools/runPrettier"; +import { getIsPrettierAvailable, getPrettier } from "../tools/runPrettier"; import { readThisNpmPackageVersion } from "../tools/readThisNpmPackageVersion"; import { getUiModuleFileSourceCodeReadyToBeCopied, @@ -100,9 +100,9 @@ export async function getUiModuleMetas(params: { return null; } - const { config } = await getPrettierAndConfig(); + const { configHash } = await getPrettier(); - return crypto.createHash("sha256").update(JSON.stringify(config)).digest("hex"); + return configHash; })(); const installedUiModules = await (async () => { diff --git a/src/bin/tools/runPrettier.ts b/src/bin/tools/runPrettier.ts index 7029c8f9..134ae213 100644 --- a/src/bin/tools/runPrettier.ts +++ b/src/bin/tools/runPrettier.ts @@ -4,6 +4,7 @@ import * as fsPr from "fs/promises"; import { id } from "tsafe/id"; import { assert } from "tsafe/assert"; import chalk from "chalk"; +import * as crypto from "crypto"; getIsPrettierAvailable.cache = id(undefined); @@ -25,28 +26,42 @@ export async function getIsPrettierAvailable(): Promise { return isPrettierAvailable; } -type PrettierAndConfig = { +type PrettierAndConfigHash = { prettier: typeof import("prettier"); - config: import("prettier").Options | null; + configHash: string; }; -getPrettierAndConfig.cache = id(undefined); +getPrettier.cache = id(undefined); -export async function getPrettierAndConfig(): Promise { +export async function getPrettier(): Promise { assert(getIsPrettierAvailable()); - if (getPrettierAndConfig.cache !== undefined) { - return getPrettierAndConfig.cache; + if (getPrettier.cache !== undefined) { + return getPrettier.cache; } const prettier = await import("prettier"); - const prettierAndConfig: PrettierAndConfig = { + const configHash = await (async () => { + const configFilePath = await prettier.resolveConfigFile( + pathJoin(getNodeModulesBinDirPath(), "..") + ); + + if (configFilePath === null) { + return ""; + } + + const data = await fsPr.readFile(configFilePath); + + return crypto.createHash("sha256").update(data).digest("hex"); + })(); + + const prettierAndConfig: PrettierAndConfigHash = { prettier, - config: await prettier.resolveConfig(pathJoin(getNodeModulesBinDirPath(), "..")) + configHash }; - getPrettierAndConfig.cache = prettierAndConfig; + getPrettier.cache = prettierAndConfig; return prettierAndConfig; } @@ -60,7 +75,7 @@ export async function runPrettier(params: { let formattedSourceCode: string; try { - const { prettier, config } = await getPrettierAndConfig(); + const { prettier } = await getPrettier(); const { ignored, inferredParser } = await prettier.getFileInfo(filePath, { resolveConfig: true @@ -70,6 +85,8 @@ export async function runPrettier(params: { return sourceCode; } + const config = await prettier.resolveConfig(filePath); + formattedSourceCode = await prettier.format(sourceCode, { ...config, filePath,