Two helpers for use by per-package generator actions: - assertOptionalPackageNotPresent — guards against double-scaffolding - addToTranspilePackages — idempotent alphabetical splice into next.config.mjs Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
65 lines
2.0 KiB
TypeScript
65 lines
2.0 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import {
|
|
existsSync,
|
|
mkdtempSync,
|
|
mkdirSync,
|
|
writeFileSync,
|
|
readFileSync,
|
|
} from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import {
|
|
assertOptionalPackageNotPresent,
|
|
addToTranspilePackages,
|
|
} from "./core-package-utils";
|
|
|
|
describe("assertOptionalPackageNotPresent", () => {
|
|
it("throws if packages/<name>/ exists in cwd", () => {
|
|
const tmp = mkdtempSync(join(tmpdir(), "core-pkg-"));
|
|
mkdirSync(join(tmp, "packages", "core-foo"), { recursive: true });
|
|
expect(() => assertOptionalPackageNotPresent("core-foo", tmp)).toThrow(/already exists/);
|
|
});
|
|
|
|
it("returns silently if packages/<name>/ is absent", () => {
|
|
const tmp = mkdtempSync(join(tmpdir(), "core-pkg-"));
|
|
expect(() => assertOptionalPackageNotPresent("core-foo", tmp)).not.toThrow();
|
|
});
|
|
});
|
|
|
|
describe("addToTranspilePackages", () => {
|
|
it("inserts package name alphabetically into transpilePackages array", () => {
|
|
const tmp = mkdtempSync(join(tmpdir(), "core-pkg-"));
|
|
const cfgPath = join(tmp, "next.config.mjs");
|
|
writeFileSync(
|
|
cfgPath,
|
|
`const nextConfig = {
|
|
transpilePackages: [
|
|
"@repo/core-cms",
|
|
"@repo/core-shared",
|
|
],
|
|
};
|
|
`,
|
|
);
|
|
addToTranspilePackages(cfgPath, "@repo/core-realtime");
|
|
const result = readFileSync(cfgPath, "utf8");
|
|
// Order should be: core-cms, core-realtime, core-shared
|
|
const order = result.match(/@repo\/core-\w+/g) ?? [];
|
|
expect(order).toEqual(["@repo/core-cms", "@repo/core-realtime", "@repo/core-shared"]);
|
|
});
|
|
|
|
it("is idempotent — duplicate insertion is a no-op", () => {
|
|
const tmp = mkdtempSync(join(tmpdir(), "core-pkg-"));
|
|
const cfgPath = join(tmp, "next.config.mjs");
|
|
writeFileSync(
|
|
cfgPath,
|
|
`const nextConfig = {
|
|
transpilePackages: ["@repo/core-realtime"],
|
|
};
|
|
`,
|
|
);
|
|
addToTranspilePackages(cfgPath, "@repo/core-realtime");
|
|
const result = readFileSync(cfgPath, "utf8");
|
|
expect(result.match(/@repo\/core-realtime/g)?.length).toBe(1);
|
|
});
|
|
});
|