cc-codeline 1.0.4 → 1.0.5
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/bin.js +149 -154
- package/package.json +2 -2
package/bin.js
CHANGED
|
@@ -1,81 +1,81 @@
|
|
|
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 } = 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
|
+
".rs": {
|
|
31
|
+
comment: /\/\*[\s\S]+?\*\/|\/\/.*/g,
|
|
32
|
+
},
|
|
33
|
+
".cs": {
|
|
34
|
+
comment: /\/\*[\s\S]+?\*\/|\/\/.*/g,
|
|
35
|
+
},
|
|
36
|
+
".c": {
|
|
37
|
+
comment: /\/\*[\s\S]+?\*\/|\/\/.*/g,
|
|
38
|
+
},
|
|
39
|
+
".cpp": {
|
|
40
|
+
comment: /\/\*[\s\S]+?\*\/|\/\/.*/g,
|
|
41
|
+
},
|
|
42
|
+
".h": {
|
|
43
|
+
comment: /\/\*[\s\S]+?\*\/|\/\/.*/g,
|
|
44
|
+
},
|
|
45
|
+
".py": {
|
|
46
|
+
comment: /'''[\s\S]+?'''|#.*/g,
|
|
47
|
+
},
|
|
48
48
|
};
|
|
49
49
|
|
|
50
50
|
const param = process.argv.slice(2);
|
|
51
|
-
if (param.includes(
|
|
52
|
-
|
|
51
|
+
if (param.includes("-h")) {
|
|
52
|
+
console.log(`
|
|
53
53
|
codeline /path/ -l -e node_modules dist
|
|
54
54
|
------------------
|
|
55
55
|
-h Show help
|
|
56
56
|
-l Print log
|
|
57
57
|
-e Exclude directory
|
|
58
58
|
`);
|
|
59
|
-
|
|
59
|
+
process.exit(0);
|
|
60
60
|
}
|
|
61
61
|
const base = process.cwd();
|
|
62
62
|
const isLog = (() => {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
63
|
+
const index = param.indexOf("-l");
|
|
64
|
+
if (index >= 0) {
|
|
65
|
+
param.splice(index, 1);
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
return false;
|
|
69
69
|
})();
|
|
70
70
|
|
|
71
71
|
const excludePath = (() => {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
72
|
+
const index = param.findIndex((it) => it == "-e");
|
|
73
|
+
if (index >= 0) {
|
|
74
|
+
return param.splice(index).map((it) => resolve(base, it));
|
|
75
|
+
}
|
|
76
76
|
})();
|
|
77
77
|
|
|
78
|
-
const path = resolve(base, param[0] ||
|
|
78
|
+
const path = resolve(base, param[0] || "");
|
|
79
79
|
|
|
80
80
|
let total = 0;
|
|
81
81
|
let empty = 0;
|
|
@@ -83,108 +83,103 @@ let source = 0;
|
|
|
83
83
|
let comment = 0;
|
|
84
84
|
let fileCount = 0;
|
|
85
85
|
|
|
86
|
-
(() => {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
86
|
+
setInterval(() => {
|
|
87
|
+
console.log("...");
|
|
88
|
+
}, 1000);
|
|
89
|
+
|
|
90
|
+
(async () => {
|
|
91
|
+
try {
|
|
92
|
+
const stat = await fs.stat(path);
|
|
93
|
+
if (stat.isDirectory()) {
|
|
94
|
+
await readDirectory(path);
|
|
95
|
+
print();
|
|
96
|
+
process.exit(0);
|
|
97
|
+
} else {
|
|
98
|
+
await readFile(path);
|
|
99
|
+
print();
|
|
100
|
+
process.exit(0);
|
|
101
|
+
}
|
|
102
|
+
} catch (err) {
|
|
103
|
+
console.log(err.message || "Error, the path may not exist");
|
|
104
|
+
}
|
|
101
105
|
})();
|
|
102
106
|
|
|
103
107
|
function print() {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
108
|
+
console.table({
|
|
109
|
+
Total: total,
|
|
110
|
+
Empty: empty,
|
|
111
|
+
Source: source,
|
|
112
|
+
Comment: comment,
|
|
113
|
+
"File Count": fileCount,
|
|
114
|
+
});
|
|
111
115
|
}
|
|
112
116
|
|
|
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
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
callback();
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
});
|
|
117
|
+
async function readDirectory(dirPath) {
|
|
118
|
+
if (excludePath && excludePath.includes(dirPath)) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
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
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
} catch (err) {
|
|
145
|
+
console.log(err.message || "Error");
|
|
146
|
+
}
|
|
149
147
|
}
|
|
150
148
|
|
|
151
|
-
function readFile(filepath
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
if (isLog) {
|
|
165
|
-
console.log(filepath);
|
|
166
|
-
}
|
|
149
|
+
async function readFile(filepath) {
|
|
150
|
+
const fileExtname = extname(filepath);
|
|
151
|
+
if (!Object.keys(languages).includes(fileExtname)) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
try {
|
|
155
|
+
const data = await fs.readFile(filepath);
|
|
156
|
+
if (isLog) {
|
|
157
|
+
console.log(filepath);
|
|
158
|
+
}
|
|
167
159
|
|
|
168
|
-
|
|
169
|
-
|
|
160
|
+
const content = data.toString();
|
|
161
|
+
fileCount += 1;
|
|
170
162
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
}
|
|
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;
|
|
181
|
+
}
|
|
182
|
+
} catch (err) {
|
|
183
|
+
console.log(err.message);
|
|
184
|
+
}
|
|
185
|
+
}
|
package/package.json
CHANGED