@sonar-autofix/validator 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +170 -0
- package/package.json +17 -0
package/index.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { exec } = require('child_process');
|
|
4
|
+
const { promisify } = require('util');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
|
|
7
|
+
const execAsync = promisify(exec);
|
|
8
|
+
|
|
9
|
+
const DEFAULT_TIMEOUT = 5 * 60 * 1000; // 5 minutes
|
|
10
|
+
|
|
11
|
+
class Validator {
|
|
12
|
+
/**
|
|
13
|
+
* @param {object} config
|
|
14
|
+
* @param {object} config.validation
|
|
15
|
+
* @param {string[]} config.validation.build - Build commands to run
|
|
16
|
+
* @param {string[]} config.validation.lint - Lint commands to run
|
|
17
|
+
* @param {string[]} config.validation.tests - Test commands to run
|
|
18
|
+
* @param {string} [cwd] - Working directory for commands
|
|
19
|
+
*/
|
|
20
|
+
constructor(config = {}, cwd = process.cwd()) {
|
|
21
|
+
const validation = config.validation || {};
|
|
22
|
+
this.commands = {
|
|
23
|
+
build: validation.build || ['npm run build'],
|
|
24
|
+
lint: validation.lint || ['npm run lint'],
|
|
25
|
+
tests: validation.tests || ['npm test'],
|
|
26
|
+
};
|
|
27
|
+
this.cwd = cwd;
|
|
28
|
+
this.timeout = config.timeout || DEFAULT_TIMEOUT;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Run a single shell command.
|
|
33
|
+
* @param {string} command
|
|
34
|
+
* @returns {Promise<{success: boolean, stdout: string, stderr: string, exitCode: number}>}
|
|
35
|
+
*/
|
|
36
|
+
async runCommand(command) {
|
|
37
|
+
console.log(`[Validator] Running: ${command} (cwd: ${this.cwd})`);
|
|
38
|
+
try {
|
|
39
|
+
const { stdout, stderr } = await execAsync(command, {
|
|
40
|
+
cwd: this.cwd,
|
|
41
|
+
timeout: this.timeout,
|
|
42
|
+
maxBuffer: 10 * 1024 * 1024, // 10 MB
|
|
43
|
+
});
|
|
44
|
+
return { success: true, stdout: stdout || '', stderr: stderr || '', exitCode: 0 };
|
|
45
|
+
} catch (err) {
|
|
46
|
+
return {
|
|
47
|
+
success: false,
|
|
48
|
+
stdout: err.stdout || '',
|
|
49
|
+
stderr: err.stderr || err.message || '',
|
|
50
|
+
exitCode: err.code != null ? err.code : 1,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Run a list of commands sequentially.
|
|
57
|
+
* @param {string[]} commands
|
|
58
|
+
* @returns {Promise<{success: boolean, output: string, errors: string[]}>}
|
|
59
|
+
*/
|
|
60
|
+
async _runCommands(commands) {
|
|
61
|
+
const outputs = [];
|
|
62
|
+
const errors = [];
|
|
63
|
+
let success = true;
|
|
64
|
+
|
|
65
|
+
for (const cmd of commands) {
|
|
66
|
+
const result = await this.runCommand(cmd);
|
|
67
|
+
if (result.stdout) outputs.push(result.stdout);
|
|
68
|
+
if (!result.success) {
|
|
69
|
+
success = false;
|
|
70
|
+
errors.push(result.stderr || `Command failed: ${cmd}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return { success, output: outputs.join('\n'), errors };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Run per-file validation using the project's configured build commands.
|
|
79
|
+
* Instead of running individual tool checks on a single file (which ignores
|
|
80
|
+
* tsconfig.json, path aliases, etc.), we run the project's own build/lint
|
|
81
|
+
* commands so validation matches the real project setup.
|
|
82
|
+
* @param {string} filePath
|
|
83
|
+
* @returns {Promise<{success: boolean, errors: string[]}>}
|
|
84
|
+
*/
|
|
85
|
+
async validateFile(filePath) {
|
|
86
|
+
console.log(`[Validator] Validating after changes to: ${filePath}`);
|
|
87
|
+
const errors = [];
|
|
88
|
+
let success = true;
|
|
89
|
+
|
|
90
|
+
// Run build commands — these respect tsconfig.json, path aliases, etc.
|
|
91
|
+
for (const cmd of this.commands.build) {
|
|
92
|
+
const result = await this.runCommand(cmd);
|
|
93
|
+
if (!result.success) {
|
|
94
|
+
success = false;
|
|
95
|
+
errors.push(result.stderr || result.stdout || `Build failed: ${cmd}`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Run lint commands
|
|
100
|
+
for (const cmd of this.commands.lint) {
|
|
101
|
+
const result = await this.runCommand(cmd);
|
|
102
|
+
if (!result.success) {
|
|
103
|
+
success = false;
|
|
104
|
+
errors.push(result.stderr || result.stdout || `Lint failed: ${cmd}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return { success, errors };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Run all configured build commands sequentially.
|
|
113
|
+
* @returns {Promise<{success: boolean, output: string, errors: string[]}>}
|
|
114
|
+
*/
|
|
115
|
+
async runBuild() {
|
|
116
|
+
console.log('[Validator] Running build commands...');
|
|
117
|
+
return this._runCommands(this.commands.build);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Run all configured lint commands.
|
|
122
|
+
* @returns {Promise<{success: boolean, output: string, errors: string[]}>}
|
|
123
|
+
*/
|
|
124
|
+
async runLint() {
|
|
125
|
+
console.log('[Validator] Running lint commands...');
|
|
126
|
+
return this._runCommands(this.commands.lint);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Run all configured test commands.
|
|
131
|
+
* @returns {Promise<{success: boolean, output: string, errors: string[]}>}
|
|
132
|
+
*/
|
|
133
|
+
async runTests() {
|
|
134
|
+
console.log('[Validator] Running test commands...');
|
|
135
|
+
return this._runCommands(this.commands.tests);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Run build, lint, and tests in sequence.
|
|
140
|
+
* @returns {Promise<{success: boolean, results: {build: object, lint: object, tests: object}}>}
|
|
141
|
+
*/
|
|
142
|
+
async validateAll() {
|
|
143
|
+
console.log('[Validator] Running full validation...');
|
|
144
|
+
const build = await this.runBuild();
|
|
145
|
+
const lint = await this.runLint();
|
|
146
|
+
const tests = await this.runTests();
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
success: build.success && lint.success && tests.success,
|
|
150
|
+
results: { build, lint, tests },
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Run per-batch validations (build + tests).
|
|
156
|
+
* @returns {Promise<{success: boolean, results: {build: object, tests: object}}>}
|
|
157
|
+
*/
|
|
158
|
+
async validateBatch() {
|
|
159
|
+
console.log('[Validator] Running batch validation (build + tests)...');
|
|
160
|
+
const build = await this.runBuild();
|
|
161
|
+
const tests = await this.runTests();
|
|
162
|
+
|
|
163
|
+
return {
|
|
164
|
+
success: build.success && tests.success,
|
|
165
|
+
results: { build, tests },
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
module.exports = Validator;
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sonar-autofix/validator",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Validator for the Sonar Autofix tool",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"sonar",
|
|
11
|
+
"autofix",
|
|
12
|
+
"github",
|
|
13
|
+
"validator"
|
|
14
|
+
],
|
|
15
|
+
"author": "Sonar Autofix Team",
|
|
16
|
+
"license": "MIT"
|
|
17
|
+
}
|