Make runPrettier work when project is linked

This commit is contained in:
Joseph Garrone 2024-11-18 04:46:06 +01:00
parent e2975503a4
commit 5517d6baf4

View File

@ -1,10 +1,13 @@
import { getNodeModulesBinDirPath } from "./nodeModulesBinDirPath"; 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 * 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"; import * as crypto from "crypto";
import { is } from "tsafe/is";
import { symToStr } from "tsafe/symToStr";
import { readThisNpmPackageVersion } from "./readThisNpmPackageVersion";
getIsPrettierAvailable.cache = id<boolean | undefined>(undefined); getIsPrettierAvailable.cache = id<boolean | undefined>(undefined);
@ -40,7 +43,25 @@ export async function getPrettier(): Promise<PrettierAndConfigHash> {
return getPrettier.cache; return getPrettier.cache;
} }
const prettier = await import("prettier"); let prettier = id<typeof import("prettier") | undefined>(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<undefined>(prettier));
break import_prettier;
}
prettier = await import("prettier");
}
const configHash = await (async () => { const configHash = await (async () => {
const configFilePath = await prettier.resolveConfigFile( const configFilePath = await prettier.resolveConfigFile(