cc-codeline 1.0.3 → 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.
Files changed (3) hide show
  1. package/README.md +4 -0
  2. package/bin.js +165 -148
  3. package/package.json +5 -2
package/README.md CHANGED
@@ -3,4 +3,8 @@
3
3
  npm i -g cc-codeline
4
4
 
5
5
  codeline /path/
6
+
7
+ -h Show help
8
+ -l Print log
9
+ -e Exclude directory
6
10
  ```
package/bin.js CHANGED
@@ -1,168 +1,185 @@
1
1
  #!/usr/bin/env node
2
- const fs = require('fs');
3
- const { extname, resolve } = require('path');
2
+ const fs = require("fs").promises;
3
+ const { extname, resolve } = require("path");
4
4
 
5
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
- '.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
- }
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
+
50
+ const param = process.argv.slice(2);
51
+ if (param.includes("-h")) {
52
+ console.log(`
53
+ codeline /path/ -l -e node_modules dist
54
+ ------------------
55
+ -h Show help
56
+ -l Print log
57
+ -e Exclude directory
58
+ `);
59
+ process.exit(0);
60
+ }
61
+ const base = process.cwd();
62
+ const isLog = (() => {
63
+ const index = param.indexOf("-l");
64
+ if (index >= 0) {
65
+ param.splice(index, 1);
66
+ return true;
67
+ }
68
+ return false;
69
+ })();
70
+
71
+ const excludePath = (() => {
72
+ const index = param.findIndex((it) => it == "-e");
73
+ if (index >= 0) {
74
+ return param.splice(index).map((it) => resolve(base, it));
75
+ }
76
+ })();
77
+
78
+ const path = resolve(base, param[0] || "");
79
+
49
80
  let total = 0;
50
81
  let empty = 0;
51
82
  let source = 0;
52
83
  let comment = 0;
53
84
  let fileCount = 0;
54
85
 
55
- const param = process.argv.slice(2);
56
- const isLog = param.includes('-l');
57
-
58
- const base = process.cwd();
59
- const path = resolve(base, param[0] || '');
86
+ setInterval(() => {
87
+ console.log("...");
88
+ }, 1000);
60
89
 
61
- (() => {
62
- if (param.includes('-h')) {
63
- console.log(`
64
- codeline /path/ -l
65
- ------------------
66
- -h Show help
67
- -l Print log
68
- `);
69
- return;
70
- }
71
- try {
72
- const stat = fs.statSync(path);
73
- if (stat.isDirectory()) {
74
- readDirectory(path, () => {
75
- print()
76
- });
77
- } else {
78
- readFile(path, () => {
79
- print()
80
- });
81
- }
82
- } catch (_) {
83
- console.log('Error, the path may not exist');
84
- }
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
+ }
85
105
  })();
86
106
 
87
107
  function print() {
88
- console.table({
89
- 'Total': total,
90
- 'Empty': empty,
91
- 'Source': source,
92
- 'Comment': comment,
93
- 'File Count': fileCount
94
- });
108
+ console.table({
109
+ Total: total,
110
+ Empty: empty,
111
+ Source: source,
112
+ Comment: comment,
113
+ "File Count": fileCount,
114
+ });
95
115
  }
96
116
 
97
- function readDirectory(dirPath, callback) {
98
- let progress = 0;
99
- fs.readdir(dirPath, (err, filenames) => {
100
- if (err) {
101
- console.log('Error reading directory');
102
- callback();
103
- return;
104
- }
105
- if (!filenames.length) {
106
- callback();
107
- return;
108
- }
109
- for (let name of filenames) {
110
- const filepath = resolve(dirPath, name);
111
- const fileStat = fs.statSync(filepath);
112
- if (fileStat.isDirectory()) {
113
- readDirectory(filepath, () => {
114
- progress += 1;
115
- if (progress >= filenames.length) {
116
- callback();
117
- }
118
- });
119
- } else {
120
- readFile(filepath, () => {
121
- progress += 1;
122
- if (progress >= filenames.length) {
123
- callback();
124
- }
125
- });
126
- }
127
- }
128
- });
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
+ }
129
147
  }
130
148
 
131
- function readFile(filepath, callback) {
132
- const fileExtname = extname(filepath);
133
- if (!Object.keys(languages).includes(fileExtname)) {
134
- callback();
135
- return;
136
- }
137
- fs.readFile(filepath, (err, data) => {
138
- if (err) {
139
- console.log('Error reading file');
140
- callback();
141
- return;
142
- }
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
+ }
143
159
 
144
- isLog && console.log(filepath);
160
+ const content = data.toString();
161
+ fileCount += 1;
145
162
 
146
- const content = data.toString();
147
- fileCount += 1;
148
-
149
- const reg = languages[fileExtname].comment;
150
- reg.lastIndex = 0;
151
- reg && content.match(reg)?.forEach(comm => {
152
- const res = comm.split('\n');
153
- comment += res.length;
154
- source -= res.length;
155
- });
156
- const lines = content.split('\n');
157
- total += lines.length;
158
- for (let line of lines) {
159
- line = line.trim();
160
- if (!line) {
161
- empty += 1;
162
- continue;
163
- }
164
- source += 1;
165
- }
166
- callback();
167
- });
168
- }
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
@@ -1,12 +1,15 @@
1
1
  {
2
2
  "name": "cc-codeline",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "main": "bin.js",
5
5
  "description": "Quickly count your lines of code",
6
6
  "bin": {
7
7
  "codeline": "./bin.js"
8
8
  },
9
- "keywords": ["codeline"],
9
+ "keywords": [
10
+ "codeline",
11
+ "cli"
12
+ ],
10
13
  "author": "",
11
14
  "license": "MIT",
12
15
  "type": "commonjs"