File tree Expand file tree Collapse file tree
implement-shell-tools/cat Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import { promises as fs } from "node:fs" ;
22import { 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 ( ) ;
You can’t perform that action at this time.
0 commit comments