analyze-codebase 1.0.3 → 1.1.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.
@@ -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('<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)
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 && options.checkFileContent === 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('\nError: Extensions must include a dot (.)\n'));
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)({ ...options, directory, checkFileContent: (_b = options.checkFileContent) !== null && _b !== void 0 ? _b : true, checkFileNames: (_c = options.checkFileNames) !== null && _c !== void 0 ? _c : true });
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 === 'undefined') {
38
+ if (typeof value === "undefined") {
32
39
  return undefined;
33
40
  }
34
- return value.toLowerCase() === 'true';
41
+ return value.toLowerCase() === "true";
35
42
  }
@@ -26,12 +26,24 @@ 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) => {
32
42
  var _a;
33
43
  const filePath = path.join(directory, file.name);
34
- if ((_a = exclude === null || exclude === void 0 ? void 0 : exclude.includes) === null || _a === void 0 ? void 0 : _a.call(exclude, file.name))
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))
35
47
  return;
36
48
  if (file.isDirectory()) {
37
49
  onDirectory === null || onDirectory === void 0 ? void 0 : onDirectory(filePath);
@@ -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 chalk_1 = __importDefault(require("chalk"));
30
+ const fs = __importStar(require("fs"));
8
31
  const cli_table3_1 = __importDefault(require("cli-table3"));
9
- const logOutput = ({ fileCount, output, fileNameCases, options }) => {
10
- console.log(chalk_1.default.bold('------------- Result -------------\n'));
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('File Name Summary:\n'));
61
+ console.log(chalk_1.default.bold("File Name Summary:\n"));
17
62
  const fileNameTable = new cli_table3_1.default({
18
- head: [chalk_1.default.bold('File Name Case'), chalk_1.default.bold('Count'), chalk_1.default.bold('Percentage')],
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('\n');
75
+ console.log("\n");
27
76
  }
28
77
  if (options.checkFileContent) {
29
- console.log(chalk_1.default.bold('Content Type Summary:\n'));
78
+ console.log(chalk_1.default.bold("Content Type Summary:\n"));
30
79
  const contentTypeTable = new cli_table3_1.default({
31
- head: [chalk_1.default.bold('Content Type'), chalk_1.default.bold('Count'), chalk_1.default.bold('Percentage')],
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
- console.log(chalk_1.default.bold('\n----------------------------'));
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.3",
8
+ "version": "1.1.1",
9
9
  "main": "dist/index.js",
10
10
  "license": "MIT",
11
11
  "bin": {
@@ -14,7 +14,8 @@
14
14
  "scripts": {
15
15
  "cli": "ts-node ./src/index.ts",
16
16
  "start": "node ./dist/index.js",
17
- "compile": "npx rimraf dist && npx tsc"
17
+ "compile": "npx rimraf dist && npx tsc",
18
+ "publish": "npm run compile && npm publish"
18
19
  },
19
20
  "devDependencies": {
20
21
  "@types/node": "^20.1.5",
package/readme.md CHANGED
@@ -46,6 +46,8 @@ analyze-codebase ./MyProject --exclude node_modules dist --extensions .tsx .ts
46
46
 
47
47
  - --checkFileContent [checkFileContent]: Check file content. Default: true.
48
48
 
49
+ - -w or --writeJsonOutput [writeJsonOutput]: Write json putput for tracking. Default false
50
+
49
51
  ## Examples
50
52
 
51
53
  Analyze a directory with default options:
@@ -78,6 +80,17 @@ Analyze only file content
78
80
  analyze-codebase ./src --exclude node_modules dist --checkFileNames=false
79
81
  ```
80
82
 
83
+ Write json output of this analyze
84
+
85
+ ```bash
86
+ analyze-codebase -w
87
+ ```
88
+ or
89
+
90
+ ```bash
91
+ analyze-codebase --writeJsonOutput
92
+ ```
93
+
81
94
  ## Contribution
82
95
 
83
96
  We welcome contributions to enhance the functionality and features of Codebase Analyzer. To contribute to the project, please follow these steps: