File tree Expand file tree Collapse file tree 3 files changed +179
-0
lines changed
implement-shell-tools/cat Expand file tree Collapse file tree 3 files changed +179
-0
lines changed Original file line number Diff line number Diff line change 1+ import { program } from "commander" ;
2+ import { promises as fs } from "node:fs" ;
3+
4+ program
5+ . name ( "cat command" )
6+ . description ( "create cat command" )
7+ . option ( "-n" , "Number all lines" )
8+ . option ( "-b" , "Number non-empty lines" )
9+ . argument ( "<paths...>" , "File paths" ) ;
10+
11+ program . parse ( ) ;
12+ const paths = program . args ;
13+ const number = program . opts ( ) . n ;
14+ const nonEmptyLine = program . opts ( ) . b ;
15+
16+ let lineNumber = 1 ;
17+
18+ for ( const path of paths ) {
19+ try {
20+ const read = await fs . readFile ( path , "utf-8" ) ;
21+ const lines = read . split ( "\n" ) ;
22+ for ( let i of lines ) {
23+ if ( number ) {
24+ console . log ( `${ String ( lineNumber ) . padStart ( 6 , " " ) } ${ i } ` ) ;
25+ lineNumber ++ ;
26+ } else if ( nonEmptyLine ) {
27+ if ( i . trim ( ) !== "" ) {
28+ console . log ( `${ String ( lineNumber ) . padStart ( 6 , " " ) } ${ i } ` ) ;
29+ lineNumber ++ ;
30+ } else {
31+ console . log ( i ) ;
32+ }
33+ } else {
34+ console . log ( i ) ;
35+ }
36+ }
37+ } catch ( err ) {
38+ console . error ( `File could not read: ${ path } ` ) ;
39+ }
40+ }
Original file line number Diff line number Diff line change 1+ {
2+ "name" : " cat" ,
3+ "version" : " 1.0.0" ,
4+ "description" : " You should already be familiar with the `cat` command line tool." ,
5+ "main" : " cat.js" ,
6+ "type" : " module" ,
7+ "dependencies" : {
8+ "commander" : " ^14.0.2" ,
9+ "glob" : " ^13.0.0" ,
10+ "lru-cache" : " ^11.2.2" ,
11+ "minimatch" : " ^10.1.1" ,
12+ "minipass" : " ^7.1.2" ,
13+ "path-scurry" : " ^2.0.1"
14+ },
15+ "scripts" : {
16+ "test" : " echo \" Error: no test specified\" && exit 1"
17+ },
18+ "keywords" : [],
19+ "author" : " " ,
20+ "license" : " ISC"
21+ }
You can’t perform that action at this time.
0 commit comments