cc-codeline 1.0.4 → 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 +158 -156
- package/package.json +2 -2
package/README.md
CHANGED
package/bin.js
CHANGED
|
@@ -1,81 +1,87 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
const fs = require(
|
|
3
|
-
const { extname, resolve } = require(
|
|
2
|
+
const fs = require("fs").promises;
|
|
3
|
+
const { extname, resolve, basename } = require("path");
|
|
4
4
|
|
|
5
5
|
const languages = {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
6
|
+
".js": {
|
|
7
|
+
comment: /\/\*[\s\S]+?\*\/|\/\/.*/g,
|
|
8
|
+
},
|
|
9
|
+
".ts": {
|
|
10
|
+
comment: /\/\*[\s\S]+?\*\/|\/\/.*/g,
|
|
11
|
+
},
|
|
12
|
+
".jsx": {
|
|
13
|
+
comment: /\/\*[\s\S]+?\*\/|\/\/.*/g,
|
|
14
|
+
},
|
|
15
|
+
".tsx": {
|
|
16
|
+
comment: /\/\*[\s\S]+?\*\/|\/\/.*/g,
|
|
17
|
+
},
|
|
18
|
+
".html": {
|
|
19
|
+
comment: /<!--[\s\S]+?-->/g,
|
|
20
|
+
},
|
|
21
|
+
".css": {
|
|
22
|
+
comment: /\/\*[\s\S]+?\*\//g,
|
|
23
|
+
},
|
|
24
|
+
".vue": {
|
|
25
|
+
comment: /\/\*[\s\S]+?\*\/|\/\/.*|<!--[\s\S]+?-->/g,
|
|
26
|
+
},
|
|
27
|
+
".java": {
|
|
28
|
+
comment: /\/\*[\s\S]+?\*\/|\/\/.*/g,
|
|
29
|
+
},
|
|
30
|
+
".go": {
|
|
31
|
+
comment: /\/\*[\s\S]+?\*\/|\/\/.*/g,
|
|
32
|
+
},
|
|
33
|
+
".rs": {
|
|
34
|
+
comment: /\/\*[\s\S]+?\*\/|\/\/.*/g,
|
|
35
|
+
},
|
|
36
|
+
".zig": {
|
|
37
|
+
comment: /\/\*[\s\S]+?\*\/|\/\/.*/g,
|
|
38
|
+
},
|
|
39
|
+
".cs": {
|
|
40
|
+
comment: /\/\*[\s\S]+?\*\/|\/\/.*/g,
|
|
41
|
+
},
|
|
42
|
+
".c": {
|
|
43
|
+
comment: /\/\*[\s\S]+?\*\/|\/\/.*/g,
|
|
44
|
+
},
|
|
45
|
+
".cpp": {
|
|
46
|
+
comment: /\/\*[\s\S]+?\*\/|\/\/.*/g,
|
|
47
|
+
},
|
|
48
|
+
".h": {
|
|
49
|
+
comment: /\/\*[\s\S]+?\*\/|\/\/.*/g,
|
|
50
|
+
},
|
|
51
|
+
".py": {
|
|
52
|
+
comment: /'''[\s\S]+?'''|#.*/g,
|
|
53
|
+
},
|
|
48
54
|
};
|
|
49
55
|
|
|
50
56
|
const param = process.argv.slice(2);
|
|
51
|
-
if (param.includes(
|
|
52
|
-
|
|
57
|
+
if (param.includes("-h")) {
|
|
58
|
+
console.log(`
|
|
53
59
|
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
|
}
|
|
61
67
|
const base = process.cwd();
|
|
62
68
|
const isLog = (() => {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
+
const index = param.indexOf("-l");
|
|
70
|
+
if (index >= 0) {
|
|
71
|
+
param.splice(index, 1);
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
return false;
|
|
69
75
|
})();
|
|
70
76
|
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
77
|
+
const excludeNames = (() => {
|
|
78
|
+
const index = param.findIndex((it) => it == "-e");
|
|
79
|
+
if (index >= 0) {
|
|
80
|
+
return param.splice(index);
|
|
81
|
+
}
|
|
76
82
|
})();
|
|
77
83
|
|
|
78
|
-
const path = resolve(base, param[0] ||
|
|
84
|
+
const path = resolve(base, param[0] || "");
|
|
79
85
|
|
|
80
86
|
let total = 0;
|
|
81
87
|
let empty = 0;
|
|
@@ -83,108 +89,104 @@ let source = 0;
|
|
|
83
89
|
let comment = 0;
|
|
84
90
|
let fileCount = 0;
|
|
85
91
|
|
|
86
|
-
(() => {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
92
|
+
setInterval(() => {
|
|
93
|
+
console.log("...");
|
|
94
|
+
}, 1000);
|
|
95
|
+
|
|
96
|
+
main();
|
|
97
|
+
|
|
98
|
+
async function main() {
|
|
99
|
+
try {
|
|
100
|
+
const stat = await fs.stat(path);
|
|
101
|
+
if (stat.isDirectory()) {
|
|
102
|
+
await readDirectory(path);
|
|
103
|
+
print();
|
|
104
|
+
process.exit(0);
|
|
105
|
+
} else {
|
|
106
|
+
await readFile(path);
|
|
107
|
+
print();
|
|
108
|
+
process.exit(0);
|
|
109
|
+
}
|
|
110
|
+
} catch (err) {
|
|
111
|
+
console.error(err.message || "Unknown error");
|
|
112
|
+
}
|
|
113
|
+
}
|
|
102
114
|
|
|
103
115
|
function print() {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
116
|
+
console.table({
|
|
117
|
+
Total: total,
|
|
118
|
+
Empty: empty,
|
|
119
|
+
Source: source,
|
|
120
|
+
Comment: comment,
|
|
121
|
+
"File Count": fileCount,
|
|
122
|
+
});
|
|
111
123
|
}
|
|
112
124
|
|
|
113
|
-
function readDirectory(dirPath
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
} else {
|
|
140
|
-
readFile(filepath, () => {
|
|
141
|
-
progress += 1;
|
|
142
|
-
if (progress >= filenames.length) {
|
|
143
|
-
callback();
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
});
|
|
125
|
+
async function readDirectory(dirPath) {
|
|
126
|
+
if (excludeNames && excludeNames.includes(basename(dirPath))) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
let progress = 0;
|
|
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;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
149
151
|
}
|
|
150
152
|
|
|
151
|
-
function readFile(filepath
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
fs.readFile(filepath, (err, data) => {
|
|
158
|
-
if (err) {
|
|
159
|
-
console.log(err.message);
|
|
160
|
-
callback();
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
153
|
+
async function readFile(filepath) {
|
|
154
|
+
let removedExt = filepath.split(".");
|
|
155
|
+
removedExt.pop()
|
|
156
|
+
removedExt = removedExt.join("");
|
|
163
157
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
158
|
+
if (excludeNames && excludeNames.includes(basename(removedExt))) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const fileExtname = extname(filepath);
|
|
162
|
+
if (!Object.keys(languages).includes(fileExtname)) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const data = await fs.readFile(filepath);
|
|
166
|
+
if (isLog) {
|
|
167
|
+
console.log(filepath);
|
|
168
|
+
}
|
|
167
169
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
+
const content = data.toString();
|
|
171
|
+
fileCount += 1;
|
|
170
172
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
}
|
|
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;
|
|
189
|
+
}
|
|
190
|
+
source += 1;
|
|
191
|
+
}
|
|
192
|
+
}
|
package/package.json
CHANGED