analyze-codebase 1.0.2 → 1.1.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/dist/analyze/index.js +3 -3
- package/dist/index.js +20 -13
- package/dist/utils/file.js +15 -4
- package/dist/utils/output.js +69 -9
- package/package.json +3 -11
- package/readme.md +12 -6
package/dist/analyze/index.js
CHANGED
|
@@ -20,7 +20,7 @@ const analyzeCodebase = async (options) => {
|
|
|
20
20
|
Mixed: 0,
|
|
21
21
|
EmptyBlockComment: 0,
|
|
22
22
|
Empty: 0,
|
|
23
|
-
ToDo: 0
|
|
23
|
+
ToDo: 0,
|
|
24
24
|
};
|
|
25
25
|
console.log(chalk_1.default.bold(`\n\nAnalyzing codebase in ${chalk_1.default.cyan(options.directory)} ${(options === null || options === void 0 ? void 0 : options.framework) ? `framework: ${chalk_1.default.cyan(options.framework)}` : ""}\n`));
|
|
26
26
|
let fileCount = 0;
|
|
@@ -37,13 +37,13 @@ const analyzeCodebase = async (options) => {
|
|
|
37
37
|
fileNameCases[fileNameCase] += 1;
|
|
38
38
|
else
|
|
39
39
|
fileNameCases[fileNameCase] = 0;
|
|
40
|
-
}
|
|
40
|
+
},
|
|
41
41
|
});
|
|
42
42
|
(0, output_1.logOutput)({
|
|
43
43
|
fileCount,
|
|
44
44
|
fileNameCases,
|
|
45
45
|
options,
|
|
46
|
-
output
|
|
46
|
+
output,
|
|
47
47
|
});
|
|
48
48
|
const totalSecond = ((Date.now() - start) / 1000).toFixed(2);
|
|
49
49
|
console.log(chalk_1.default.bold(`Time taken: ${chalk_1.default.yellow(totalSecond)}s\n`));
|
package/dist/index.js
CHANGED
|
@@ -9,27 +9,34 @@ const commander_1 = require("commander");
|
|
|
9
9
|
const runner_1 = __importDefault(require("./runner"));
|
|
10
10
|
chalk_1.default.level = 3;
|
|
11
11
|
commander_1.program
|
|
12
|
-
.arguments(
|
|
13
|
-
.description(
|
|
14
|
-
.option(
|
|
15
|
-
.option(
|
|
16
|
-
.option(
|
|
17
|
-
.option(
|
|
18
|
-
.option(
|
|
12
|
+
.arguments("<directory>")
|
|
13
|
+
.description("Analyze the specified directory")
|
|
14
|
+
.option("-f, --framework <framework>", "Specify the framework")
|
|
15
|
+
.option("-e, --extensions <extensions...>", "Specify the extensions", (value, previous) => previous.concat(value), [])
|
|
16
|
+
.option("-exc, --exclude <exclude...>", "Specify the exclude", (value, previous) => previous.concat(value), [])
|
|
17
|
+
.option("--checkFileNames [checkFileNames]", "Check file names", parseBoolean)
|
|
18
|
+
.option("--checkFileContent [checkFileContent]", "Check directories", parseBoolean)
|
|
19
|
+
.option("-w --writeJsonOutput [writeJsonOutput]", "Write json output", parseBoolean)
|
|
19
20
|
.action((directory, options) => {
|
|
20
21
|
var _a, _b, _c;
|
|
21
|
-
if (options.checkFileNames === false &&
|
|
22
|
+
if (options.checkFileNames === false &&
|
|
23
|
+
options.checkFileContent === false) {
|
|
22
24
|
return console.error(chalk_1.default.red(`\nError: You must specify ${chalk_1.default.yellow("true")} at least one of the following options: ${chalk_1.default.yellow("--checkFileNames")}, ${chalk_1.default.yellow("--checkFileContent")}\n`));
|
|
23
25
|
}
|
|
24
|
-
if ((_a = options === null || options === void 0 ? void 0 : options.extensions) === null || _a === void 0 ? void 0 : _a.find((d) => !d.includes(
|
|
25
|
-
return console.error(chalk_1.default.red(
|
|
26
|
+
if ((_a = options === null || options === void 0 ? void 0 : options.extensions) === null || _a === void 0 ? void 0 : _a.find((d) => !d.includes("."))) {
|
|
27
|
+
return console.error(chalk_1.default.red("\nError: Extensions must include a dot (.)\n"));
|
|
26
28
|
}
|
|
27
|
-
(0, runner_1.default)({
|
|
29
|
+
(0, runner_1.default)({
|
|
30
|
+
...options,
|
|
31
|
+
directory,
|
|
32
|
+
checkFileContent: (_b = options.checkFileContent) !== null && _b !== void 0 ? _b : true,
|
|
33
|
+
checkFileNames: (_c = options.checkFileNames) !== null && _c !== void 0 ? _c : true,
|
|
34
|
+
});
|
|
28
35
|
});
|
|
29
36
|
commander_1.program.parse(process.argv);
|
|
30
37
|
function parseBoolean(value) {
|
|
31
|
-
if (typeof value ===
|
|
38
|
+
if (typeof value === "undefined") {
|
|
32
39
|
return undefined;
|
|
33
40
|
}
|
|
34
|
-
return value.toLowerCase() ===
|
|
41
|
+
return value.toLowerCase() === "true";
|
|
35
42
|
}
|
package/dist/utils/file.js
CHANGED
|
@@ -26,14 +26,25 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
26
26
|
exports.traverseDirectory = void 0;
|
|
27
27
|
const fs = __importStar(require("fs/promises"));
|
|
28
28
|
const path = __importStar(require("path"));
|
|
29
|
+
const blackList = [
|
|
30
|
+
"node_modules",
|
|
31
|
+
"dist",
|
|
32
|
+
"build",
|
|
33
|
+
"coverage",
|
|
34
|
+
"public",
|
|
35
|
+
"test",
|
|
36
|
+
"tests",
|
|
37
|
+
"mocks",
|
|
38
|
+
];
|
|
29
39
|
const traverseDirectory = async ({ directory, onFile, onDirectory, exclude, extensions, checkFileNames = true, }) => {
|
|
30
40
|
const files = await fs.readdir(directory, { withFileTypes: true });
|
|
31
41
|
const tasks = files.map(async (file) => {
|
|
42
|
+
var _a;
|
|
32
43
|
const filePath = path.join(directory, file.name);
|
|
33
|
-
if (exclude === null || exclude === void 0 ? void 0 : exclude.
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
44
|
+
if (((_a = exclude === null || exclude === void 0 ? void 0 : exclude.includes) === null || _a === void 0 ? void 0 : _a.call(exclude, file.name)) ||
|
|
45
|
+
file.name.startsWith(".") ||
|
|
46
|
+
blackList.includes(file.name))
|
|
47
|
+
return;
|
|
37
48
|
if (file.isDirectory()) {
|
|
38
49
|
onDirectory === null || onDirectory === void 0 ? void 0 : onDirectory(filePath);
|
|
39
50
|
await (0, exports.traverseDirectory)({
|
package/dist/utils/output.js
CHANGED
|
@@ -1,21 +1,70 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
27
|
};
|
|
5
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
29
|
exports.logOutput = void 0;
|
|
7
|
-
const
|
|
30
|
+
const fs = __importStar(require("fs"));
|
|
8
31
|
const cli_table3_1 = __importDefault(require("cli-table3"));
|
|
9
|
-
const
|
|
10
|
-
|
|
32
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
33
|
+
const logOutput = ({ fileCount, output, fileNameCases, options, }) => {
|
|
34
|
+
// Create an object to store the result information
|
|
35
|
+
const resultObject = {
|
|
36
|
+
date: new Date().toISOString(),
|
|
37
|
+
fileCount,
|
|
38
|
+
fileNameCases,
|
|
39
|
+
options,
|
|
40
|
+
output,
|
|
41
|
+
// ... other result information ...
|
|
42
|
+
};
|
|
43
|
+
// Convert the object to a JSON string
|
|
44
|
+
const resultJson = JSON.stringify(resultObject, null, 2); // The third parameter (2) adds indentation for better readability
|
|
45
|
+
// Log the JSON output to the console
|
|
46
|
+
console.log(chalk_1.default.bold("------------- Result -------------\n"));
|
|
47
|
+
// console.log(resultJson);
|
|
48
|
+
if (options.writeJsonOutput) {
|
|
49
|
+
// Create the directory if it doesn't exist
|
|
50
|
+
const directoryPath = `${options.directory}/.analyze-codebase`;
|
|
51
|
+
if (!fs.existsSync(directoryPath))
|
|
52
|
+
fs.mkdirSync(directoryPath, { recursive: true });
|
|
53
|
+
}
|
|
54
|
+
// Write the JSON output to a file (optional)
|
|
55
|
+
// Continue with your existing console logs (if needed)
|
|
11
56
|
if (fileCount === 0) {
|
|
12
57
|
console.log(chalk_1.default.red(`No files found in ${chalk_1.default.cyan(options.directory)} with extensions ${chalk_1.default.cyan(options.extensions)}\n`));
|
|
13
58
|
}
|
|
14
59
|
else {
|
|
15
60
|
if (options.checkFileNames) {
|
|
16
|
-
console.log(chalk_1.default.bold(
|
|
61
|
+
console.log(chalk_1.default.bold("File Name Summary:\n"));
|
|
17
62
|
const fileNameTable = new cli_table3_1.default({
|
|
18
|
-
head: [
|
|
63
|
+
head: [
|
|
64
|
+
chalk_1.default.bold("File Name Case"),
|
|
65
|
+
chalk_1.default.bold("Count"),
|
|
66
|
+
chalk_1.default.bold("Percentage"),
|
|
67
|
+
],
|
|
19
68
|
colWidths: [30, 10, 15],
|
|
20
69
|
});
|
|
21
70
|
Object.entries(fileNameCases).forEach(([key, value]) => {
|
|
@@ -23,12 +72,16 @@ const logOutput = ({ fileCount, output, fileNameCases, options }) => {
|
|
|
23
72
|
fileNameTable.push([key, value.toString(), `${percentage}%`]);
|
|
24
73
|
});
|
|
25
74
|
console.log(fileNameTable.toString());
|
|
26
|
-
console.log(
|
|
75
|
+
console.log("\n");
|
|
27
76
|
}
|
|
28
77
|
if (options.checkFileContent) {
|
|
29
|
-
console.log(chalk_1.default.bold(
|
|
78
|
+
console.log(chalk_1.default.bold("Content Type Summary:\n"));
|
|
30
79
|
const contentTypeTable = new cli_table3_1.default({
|
|
31
|
-
head: [
|
|
80
|
+
head: [
|
|
81
|
+
chalk_1.default.bold("Content Type"),
|
|
82
|
+
chalk_1.default.bold("Count"),
|
|
83
|
+
chalk_1.default.bold("Percentage"),
|
|
84
|
+
],
|
|
32
85
|
colWidths: [30, 10, 15],
|
|
33
86
|
});
|
|
34
87
|
Object.entries(output).forEach(([key, value]) => {
|
|
@@ -38,7 +91,14 @@ const logOutput = ({ fileCount, output, fileNameCases, options }) => {
|
|
|
38
91
|
console.log(contentTypeTable.toString());
|
|
39
92
|
}
|
|
40
93
|
}
|
|
41
|
-
|
|
94
|
+
if (options.writeJsonOutput) {
|
|
95
|
+
const writePath = `${options.directory}/.analyze-codebase/${resultObject.date}.json`;
|
|
96
|
+
fs.writeFileSync(writePath, resultJson, {
|
|
97
|
+
flag: "w",
|
|
98
|
+
});
|
|
99
|
+
console.log(chalk_1.default.bold(`\nJSON output written to: ${chalk_1.default.cyan(writePath)}\n`));
|
|
100
|
+
}
|
|
101
|
+
console.log(chalk_1.default.bold("\n----------------------------"));
|
|
42
102
|
console.log(chalk_1.default.bold(`\nNumber of files read: ${chalk_1.default.yellow(fileCount)}\n`));
|
|
43
103
|
};
|
|
44
104
|
exports.logOutput = logOutput;
|
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/mtahagocer/analyze-codebase"
|
|
7
7
|
},
|
|
8
|
-
"version": "1.0
|
|
8
|
+
"version": "1.1.0",
|
|
9
9
|
"main": "dist/index.js",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"bin": {
|
|
@@ -16,18 +16,10 @@
|
|
|
16
16
|
"start": "node ./dist/index.js",
|
|
17
17
|
"compile": "npx rimraf dist && npx tsc"
|
|
18
18
|
},
|
|
19
|
-
"contributes": {
|
|
20
|
-
"commands": [
|
|
21
|
-
{
|
|
22
|
-
"command": "extension.analyzeCode",
|
|
23
|
-
"title": "Analyze Codebase"
|
|
24
|
-
}
|
|
25
|
-
]
|
|
26
|
-
},
|
|
27
19
|
"devDependencies": {
|
|
28
20
|
"@types/node": "^20.1.5",
|
|
29
|
-
"
|
|
30
|
-
"
|
|
21
|
+
"ts-node": "^10.9.1",
|
|
22
|
+
"typescript": "^5.0.4"
|
|
31
23
|
},
|
|
32
24
|
"dependencies": {
|
|
33
25
|
"chalk": "4.1.0",
|
package/readme.md
CHANGED
|
@@ -21,12 +21,18 @@ You can install Codebase Analyzer globally using NPM:
|
|
|
21
21
|
npm install -g analyze-codebase
|
|
22
22
|
```
|
|
23
23
|
|
|
24
|
+
or use with npx
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npx analyze-codebase ./MyProject --exclude node_modules dist --extensions .tsx .ts
|
|
28
|
+
```
|
|
29
|
+
|
|
24
30
|
## Usage
|
|
25
31
|
|
|
26
32
|
To analyze a directory, use the analyze command followed by the directory path. Here's an example:
|
|
27
33
|
|
|
28
34
|
```bash
|
|
29
|
-
analyze-codebase
|
|
35
|
+
analyze-codebase ./MyProject --exclude node_modules dist --extensions .tsx .ts
|
|
30
36
|
```
|
|
31
37
|
|
|
32
38
|
## Options
|
|
@@ -45,31 +51,31 @@ analyze-codebase analyze ./src
|
|
|
45
51
|
Analyze a directory with default options:
|
|
46
52
|
|
|
47
53
|
```bash
|
|
48
|
-
analyze-codebase
|
|
54
|
+
analyze-codebase ./src
|
|
49
55
|
```
|
|
50
56
|
|
|
51
57
|
Analyze a directory with a specified framework and file extensions:
|
|
52
58
|
|
|
53
59
|
```bash
|
|
54
|
-
analyze-codebase
|
|
60
|
+
analyze-codebase ./src -f react --extensions .js .jsx .ts .tsx
|
|
55
61
|
```
|
|
56
62
|
|
|
57
63
|
Exclude specific directories from the analysis:
|
|
58
64
|
|
|
59
65
|
```bash
|
|
60
|
-
analyze-codebase
|
|
66
|
+
analyze-codebase ./src --exclude node_modules dist
|
|
61
67
|
```
|
|
62
68
|
|
|
63
69
|
Analyze only file names
|
|
64
70
|
|
|
65
71
|
```bash
|
|
66
|
-
analyze-codebase
|
|
72
|
+
analyze-codebase ./src --exclude node_modules dist --checkFileContent=false
|
|
67
73
|
```
|
|
68
74
|
|
|
69
75
|
Analyze only file content
|
|
70
76
|
|
|
71
77
|
```bash
|
|
72
|
-
analyze-codebase
|
|
78
|
+
analyze-codebase ./src --exclude node_modules dist --checkFileNames=false
|
|
73
79
|
```
|
|
74
80
|
|
|
75
81
|
## Contribution
|