1
1
#!/usr/bin/env node
2
2
3
3
var program = require ( 'commander' )
4
- . usage ( '[package name]' )
5
- . parse ( process . argv )
4
+ . usage ( '[package name]' )
5
+ . parse ( process . argv ) ;
6
6
7
- var name = program . args [ 0 ]
7
+ var name = program . args [ 0 ] ;
8
8
if ( ! name ) {
9
- console . error ( 'a package name was not defined!' )
10
- process . exit ( 1 )
9
+ console . error ( 'a package name was not defined!' ) ;
10
+ process . exit ( 1 ) ;
11
11
}
12
12
13
- require ( 'request' ) ( 'https://registry.npmjs.org/' + name , function ( err , res , body ) {
14
- if ( err ) {
15
- console . error ( err . stack )
16
- process . exit ( 1 )
17
- }
18
- if ( res . statusCode !== 200 ) {
19
- console . error ( 'a package by the name of "%s" was not found' , name )
20
- process . exit ( 1 )
21
- }
22
- body = JSON . parse ( body ) // you're useless, request!
23
-
24
- var repo = body . repository
25
- if ( ! repo ) {
26
- console . error ( 'package "%s" did not define a repository' , name )
27
- process . exit ( 1 )
28
- }
29
-
30
- var url = require ( 'url' )
31
- var obj = url . parse ( repo . url )
32
- if ( obj . protocol !== 'https:' ) obj . protocol = 'https:'
33
- require ( 'open' ) ( url . format ( obj ) )
34
- } )
13
+ var npm = require ( 'npm' ) ,
14
+ open = require ( 'open' ) ;
15
+ npm . load ( { } , function ( er ) {
16
+ if ( er ) {
17
+ console . error ( err ) ;
18
+ process . exit ( 1 ) ;
19
+ }
20
+ npm . commands . view ( [ name , "repository.url" ] , function ( err , rurl ) {
21
+ if ( err ) {
22
+ console . error ( err ) ;
23
+ process . exit ( 1 ) ;
24
+ }
25
+ var url = require ( 'url' )
26
+ var obj = url . parse ( rurl [ Object . keys ( rurl ) [ 0 ] ] [ 'repository.url' ] ) ;
27
+ if ( obj . protocol !== 'https:' ) obj . protocol = 'https:'
28
+ require ( 'open' ) ( url . format ( obj ) )
29
+ } ) ;
30
+ } ) ;
0 commit comments