code-quality-lib 1.0.1 ā 1.0.3
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/.code-quality.json.example +8 -0
- package/index.js +172 -39
- package/package.json +3 -2
package/index.js
CHANGED
|
@@ -29,6 +29,89 @@ function loadEnvFile() {
|
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
// Check if this is first run and setup configuration
|
|
33
|
+
function checkFirstRun() {
|
|
34
|
+
const configPath = path.join(process.cwd(), '.code-quality.json');
|
|
35
|
+
|
|
36
|
+
if (!fs.existsSync(configPath)) {
|
|
37
|
+
colorLog('\nš§ Code Quality Library - First Time Setup', 'bright');
|
|
38
|
+
colorLog('ā'.repeat(50), 'cyan');
|
|
39
|
+
colorLog('\nChoose your quality rules configuration:', 'yellow');
|
|
40
|
+
colorLog('1) Use library rules (recommended for new projects)', 'white');
|
|
41
|
+
colorLog('2) Use project rules (keep existing configurations)', 'white');
|
|
42
|
+
colorLog('3) Custom setup (choose specific tools)', 'white');
|
|
43
|
+
|
|
44
|
+
// For now, default to library rules
|
|
45
|
+
// In a real implementation, you would read user input here
|
|
46
|
+
const choice = process.env.CODE_QUALITY_CHOICE || '1';
|
|
47
|
+
|
|
48
|
+
const config = {
|
|
49
|
+
useLibraryRules: choice === '1',
|
|
50
|
+
useProjectRules: choice === '2',
|
|
51
|
+
customSetup: choice === '3',
|
|
52
|
+
tools: ['TypeScript', 'ESLint', 'Prettier', 'Knip', 'Snyk'],
|
|
53
|
+
copyConfigs: choice === '1',
|
|
54
|
+
version: '1.0.1'
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
|
|
58
|
+
|
|
59
|
+
if (choice === '1') {
|
|
60
|
+
colorLog('\nā
Using library rules - Config files will be copied', 'green');
|
|
61
|
+
} else if (choice === '2') {
|
|
62
|
+
colorLog('\nā
Using project rules - Existing configs preserved', 'green');
|
|
63
|
+
} else {
|
|
64
|
+
colorLog('\nā
Custom setup - Configure as needed', 'green');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
colorLog('\nš” To change this later, edit: .code-quality.json', 'cyan');
|
|
68
|
+
colorLog('š” Or run: code-quality --config to reconfigure', 'cyan');
|
|
69
|
+
colorLog('ā'.repeat(50), 'cyan');
|
|
70
|
+
|
|
71
|
+
return config;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
return JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
|
76
|
+
} catch (error) {
|
|
77
|
+
colorLog('ā ļø Invalid config file, using defaults', 'yellow');
|
|
78
|
+
return {
|
|
79
|
+
useLibraryRules: true,
|
|
80
|
+
useProjectRules: false,
|
|
81
|
+
customSetup: false,
|
|
82
|
+
tools: ['TypeScript', 'ESLint', 'Prettier', 'Knip', 'Snyk'],
|
|
83
|
+
copyConfigs: true
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Copy config files from library to project
|
|
89
|
+
function copyConfigFiles(packageManager) {
|
|
90
|
+
const libPath = path.dirname(__dirname);
|
|
91
|
+
const projectPath = process.cwd();
|
|
92
|
+
|
|
93
|
+
const configs = [
|
|
94
|
+
{ src: '.eslintrc.js', dest: '.eslintrc.js' },
|
|
95
|
+
{ src: '.prettierrc', dest: '.prettierrc' },
|
|
96
|
+
{ src: 'knip.json', dest: 'knip.json' },
|
|
97
|
+
{ src: 'tsconfig.json', dest: 'tsconfig.json' }
|
|
98
|
+
];
|
|
99
|
+
|
|
100
|
+
configs.forEach(({ src, dest }) => {
|
|
101
|
+
const srcPath = path.join(libPath, src);
|
|
102
|
+
const destPath = path.join(projectPath, dest);
|
|
103
|
+
|
|
104
|
+
try {
|
|
105
|
+
if (fs.existsSync(srcPath)) {
|
|
106
|
+
fs.copyFileSync(srcPath, destPath);
|
|
107
|
+
colorLog(`ā
Copied ${dest}`, 'green');
|
|
108
|
+
}
|
|
109
|
+
} catch (error) {
|
|
110
|
+
colorLog(`ā ļø Could not copy ${dest}: ${error.message}`, 'yellow');
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
32
115
|
// Color codes for beautiful output
|
|
33
116
|
const colors = {
|
|
34
117
|
reset: '\x1b[0m',
|
|
@@ -39,7 +122,9 @@ const colors = {
|
|
|
39
122
|
blue: '\x1b[34m',
|
|
40
123
|
magenta: '\x1b[35m',
|
|
41
124
|
cyan: '\x1b[36m',
|
|
42
|
-
white: '\x1b[37m'
|
|
125
|
+
white: '\x1b[37m',
|
|
126
|
+
dim: '\x1b[2m',
|
|
127
|
+
header: '\x1b[1m\x1b[36m'
|
|
43
128
|
};
|
|
44
129
|
|
|
45
130
|
function colorLog(message, color = 'reset') {
|
|
@@ -89,29 +174,17 @@ function getExecCommand(packageManager) {
|
|
|
89
174
|
// Main class for the library
|
|
90
175
|
class CodeQualityChecker {
|
|
91
176
|
constructor(options = {}) {
|
|
92
|
-
|
|
93
|
-
const
|
|
94
|
-
const execCommand = getExecCommand(packageManager);
|
|
177
|
+
// Check first run configuration
|
|
178
|
+
const config = checkFirstRun();
|
|
95
179
|
|
|
96
180
|
this.options = {
|
|
97
181
|
loadEnv: true,
|
|
98
|
-
tools: ['TypeScript', 'ESLint', 'Prettier', 'Knip', 'Snyk'],
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
Snyk: `${runCommand} snyk`
|
|
105
|
-
},
|
|
106
|
-
descriptions: {
|
|
107
|
-
TypeScript: 'TypeScript compilation',
|
|
108
|
-
ESLint: 'ESLint validation',
|
|
109
|
-
Prettier: 'Prettier formatting',
|
|
110
|
-
Knip: 'Dead code detection',
|
|
111
|
-
Snyk: 'Security vulnerability scan'
|
|
112
|
-
},
|
|
113
|
-
packageManager,
|
|
114
|
-
copyConfigs: true,
|
|
182
|
+
tools: config.tools || ['TypeScript', 'ESLint', 'Prettier', 'Knip', 'Snyk'],
|
|
183
|
+
packageManager: detectPackageManager(),
|
|
184
|
+
copyConfigs: config.copyConfigs !== false,
|
|
185
|
+
useLibraryRules: config.useLibraryRules !== false,
|
|
186
|
+
useProjectRules: config.useProjectRules || false,
|
|
187
|
+
customSetup: config.customSetup || false,
|
|
115
188
|
...options
|
|
116
189
|
};
|
|
117
190
|
|
|
@@ -119,13 +192,33 @@ class CodeQualityChecker {
|
|
|
119
192
|
loadEnvFile();
|
|
120
193
|
}
|
|
121
194
|
|
|
122
|
-
if (this.options.copyConfigs) {
|
|
195
|
+
if (this.options.copyConfigs && this.options.useLibraryRules) {
|
|
123
196
|
copyConfigFiles(this.options.packageManager);
|
|
124
197
|
}
|
|
125
198
|
}
|
|
126
199
|
|
|
127
200
|
runCommand(command, description) {
|
|
128
|
-
|
|
201
|
+
const startTime = Date.now();
|
|
202
|
+
const result = {
|
|
203
|
+
success: true,
|
|
204
|
+
message: ''
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
try {
|
|
208
|
+
const output = execSync(command, { encoding: 'utf8' });
|
|
209
|
+
result.message = output.trim();
|
|
210
|
+
} catch (error) {
|
|
211
|
+
result.success = false;
|
|
212
|
+
result.message = error.stdout.trim();
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const endTime = Date.now();
|
|
216
|
+
const duration = (endTime - startTime) / 1000;
|
|
217
|
+
|
|
218
|
+
return {
|
|
219
|
+
...result,
|
|
220
|
+
duration
|
|
221
|
+
};
|
|
129
222
|
}
|
|
130
223
|
|
|
131
224
|
formatOutput(tool, result) {
|
|
@@ -167,8 +260,12 @@ class CodeQualityChecker {
|
|
|
167
260
|
}
|
|
168
261
|
|
|
169
262
|
const args = process.argv.slice(2);
|
|
170
|
-
|
|
171
|
-
|
|
263
|
+
for (const arg of args) {
|
|
264
|
+
if (arg === '--no-configs') {
|
|
265
|
+
this.options.copyConfigs = false;
|
|
266
|
+
} else if (arg === '--config') {
|
|
267
|
+
this.options.reconfigure = true;
|
|
268
|
+
}
|
|
172
269
|
}
|
|
173
270
|
|
|
174
271
|
console.log('\n');
|
|
@@ -196,26 +293,62 @@ module.exports = { CodeQualityChecker };
|
|
|
196
293
|
// If run directly, execute with default options
|
|
197
294
|
if (require.main === module) {
|
|
198
295
|
const args = process.argv.slice(2);
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
296
|
+
const options = {};
|
|
297
|
+
|
|
298
|
+
// Check for reconfigure flag
|
|
299
|
+
if (args.includes('--config')) {
|
|
300
|
+
// Remove existing config and run setup again
|
|
301
|
+
const configPath = path.join(process.cwd(), '.code-quality.json');
|
|
302
|
+
if (fs.existsSync(configPath)) {
|
|
303
|
+
fs.unlinkSync(configPath);
|
|
304
|
+
colorLog('š§ Configuration reset - Running setup again', 'yellow');
|
|
305
|
+
}
|
|
209
306
|
}
|
|
307
|
+
|
|
308
|
+
// Parse command line arguments
|
|
309
|
+
for (let i = 0; i < args.length; i++) {
|
|
310
|
+
const arg = args[i];
|
|
311
|
+
if (arg === '--no-env') {
|
|
312
|
+
options.loadEnv = false;
|
|
313
|
+
} else if (arg === '--no-configs') {
|
|
314
|
+
options.copyConfigs = false;
|
|
315
|
+
} else if (arg.startsWith('--tools=')) {
|
|
316
|
+
options.tools = arg.split('=')[1].split(',');
|
|
317
|
+
} else if (arg === '--help' || arg === '-h') {
|
|
318
|
+
console.log(`
|
|
319
|
+
Professional Code Quality Checker
|
|
320
|
+
|
|
321
|
+
Usage: code-quality [options]
|
|
210
322
|
|
|
323
|
+
Options:
|
|
324
|
+
--no-env Skip loading .env file
|
|
325
|
+
--no-configs Skip copying config files
|
|
326
|
+
--config Reconfigure setup choices
|
|
327
|
+
--tools=tools Comma-separated list of tools to run
|
|
328
|
+
(TypeScript,ESLint,Prettier,Knip,Snyk)
|
|
329
|
+
--help, -h Show this help message
|
|
330
|
+
|
|
331
|
+
Examples:
|
|
332
|
+
code-quality # Run all tools
|
|
333
|
+
code-quality --tools=ESLint # Run only ESLint
|
|
334
|
+
code-quality --no-env # Skip .env loading
|
|
335
|
+
code-quality --config # Reconfigure setup
|
|
336
|
+
`);
|
|
337
|
+
process.exit(0);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
211
341
|
if (args.includes('--version') || args.includes('-v')) {
|
|
212
342
|
const pkg = require('./package.json');
|
|
213
343
|
console.log(pkg.version);
|
|
214
344
|
process.exit(0);
|
|
215
345
|
}
|
|
216
|
-
|
|
217
|
-
const checker = new CodeQualityChecker();
|
|
218
|
-
checker.run().then(
|
|
219
|
-
process.exit(
|
|
346
|
+
|
|
347
|
+
const checker = new CodeQualityChecker(options);
|
|
348
|
+
checker.run().then(({ success }) => {
|
|
349
|
+
process.exit(success ? 0 : 1);
|
|
350
|
+
}).catch(error => {
|
|
351
|
+
console.error('Error running quality checks:', error);
|
|
352
|
+
process.exit(1);
|
|
220
353
|
});
|
|
221
354
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "code-quality-lib",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "A configurable code quality checker library for Node.js projects",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": {
|
|
@@ -38,7 +38,8 @@
|
|
|
38
38
|
".eslintrc.js",
|
|
39
39
|
".prettierrc",
|
|
40
40
|
"knip.json",
|
|
41
|
-
"tsconfig.json"
|
|
41
|
+
"tsconfig.json",
|
|
42
|
+
".code-quality.json.example"
|
|
42
43
|
],
|
|
43
44
|
"repository": {
|
|
44
45
|
"type": "git",
|