44
55const fs = require ( `fs` ) ;
66const getPkgRepo = require ( `../` ) ;
7- const meow = require ( `meow` ) ;
87const through = require ( `through2` ) ;
98const util = require ( `util` ) ;
109
11- const cli = meow ( {
12- help :
13- `Practice writing repository URL or validate the repository in a package.json file.
14- If used without specifying a package.json file path, you will enter an interactive shell.
15- Otherwise, the repository info in package.json is printed.
10+ const yargs = require ( 'yargs/yargs' ) ( process . argv . slice ( 2 ) )
11+ . usage (
12+ '\nPractice writing repository URL or validate the repository in a package.json file. If used without specifying a package.json file path, you will enter an interactive shell. Otherwise, the repository info in package.json is printed.'
13+ )
14+ . scriptName ( 'get-pkg-repo' )
15+ . command ( '$0' )
16+ . command ( '<path> [<path> ...]' )
17+ . example ( 'get-pkg-repo' )
18+ . example ( 'get-pkg-repo package.json' )
19+ . example ( 'cat package.json | get-pkg-repo' )
20+ . help ( ) . argv ;
1621
17- Usage
18- get-pkg-repo
19- get-pkg-repo <path> [<path> ...]
20- cat <path> | get-pkg-repo
21-
22- Examples
23- get-pkg-repo
24- get-pkg-repo package.json
25- cat package.json | get-pkg-repo` ,
26- } ) ;
27-
28- const { input} = cli ;
22+ const input = yargs . _ ;
2923
3024if ( process . stdin . isTTY ) {
3125 if ( input . length > 0 ) {
3226 input . forEach ( path => {
3327 let repo ;
34- fs . readFile ( path , ` utf8` , ( err , data ) => {
28+ fs . readFile ( path , ' utf8' , ( err , data ) => {
3529 if ( err ) {
3630 console . error ( err ) ;
3731 return ;
@@ -47,33 +41,37 @@ if (process.stdin.isTTY) {
4741 } ) ;
4842 } else {
4943 process . stdin
50- . pipe ( through . obj ( ( chunk , enc , cb ) => {
51- let repo ;
52- const pkgData = {
53- repository : chunk . toString ( ) ,
54- } ;
44+ . pipe (
45+ through . obj ( ( chunk , enc , cb ) => {
46+ let repo ;
47+ const pkgData = {
48+ repository : chunk . toString ( ) ,
49+ } ;
5550
56- try {
57- repo = getPkgRepo ( pkgData ) ;
58- cb ( null , util . format ( repo ) + `\n` ) ;
59- } catch ( e ) {
60- console . error ( e . toString ( ) ) ;
61- cb ( ) ;
62- }
63- } ) )
51+ try {
52+ repo = getPkgRepo ( pkgData ) ;
53+ cb ( null , util . format ( repo ) + '\n' ) ;
54+ } catch ( e ) {
55+ console . error ( e . toString ( ) ) ;
56+ cb ( ) ;
57+ }
58+ } )
59+ )
6460 . pipe ( process . stdout ) ;
6561 }
6662} else {
6763 process . stdin
68- . pipe ( through . obj ( ( chunk , enc , cb ) => {
69- let repo ;
70- try {
71- repo = getPkgRepo ( JSON . parse ( chunk . toString ( ) ) ) ;
72- } catch ( e ) {
73- console . error ( e . toString ( ) ) ;
74- process . exit ( 1 ) ;
75- }
76- cb ( null , `${ util . format ( repo ) } \n` ) ;
77- } ) )
64+ . pipe (
65+ through . obj ( ( chunk , enc , cb ) => {
66+ let repo ;
67+ try {
68+ repo = getPkgRepo ( JSON . parse ( chunk . toString ( ) ) ) ;
69+ } catch ( e ) {
70+ console . error ( e . toString ( ) ) ;
71+ process . exit ( 1 ) ;
72+ }
73+ cb ( null , util . format ( repo ) + '\n' ) ;
74+ } )
75+ )
7876 . pipe ( process . stdout ) ;
7977}
0 commit comments