feat(generators): splicePluginRulesAt + splicePluginImportsAt + addBoundariesEntry + emitTemplateTree helpers
This commit is contained in:
@@ -11,6 +11,10 @@ import { join } from "node:path";
|
||||
import {
|
||||
assertOptionalPackageNotPresent,
|
||||
addToTranspilePackages,
|
||||
splicePluginRulesAt,
|
||||
splicePluginImportsAt,
|
||||
addBoundariesEntry,
|
||||
emitTemplateTree,
|
||||
} from "./core-package-utils";
|
||||
|
||||
describe("assertOptionalPackageNotPresent", () => {
|
||||
@@ -62,3 +66,83 @@ describe("addToTranspilePackages", () => {
|
||||
expect(result.match(/@repo\/core-realtime/g)?.length).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("splicePluginRulesAt", () => {
|
||||
it("inserts rule block at the named anchor", () => {
|
||||
const tmp = mkdtempSync(join(tmpdir(), "core-pkg-"));
|
||||
const path = join(tmp, "base.js");
|
||||
writeFileSync(
|
||||
path,
|
||||
`before\n// <gen:realtime-rules>\nafter\n`,
|
||||
);
|
||||
splicePluginRulesAt(path, "realtime-rules", "INSERTED_BLOCK");
|
||||
const result = readFileSync(path, "utf8");
|
||||
expect(result).toContain("// <gen:realtime-rules>\nINSERTED_BLOCK\nafter");
|
||||
});
|
||||
|
||||
it("is idempotent — re-inserting same block at anchor is a no-op", () => {
|
||||
const tmp = mkdtempSync(join(tmpdir(), "core-pkg-"));
|
||||
const path = join(tmp, "base.js");
|
||||
writeFileSync(
|
||||
path,
|
||||
`// <gen:realtime-rules>\nINSERTED_BLOCK\nrest\n`,
|
||||
);
|
||||
splicePluginRulesAt(path, "realtime-rules", "INSERTED_BLOCK");
|
||||
const result = readFileSync(path, "utf8");
|
||||
expect(result.match(/INSERTED_BLOCK/g)?.length).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("addBoundariesEntry", () => {
|
||||
it("inserts entry before the packages/core-* wildcard", () => {
|
||||
const tmp = mkdtempSync(join(tmpdir(), "core-pkg-"));
|
||||
const path = join(tmp, "base.js");
|
||||
writeFileSync(
|
||||
path,
|
||||
`"boundaries/elements": [
|
||||
{ type: "core-composition", pattern: "packages/core-cms" },
|
||||
{ type: "core", pattern: "packages/core-*" },
|
||||
{ type: "feature", pattern: "packages/!(core-*)" },
|
||||
],`,
|
||||
);
|
||||
addBoundariesEntry(path, "packages/core-realtime", { mode: "folder" });
|
||||
const result = readFileSync(path, "utf8");
|
||||
// The new entry must appear BEFORE the packages/core-* wildcard
|
||||
const newIdx = result.indexOf(`pattern: "packages/core-realtime"`);
|
||||
const wildcardIdx = result.indexOf(`pattern: "packages/core-*"`);
|
||||
expect(newIdx).toBeGreaterThan(0);
|
||||
expect(newIdx).toBeLessThan(wildcardIdx);
|
||||
});
|
||||
|
||||
it("is idempotent — re-inserting same entry is a no-op", () => {
|
||||
const tmp = mkdtempSync(join(tmpdir(), "core-pkg-"));
|
||||
const path = join(tmp, "base.js");
|
||||
writeFileSync(
|
||||
path,
|
||||
`"boundaries/elements": [
|
||||
{ type: "core", pattern: "packages/core-realtime", mode: "folder" },
|
||||
{ type: "core", pattern: "packages/core-*" },
|
||||
],`,
|
||||
);
|
||||
addBoundariesEntry(path, "packages/core-realtime", { mode: "folder" });
|
||||
const result = readFileSync(path, "utf8");
|
||||
expect(result.match(/packages\/core-realtime/g)?.length).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("emitTemplateTree", () => {
|
||||
it("produces an `add` plop action per .hbs file in the template directory", () => {
|
||||
// emitTemplateTree reads from turbo/generators/templates/ — the test uses a
|
||||
// temp template directory injected via the `templatesRoot` arg.
|
||||
const tmpTemplates = mkdtempSync(join(tmpdir(), "tpl-"));
|
||||
mkdirSync(join(tmpTemplates, "core-package", "demo", "src"), { recursive: true });
|
||||
writeFileSync(join(tmpTemplates, "core-package", "demo", "package.json.hbs"), "{}");
|
||||
writeFileSync(join(tmpTemplates, "core-package", "demo", "src", "index.ts.hbs"), "export {};");
|
||||
const actions = emitTemplateTree("core-package/demo", "packages/demo", { templatesRoot: tmpTemplates });
|
||||
expect(actions).toHaveLength(2);
|
||||
expect(actions[0]!.type).toBe("add");
|
||||
const paths = actions.map((a) => (a as { path: string }).path);
|
||||
expect(paths).toContain("packages/demo/package.json");
|
||||
expect(paths).toContain("packages/demo/src/index.ts");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user