Spaces:
Sleeping
Sleeping
File size: 3,652 Bytes
ec675f2 c850f62 ec675f2 fdae3ee ec675f2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | import { spawn } from "child_process";
import fs from "fs";
import path from "path";
export interface ValidationResult {
success: boolean;
installLog: string;
buildLog: string;
error?: string;
duration: number; // milliseconds
}
/**
* Run a command with timeout and capture stdout/stderr
*/
function runCommand(
cmd: string,
args: string[],
cwd: string,
timeout: number = 300000 // 5 minutes default
): Promise<{ stdout: string; stderr: string; code: number | null }> {
return new Promise((resolve) => {
const child = spawn(cmd, args, { cwd, shell: true });
let stdout = "";
let stderr = "";
child.stdout.on("data", (data) => {
stdout += data.toString();
});
child.stderr.on("data", (data) => {
stderr += data.toString();
});
const timer = setTimeout(() => {
child.kill("SIGTERM");
resolve({
stdout,
stderr: stderr + "\n[ERROR] Command timed out after " + timeout + "ms",
code: null,
});
}, timeout);
child.on("close", (code) => {
clearTimeout(timer);
resolve({ stdout, stderr, code });
});
child.on("error", (err) => {
clearTimeout(timer);
resolve({
stdout,
stderr: stderr + "\n" + err.message,
code: null,
});
});
});
}
export async function validateProject(
projectDir: string,
options: { installTimeout?: number; buildTimeout?: number } = {}
): Promise<ValidationResult> {
const start = Date.now();
const installTimeout = options.installTimeout || 300000;
const buildTimeout = options.buildTimeout || 300000;
// Check if project directory exists
if (!fs.existsSync(projectDir)) {
return {
success: false,
installLog: "",
buildLog: "",
error: `Project directory not found: ${projectDir}`,
duration: 0,
};
}
// Check for package.json
const pkgPath = path.join(projectDir, "package.json");
if (!fs.existsSync(pkgPath)) {
return {
success: false,
installLog: "",
buildLog: "",
error: "No package.json found in project directory",
duration: 0,
};
}
// Step 1: npm install
const installResult = await runCommand(
"npm",
["install", "--no-audit", "--no-fund", "--loglevel=error", "--include=dev"],
projectDir,
installTimeout
);
const installLog = installResult.stdout + "\n" + installResult.stderr;
if (installResult.code !== 0) {
return {
success: false,
installLog,
buildLog: "",
error: `npm install failed with code ${installResult.code}`,
duration: Date.now() - start,
};
}
// Step 2: npm run build
const buildResult = await runCommand(
"npm",
["run", "build"],
projectDir,
buildTimeout
);
const buildLog = buildResult.stdout + "\n" + buildResult.stderr;
if (buildResult.code !== 0 && buildResult.code !== null) {
return {
success: false,
installLog,
buildLog,
error: `npm run build failed with code ${buildResult.code}`,
duration: Date.now() - start,
};
}
return {
success: true,
installLog,
buildLog,
duration: Date.now() - start,
};
}
/**
* Validate the project and write a JSON report inside the project directory
*/
export async function validateAndSave(
projectDir: string,
options?: { installTimeout?: number; buildTimeout?: number }
): Promise<ValidationResult> {
const result = await validateProject(projectDir, options);
const reportPath = path.join(projectDir, ".validation.json");
fs.writeFileSync(reportPath, JSON.stringify(result, null, 2), "utf-8");
return result;
}
|