-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCodeValidator.ts
121 lines (104 loc) · 3.74 KB
/
CodeValidator.ts
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
import { spawn } from 'child_process';
import { Logger } from '@nestjs/common';
export interface ValidationResult {
success: boolean;
error?: string;
}
/**
* FrontendCodeValidator is responsible for checking the correctness of the generated frontend code.
* It runs an npm build command (or any custom build script) in the given project directory (frontendPath)
* and captures any errors produced during the build process.
*/
export class FrontendCodeValidator {
private readonly logger = new Logger('FrontendCodeValidator');
/**
* @param frontendPath - The absolute path to the generated frontend project.
*/
constructor(private readonly frontendPath: string) {}
/**
* Runs the build command (npm run build) inside the frontend project directory.
* This method returns a promise that resolves with a ValidationResult, indicating whether
* the build succeeded or failed along with any error messages.
*
* @returns A promise that resolves with the ValidationResult.
*/
public async validate(): Promise<ValidationResult> {
await this.installDependencies();
return new Promise<ValidationResult>((resolve, reject) => {
this.logger.log('Starting frontend code validation...');
// Spawn the npm build process in the provided frontend project path.
const npmProcess = spawn('npm', ['run', 'build'], {
cwd: this.frontendPath,
shell: true,
});
this.logger.log('Running npm build command in', this.frontendPath);
let stdoutBuffer = '';
let stderrBuffer = '';
npmProcess.stdout.on('data', (data: Buffer) => {
const output = data.toString();
stdoutBuffer += output;
});
npmProcess.stderr.on('data', (data: Buffer) => {
const output = data.toString();
stderrBuffer += output;
});
npmProcess.on('close', (code: number) => {
if (code !== 0) {
// Build failed – use stderr if available, else fallback to stdout.
const errorMessage = stderrBuffer || stdoutBuffer;
this.logger.verbose(
`Build process exited with code ${code}. Error: ${errorMessage}`,
);
// this.logger.error(`Build process exited with code ${code}.`);
resolve({
success: false,
error: errorMessage,
});
} else {
// Build succeeded
this.logger.log('Build process completed successfully.');
resolve({
success: true,
});
}
});
npmProcess.on('error', (err: Error) => {
reject(err);
});
});
}
public async installDependencies(): Promise<ValidationResult> {
return new Promise<ValidationResult>((resolve, reject) => {
this.logger.log('Starting npm install in', this.frontendPath);
const npmInstall = spawn('npm', ['install'], {
cwd: this.frontendPath,
shell: true,
});
let stdoutBuffer = '';
let stderrBuffer = '';
npmInstall.stdout.on('data', (data: Buffer) => {
stdoutBuffer += data.toString();
});
npmInstall.stderr.on('data', (data: Buffer) => {
stderrBuffer += data.toString();
});
npmInstall.on('close', (code: number) => {
if (code !== 0) {
const errorMessage = stderrBuffer || stdoutBuffer;
this.logger.error(`npm install exited with code ${code}.`);
resolve({
success: false,
error: errorMessage,
});
} else {
this.logger.log('npm install completed successfully.');
resolve({ success: true });
}
});
npmInstall.on('error', (err: Error) => {
this.logger.error('Failed to run npm install command:', err);
reject(err);
});
});
}
}