@@ -10,6 +10,7 @@ import { XHRRequest } from 'request-light';
1010import { Location } from 'jsonc-parser' ;
1111import { textToMarkedString } from './markedTextUtil' ;
1212
13+ import * as cp from 'child_process' ;
1314import * as nls from 'vscode-nls' ;
1415const localize = nls . loadMessageBundle ( ) ;
1516
@@ -281,34 +282,34 @@ export class PackageJSONContribution implements IJSONContribution {
281282 }
282283
283284 private getInfo ( pack : string ) : Thenable < string [ ] > {
284-
285- const queryUrl = 'https://registry.npmjs.org/' + encodeURIComponent ( pack ) . replace ( '%40' , '@' ) ;
286- return this . xhr ( {
287- url : queryUrl ,
288- agent : USER_AGENT
289- } ) . then ( ( success ) => {
290- try {
291- const obj = JSON . parse ( success . responseText ) ;
292- if ( obj ) {
285+ return new Promise ( ( resolve , reject ) => {
286+ const command = 'npm view ' + pack + ' description dist-tags.latest homepage' ;
287+ cp . exec ( command , ( error : object , stdout : string , stderr : string ) => {
288+ if ( error ) {
289+ return resolve ( [ ] ) ;
290+ }
291+ const lines = stdout . split ( '\n' ) ;
292+ if ( lines . length ) {
293+ const info : any = { } ;
294+ lines . forEach ( ( line ) => {
295+ const nameval = line . split ( ' = ' ) ;
296+ if ( nameval . length === 2 ) {
297+ /* tslint:disable:no-unexternalized-strings */
298+ const fq = nameval [ 1 ] . indexOf ( "'" ) ;
299+ const lq = nameval [ 1 ] . lastIndexOf ( "'" ) ;
300+ const val = nameval [ 1 ] . slice ( fq + 1 , lq ) . replace ( "\'" , "'" ) ;
301+ /* tslint:enable:no-unexternalized-strings */
302+ info [ nameval [ 0 ] ] = val ;
303+ }
304+ } ) ;
293305 const result : string [ ] = [ ] ;
294- if ( obj . description ) {
295- result . push ( obj . description ) ;
296- }
297- const latest = obj && obj [ 'dist-tags' ] && obj [ 'dist-tags' ] [ 'latest' ] ;
298- if ( latest ) {
299- result . push ( localize ( 'json.npm.version.hover' , 'Latest version: {0}' , latest ) ) ;
300- }
301- if ( obj . homepage ) {
302- result . push ( obj . homepage ) ;
303- }
304- return result ;
306+ result . push ( info . description || '' ) ;
307+ result . push ( info [ 'dist-tags.latest' ] ? localize ( 'json.npm.version.hover' , 'Latest version: {0}' , info [ 'dist-tags.latest' ] ) : '' ) ;
308+ result . push ( info . homepage || '' ) ;
309+ return resolve ( result ) ;
305310 }
306- } catch ( e ) {
307- // ignore
308- }
309- return [ ] ;
310- } , ( ) => {
311- return [ ] ;
311+ return resolve ( [ ] ) ;
312+ } ) ;
312313 } ) ;
313314 }
314315
0 commit comments