feat(core-eslint): required-cores-installed rule
This commit is contained in:
68
packages/core-eslint/rules/required-cores-installed.js
Normal file
68
packages/core-eslint/rules/required-cores-installed.js
Normal file
@@ -0,0 +1,68 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { readManifestSource } from "./_manifest-source.js";
|
||||
import { readWorkspacePackages } from "./_workspace.js";
|
||||
|
||||
/**
|
||||
* Check whether `packages/core-<name>` exists under any of the workspace
|
||||
* globs. The glob set is small and predictable (e.g. ["apps/*", "packages/*"]);
|
||||
* we simulate matching by checking each glob's directory portion + verifying
|
||||
* `core-<name>` exists in that directory.
|
||||
*/
|
||||
function coreExistsInWorkspace(coreName, repoRoot, packageGlobs) {
|
||||
for (const glob of packageGlobs) {
|
||||
const slashStar = glob.endsWith("/*") ? glob.slice(0, -2) : null;
|
||||
if (!slashStar) continue;
|
||||
const candidate = path.join(repoRoot, slashStar, `core-${coreName}`);
|
||||
if (fs.existsSync(candidate)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @type {import("eslint").Rule.RuleModule} */
|
||||
export default {
|
||||
meta: {
|
||||
type: "problem",
|
||||
docs: {
|
||||
description:
|
||||
"Cores declared in a feature.manifest.ts's requiredCores must exist as core-<name> packages within a workspace glob.",
|
||||
},
|
||||
schema: [
|
||||
{
|
||||
type: "object",
|
||||
properties: {
|
||||
repoRoot: { type: "string" },
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
],
|
||||
messages: {
|
||||
coreNotInstalled:
|
||||
"Manifest declares requiredCores: [..., \"{{core}}\", ...] but `core-{{core}}` is not present in any workspace glob. Run `pnpm turbo gen core-package {{core}}` or drop the entry.",
|
||||
},
|
||||
},
|
||||
create(context) {
|
||||
const opts = context.options[0] ?? {};
|
||||
const repoRoot = opts.repoRoot ?? context.cwd ?? process.cwd();
|
||||
return {
|
||||
Program(node) {
|
||||
const filename = context.filename;
|
||||
if (!filename.endsWith("/feature.manifest.ts") && !filename.endsWith("\\feature.manifest.ts")) {
|
||||
return;
|
||||
}
|
||||
const manifest = readManifestSource(filename);
|
||||
if (!manifest) return;
|
||||
const globs = readWorkspacePackages(repoRoot);
|
||||
for (const core of manifest.requiredCores) {
|
||||
if (!coreExistsInWorkspace(core, repoRoot, globs)) {
|
||||
context.report({
|
||||
node,
|
||||
messageId: "coreNotInstalled",
|
||||
data: { core },
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
95
packages/core-eslint/rules/required-cores-installed.test.js
Normal file
95
packages/core-eslint/rules/required-cores-installed.test.js
Normal file
@@ -0,0 +1,95 @@
|
||||
import { describe, it } from "vitest";
|
||||
import { RuleTester } from "eslint";
|
||||
import path from "node:path";
|
||||
import os from "node:os";
|
||||
import fs from "node:fs";
|
||||
import rule from "./required-cores-installed.js";
|
||||
|
||||
function makeFixture({ workspacePackages, manifestCores }) {
|
||||
const repoRoot = fs.mkdtempSync(path.join(os.tmpdir(), "rci-"));
|
||||
fs.writeFileSync(
|
||||
path.join(repoRoot, "pnpm-workspace.yaml"),
|
||||
`packages:\n${workspacePackages.map((p) => ` - "${p}"`).join("\n")}\n`,
|
||||
);
|
||||
const manifestDir = path.join(repoRoot, "packages", "demo", "src");
|
||||
fs.mkdirSync(manifestDir, { recursive: true });
|
||||
// We also need each declared core to exist as a package directory for the glob to resolve.
|
||||
for (const core of manifestCores) {
|
||||
fs.mkdirSync(path.join(repoRoot, "packages", `core-${core}`), { recursive: true });
|
||||
}
|
||||
const manifest = path.join(manifestDir, "feature.manifest.ts");
|
||||
fs.writeFileSync(
|
||||
manifest,
|
||||
`export const demoManifest = defineFeature({
|
||||
name: "demo",
|
||||
requiredCores: [${manifestCores.map((c) => `"${c}"`).join(", ")}],
|
||||
useCases: {},
|
||||
realtimeChannels: [],
|
||||
jobs: [],
|
||||
} as const);`,
|
||||
);
|
||||
return { repoRoot, manifest };
|
||||
}
|
||||
|
||||
const tester = new RuleTester({ languageOptions: { ecmaVersion: "latest", sourceType: "module" } });
|
||||
|
||||
describe("required-cores-installed", () => {
|
||||
it("passes when all declared cores are present as core-<x> packages under a workspace glob", () => {
|
||||
const { repoRoot, manifest } = makeFixture({
|
||||
workspacePackages: ["packages/*"],
|
||||
manifestCores: ["audit", "events"],
|
||||
});
|
||||
tester.run("required-cores-installed", rule, {
|
||||
valid: [
|
||||
{
|
||||
filename: manifest,
|
||||
code: fs.readFileSync(manifest, "utf8"),
|
||||
options: [{ repoRoot }],
|
||||
},
|
||||
],
|
||||
invalid: [],
|
||||
});
|
||||
});
|
||||
|
||||
it("fires for any declared core that has no matching package", () => {
|
||||
const repoRoot = fs.mkdtempSync(path.join(os.tmpdir(), "rci-"));
|
||||
fs.writeFileSync(path.join(repoRoot, "pnpm-workspace.yaml"), `packages:\n - "packages/*"\n`);
|
||||
const manifestDir = path.join(repoRoot, "packages", "demo", "src");
|
||||
fs.mkdirSync(manifestDir, { recursive: true });
|
||||
// NB: do NOT create packages/core-realtime — that's the missing one.
|
||||
fs.mkdirSync(path.join(repoRoot, "packages", "core-audit"), { recursive: true });
|
||||
const manifest = path.join(manifestDir, "feature.manifest.ts");
|
||||
fs.writeFileSync(
|
||||
manifest,
|
||||
`export const demoManifest = defineFeature({
|
||||
name: "demo",
|
||||
requiredCores: ["audit", "realtime"],
|
||||
useCases: {},
|
||||
realtimeChannels: [],
|
||||
jobs: [],
|
||||
} as const);`,
|
||||
);
|
||||
|
||||
tester.run("required-cores-installed", rule, {
|
||||
valid: [],
|
||||
invalid: [
|
||||
{
|
||||
filename: manifest,
|
||||
code: fs.readFileSync(manifest, "utf8"),
|
||||
options: [{ repoRoot }],
|
||||
errors: [{ messageId: "coreNotInstalled", data: { core: "realtime" } }],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("is a no-op for files that are not feature.manifest.ts", () => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "rci-"));
|
||||
const other = path.join(dir, "not-a-manifest.ts");
|
||||
fs.writeFileSync(other, `export const x = 1;`);
|
||||
tester.run("required-cores-installed", rule, {
|
||||
valid: [{ filename: other, code: fs.readFileSync(other, "utf8"), options: [{ repoRoot: dir }] }],
|
||||
invalid: [],
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user