Skip to content

Commit dfc9d45

Browse files
committed
implementing flag -n
1 parent cb08903 commit dfc9d45

1 file changed

Lines changed: 34 additions & 6 deletions

File tree

  • implement-shell-tools/cat

implement-shell-tools/cat/cat.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,42 @@
11
import { promises as fs } from "node:fs";
22
import { glob } from "glob";
33
async function readFile(){
4-
const fileNamePattern=process.argv[2];
5-
const filesNameArray=await glob(fileNamePattern);
6-
filesNameArray.sort();
4+
let fileNamePattern;
5+
let flag;
6+
if (process.argv[2].startsWith('-')){
7+
flag=process.argv[2];
8+
fileNamePattern = process.argv[3];
9+
} else {
10+
fileNamePattern = process.argv[2];
11+
}
12+
13+
const filesNameArray = await glob(fileNamePattern);
14+
filesNameArray.sort();
15+
let lineNumber = 1;
16+
//console.log(filesNameArray);
17+
18+
719
for(const file of filesNameArray){
820
const fileContent=await fs.readFile(file,"utf-8");
9-
console.log(fileContent);
10-
}
21+
if(flag == null){
22+
console.log(fileContent);
23+
}else{
24+
25+
const linesOfText=fileContent.split(/\r?\n/);
26+
if(linesOfText[linesOfText.length-1].trim()===""){
27+
linesOfText.pop();
28+
}
29+
//console.log(linesOfText);
30+
for(const line of linesOfText){
31+
console.log(`${lineNumber} ${line}`);
32+
lineNumber ++ ;
33+
}
34+
}
1135

36+
37+
38+
}
39+
}
1240

13-
}
41+
1442
readFile();

0 commit comments

Comments
 (0)