From 5517d6baf45bf17e449ae7d20ff6f1ddb96f81b7 Mon Sep 17 00:00:00 2001 From: Joseph Garrone Date: Mon, 18 Nov 2024 04:46:06 +0100 Subject: [PATCH] Make runPrettier work when project is linked --- src/bin/tools/runPrettier.ts | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/bin/tools/runPrettier.ts b/src/bin/tools/runPrettier.ts index 134ae213..2e1750d1 100644 --- a/src/bin/tools/runPrettier.ts +++ b/src/bin/tools/runPrettier.ts @@ -1,10 +1,13 @@ import { getNodeModulesBinDirPath } from "./nodeModulesBinDirPath"; -import { join as pathJoin } from "path"; +import { join as pathJoin, resolve as pathResolve } from "path"; 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"; +import { is } from "tsafe/is"; +import { symToStr } from "tsafe/symToStr"; +import { readThisNpmPackageVersion } from "./readThisNpmPackageVersion"; getIsPrettierAvailable.cache = id(undefined); @@ -40,7 +43,25 @@ export async function getPrettier(): Promise { return getPrettier.cache; } - const prettier = await import("prettier"); + let prettier = id(undefined); + + import_prettier: { + // NOTE: When module is linked we want to make sure we import the correct version + // of prettier, that is the one of the project, not the one of this repo. + // So we do a sketchy eval to bypass ncc. + // We make sure to only do that when linking, otherwise we import properly. + if (readThisNpmPackageVersion() === "0.0.0") { + eval( + `${symToStr({ prettier })} = require("${pathResolve(pathJoin(getNodeModulesBinDirPath(), "..", "prettier"))}")` + ); + + assert(!is(prettier)); + + break import_prettier; + } + + prettier = await import("prettier"); + } const configHash = await (async () => { const configFilePath = await prettier.resolveConfigFile(