code-quality-lib 1.0.1 → 1.0.2

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.
@@ -0,0 +1,8 @@
1
+ {
2
+ "useLibraryRules": true,
3
+ "useProjectRules": false,
4
+ "customSetup": false,
5
+ "tools": ["TypeScript", "ESLint", "Prettier", "Knip", "Snyk"],
6
+ "copyConfigs": true,
7
+ "version": "1.0.1"
8
+ }
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',
@@ -89,29 +172,17 @@ function getExecCommand(packageManager) {
89
172
  // Main class for the library
90
173
  class CodeQualityChecker {
91
174
  constructor(options = {}) {
92
- const packageManager = options.packageManager || detectPackageManager();
93
- const runCommand = getRunCommand(packageManager);
94
- const execCommand = getExecCommand(packageManager);
175
+ // Check first run configuration
176
+ const config = checkFirstRun();
95
177
 
96
178
  this.options = {
97
179
  loadEnv: true,
98
- tools: ['TypeScript', 'ESLint', 'Prettier', 'Knip', 'Snyk'],
99
- commands: {
100
- TypeScript: `${runCommand} type:check`,
101
- ESLint: `${runCommand} lint:check`,
102
- Prettier: `${runCommand} format:check`,
103
- Knip: `${runCommand} knip:error`,
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,
180
+ tools: config.tools || ['TypeScript', 'ESLint', 'Prettier', 'Knip', 'Snyk'],
181
+ packageManager: detectPackageManager(),
182
+ copyConfigs: config.copyConfigs !== false,
183
+ useLibraryRules: config.useLibraryRules !== false,
184
+ useProjectRules: config.useProjectRules || false,
185
+ customSetup: config.customSetup || false,
115
186
  ...options
116
187
  };
117
188
 
@@ -119,13 +190,33 @@ class CodeQualityChecker {
119
190
  loadEnvFile();
120
191
  }
121
192
 
122
- if (this.options.copyConfigs) {
193
+ if (this.options.copyConfigs && this.options.useLibraryRules) {
123
194
  copyConfigFiles(this.options.packageManager);
124
195
  }
125
196
  }
126
197
 
127
198
  runCommand(command, description) {
128
- return runCommand(command, description);
199
+ const startTime = Date.now();
200
+ const result = {
201
+ success: true,
202
+ message: ''
203
+ };
204
+
205
+ try {
206
+ const output = execSync(command, { encoding: 'utf8' });
207
+ result.message = output.trim();
208
+ } catch (error) {
209
+ result.success = false;
210
+ result.message = error.stdout.trim();
211
+ }
212
+
213
+ const endTime = Date.now();
214
+ const duration = (endTime - startTime) / 1000;
215
+
216
+ return {
217
+ ...result,
218
+ duration
219
+ };
129
220
  }
130
221
 
131
222
  formatOutput(tool, result) {
@@ -167,8 +258,12 @@ class CodeQualityChecker {
167
258
  }
168
259
 
169
260
  const args = process.argv.slice(2);
170
- if (args.includes('--no-configs')) {
171
- this.options.copyConfigs = false;
261
+ for (const arg of args) {
262
+ if (arg === '--no-configs') {
263
+ this.options.copyConfigs = false;
264
+ } else if (arg === '--config') {
265
+ this.options.reconfigure = true;
266
+ }
172
267
  }
173
268
 
174
269
  console.log('\n');
@@ -196,26 +291,62 @@ module.exports = { CodeQualityChecker };
196
291
  // If run directly, execute with default options
197
292
  if (require.main === module) {
198
293
  const args = process.argv.slice(2);
199
-
200
- if (args.includes('--help') || args.includes('-h')) {
201
- console.log('Usage: code-quality-lib [options]');
202
- console.log('');
203
- console.log('Options:');
204
- console.log(' --help, -h Show this help message');
205
- console.log(' --version, -v Show version number');
206
- console.log('');
207
- console.log('Runs TypeScript, ESLint, Prettier, Knip, and Snyk checks.');
208
- process.exit(0);
294
+ const options = {};
295
+
296
+ // Check for reconfigure flag
297
+ if (args.includes('--config')) {
298
+ // Remove existing config and run setup again
299
+ const configPath = path.join(process.cwd(), '.code-quality.json');
300
+ if (fs.existsSync(configPath)) {
301
+ fs.unlinkSync(configPath);
302
+ colorLog('šŸ”§ Configuration reset - Running setup again', 'yellow');
303
+ }
209
304
  }
305
+
306
+ // Parse command line arguments
307
+ for (let i = 0; i < args.length; i++) {
308
+ const arg = args[i];
309
+ if (arg === '--no-env') {
310
+ options.loadEnv = false;
311
+ } else if (arg === '--no-configs') {
312
+ options.copyConfigs = false;
313
+ } else if (arg.startsWith('--tools=')) {
314
+ options.tools = arg.split('=')[1].split(',');
315
+ } else if (arg === '--help' || arg === '-h') {
316
+ console.log(`
317
+ Professional Code Quality Checker
318
+
319
+ Usage: code-quality [options]
210
320
 
321
+ Options:
322
+ --no-env Skip loading .env file
323
+ --no-configs Skip copying config files
324
+ --config Reconfigure setup choices
325
+ --tools=tools Comma-separated list of tools to run
326
+ (TypeScript,ESLint,Prettier,Knip,Snyk)
327
+ --help, -h Show this help message
328
+
329
+ Examples:
330
+ code-quality # Run all tools
331
+ code-quality --tools=ESLint # Run only ESLint
332
+ code-quality --no-env # Skip .env loading
333
+ code-quality --config # Reconfigure setup
334
+ `);
335
+ process.exit(0);
336
+ }
337
+ }
338
+
211
339
  if (args.includes('--version') || args.includes('-v')) {
212
340
  const pkg = require('./package.json');
213
341
  console.log(pkg.version);
214
342
  process.exit(0);
215
343
  }
216
-
217
- const checker = new CodeQualityChecker();
218
- checker.run().then(result => {
219
- process.exit(result.success ? 0 : 1);
344
+
345
+ const checker = new CodeQualityChecker(options);
346
+ checker.run().then(({ success }) => {
347
+ process.exit(success ? 0 : 1);
348
+ }).catch(error => {
349
+ console.error('Error running quality checks:', error);
350
+ process.exit(1);
220
351
  });
221
352
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "code-quality-lib",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
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",