File tree 8 files changed +191
-0
lines changed
8 files changed +191
-0
lines changed Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require ( 'fs' ) ;
4
+ const path = process . argv [ 2 ] ;
5
+
6
+ fs . readFile ( process . cwd ( ) + '/' + path , function ( err , data ) {
7
+ if ( err ) {
8
+ process . stdout . write ( 'file dosnt exist' ) ;
9
+ } else {
10
+ process . stdout . write ( data ) ;
11
+ }
12
+ } ) ;
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require ( 'fs' ) ;
4
+ const pattern = process . argv [ 2 ] ; // the word would you like to search
5
+ const fileName = process . argv [ 3 ] ; //the file name or path
6
+
7
+ fs . readFile ( process . cwd ( ) + '/' + fileName , function ( err , data ) {
8
+ if ( err ) {
9
+ process . stdout . write ( 'file dosnt exist' ) ;
10
+ } else {
11
+ var arr = data . toString ( ) . split ( '\n' ) ;
12
+ //console.log(arr);
13
+ arr . forEach ( function ( line ) {
14
+ if ( line . includes ( pattern ) )
15
+ process . stdout . write ( line + '\n' ) ;
16
+ } ) ;
17
+
18
+ }
19
+ } ) ;
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require ( 'fs' ) ;
4
+ const path = process . cwd ( ) ;
5
+
6
+ if ( process . argv [ 2 ] === '-ex' ) {
7
+ writeFiles ( path , function ( data ) {
8
+ data . forEach ( function ( file ) {
9
+ var fileExten = file . split ( '.' ) ;
10
+ if ( fileExten [ 1 ] === process . argv [ 3 ] )
11
+ process . stdout . write ( file + '\n' ) ;
12
+ } ) ;
13
+ } ) ;
14
+ } else {
15
+ writeFiles ( path , function ( data ) {
16
+ data . forEach ( function ( file ) {
17
+ process . stdout . write ( file + '\n' ) ;
18
+ } ) ;
19
+ } ) ;
20
+ }
21
+
22
+ function writeFiles ( pth , cb ) {
23
+ fs . readdir ( pth , function ( err , data ) {
24
+ if ( err ) {
25
+ process . stdout . write ( 'file dosn\'t exist' ) ;
26
+ } else {
27
+ cb ( data ) ;
28
+ }
29
+ } ) ;
30
+ }
Original file line number Diff line number Diff line change
1
+ const fs = require ( 'fs' ) ;
2
+ const path = process . argv [ 2 ] ;
3
+
4
+ if ( process . argv [ 3 ] === '>>' ) {
5
+ var readStream = fs . createReadStream ( path ) ;
6
+ var writeStream = fs . createWriteStream ( process . argv [ 4 ] , { 'flags' : 'a' } ) ;
7
+
8
+ readStream . pipe ( writeStream ) ;
9
+ } else {
10
+
11
+ fs . readFile ( process . cwd ( ) + '/' + path , function ( err , file ) {
12
+ if ( err ) {
13
+ process . stdout . write ( 'file dosnt exist' ) ;
14
+ } else {
15
+ process . stdout . write ( file ) ;
16
+ }
17
+ } ) ;
18
+ }
19
+
20
+
21
+ fs . readFile = function ( file , cb ) {
22
+
23
+ var readStream = fs . createReadStream ( file ) ;
24
+ var fileContent = '' ;
25
+
26
+ readStream . on ( 'data' , function ( chunk ) {
27
+ fileContent += chunk ;
28
+ } ) ;
29
+
30
+ readStream . on ( 'error' , function ( err ) {
31
+ cb ( err , fileContent )
32
+ } ) ;
33
+
34
+ readStream . on ( 'end' , function ( ) {
35
+ cb ( null , fileContent ) ;
36
+ } ) ;
37
+ }
Original file line number Diff line number Diff line change
1
+ const fs = require ( 'fs' ) ;
2
+ const path = process . argv [ 2 ] ;
3
+
4
+ fs . readFile = function ( file , cb ) {
5
+
6
+ var readStream = fs . createReadStream ( file ) ;
7
+ var fileContent = '' ;
8
+
9
+ readStream . on ( 'data' , function ( chunk ) {
10
+ fileContent += chunk ;
11
+ } ) ;
12
+
13
+ readStream . on ( 'error' , function ( err ) {
14
+ cb ( err , fileContent )
15
+ } ) ;
16
+
17
+ readStream . on ( 'end' , function ( ) {
18
+ cb ( null , fileContent ) ;
19
+ } ) ;
20
+ }
21
+
22
+
23
+ fs . readFile ( process . cwd ( ) + '/' + path , function ( err , file ) {
24
+ if ( err ) {
25
+ process . stdout . write ( 'file dosnt exist' ) ;
26
+ } else {
27
+ process . stdout . write ( file ) ;
28
+ }
29
+ } ) ;
Original file line number Diff line number Diff line change
1
+ const fs = require ( 'fs' ) ;
2
+ const path = process . argv [ 2 ] ;
3
+
4
+
5
+ if ( ! path ) {
6
+ var stdin = process . openStdin ( ) ;
7
+ var data = '' ;
8
+ stdin . on ( 'data' , function ( chunk ) {
9
+ data += chunk ;
10
+ } ) ;
11
+ stdin . on ( 'end' , ( ) => {
12
+ var arr = data . toString ( ) . split ( '\n' ) ;
13
+ process . stdout . write ( arr . length + '\n' ) ;
14
+ } ) ;
15
+ } else {
16
+ var readStream = fs . createReadStream ( path ) ;
17
+ var fileContent = '' ;
18
+
19
+ readStream . on ( 'data' , function ( chunk ) {
20
+ fileContent += chunk ;
21
+ } ) ;
22
+
23
+ readStream . on ( 'end' , function ( ) {
24
+ var arr = fileContent . toString ( ) . split ( '\n' ) ;
25
+ process . stdout . write ( arr . length + '\n' ) ;
26
+ } ) ;
27
+ }
Original file line number Diff line number Diff line change
1
+ const fs = require ( 'fs' ) ;
2
+ const path = process . argv [ 2 ] ;
3
+
4
+ if ( process . argv [ 3 ] === '>' ) {
5
+ var readStream = fs . createReadStream ( path ) ;
6
+ var writeStream = fs . createWriteStream ( process . argv [ 4 ] ) ;
7
+
8
+ readStream . pipe ( writeStream ) ;
9
+ } else {
10
+
11
+ fs . readFile ( process . cwd ( ) + '/' + path , function ( err , file ) {
12
+ if ( err ) {
13
+ process . stdout . write ( 'file dosnt exist' ) ;
14
+ } else {
15
+ process . stdout . write ( file ) ;
16
+ }
17
+ } ) ;
18
+ }
19
+
20
+
21
+ fs . readFile = function ( file , cb ) {
22
+
23
+ var readStream = fs . createReadStream ( file ) ;
24
+ var fileContent = '' ;
25
+
26
+ readStream . on ( 'data' , function ( chunk ) {
27
+ fileContent += chunk ;
28
+ } ) ;
29
+
30
+ readStream . on ( 'error' , function ( err ) {
31
+ cb ( err , fileContent )
32
+ } ) ;
33
+
34
+ readStream . on ( 'end' , function ( ) {
35
+ cb ( null , fileContent ) ;
36
+ } ) ;
37
+ }
You can’t perform that action at this time.
0 commit comments