mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 22:11:12 +00:00
70 lines
2.2 KiB
TypeScript
70 lines
2.2 KiB
TypeScript
import { spawnSync } from "node:child_process";
|
|
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
import { dirname, join, resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { z } from "zod";
|
|
import { createTestTempDirectory } from "./temporaryDirectory.js";
|
|
|
|
const repository = fileURLToPath(new URL("../../", import.meta.url));
|
|
const configurationSchema = z.looseObject({
|
|
jsPlugins: z
|
|
.array(z.union([z.string(), z.looseObject({ specifier: z.string() })]))
|
|
.optional(),
|
|
});
|
|
|
|
/** Preserve repository lint policy while resolving local plugins at their owner. */
|
|
export const writeRepositoryOxlintConfiguration = async (
|
|
project: string,
|
|
): Promise<string> => {
|
|
const configuration = configurationSchema.parse(
|
|
JSON.parse(await readFile(join(repository, ".oxlintrc.json"), "utf8")),
|
|
);
|
|
const rebase = (specifier: string): string =>
|
|
specifier.startsWith(".") ? resolve(repository, specifier) : specifier;
|
|
const path = join(project, ".oxlintrc.json");
|
|
await writeFile(
|
|
path,
|
|
JSON.stringify({
|
|
...configuration,
|
|
...(configuration.jsPlugins === undefined
|
|
? {}
|
|
: {
|
|
jsPlugins: configuration.jsPlugins.map((plugin) =>
|
|
typeof plugin === "string"
|
|
? rebase(plugin)
|
|
: { ...plugin, specifier: rebase(plugin.specifier) },
|
|
),
|
|
}),
|
|
}),
|
|
);
|
|
return path;
|
|
};
|
|
|
|
export const lintRepositoryProject = async (
|
|
prefix: string,
|
|
files: readonly (readonly [filename: string, source: string])[],
|
|
) => {
|
|
const root = await createTestTempDirectory(prefix);
|
|
const config = await writeRepositoryOxlintConfiguration(root);
|
|
for (const [file, source] of files) {
|
|
const path = join(root, file);
|
|
await mkdir(dirname(path), { recursive: true });
|
|
await writeFile(path, source);
|
|
}
|
|
const result = spawnSync(
|
|
process.execPath,
|
|
[
|
|
join(repository, "node_modules", "oxlint", "bin", "oxlint"),
|
|
"--threads=1",
|
|
"-c",
|
|
config,
|
|
"--format",
|
|
"json",
|
|
...files.map(([file]) => file),
|
|
],
|
|
{ cwd: root, encoding: "utf8" },
|
|
);
|
|
if (result.error !== undefined) throw result.error;
|
|
return result;
|
|
};
|