cc-codeline 1.0.5 → 1.0.7

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 +68 -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
  },
@@ -45,6 +51,9 @@ const languages = {
45
51
  ".py": {
46
52
  comment: /'''[\s\S]+?'''|#.*/g,
47
53
  },
54
+ ".lua": {
55
+ comment: /--\[\[[\s\S]+?\]\]|--.*/g,
56
+ }
48
57
  };
49
58
 
50
59
  const param = process.argv.slice(2);
@@ -54,7 +63,7 @@ codeline /path/ -l -e node_modules dist
54
63
  ------------------
55
64
  -h Show help
56
65
  -l Print log
57
- -e Exclude directory
66
+ -e Exclude files or directories by name
58
67
  `);
59
68
  process.exit(0);
60
69
  }
@@ -68,10 +77,10 @@ const isLog = (() => {
68
77
  return false;
69
78
  })();
70
79
 
71
- const excludePath = (() => {
80
+ const excludeNames = (() => {
72
81
  const index = param.findIndex((it) => it == "-e");
73
82
  if (index >= 0) {
74
- return param.splice(index).map((it) => resolve(base, it));
83
+ return param.splice(index);
75
84
  }
76
85
  })();
77
86
 
@@ -87,7 +96,9 @@ setInterval(() => {
87
96
  console.log("...");
88
97
  }, 1000);
89
98
 
90
- (async () => {
99
+ main();
100
+
101
+ async function main() {
91
102
  try {
92
103
  const stat = await fs.stat(path);
93
104
  if (stat.isDirectory()) {
@@ -100,9 +111,10 @@ setInterval(() => {
100
111
  process.exit(0);
101
112
  }
102
113
  } catch (err) {
103
- console.log(err.message || "Error, the path may not exist");
114
+ console.error(err.message || "Unknown error");
115
+ process.exit(0);
104
116
  }
105
- })();
117
+ }
106
118
 
107
119
  function print() {
108
120
  console.table({
@@ -115,71 +127,70 @@ function print() {
115
127
  }
116
128
 
117
129
  async function readDirectory(dirPath) {
118
- if (excludePath && excludePath.includes(dirPath)) {
130
+ if (excludeNames && excludeNames.includes(basename(dirPath))) {
119
131
  return;
120
132
  }
121
133
  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
- }
134
+ const filenames = await fs.readdir(dirPath);
135
+ if (!filenames.length) {
136
+ return;
137
+ }
138
+ for (let name of filenames) {
139
+ const filepath = resolve(dirPath, name);
140
+ const fileStat = await fs.stat(filepath);
141
+ if (fileStat.isDirectory()) {
142
+ await readDirectory(filepath);
143
+ progress += 1;
144
+ if (progress >= filenames.length) {
145
+ return;
146
+ }
147
+ } else {
148
+ await readFile(filepath);
149
+ progress += 1;
150
+ if (progress >= filenames.length) {
151
+ return;
142
152
  }
143
153
  }
144
- } catch (err) {
145
- console.log(err.message || "Error");
146
154
  }
147
155
  }
148
156
 
149
157
  async function readFile(filepath) {
158
+ let removedExt = filepath.split(".");
159
+ removedExt.pop()
160
+ removedExt = removedExt.join("");
161
+
162
+ if (excludeNames && excludeNames.includes(basename(removedExt))) {
163
+ return;
164
+ }
150
165
  const fileExtname = extname(filepath);
151
166
  if (!Object.keys(languages).includes(fileExtname)) {
152
167
  return;
153
168
  }
154
- try {
155
- const data = await fs.readFile(filepath);
156
- if (isLog) {
157
- console.log(filepath);
158
- }
169
+ const data = await fs.readFile(filepath);
170
+ if (isLog) {
171
+ console.log(filepath);
172
+ }
159
173
 
160
- const content = data.toString();
161
- fileCount += 1;
174
+ const content = data.toString();
175
+ fileCount += 1;
162
176
 
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;
177
+ const reg = languages[fileExtname].comment;
178
+ reg.lastIndex = 0;
179
+ if (reg) {
180
+ content.match(reg)?.forEach((comm) => {
181
+ const res = comm.split("\n");
182
+ comment += res.length;
183
+ source -= res.length;
184
+ });
185
+ }
186
+ const lines = content.split("\n");
187
+ total += lines.length;
188
+ for (let line of lines) {
189
+ line = line.trim();
190
+ if (!line) {
191
+ empty += 1;
192
+ continue;
181
193
  }
182
- } catch (err) {
183
- console.log(err.message);
194
+ source += 1;
184
195
  }
185
196
  }
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "cc-codeline",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
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
+ }