11import { promises as fs } from "node:fs" ;
22import { glob } from "glob" ;
3- async function readFile ( ) {
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-
19- for ( const file of filesNameArray ) {
20- const fileContent = await fs . readFile ( file , "utf-8" ) ;
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- if ( line . trim ( ) === "" && flag === '-b' ) {
32- console . log ( line ) ;
33- } else {
34- console . log ( `${ lineNumber } ${ line } ` ) ;
35- lineNumber ++ ;
36- }
37- }
38- }
3+ async function readFile ( ) {
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+ }
3912
40-
41-
42- }
13+ const filesNameArray = await glob ( fileNamePattern ) ;
14+ filesNameArray . sort ( ) ;
15+ let lineNumber = 1 ;
16+
17+ for ( const file of filesNameArray ) {
18+ const fileContent = await fs . readFile ( file , "utf-8" ) ;
19+ if ( flag == null ) {
20+ console . log ( fileContent ) ;
21+ } else {
22+ const linesOfText = fileContent . split ( / \r ? \n / ) ;
23+ if ( linesOfText [ linesOfText . length - 1 ] . trim ( ) === "" ) {
24+ linesOfText . pop ( ) ;
25+ }
26+
27+ for ( const line of linesOfText ) {
28+ if ( line . trim ( ) === "" && flag === "-b" ) {
29+ console . log ( line ) ;
30+ } else {
31+ console . log ( `${ lineNumber } ${ line } ` ) ;
32+ lineNumber ++ ;
33+ }
34+ }
4335 }
44-
45-
46- readFile ( ) ;
36+ }
37+ }
38+
39+ readFile ( ) ;
0 commit comments