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.
- package/README.md +2 -2
- package/bin.js +64 -57
- package/package.json +3 -3
package/README.md
CHANGED
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
|
|
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
|
|
77
|
+
const excludeNames = (() => {
|
|
72
78
|
const index = param.findIndex((it) => it == "-e");
|
|
73
79
|
if (index >= 0) {
|
|
74
|
-
return param.splice(index)
|
|
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
|
-
(
|
|
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.
|
|
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 (
|
|
126
|
+
if (excludeNames && excludeNames.includes(basename(dirPath))) {
|
|
119
127
|
return;
|
|
120
128
|
}
|
|
121
129
|
let progress = 0;
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
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
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
165
|
+
const data = await fs.readFile(filepath);
|
|
166
|
+
if (isLog) {
|
|
167
|
+
console.log(filepath);
|
|
168
|
+
}
|
|
159
169
|
|
|
160
|
-
|
|
161
|
-
|
|
170
|
+
const content = data.toString();
|
|
171
|
+
fileCount += 1;
|
|
162
172
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
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
|
-
|
|
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.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"main": "bin.js",
|
|
5
|
-
"description": "Quickly count
|
|
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
|
+
}
|