cc-codeline 1.0.0

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 (2) hide show
  1. package/bin.js +163 -0
  2. package/package.json +13 -0
package/bin.js ADDED
@@ -0,0 +1,163 @@
1
+ #!/usr/bin/env node
2
+ const fs = require('fs');
3
+ const { join, extname } = require('path');
4
+
5
+ const languages = {
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
+ '.rs': {
28
+ comment: /\/\*[\s\S]+?\*\/|\/\/.*/g
29
+ },
30
+ '.cs': {
31
+ comment: /\/\*[\s\S]+?\*\/|\/\/.*/g
32
+ },
33
+ '.c': {
34
+ comment: /\/\*[\s\S]+?\*\/|\/\/.*/g
35
+ },
36
+ '.cpp': {
37
+ comment: /\/\*[\s\S]+?\*\/|\/\/.*/g
38
+ },
39
+ '.h': {
40
+ comment: /\/\*[\s\S]+?\*\/|\/\/.*/g
41
+ },
42
+ '.py': {
43
+ comment: /'''[\s\S]+?'''/g
44
+ }
45
+ };
46
+ let total = 0;
47
+ let empty = 0;
48
+ let source = 0;
49
+ let comment = 0;
50
+ let fileCount = 0;
51
+
52
+ const param = process.argv.slice(2);
53
+ const isLog = param.includes('-l');
54
+
55
+ const base = process.cwd();
56
+ const path = join(base, param[0] || '');
57
+
58
+ (() => {
59
+ if (param.includes('-h')) {
60
+ console.table({
61
+ '-h': 'Show help',
62
+ '-l': 'Print log'
63
+ });
64
+ return;
65
+ }
66
+ try {
67
+ const stat = fs.statSync(path);
68
+ if (stat.isDirectory()) {
69
+ readDirectory(path, () => {
70
+ print()
71
+ });
72
+ } else {
73
+ readFile(path, () => {
74
+ print()
75
+ });
76
+ }
77
+ } catch (_) {
78
+ console.log('Path no found');
79
+ }
80
+ })();
81
+
82
+ function print() {
83
+ console.table({
84
+ 'Total': total,
85
+ 'Empty': empty,
86
+ 'Source': source,
87
+ 'Comment': comment,
88
+ 'File Count': fileCount
89
+ });
90
+ }
91
+
92
+ function readDirectory(dirPath, callback) {
93
+ let progress = 0;
94
+ fs.readdir(dirPath, (err, filenames) => {
95
+ if (err) {
96
+ console.log('ReadDirectory ERROR');
97
+ callback();
98
+ return;
99
+ }
100
+ if (!filenames.length) {
101
+ callback();
102
+ return;
103
+ }
104
+ for (let name of filenames) {
105
+ const filepath = join(dirPath, name);
106
+ const fileStat = fs.statSync(filepath);
107
+ if (fileStat.isDirectory()) {
108
+ readDirectory(filepath, () => {
109
+ progress += 1;
110
+ if (progress >= filenames.length) {
111
+ callback();
112
+ }
113
+ });
114
+ } else {
115
+ readFile(filepath, () => {
116
+ progress += 1;
117
+ if (progress >= filenames.length) {
118
+ callback();
119
+ }
120
+ });
121
+ }
122
+ }
123
+ });
124
+ }
125
+
126
+ function readFile(filepath, callback) {
127
+ const fileExtname = extname(filepath);
128
+ if (!Object.keys(languages).includes(fileExtname)) {
129
+ callback();
130
+ return;
131
+ }
132
+ fs.readFile(filepath, (err, data) => {
133
+ if (err) {
134
+ console.log('ReadFile ERROR');
135
+ callback();
136
+ return;
137
+ }
138
+
139
+ isLog && console.log(filepath);
140
+
141
+ const content = data.toString();
142
+ fileCount += 1;
143
+
144
+ const reg = languages[fileExtname].comment;
145
+ reg.lastIndex = 0;
146
+ reg && content.match(reg)?.forEach(comm => {
147
+ const res = comm.split('\n');
148
+ comment += res.length;
149
+ source -= res.length;
150
+ });
151
+ const lines = content.split('\n');
152
+ total += lines.length;
153
+ for (let line of lines) {
154
+ line = line.trim();
155
+ if (!line) {
156
+ empty += 1;
157
+ continue;
158
+ }
159
+ source += 1;
160
+ }
161
+ callback();
162
+ });
163
+ }
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "cc-codeline",
3
+ "version": "1.0.0",
4
+ "main": "index.js",
5
+ "describe": "Quickly count your lines of code",
6
+ "bin": {
7
+ "codeline": "./bin.js"
8
+ },
9
+ "keywords": [],
10
+ "author": "",
11
+ "license": "ISC",
12
+ "type": "commonjs"
13
+ }