File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import process from "node:process" ;
2+ import { promises as fs } from "node:fs" ;
3+
4+ const argv = process . argv . slice ( 2 ) ;
5+
6+ const dash = argv . filter ( arg => arg . startsWith ( '-' ) ) ;
7+ const filePaths = argv . filter ( arg => ! arg . startsWith ( '-' ) ) ;
8+
9+ const showLineNumbers = dash . includes ( '-n' ) ;
10+ const showNonBlank = dash . includes ( '-b' ) ;
11+
12+ let lineCounter = 1 ;
13+
14+ for ( const filePath of filePaths ) {
15+ let content ;
16+ try {
17+ content = await fs . readFile ( filePath , 'utf8' ) ;
18+ } catch {
19+ console . error ( `cat: ${ filePath } : No file or directory exists` ) ;
20+ process . exit ( 1 ) ;
21+ }
22+
23+ const lines = content . split ( '\n' ) ;
24+
25+ if ( lines [ lines . length - 1 ] === '' ) lines . pop ( ) ;
26+
27+ for ( const line of lines ) {
28+ const isBlank = line . trim ( ) === '' ;
29+
30+ if ( showNonBlank ) {
31+ if ( isBlank ) {
32+ console . log ( '' ) ;
33+ } else {
34+ console . log ( `${ String ( lineCounter ) . padStart ( 6 ) } \t${ line } ` ) ;
35+ lineCounter ++ ;
36+ }
37+ } else if ( showLineNumbers ) {
38+ console . log ( `${ String ( lineCounter ) . padStart ( 6 ) } \t${ line } ` ) ;
39+ lineCounter ++ ;
40+ } else {
41+ console . log ( line ) ;
42+ }
43+ }
44+ }
Original file line number Diff line number Diff line change 1+ import process from "node:process" ;
2+ import { promises as fs } from "node:fs" ;
3+
4+ const argv = process . argv . slice ( 2 ) ;
5+
6+ const dash = argv . filter ( arg => arg . startsWith ( '-' ) ) ;
7+ const paths = argv . filter ( arg => ! arg . startsWith ( '-' ) ) ;
8+
9+ const showAll = dash . includes ( '-a' ) ;
10+
11+ const targetDir = paths [ 0 ] ?? '.' ;
12+
13+ const entries = await fs . readdir ( targetDir ) ;
14+
15+ const result = showAll ? [ '.' , '..' , ...entries ] : entries . filter ( e => ! e . startsWith ( '.' ) ) ;
16+
17+ for ( const entry of result ) {
18+ console . log ( entry ) ;
19+ }
Original file line number Diff line number Diff line change 1+ import process from "node:process" ;
2+ import { promises as fs } from "node:fs" ;
3+
4+ const argv = process . argv . slice ( 2 ) ;
5+
6+ const dash = argv . filter ( arg => arg . startsWith ( '-' ) ) ;
7+ const filePaths = argv . filter ( arg => ! arg . startsWith ( '-' ) ) ;
8+
9+ const forLines = dash . includes ( '-l' ) ;
10+ const forWords = dash . includes ( '-w' ) ;
11+ const forBytes = dash . includes ( '-c' ) ;
12+
13+ let totalLines = 0 ;
14+ let totalWords = 0 ;
15+ let totalBytes = 0 ;
16+
17+ for ( const filePath of filePaths ) {
18+ let content ;
19+ try {
20+ content = await fs . readFile ( filePath , 'utf8' ) ;
21+ } catch {
22+ console . error ( `wc: ${ filePath } : No file or directory exists` ) ;
23+ process . exit ( 1 ) ;
24+ }
25+
26+ const lines = content . split ( '\n' ) . length - 1 ;
27+ const words = content . trim ( ) === '' ? 0 : content . trim ( ) . split ( / \s + / ) . length ;
28+ const bytes = Buffer . byteLength ( content , 'utf8' ) ;
29+
30+ totalLines += lines ;
31+ totalWords += words ;
32+ totalBytes += bytes ;
33+
34+ if ( forLines ) {
35+ console . log ( `${ String ( lines ) . padStart ( 8 ) } ${ filePath } ` ) ;
36+ } else if ( forWords ) {
37+ console . log ( `${ String ( words ) . padStart ( 8 ) } ${ filePath } ` ) ;
38+ } else if ( forBytes ) {
39+ console . log ( `${ String ( bytes ) . padStart ( 8 ) } ${ filePath } ` ) ;
40+ } else {
41+ console . log ( `${ String ( lines ) . padStart ( 8 ) } ${ String ( words ) . padStart ( 8 ) } ${ String ( bytes ) . padStart ( 8 ) } ${ filePath } ` ) ;
42+ }
43+ }
44+
45+ if ( filePaths . length > 1 ) {
46+ if ( forLines ) {
47+ console . log ( `${ String ( totalLines ) . padStart ( 8 ) } total` ) ;
48+ } else if ( forWords ) {
49+ console . log ( `${ String ( totalWords ) . padStart ( 8 ) } total` ) ;
50+ } else if ( forBytes ) {
51+ console . log ( `${ String ( totalBytes ) . padStart ( 8 ) } total` ) ;
52+ } else {
53+ console . log ( `${ String ( totalLines ) . padStart ( 8 ) } ${ String ( totalWords ) . padStart ( 8 ) } ${ String ( totalBytes ) . padStart ( 8 ) } total` ) ;
54+ }
55+ }
You can’t perform that action at this time.
0 commit comments