git-subtree-dir: third_party/pi git-subtree-split: d981de1229ef899957bbe968bc8dcda02a21f477
35 lines
1.5 KiB
TypeScript
35 lines
1.5 KiB
TypeScript
import { readdir, readFile } from "node:fs/promises";
|
|
import { dirname, resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { describe, expect, test } from "vitest";
|
|
|
|
const packageDirectory = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
const sourceDirectory = resolve(packageDirectory, "src");
|
|
const IMPORT_SPECIFIER = /(?:import|export)\s+(?:type\s+)?(?:[^;]*?\sfrom\s*)?["']([^"']+)["']/gu;
|
|
|
|
describe("package boundary", () => {
|
|
test("does not depend on Pi packages or files outside Chord", async () => {
|
|
const manifest = JSON.parse(await readFile(resolve(packageDirectory, "package.json"), "utf8")) as {
|
|
dependencies?: Record<string, string>;
|
|
};
|
|
expect(Object.keys(manifest.dependencies ?? {}).filter((name) => name.startsWith("@earendil-works/pi-"))).toEqual(
|
|
[],
|
|
);
|
|
|
|
const paths = (await readdir(sourceDirectory, { recursive: true })).filter((path) => path.endsWith(".ts")).sort();
|
|
const violations: string[] = [];
|
|
for (const path of paths) {
|
|
const file = resolve(sourceDirectory, path);
|
|
const source = await readFile(file, "utf8");
|
|
for (const match of source.matchAll(IMPORT_SPECIFIER)) {
|
|
const specifier = match[1]!;
|
|
if (specifier.startsWith("@earendil-works/pi-")) violations.push(`${path}: ${specifier}`);
|
|
if (specifier.startsWith(".") && !resolve(dirname(file), specifier).startsWith(`${sourceDirectory}/`)) {
|
|
violations.push(`${path}: ${specifier}`);
|
|
}
|
|
}
|
|
}
|
|
expect(violations).toEqual([]);
|
|
});
|
|
});
|