79 lines
2.6 KiB
JavaScript
79 lines
2.6 KiB
JavaScript
import { cp, mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
|
|
import { spawnSync } from "node:child_process";
|
|
import path from "node:path";
|
|
|
|
const fixtureRoot = path.resolve(".tmp/sample-removal");
|
|
const sampleRoot = path.resolve("src/sample/contract-fixture");
|
|
const sourceRoot = path.resolve("src");
|
|
const pnpmCli = /** @type {string} */ (process.env.npm_execpath);
|
|
|
|
/** @param {string} directory @returns {Promise<string[]>} */
|
|
async function sourceFiles(directory) {
|
|
const entries = await readdir(directory, { withFileTypes: true });
|
|
const nested = /** @type {string[][]} */ (await Promise.all(
|
|
entries.map((entry) => {
|
|
const target = path.join(directory, entry.name);
|
|
return entry.isDirectory() ? sourceFiles(target) : [target];
|
|
}),
|
|
));
|
|
return nested.flat();
|
|
}
|
|
|
|
await rm(fixtureRoot, { recursive: true, force: true });
|
|
await mkdir(fixtureRoot, { recursive: true });
|
|
|
|
const incomingImports = [];
|
|
for (const sourceFile of await sourceFiles(sourceRoot)) {
|
|
if (sourceFile.startsWith(sampleRoot)) continue;
|
|
const content = await readFile(sourceFile, "utf8");
|
|
if (/from\s+["'][^"']*sample\/contract-fixture/.test(content)) {
|
|
incomingImports.push(path.relative(".", sourceFile));
|
|
}
|
|
}
|
|
|
|
let buildStatus = 1;
|
|
if (incomingImports.length === 0) {
|
|
await cp("src", path.join(fixtureRoot, "src"), {
|
|
recursive: true,
|
|
filter: (source) => !source.startsWith(sampleRoot),
|
|
});
|
|
await cp("public", path.join(fixtureRoot, "public"), { recursive: true });
|
|
await cp("index.html", path.join(fixtureRoot, "index.html"));
|
|
await cp("vite.config.js", path.join(fixtureRoot, "vite.config.js"));
|
|
|
|
const result = spawnSync(
|
|
process.execPath,
|
|
[
|
|
pnpmCli,
|
|
"exec",
|
|
"vite",
|
|
"build",
|
|
fixtureRoot,
|
|
"--outDir",
|
|
path.join(fixtureRoot, "dist"),
|
|
],
|
|
{ stdio: "inherit" },
|
|
);
|
|
buildStatus = result.status ?? 1;
|
|
}
|
|
|
|
await mkdir("artifacts/tests", { recursive: true });
|
|
const passed = incomingImports.length === 0 && buildStatus === 0;
|
|
await writeFile(
|
|
"artifacts/tests/sample-removal.xml",
|
|
`<?xml version="1.0" encoding="UTF-8"?>\n` +
|
|
`<testsuite name="sample-removal" tests="2" failures="${passed ? 0 : 1}">` +
|
|
`<testcase name="no-product-import"/>` +
|
|
`<testcase name="production-build">${passed ? "" : "<failure/>"}</testcase>` +
|
|
`</testsuite>\n`,
|
|
);
|
|
await rm(fixtureRoot, { recursive: true, force: true });
|
|
|
|
if (!passed) {
|
|
process.stderr.write(
|
|
`Sample removal failed. Incoming imports: ${incomingImports.join(", ")}\n`,
|
|
);
|
|
process.exit(1);
|
|
}
|
|
process.stdout.write("Sample removal smoke: PASS\n");
|