-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwincat.js
127 lines (109 loc) · 3.32 KB
/
wincat.js
1
2
3
4
5
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env node
const fs = require('fs');
(function(){
let cmd = process.argv.slice(2) //access command arguments
let options = [] // will contains the options
let files = [] // will contains the files
let str = '' // will represent the final output
// filter out options and files
for(let i=0;i<cmd.length;i++){
if(cmd[i].startsWith('-')){
options.push(cmd[i])
}
else if(cmd[i].startsWith("*.")==false){
files.push(cmd[i])
}
}
// push all files for *.
let currentFiles = fs.readdirSync(process.cwd()) //reading cwd files
for(let i=0;i<cmd.length;i++){
if(cmd[i].startsWith("*.")){
let ext = cmd[i].split(".").pop()
for(let j=0;j<currentFiles.length;j++){
let targetExt = currentFiles[j].split(".").pop()
if(ext==targetExt && files.includes(currentFiles[j])==false){
files.push(currentFiles[j])
}
}
}
}
// read from files
for(let j=0;j<files.length;j++){
if(fs.existsSync(files[j])){ // file exist?
str += fs.readFileSync(files[j]).toString() // read file
}
else{
console.log('invalid file path')
return //cancel whole operation
}
}
str = str.split('\n') //convert string to array based on new line (vscode makes '' to \r)
// -s
if(options.includes('-s')){
str = trimLargeSpaces(str)
}
// -n and -b
if(options.includes("-n") && options.includes("-b")){
if(options.indexOf("-b")<options.indexOf("-n")){ // if -b present before -n. then we ignore -n
str=addNonEmptyNum(str); // -b
}
else{
str= addAllNum(str); // -n
}
}else{
if(options.includes("-n")){
str= addAllNum(str); // -n
}
if(options.includes("-b")){
str=addNonEmptyNum(str); // -b
}
}
str = str.join("\n")
console.log(str) // final string result.
})();
// -s - to remove big line break.
function trimLargeSpaces(arr){
let temp = []
let flag = false
for(let i=0;i<arr.length;i++){
if(arr[i]=='' || arr[i]=='\r'){
if(flag)
continue
else{
flag = true
temp.push(arr[i])
}
}
else{
temp.push(arr[i])
flag = false
}
}
return temp
}
// -n implementation - To add line numbers to all lines.
function addAllNum(arr){
let lineNumber=1;
for(let i=0;i<arr.length;i++){
arr[i]=lineNumber+" "+arr[i];
lineNumber++;
}
return arr;
}
// -b implementation - To add line numbers to non-empty lines.
function addNonEmptyNum(arr){
let lineNumber=1;
for(let i=0;i<arr.length;i++){
if(arr[i]!=="" && arr[i]!=="\r"){
arr[i]=lineNumber+" "+arr[i];
lineNumber++;
}
}
return arr;
}
/*
how to make a nodejs script global?
---> mention nodejs environment at the top of your script (add shebang - #!/usr/bin/env node)
---> package.json - "bin": {"winux": "winux.js"}
---> run npm link
*/