x-fidelity 1.17.0 → 2.0.0
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/CHANGELOG.md +52 -0
- package/README.md +154 -27
- package/dist/core/cli.js +32 -6
- package/dist/core/engine/analyzer.js +40 -23
- package/dist/core/{engine.test.js → engine/analyzer.test.js} +96 -41
- package/dist/core/engine/engineRunner.js +6 -5
- package/dist/core/engine/engineRunner.test.js +66 -0
- package/dist/core/engine/engineSetup.js +3 -3
- package/dist/core/engine/engineSetup.test.js +139 -0
- package/dist/core/engine/telemetryCollector.js +11 -2
- package/dist/core/engine/telemetryCollector.test.js +98 -0
- package/dist/facts/repoDependencyFacts.test.js +1 -1
- package/dist/index.js +58 -32
- package/dist/operators/nonStandardDirectoryStructure.js +2 -2
- package/dist/operators/nonStandardDirectoryStructure.test.js +7 -7
- package/dist/rules/index.js +18 -17
- package/dist/rules/index.test.js +4 -12
- package/dist/rules/noDatabases-iterative-rule.json +1 -1
- package/dist/rules/nonStandardDirectoryStructure-global-rule.json +1 -2
- package/dist/rules/openaiAnalysisA11y-global-rule.json +2 -2
- package/dist/rules/openaiAnalysisTop5-global-rule.json +2 -2
- package/dist/rules/outdatedFramework-global-rule.json +0 -1
- package/dist/rules/sensitiveLogging-iterative-rule.json +1 -3
- package/dist/server/configServer.js +8 -7
- package/dist/server/configServer.test.js +1 -1
- package/dist/utils/{config.js → configManager.js} +6 -14
- package/dist/utils/{config.test.js → configManager.test.js} +24 -32
- package/dist/utils/logger.js +11 -8
- package/dist/utils/utils.js +12 -0
- package/dist/xfidelity +58 -32
- package/package.json +1 -1
- package/src/core/cli.ts +32 -7
- package/src/core/{engine.test.ts → engine/analyzer.test.ts} +96 -42
- package/src/core/engine/analyzer.ts +37 -25
- package/src/core/engine/engineRunner.test.ts +70 -0
- package/src/core/engine/engineRunner.ts +3 -2
- package/src/core/engine/engineSetup.test.ts +152 -0
- package/src/core/engine/engineSetup.ts +3 -3
- package/src/core/engine/telemetryCollector.test.ts +97 -0
- package/src/core/engine/telemetryCollector.ts +12 -5
- package/src/facts/repoDependencyFacts.test.ts +1 -1
- package/src/index.ts +64 -35
- package/src/operators/nonStandardDirectoryStructure.test.ts +1 -1
- package/src/operators/nonStandardDirectoryStructure.ts +1 -1
- package/src/rules/index.test.ts +5 -14
- package/src/rules/index.ts +18 -17
- package/src/rules/noDatabases-iterative-rule.json +1 -1
- package/src/rules/nonStandardDirectoryStructure-global-rule.json +1 -2
- package/src/rules/openaiAnalysisA11y-global-rule.json +2 -2
- package/src/rules/openaiAnalysisTop5-global-rule.json +2 -2
- package/src/rules/outdatedFramework-global-rule.json +0 -1
- package/src/rules/sensitiveLogging-iterative-rule.json +1 -3
- package/src/server/configServer.test.ts +1 -1
- package/src/server/configServer.ts +6 -4
- package/src/types/typeDefs.ts +54 -6
- package/src/utils/{config.test.ts → configManager.test.ts} +10 -18
- package/src/utils/{config.ts → configManager.ts} +6 -14
- package/src/utils/logger.ts +11 -6
- package/src/utils/utils.ts +10 -0
- package/tsconfig.json +2 -1
- package/dist/core/engine/utils.js +0 -41
- package/src/core/engine/utils.ts +0 -43
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,55 @@
|
|
|
1
|
+
# [2.0.0](https://github.com/zotoio/x-fidelity/compare/v1.17.0...v2.0.0) (2024-08-17)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* Add additional logging to configManager.ts ([bab63c1](https://github.com/zotoio/x-fidelity/commit/bab63c13a714d6d69440380c886a227b388baa41))
|
|
7
|
+
* Add missing properties to ResultMetadata in engineSetup.ts ([1c2d276](https://github.com/zotoio/x-fidelity/commit/1c2d276512a4958b5ae75cec44df00e36551bccc))
|
|
8
|
+
* Add missing properties to ResultMetadata interface ([eb2b71f](https://github.com/zotoio/x-fidelity/commit/eb2b71f375f7d0f8615213f693704e91f8a610f0))
|
|
9
|
+
* Add missing properties to telemetry metadata ([30fefbc](https://github.com/zotoio/x-fidelity/commit/30fefbc3709bb1d13a3624d2e4d66c6b6f78e7a7))
|
|
10
|
+
* Correct the typo in the GPT-4 model name ([438c772](https://github.com/zotoio/x-fidelity/commit/438c77288fe58441adc7956b4ba64db445e7d82f))
|
|
11
|
+
* Fix TypeScript errors in src/index.ts ([30fc784](https://github.com/zotoio/x-fidelity/commit/30fc784580763d956c75003c965821e646bf716d))
|
|
12
|
+
* Fix TypeScript errors in test files ([25af375](https://github.com/zotoio/x-fidelity/commit/25af3759ff321a91d4e0f6a90531a384888c7a39))
|
|
13
|
+
* handle network error in loadRules test ([05a26e6](https://github.com/zotoio/x-fidelity/commit/05a26e634b2a6b43ec860e01ad74caf26dc924db))
|
|
14
|
+
* Import fs module in telemetryCollector.test.ts ([64245d9](https://github.com/zotoio/x-fidelity/commit/64245d90786a3be99a255b5d240afc6dd4737c81))
|
|
15
|
+
* Improve error handling in ConfigManager ([d1bb512](https://github.com/zotoio/x-fidelity/commit/d1bb5127a3e0bac630d613afced3fdf849b8b211))
|
|
16
|
+
* refactor `src/core/engine.test.ts` into separate test files ([4055f8f](https://github.com/zotoio/x-fidelity/commit/4055f8f3179d44862fd779d8bb09022cb4d946b9))
|
|
17
|
+
* Refactor analyzeCodebase function to return ResultMetadata ([81d1936](https://github.com/zotoio/x-fidelity/commit/81d1936618ac2abd0a85718c2f1f0b910fb8b910))
|
|
18
|
+
* Remove expectation of `mockLogPrefix` in `loadRules` calls ([7b8669e](https://github.com/zotoio/x-fidelity/commit/7b8669e864150123e02ba025f73652a82898c723))
|
|
19
|
+
* Remove unused import and update metadata object ([ff4bfc0](https://github.com/zotoio/x-fidelity/commit/ff4bfc0ad0ed13bc3c919722c34de81160e486fa))
|
|
20
|
+
* Update event handling in engineSetup.ts ([73e5358](https://github.com/zotoio/x-fidelity/commit/73e53583b82fab78b72f48c8daa14da41fe244bf))
|
|
21
|
+
* Update import path for ConfigManager ([e87f101](https://github.com/zotoio/x-fidelity/commit/e87f10135d9afddd43ac145c6308ca1fd70d3fd7))
|
|
22
|
+
* Update import path for ConfigManager ([6a6b05a](https://github.com/zotoio/x-fidelity/commit/6a6b05af2dc9e217ae53d5f315116ffd565a2fad))
|
|
23
|
+
* Update ResultMetadata interface ([b95fab7](https://github.com/zotoio/x-fidelity/commit/b95fab7731d8040913b7fbd832b1589a31acef0b))
|
|
24
|
+
* Update telemetryCollector.test.ts to use mocked fs functions ([29d435d](https://github.com/zotoio/x-fidelity/commit/29d435dd24fc4cc01c07d2eecbebb43339474005))
|
|
25
|
+
* Update test case for loadRules function ([5b6278d](https://github.com/zotoio/x-fidelity/commit/5b6278d6323c576ac1b980f41cef78f683b3ae5d))
|
|
26
|
+
* Update test case to expect resolved value instead of rejection ([580108a](https://github.com/zotoio/x-fidelity/commit/580108ae927c00f17673f82125718ca8c4205493))
|
|
27
|
+
* Update test expectation for `analyzeCodebase` results ([bdafd3f](https://github.com/zotoio/x-fidelity/commit/bdafd3f3eaf5a70315e323dd9dfe2af1f82908be))
|
|
28
|
+
* Update test expectations for addOperator and addFact ([c86a33a](https://github.com/zotoio/x-fidelity/commit/c86a33a9042f234c5a934d0ff555af438ebaff1f))
|
|
29
|
+
* Update test expectations to match actual results structure ([3398a84](https://github.com/zotoio/x-fidelity/commit/3398a8433e5a543084c1dcaaf69dd910fae6d297))
|
|
30
|
+
* Update tests to expect new ResultMetadata structure ([fc16223](https://github.com/zotoio/x-fidelity/commit/fc162232c976f6638228a2e450a793ea7d087bb4))
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
### Features
|
|
34
|
+
|
|
35
|
+
* Add BasicTelemetryMetadata interface ([be87308](https://github.com/zotoio/x-fidelity/commit/be873084164d592822193cad70badeedd2fa17de))
|
|
36
|
+
* Add details on creating custom OpenAI rules ([80abb24](https://github.com/zotoio/x-fidelity/commit/80abb24652565bce89ce61bba16dc4de8c0f5654))
|
|
37
|
+
* add interface for the result of collectTelemetryData ([ea6089a](https://github.com/zotoio/x-fidelity/commit/ea6089a2a35b6445550adde1aa5c63ac1a701dc9))
|
|
38
|
+
* Add new CLI options and examples ([fab7b81](https://github.com/zotoio/x-fidelity/commit/fab7b81db053bcf144fb104b0bba82ff7679ab57))
|
|
39
|
+
* Add ResultMetadata interface ([08c3fe2](https://github.com/zotoio/x-fidelity/commit/08c3fe2f20bfac98ddb173ac482fb8f09a661780))
|
|
40
|
+
* Add telemetry reporting and improve error handling ([ac16e44](https://github.com/zotoio/x-fidelity/commit/ac16e4400197b59fa40384b3802f40dfe9bc5941))
|
|
41
|
+
* Expand OpenAI Integration section in README.md ([c4e34e7](https://github.com/zotoio/x-fidelity/commit/c4e34e7626c264b3972bfc28eaaff172bbba6295))
|
|
42
|
+
* Implement ConfigManager class to manage application configuration ([6836652](https://github.com/zotoio/x-fidelity/commit/6836652ce82791eaa85ddc1eb5a27d63dbaf9b4a))
|
|
43
|
+
* Refactor codebase analysis and reporting ([7f7d30e](https://github.com/zotoio/x-fidelity/commit/7f7d30e607bde3d29a68c4fcf42ecd0779029b24))
|
|
44
|
+
* Refactor ResultMetadata interface ([7d9ddf2](https://github.com/zotoio/x-fidelity/commit/7d9ddf2457b5d3d0981f66dcf07cd4ec03e79554))
|
|
45
|
+
* **system:** fixes, refactor, optimisations ([4da8c9f](https://github.com/zotoio/x-fidelity/commit/4da8c9f0d0dbc8b151ee01b21dfaab150ee02dfe))
|
|
46
|
+
* Update README with callout for OpenAI rule naming convention ([103a470](https://github.com/zotoio/x-fidelity/commit/103a4704213f417f30be0936965ae2d90b29ee75))
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
### BREAKING CHANGES
|
|
50
|
+
|
|
51
|
+
* **system:** some of the contract for cli has changed, and output interfaces updated
|
|
52
|
+
|
|
1
53
|
# [1.17.0](https://github.com/zotoio/x-fidelity/compare/v1.16.1...v1.17.0) (2024-08-16)
|
|
2
54
|
|
|
3
55
|
|
package/README.md
CHANGED
|
@@ -23,13 +23,15 @@ x-fidelity is an advanced CLI tool and paired config server designed to perform
|
|
|
23
23
|
yarn global add x-fidelity
|
|
24
24
|
export PATH="$PATH:$(yarn global bin)"
|
|
25
25
|
```
|
|
26
|
-
|
|
27
|
-
2. Run in your project directory:
|
|
26
|
+
2. Run cli without parameters to view help
|
|
28
27
|
```
|
|
29
28
|
xfidelity
|
|
30
29
|
```
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
3. Execute in current project directory with built-in demo rules for node-fullstack:
|
|
31
|
+
```
|
|
32
|
+
xfidelity .
|
|
33
|
+
```
|
|
34
|
+
4. For more options:
|
|
33
35
|
```
|
|
34
36
|
xfidelity --help
|
|
35
37
|
```
|
|
@@ -163,35 +165,62 @@ xfidelity
|
|
|
163
165
|
Use command-line options for more control:
|
|
164
166
|
|
|
165
167
|
```sh
|
|
166
|
-
|
|
168
|
+
Usage: x-fidelity [options] [directory]
|
|
169
|
+
|
|
170
|
+
Arguments:
|
|
171
|
+
directory code directory to analyze
|
|
172
|
+
|
|
173
|
+
Options:
|
|
174
|
+
-d, --dir <directory> code directory to analyze. if an arg was passed to command it
|
|
175
|
+
will be treated as the dir (default: ".")
|
|
176
|
+
-a, --archetype <archetype> The archetype to use for analysis (default: "node-fullstack")
|
|
177
|
+
-c, --configServer <configServer> The config server URL for fetching remote archetype
|
|
178
|
+
configurations and rules
|
|
179
|
+
-o, --openaiEnabled <boolean> Enable OpenAI analysis (default: false)
|
|
180
|
+
-t, --telemetryCollector <telemetryCollector> The URL telemetry data will be sent to for usage analysis
|
|
181
|
+
-m, --mode <mode> Run mode: 'client' or 'server' (default: "client")
|
|
182
|
+
-p, --port <port> The port to run the server on (default: "8888")
|
|
183
|
+
-l, --localConfigPath <path> Path to local archetype config and rules
|
|
184
|
+
-j, --jsonTTL <minutes> Set the server json cache TTL in minutes (default: "10")
|
|
185
|
+
-v, --version Output the version number of xfidelity
|
|
186
|
+
-h, --help Display help for command
|
|
167
187
|
```
|
|
168
188
|
|
|
169
|
-
- `-d --dir <directory>`: Specify the root directory to analyze (default: current directory)
|
|
170
|
-
- `-c --configServer <url>`: URL to fetch the configuration from. eg. https://localhost:8888
|
|
171
|
-
- `-a --archetype <archetype>`: Archetype to use for analysis (default: 'node-fullstack')
|
|
172
|
-
- `-m --mode <mode>`: Run mode: 'client' or 'server' (default: 'client')
|
|
173
|
-
- `-p --port <port>`: Port number for server mode (default: 8888)
|
|
174
|
-
- `-o --openaiEnabled <boolean>`: Enable OpenAI analysis (default: false)
|
|
175
|
-
- `-t --telemetryCollector <url>`: The URL telemetry data will be sent to for usage analysis
|
|
176
|
-
- `-l --localConfigPath <path>`: Path to local archetype config and rules
|
|
177
|
-
- `-j --jsonTTL <minutes>` : The server json cache TTL in minutes (default: 10)
|
|
178
|
-
|
|
179
189
|
Examples:
|
|
180
190
|
|
|
181
191
|
```sh
|
|
182
|
-
#
|
|
183
|
-
xfidelity --configServer
|
|
192
|
+
# Run client to analyse current dir using config from remote config server
|
|
193
|
+
xfidelity --configServer http://localhost:8888
|
|
184
194
|
|
|
185
|
-
# Analyze
|
|
186
|
-
xfidelity -
|
|
195
|
+
# Analyze a specific directory with java-microservice archetype config from remote config server and enable OpenAI analysis
|
|
196
|
+
xfidelity /home/projects/my-codebase -a java-microservice -c https://localhost:8888 -o true
|
|
187
197
|
|
|
188
|
-
#Run in server mode with custom port
|
|
189
|
-
xfidelity --mode server --port 9999
|
|
198
|
+
# Run in config server mode with custom port
|
|
199
|
+
xfidelity --mode server --port 9999
|
|
190
200
|
|
|
191
201
|
# Use local config and rules
|
|
192
202
|
xfidelity -l /path/to/local/config
|
|
193
203
|
```
|
|
194
204
|
|
|
205
|
+
### Environment Variables
|
|
206
|
+
|
|
207
|
+
x-fidelity supports the following environment variables:
|
|
208
|
+
|
|
209
|
+
- `OPENAI_API_KEY`: Your OpenAI API key for AI-powered analysis.
|
|
210
|
+
- `OPENAI_MODEL`: The OpenAI model to use (default is 'gpt-4o').
|
|
211
|
+
- `XFI_LISTEN_PORT`: The port for the config server to listen on (default is 8888).
|
|
212
|
+
- `CERT_PATH`: The path to SSL certificates for HTTPS config server.
|
|
213
|
+
- `NODE_TLS_REJECT_UNAUTHORIZED`: Set to '0' to allow self-signed certificates (use with caution).
|
|
214
|
+
|
|
215
|
+
Example usage:
|
|
216
|
+
|
|
217
|
+
```sh
|
|
218
|
+
export OPENAI_API_KEY=your_api_key_here
|
|
219
|
+
export OPENAI_MODEL=gpt-4
|
|
220
|
+
export XFI_LISTEN_PORT=9999
|
|
221
|
+
xfidelity -o true
|
|
222
|
+
```
|
|
223
|
+
|
|
195
224
|
## Configuration
|
|
196
225
|
|
|
197
226
|
x-fidelity uses archetypes to define project-specific configurations. Archetypes are now managed as JSON files, which can be stored locally or on a remote server.
|
|
@@ -362,11 +391,11 @@ x-fidelity is designed to be highly extensible. You can add custom rules, operat
|
|
|
362
391
|
3. **Custom Facts**: Create new fact providers and add them to your x-fidelity fork or plugin.
|
|
363
392
|
4. **New Archetypes**: Define new archetypes as JSON files in your local config directory or on your config server.
|
|
364
393
|
|
|
365
|
-
Example of a custom rule JSON file (`
|
|
394
|
+
Example of a custom rule JSON file (`myCustomRule.json`):
|
|
366
395
|
|
|
367
396
|
```json
|
|
368
397
|
{
|
|
369
|
-
"name": "
|
|
398
|
+
"name": "myCustomRule",
|
|
370
399
|
"conditions": {
|
|
371
400
|
"all": [
|
|
372
401
|
{
|
|
@@ -378,10 +407,9 @@ Example of a custom rule JSON file (`my-custom-rule.json`):
|
|
|
378
407
|
]
|
|
379
408
|
},
|
|
380
409
|
"event": {
|
|
381
|
-
"type": "
|
|
410
|
+
"type": "warning",
|
|
382
411
|
"params": {
|
|
383
412
|
"message": "TODO comments should be resolved before committing",
|
|
384
|
-
"level": "warning",
|
|
385
413
|
"details": {
|
|
386
414
|
"fact": "fileData",
|
|
387
415
|
"path": "$.filePath"
|
|
@@ -392,7 +420,7 @@ Example of a custom rule JSON file (`my-custom-rule.json`):
|
|
|
392
420
|
```
|
|
393
421
|
|
|
394
422
|
Note on rule event types:
|
|
395
|
-
- Events of type "
|
|
423
|
+
- Events of type "warning" are treated as such and do not cause the tool to return an error code.
|
|
396
424
|
- Events of type "fatality" are strictly enforced and will cause the tool to return an error code 1.
|
|
397
425
|
|
|
398
426
|
Example of a custom archetype JSON file (`my-custom-archetype.json`):
|
|
@@ -421,6 +449,38 @@ Example of a custom archetype JSON file (`my-custom-archetype.json`):
|
|
|
421
449
|
|
|
422
450
|
## OpenAI Integration
|
|
423
451
|
|
|
452
|
+
x-fidelity offers advanced AI-powered code analysis through integration with OpenAI's language models. This feature provides in-depth insights and suggestions for improving your codebase.
|
|
453
|
+
|
|
454
|
+
> [!IMPORTANT]
|
|
455
|
+
> Carefully consider the costs and data privacy implications before enabling OpenAI integration, especially for large codebases or sensitive projects.
|
|
456
|
+
|
|
457
|
+
### How It Works
|
|
458
|
+
|
|
459
|
+
1. **Data Collection**: x-fidelity gathers relevant information about your codebase, including file contents, structure, and dependencies.
|
|
460
|
+
|
|
461
|
+
2. **AI Analysis**: This data is sent to OpenAI's API, where it's analyzed by a powerful language model (default is GPT-4o).
|
|
462
|
+
|
|
463
|
+
3. **Insights Generation**: The AI generates detailed insights, suggestions, and potential issues based on best practices, common pitfalls, and the specific context of your project.
|
|
464
|
+
|
|
465
|
+
4. **Results Integration**: These insights are integrated into x-fidelity's output, providing you with AI-enhanced analysis alongside the tool's standard checks.
|
|
466
|
+
|
|
467
|
+
### Key Features
|
|
468
|
+
|
|
469
|
+
- **Code Quality Assessment**: Identifies potential bugs, anti-patterns, and areas for improvement.
|
|
470
|
+
- **Architecture Suggestions**: Offers insights on overall code structure and architectural decisions.
|
|
471
|
+
- **Performance Optimization**: Highlights areas where performance could be improved.
|
|
472
|
+
- **Security Analysis**: Flags potential security vulnerabilities.
|
|
473
|
+
- **Best Practices**: Suggests adherence to industry-standard best practices.
|
|
474
|
+
|
|
475
|
+
### Limitations and Considerations
|
|
476
|
+
|
|
477
|
+
- **Cost**: Using OpenAI's API incurs charges based on the amount of text processed.
|
|
478
|
+
- **Privacy**: Code snippets are sent to OpenAI's servers for analysis. Ensure this complies with your organization's data policies.
|
|
479
|
+
- **Accuracy**: While highly advanced, AI suggestions should be reviewed by human developers for context and applicability.
|
|
480
|
+
- **Rate Limits**: OpenAI's API has rate limits that may affect analysis of very large codebases.
|
|
481
|
+
|
|
482
|
+
### Setup and Usage
|
|
483
|
+
|
|
424
484
|
To enable AI-powered code analysis:
|
|
425
485
|
|
|
426
486
|
1. Sign up for an [OpenAI API key](https://platform.openai.com).
|
|
@@ -442,8 +502,61 @@ You can also set the OpenAI model using an environment variable (optional):
|
|
|
442
502
|
export OPENAI_MODEL=gpt-4 # Optional, default is gpt-4o
|
|
443
503
|
```
|
|
444
504
|
|
|
505
|
+
By leveraging OpenAI's advanced language models, x-fidelity provides a unique blend of rule-based checks and AI-powered insights, offering a comprehensive analysis of your codebase that goes beyond traditional static analysis tools.
|
|
506
|
+
|
|
507
|
+
### Creating Custom OpenAI Rules
|
|
508
|
+
|
|
509
|
+
You can create custom OpenAI rules to leverage AI-powered analysis for specific aspects of your codebase. Here's how to create a new OpenAI rule:
|
|
510
|
+
|
|
445
511
|
> [!IMPORTANT]
|
|
446
|
-
>
|
|
512
|
+
> By convention, all OpenAI rules must have a name starting with 'openai'. This naming convention is used to identify and handle OpenAI-specific rules in the system.
|
|
513
|
+
|
|
514
|
+
1. Create a new JSON file in your rules directory (e.g., `openaiCustomAnalysis-global-rule.json`).
|
|
515
|
+
2. Use the following template structure for your rule:
|
|
516
|
+
|
|
517
|
+
```json
|
|
518
|
+
{
|
|
519
|
+
"name": "openai-custom-analysis",
|
|
520
|
+
"conditions": {
|
|
521
|
+
"all": [
|
|
522
|
+
{
|
|
523
|
+
"fact": "fileData",
|
|
524
|
+
"path": "$.fileName",
|
|
525
|
+
"operator": "equal",
|
|
526
|
+
"value": "REPO_GLOBAL_CHECK"
|
|
527
|
+
},
|
|
528
|
+
{
|
|
529
|
+
"fact": "openaiAnalysis",
|
|
530
|
+
"params": {
|
|
531
|
+
"prompt": "Your custom prompt here",
|
|
532
|
+
"resultFact": "openaiCustomAnalysisResult"
|
|
533
|
+
},
|
|
534
|
+
"operator": "openaiAnalysisHighSeverity",
|
|
535
|
+
"value": 8
|
|
536
|
+
}
|
|
537
|
+
]
|
|
538
|
+
},
|
|
539
|
+
"event": {
|
|
540
|
+
"type": "warning",
|
|
541
|
+
"params": {
|
|
542
|
+
"message": "Custom message for the warning",
|
|
543
|
+
"results": {
|
|
544
|
+
"fact": "openaiCustomAnalysisResult"
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
```
|
|
550
|
+
|
|
551
|
+
3. Customize the rule:
|
|
552
|
+
- Set a unique `name` for your rule, ensuring it starts with 'openai'.
|
|
553
|
+
- Modify the `prompt` in the `params` section to specify what you want the AI to analyze.
|
|
554
|
+
- Adjust the `value` in the `openaiAnalysisHighSeverity` operator to set the severity threshold (1-10).
|
|
555
|
+
- Customize the `message` in the `event` params to describe the warning.
|
|
556
|
+
|
|
557
|
+
4. Add your new rule to the appropriate archetype configuration file.
|
|
558
|
+
|
|
559
|
+
This structure allows you to create custom AI-powered rules that can analyze your codebase for specific patterns, best practices, or potential issues. Remember to follow the naming convention to ensure proper handling of OpenAI rules in the system.
|
|
447
560
|
|
|
448
561
|
## Best Practices
|
|
449
562
|
|
|
@@ -460,6 +573,20 @@ export OPENAI_MODEL=gpt-4 # Optional, default is gpt-4o
|
|
|
460
573
|
|
|
461
574
|
Contributions to x-fidelity are welcome! Please refer to the `CONTRIBUTING.md` file for guidelines on how to contribute to this project.
|
|
462
575
|
|
|
576
|
+
## Glossary
|
|
577
|
+
|
|
578
|
+
- **Archetype**: A predefined configuration template for a specific type of project or technology stack. It defines the rules, operators, facts, and other settings to be applied during analysis.
|
|
579
|
+
|
|
580
|
+
- **Rule**: A set of conditions and corresponding actions that define a specific check or requirement for the codebase. Rules are used to identify warnings or fatal issues in the codebase.
|
|
581
|
+
|
|
582
|
+
- **Operator**: A function that performs a specific comparison or check within a rule. Operators are used to evaluate conditions in rules.
|
|
583
|
+
|
|
584
|
+
- **Fact**: A piece of information about the codebase or its environment that is collected and used during the analysis process. Facts can include file contents, dependency versions, or other relevant data.
|
|
585
|
+
|
|
586
|
+
- **Config Server**: A server that hosts and distributes archetype configurations and rules, allowing for centralized management of x-fidelity settings.
|
|
587
|
+
|
|
588
|
+
- **Telemetry**: Data collected about the usage and performance of x-fidelity, which can be used for improving the tool and understanding its impact.
|
|
589
|
+
|
|
463
590
|
## License
|
|
464
591
|
|
|
465
592
|
This project is licensed under the MIT License. See the `LICENSE` file for details.
|
package/dist/core/cli.js
CHANGED
|
@@ -7,6 +7,8 @@ exports.options = void 0;
|
|
|
7
7
|
const logger_1 = require("../utils/logger");
|
|
8
8
|
const commander_1 = require("commander");
|
|
9
9
|
const path_1 = __importDefault(require("path"));
|
|
10
|
+
const fs_1 = __importDefault(require("fs"));
|
|
11
|
+
const package_json_1 = require("../../package.json");
|
|
10
12
|
// Ensure logger is initialized
|
|
11
13
|
if (!logger_1.logger || typeof logger_1.logger.info !== 'function') {
|
|
12
14
|
console.error('Logger is not properly initialized');
|
|
@@ -20,7 +22,7 @@ if (!logger_1.logger || typeof logger_1.logger.info !== 'function') {
|
|
|
20
22
|
global.logger = fallbackLogger;
|
|
21
23
|
}
|
|
22
24
|
commander_1.program
|
|
23
|
-
.option("-d, --dir <directory>", "
|
|
25
|
+
.option("-d, --dir <directory>", "code directory to analyze. equivalent of directory argument")
|
|
24
26
|
.option("-a, --archetype <archetype>", "The archetype to use for analysis", "node-fullstack")
|
|
25
27
|
.option("-c, --configServer <configServer>", "The config server URL for fetching remote archetype configurations and rules")
|
|
26
28
|
.option("-o, --openaiEnabled <boolean>", "Enable OpenAI analysis", false)
|
|
@@ -28,16 +30,39 @@ commander_1.program
|
|
|
28
30
|
.option("-m, --mode <mode>", "Run mode: 'client' or 'server'", "client")
|
|
29
31
|
.option("-p, --port <port>", "The port to run the server on", "8888")
|
|
30
32
|
.option("-l, --localConfigPath <path>", "Path to local archetype config and rules")
|
|
31
|
-
.option("-j, --jsonTTL <minutes>", "Set the server json cache TTL in minutes", "10")
|
|
32
|
-
|
|
33
|
+
.option("-j, --jsonTTL <minutes>", "Set the server json cache TTL in minutes", "10")
|
|
34
|
+
.version(package_json_1.version, "-v, --version", "Output the version number of xfidelity")
|
|
35
|
+
.helpOption("-h, --help", "Display help for command")
|
|
36
|
+
.argument('[directory]', 'code directory to analyze');
|
|
33
37
|
const options = commander_1.program.opts();
|
|
34
38
|
exports.options = options;
|
|
39
|
+
commander_1.program.parse(process.argv);
|
|
40
|
+
// If no options or args are provided, display the help message
|
|
41
|
+
if (process.argv.length === 2 && commander_1.program.args.length === 0) {
|
|
42
|
+
// dont exit in tests
|
|
43
|
+
if (process.env.NODE_ENV !== 'test')
|
|
44
|
+
commander_1.program.help();
|
|
45
|
+
}
|
|
35
46
|
// Resolve paths
|
|
36
|
-
|
|
37
|
-
options.dir =
|
|
47
|
+
if (process.env.NODE_ENV === 'test' || options.mode === 'server')
|
|
48
|
+
options.dir = '.';
|
|
49
|
+
const resolvePath = (inputPath) => path_1.default === null || path_1.default === void 0 ? void 0 : path_1.default.resolve(process.cwd(), inputPath);
|
|
50
|
+
options.dir = commander_1.program.args.length == 1 ? resolvePath(commander_1.program.args[0]) : resolvePath(options.dir);
|
|
38
51
|
if (options.localConfigPath) {
|
|
39
52
|
options.localConfigPath = resolvePath(options.localConfigPath);
|
|
40
53
|
}
|
|
54
|
+
// if dir does not exist, exit
|
|
55
|
+
if (!options.dir || !fs_1.default.existsSync(options.dir)) {
|
|
56
|
+
logger_1.logger.error(`target directory ${options.dir} does not exist`);
|
|
57
|
+
if (process.env.NODE_ENV !== 'test')
|
|
58
|
+
process.exit(1);
|
|
59
|
+
}
|
|
60
|
+
// if localConfig path does not exist, exit
|
|
61
|
+
if (options.localConfigPath && !fs_1.default.existsSync(options.localConfigPath)) {
|
|
62
|
+
logger_1.logger.error(`localConfigPath ${options.localConfigPath} does not exist`);
|
|
63
|
+
if (process.env.NODE_ENV !== 'test')
|
|
64
|
+
process.exit(1);
|
|
65
|
+
}
|
|
41
66
|
const banner = (`
|
|
42
67
|
=====================================
|
|
43
68
|
__ __ ________ ______
|
|
@@ -51,6 +76,7 @@ const banner = (`
|
|
|
51
76
|
|
|
52
77
|
-------------------------------------
|
|
53
78
|
${new Date().toString().slice(0, 24)}
|
|
79
|
+
version: ${package_json_1.version}
|
|
54
80
|
archetype: ${options.archetype}
|
|
55
81
|
directory: ${options.dir}
|
|
56
82
|
configServer: ${options.configServer ? options.configServer : 'none'}
|
|
@@ -58,7 +84,7 @@ mode: ${options.mode}
|
|
|
58
84
|
port: ${options.mode === 'server' ? options.port : 'n/a'}
|
|
59
85
|
localConfigPath: ${options.localConfigPath ? options.localConfigPath : 'none'}
|
|
60
86
|
jsonTTL: ${options.jsonTTL} minutes
|
|
61
|
-
for
|
|
87
|
+
for options run: xfidelity --help
|
|
62
88
|
=====================================`);
|
|
63
89
|
logger_1.logger.info(banner);
|
|
64
90
|
// print help if no arguments are passed
|
|
@@ -11,7 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.analyzeCodebase = analyzeCodebase;
|
|
13
13
|
const logger_1 = require("../../utils/logger");
|
|
14
|
-
const
|
|
14
|
+
const configManager_1 = require("../../utils/configManager");
|
|
15
15
|
const openaiUtils_1 = require("../../utils/openaiUtils");
|
|
16
16
|
const telemetry_1 = require("../../utils/telemetry");
|
|
17
17
|
const repoFilesystemFacts_1 = require("../../facts/repoFilesystemFacts");
|
|
@@ -20,30 +20,33 @@ const openaiAnalysisFacts_1 = require("../../facts/openaiAnalysisFacts");
|
|
|
20
20
|
const telemetryCollector_1 = require("./telemetryCollector");
|
|
21
21
|
const engineSetup_1 = require("./engineSetup");
|
|
22
22
|
const engineRunner_1 = require("./engineRunner");
|
|
23
|
-
const utils_1 = require("
|
|
23
|
+
const utils_1 = require("../../utils/utils");
|
|
24
|
+
const cli_1 = require("../cli");
|
|
24
25
|
function analyzeCodebase(params) {
|
|
25
26
|
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
-
const { repoPath, archetype = 'node-fullstack', configServer = '', localConfigPath = '' } = params;
|
|
27
|
-
|
|
28
|
-
(0, logger_1.setLogPrefix)(executionLogPrefix);
|
|
29
|
-
logger_1.logger.info(`INITIALISING..`);
|
|
27
|
+
const { repoPath, archetype = 'node-fullstack', configServer = '', localConfigPath = '', executionLogPrefix = '' } = params;
|
|
28
|
+
logger_1.logger.info(`STARTING..`);
|
|
30
29
|
const telemetryData = yield (0, telemetryCollector_1.collectTelemetryData)({ repoPath, configServer });
|
|
31
30
|
// Send telemetry for analysis start
|
|
32
31
|
yield (0, telemetry_1.sendTelemetry)({
|
|
33
32
|
eventType: 'analysisStart',
|
|
34
|
-
metadata:
|
|
35
|
-
|
|
33
|
+
metadata: {
|
|
34
|
+
archetype,
|
|
35
|
+
repoPath,
|
|
36
|
+
telemetryData,
|
|
37
|
+
options: cli_1.options
|
|
38
|
+
},
|
|
36
39
|
timestamp: new Date().toISOString()
|
|
37
40
|
}, executionLogPrefix);
|
|
38
|
-
const executionConfig = yield
|
|
41
|
+
const executionConfig = yield configManager_1.ConfigManager.getConfig({ archetype, logPrefix: executionLogPrefix });
|
|
39
42
|
const archetypeConfig = executionConfig.archetype;
|
|
40
43
|
const installedDependencyVersions = yield (0, repoDependencyFacts_1.getDependencyVersionFacts)(archetypeConfig);
|
|
41
44
|
const fileData = yield (0, repoFilesystemFacts_1.collectRepoFileData)(repoPath, archetypeConfig);
|
|
42
45
|
// add REPO_GLOBAL_CHECK to fileData, which is the trigger for global checks
|
|
43
46
|
fileData.push({
|
|
44
|
-
fileName:
|
|
45
|
-
filePath:
|
|
46
|
-
fileContent:
|
|
47
|
+
fileName: configManager_1.REPO_GLOBAL_CHECK,
|
|
48
|
+
filePath: configManager_1.REPO_GLOBAL_CHECK,
|
|
49
|
+
fileContent: configManager_1.REPO_GLOBAL_CHECK
|
|
47
50
|
});
|
|
48
51
|
const { minimumDependencyVersions, standardStructure } = archetypeConfig.config;
|
|
49
52
|
let openaiSystemPrompt;
|
|
@@ -53,7 +56,7 @@ function analyzeCodebase(params) {
|
|
|
53
56
|
const engine = yield (0, engineSetup_1.setupEngine)({
|
|
54
57
|
archetypeConfig,
|
|
55
58
|
archetype,
|
|
56
|
-
configManager:
|
|
59
|
+
configManager: configManager_1.ConfigManager,
|
|
57
60
|
executionLogPrefix,
|
|
58
61
|
localConfigPath
|
|
59
62
|
});
|
|
@@ -73,19 +76,33 @@ function analyzeCodebase(params) {
|
|
|
73
76
|
});
|
|
74
77
|
const finishMsg = `\n==========================\nCHECKS COMPLETED..\n==========================`;
|
|
75
78
|
logger_1.logger.info(finishMsg);
|
|
76
|
-
|
|
77
|
-
|
|
79
|
+
const totalFailureCount = (0, utils_1.countRuleFailures)(failures);
|
|
80
|
+
logger_1.logger.info(`${fileData.length} files analyzed. ${totalFailureCount} rule failures.`);
|
|
81
|
+
const fatalityCount = (0, utils_1.countRuleFailures)(failures, 'fatality');
|
|
82
|
+
const warningCount = (0, utils_1.countRuleFailures)(failures, 'warning');
|
|
83
|
+
const finishTime = new Date().getTime();
|
|
84
|
+
const resultMetadata = {
|
|
85
|
+
XFI_RESULT: {
|
|
86
|
+
archetype,
|
|
87
|
+
repoPath,
|
|
88
|
+
fileCount: fileData.length,
|
|
89
|
+
totalIssues: totalFailureCount,
|
|
90
|
+
warningCount: warningCount,
|
|
91
|
+
fatalityCount: fatalityCount,
|
|
92
|
+
issueDetails: failures,
|
|
93
|
+
startTime: telemetryData.startTime,
|
|
94
|
+
finishTime: finishTime,
|
|
95
|
+
durationSeconds: (finishTime - telemetryData.startTime) / 1000,
|
|
96
|
+
telemetryData,
|
|
97
|
+
options: cli_1.options
|
|
98
|
+
}
|
|
99
|
+
};
|
|
78
100
|
// Send telemetry for analysis end
|
|
79
101
|
yield (0, telemetry_1.sendTelemetry)({
|
|
80
|
-
eventType: '
|
|
81
|
-
metadata:
|
|
82
|
-
repoPath }, telemetryData), { fileCount: fileData.length, failureCount: failures.length, fatalityCount: fatalities.length }),
|
|
102
|
+
eventType: 'analysisResults',
|
|
103
|
+
metadata: resultMetadata,
|
|
83
104
|
timestamp: new Date().toISOString()
|
|
84
105
|
}, executionLogPrefix);
|
|
85
|
-
|
|
86
|
-
throw new Error(JSON.stringify(fatalities));
|
|
87
|
-
}
|
|
88
|
-
return failures;
|
|
106
|
+
return resultMetadata;
|
|
89
107
|
});
|
|
90
108
|
}
|
|
91
|
-
// ... (rest of the helper functions)
|