cc-codeline 1.0.5 → 1.0.6

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.
Files changed (3) hide show
  1. package/README.md +2 -2
  2. package/bin.js +64 -57
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -2,9 +2,9 @@
2
2
  ```
3
3
  npm i -g cc-codeline
4
4
 
5
- codeline /path/
5
+ codeline /path/ -l -e node_modules dist
6
6
 
7
7
  -h Show help
8
8
  -l Print log
9
- -e Exclude directory
9
+ -e Exclude files or directories by name
10
10
  ```
package/bin.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  const fs = require("fs").promises;
3
- const { extname, resolve } = require("path");
3
+ const { extname, resolve, basename } = require("path");
4
4
 
5
5
  const languages = {
6
6
  ".js": {
@@ -27,9 +27,15 @@ const languages = {
27
27
  ".java": {
28
28
  comment: /\/\*[\s\S]+?\*\/|\/\/.*/g,
29
29
  },
30
+ ".go": {
31
+ comment: /\/\*[\s\S]+?\*\/|\/\/.*/g,
32
+ },
30
33
  ".rs": {
31
34
  comment: /\/\*[\s\S]+?\*\/|\/\/.*/g,
32
35
  },
36
+ ".zig": {
37
+ comment: /\/\*[\s\S]+?\*\/|\/\/.*/g,
38
+ },
33
39
  ".cs": {
34
40
  comment: /\/\*[\s\S]+?\*\/|\/\/.*/g,
35
41
  },
@@ -54,7 +60,7 @@ codeline /path/ -l -e node_modules dist
54
60
  ------------------
55
61
  -h Show help
56
62
  -l Print log
57
- -e Exclude directory
63
+ -e Exclude files or directories by name
58
64
  `);
59
65
  process.exit(0);
60
66
  }
@@ -68,10 +74,10 @@ const isLog = (() => {
68
74
  return false;
69
75
  })();
70
76
 
71
- const excludePath = (() => {
77
+ const excludeNames = (() => {
72
78
  const index = param.findIndex((it) => it == "-e");
73
79
  if (index >= 0) {
74
- return param.splice(index).map((it) => resolve(base, it));
80
+ return param.splice(index);
75
81
  }
76
82
  })();
77
83
 
@@ -87,7 +93,9 @@ setInterval(() => {
87
93
  console.log("...");
88
94
  }, 1000);
89
95
 
90
- (async () => {
96
+ main();
97
+
98
+ async function main() {
91
99
  try {
92
100
  const stat = await fs.stat(path);
93
101
  if (stat.isDirectory()) {
@@ -100,9 +108,9 @@ setInterval(() => {
100
108
  process.exit(0);
101
109
  }
102
110
  } catch (err) {
103
- console.log(err.message || "Error, the path may not exist");
111
+ console.error(err.message || "Unknown error");
104
112
  }
105
- })();
113
+ }
106
114
 
107
115
  function print() {
108
116
  console.table({
@@ -115,71 +123,70 @@ function print() {
115
123
  }
116
124
 
117
125
  async function readDirectory(dirPath) {
118
- if (excludePath && excludePath.includes(dirPath)) {
126
+ if (excludeNames && excludeNames.includes(basename(dirPath))) {
119
127
  return;
120
128
  }
121
129
  let progress = 0;
122
- try {
123
- const filenames = await fs.readdir(dirPath);
124
- if (!filenames.length) {
125
- return;
126
- }
127
- for (let name of filenames) {
128
- const filepath = resolve(dirPath, name);
129
- const fileStat = await fs.stat(filepath);
130
- if (fileStat.isDirectory()) {
131
- await readDirectory(filepath);
132
- progress += 1;
133
- if (progress >= filenames.length) {
134
- return;
135
- }
136
- } else {
137
- await readFile(filepath);
138
- progress += 1;
139
- if (progress >= filenames.length) {
140
- return;
141
- }
130
+ const filenames = await fs.readdir(dirPath);
131
+ if (!filenames.length) {
132
+ return;
133
+ }
134
+ for (let name of filenames) {
135
+ const filepath = resolve(dirPath, name);
136
+ const fileStat = await fs.stat(filepath);
137
+ if (fileStat.isDirectory()) {
138
+ await readDirectory(filepath);
139
+ progress += 1;
140
+ if (progress >= filenames.length) {
141
+ return;
142
+ }
143
+ } else {
144
+ await readFile(filepath);
145
+ progress += 1;
146
+ if (progress >= filenames.length) {
147
+ return;
142
148
  }
143
149
  }
144
- } catch (err) {
145
- console.log(err.message || "Error");
146
150
  }
147
151
  }
148
152
 
149
153
  async function readFile(filepath) {
154
+ let removedExt = filepath.split(".");
155
+ removedExt.pop()
156
+ removedExt = removedExt.join("");
157
+
158
+ if (excludeNames && excludeNames.includes(basename(removedExt))) {
159
+ return;
160
+ }
150
161
  const fileExtname = extname(filepath);
151
162
  if (!Object.keys(languages).includes(fileExtname)) {
152
163
  return;
153
164
  }
154
- try {
155
- const data = await fs.readFile(filepath);
156
- if (isLog) {
157
- console.log(filepath);
158
- }
165
+ const data = await fs.readFile(filepath);
166
+ if (isLog) {
167
+ console.log(filepath);
168
+ }
159
169
 
160
- const content = data.toString();
161
- fileCount += 1;
170
+ const content = data.toString();
171
+ fileCount += 1;
162
172
 
163
- const reg = languages[fileExtname].comment;
164
- reg.lastIndex = 0;
165
- if (reg) {
166
- content.match(reg)?.forEach((comm) => {
167
- const res = comm.split("\n");
168
- comment += res.length;
169
- source -= res.length;
170
- });
171
- }
172
- const lines = content.split("\n");
173
- total += lines.length;
174
- for (let line of lines) {
175
- line = line.trim();
176
- if (!line) {
177
- empty += 1;
178
- continue;
179
- }
180
- source += 1;
173
+ const reg = languages[fileExtname].comment;
174
+ reg.lastIndex = 0;
175
+ if (reg) {
176
+ content.match(reg)?.forEach((comm) => {
177
+ const res = comm.split("\n");
178
+ comment += res.length;
179
+ source -= res.length;
180
+ });
181
+ }
182
+ const lines = content.split("\n");
183
+ total += lines.length;
184
+ for (let line of lines) {
185
+ line = line.trim();
186
+ if (!line) {
187
+ empty += 1;
188
+ continue;
181
189
  }
182
- } catch (err) {
183
- console.log(err.message);
190
+ source += 1;
184
191
  }
185
192
  }
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "cc-codeline",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "main": "bin.js",
5
- "description": "Quickly count your lines of code",
5
+ "description": "Quickly count the number of lines of code and files",
6
6
  "bin": {
7
7
  "codeline": "./bin.js"
8
8
  },
@@ -13,4 +13,4 @@
13
13
  "author": "",
14
14
  "license": "MIT",
15
15
  "type": "commonjs"
16
- }
16
+ }