Fix runPrettier script

This commit is contained in:
Joseph Garrone 2024-11-17 19:22:34 +01:00
parent 7c3c6d3643
commit 32fb1e2f71
2 changed files with 30 additions and 13 deletions

View File

@ -8,7 +8,7 @@ import { is } from "tsafe/is";
import { existsAsync } from "../tools/fs.existsAsync"; import { existsAsync } from "../tools/fs.existsAsync";
import { listInstalledModules } from "../tools/listInstalledModules"; import { listInstalledModules } from "../tools/listInstalledModules";
import { crawlAsync } from "../tools/crawlAsync"; import { crawlAsync } from "../tools/crawlAsync";
import { getIsPrettierAvailable, getPrettierAndConfig } from "../tools/runPrettier"; import { getIsPrettierAvailable, getPrettier } from "../tools/runPrettier";
import { readThisNpmPackageVersion } from "../tools/readThisNpmPackageVersion"; import { readThisNpmPackageVersion } from "../tools/readThisNpmPackageVersion";
import { import {
getUiModuleFileSourceCodeReadyToBeCopied, getUiModuleFileSourceCodeReadyToBeCopied,
@ -100,9 +100,9 @@ export async function getUiModuleMetas(params: {
return null; 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 () => { const installedUiModules = await (async () => {

View File

@ -4,6 +4,7 @@ import * as fsPr from "fs/promises";
import { id } from "tsafe/id"; import { id } from "tsafe/id";
import { assert } from "tsafe/assert"; import { assert } from "tsafe/assert";
import chalk from "chalk"; import chalk from "chalk";
import * as crypto from "crypto";
getIsPrettierAvailable.cache = id<boolean | undefined>(undefined); getIsPrettierAvailable.cache = id<boolean | undefined>(undefined);
@ -25,28 +26,42 @@ export async function getIsPrettierAvailable(): Promise<boolean> {
return isPrettierAvailable; return isPrettierAvailable;
} }
type PrettierAndConfig = { type PrettierAndConfigHash = {
prettier: typeof import("prettier"); prettier: typeof import("prettier");
config: import("prettier").Options | null; configHash: string;
}; };
getPrettierAndConfig.cache = id<PrettierAndConfig | undefined>(undefined); getPrettier.cache = id<PrettierAndConfigHash | undefined>(undefined);
export async function getPrettierAndConfig(): Promise<PrettierAndConfig> { export async function getPrettier(): Promise<PrettierAndConfigHash> {
assert(getIsPrettierAvailable()); assert(getIsPrettierAvailable());
if (getPrettierAndConfig.cache !== undefined) { if (getPrettier.cache !== undefined) {
return getPrettierAndConfig.cache; return getPrettier.cache;
} }
const prettier = await import("prettier"); 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, prettier,
config: await prettier.resolveConfig(pathJoin(getNodeModulesBinDirPath(), "..")) configHash
}; };
getPrettierAndConfig.cache = prettierAndConfig; getPrettier.cache = prettierAndConfig;
return prettierAndConfig; return prettierAndConfig;
} }
@ -60,7 +75,7 @@ export async function runPrettier(params: {
let formattedSourceCode: string; let formattedSourceCode: string;
try { try {
const { prettier, config } = await getPrettierAndConfig(); const { prettier } = await getPrettier();
const { ignored, inferredParser } = await prettier.getFileInfo(filePath, { const { ignored, inferredParser } = await prettier.getFileInfo(filePath, {
resolveConfig: true resolveConfig: true
@ -70,6 +85,8 @@ export async function runPrettier(params: {
return sourceCode; return sourceCode;
} }
const config = await prettier.resolveConfig(filePath);
formattedSourceCode = await prettier.format(sourceCode, { formattedSourceCode = await prettier.format(sourceCode, {
...config, ...config,
filePath, filePath,