76 lines
3.4 KiB
JavaScript
76 lines
3.4 KiB
JavaScript
import fs from "node:fs/promises";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { afterEach, describe, expect, it } from "vitest";
|
|
|
|
import { createToolctrlProject } from "./create.js";
|
|
|
|
const tempRoots = [];
|
|
|
|
async function exists(filePath) {
|
|
try {
|
|
await fs.access(filePath);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function collectFiles(dir) {
|
|
const entries = await fs.readdir(dir, { withFileTypes: true });
|
|
const files = [];
|
|
for (const entry of entries) {
|
|
const fullPath = path.join(dir, entry.name);
|
|
if (entry.isDirectory()) {
|
|
files.push(...(await collectFiles(fullPath)));
|
|
} else {
|
|
files.push(fullPath);
|
|
}
|
|
}
|
|
return files;
|
|
}
|
|
|
|
async function createSourceFixture(rootDir) {
|
|
const sourceDir = path.join(rootDir, "source");
|
|
await fs.mkdir(path.join(sourceDir, "apps", "web", "build"), { recursive: true });
|
|
await fs.mkdir(path.join(sourceDir, "docs", "feature-manifests"), { recursive: true });
|
|
await fs.mkdir(path.join(sourceDir, ".git"), { recursive: true });
|
|
await fs.writeFile(path.join(sourceDir, "package.json"), JSON.stringify({ name: "toolctrl-boilerplate", private: true, scripts: {} }, null, 2));
|
|
await fs.writeFile(path.join(sourceDir, ".env.example"), "APP_NAME=ToolCTRL\nSESSION_SECRET=\n");
|
|
await fs.writeFile(path.join(sourceDir, ".env"), "SESSION_SECRET=secret\n");
|
|
await fs.writeFile(path.join(sourceDir, "README.md"), "# ToolCTRL\n");
|
|
await fs.writeFile(path.join(sourceDir, "apps", "web", "build", "artifact.js"), "build");
|
|
await fs.writeFile(path.join(sourceDir, ".git", "HEAD"), "ref: main\n");
|
|
return sourceDir;
|
|
}
|
|
|
|
afterEach(async () => {
|
|
await Promise.all(tempRoots.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true })));
|
|
});
|
|
|
|
describe("createToolctrlProject", () => {
|
|
it("scaffolds a local workspace without secrets or build artifacts", async () => {
|
|
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "toolctrl-create-"));
|
|
tempRoots.push(tempRoot);
|
|
const sourceDir = await createSourceFixture(tempRoot);
|
|
const targetDir = path.join(tempRoot, "acme-ops");
|
|
|
|
const result = await createToolctrlProject({ sourceDir, targetDir, appName: "Acme Ops", profile: "core" });
|
|
const packageJson = JSON.parse(await fs.readFile(path.join(targetDir, "package.json"), "utf8"));
|
|
const envExample = await fs.readFile(path.join(targetDir, ".env.example"), "utf8");
|
|
const profile = JSON.parse(await fs.readFile(path.join(targetDir, "docs", "feature-manifests", "scaffold-profile.json"), "utf8"));
|
|
const files = (await collectFiles(targetDir)).map((file) => path.relative(targetDir, file));
|
|
|
|
expect(result).toMatchObject({ appName: "Acme Ops", packageName: "acme-ops", profile: "core" });
|
|
expect(packageJson.name).toBe("acme-ops");
|
|
expect(envExample).toContain("APP_NAME=Acme Ops");
|
|
expect(profile).toMatchObject({ appName: "Acme Ops", profile: "core", modules: ["core", "auth-tenancy"] });
|
|
expect(await exists(path.join(targetDir, ".git"))).toBe(false);
|
|
expect(await exists(path.join(targetDir, "node_modules"))).toBe(false);
|
|
expect(await exists(path.join(targetDir, "apps", "web", "build"))).toBe(false);
|
|
expect(await exists(path.join(targetDir, ".env"))).toBe(false);
|
|
expect(files.some((file) => /(^|\/)\.env($|\.test$|\.local$|\.production$)/.test(file))).toBe(false);
|
|
expect(files.some((file) => /(^|\/)(build|dist|coverage)(\/|$)/.test(file))).toBe(false);
|
|
});
|
|
});
|