@@ -9,6 +9,7 @@ import { XHRRequest } from 'request-light';
99import { Location } from 'jsonc-parser' ;
1010import { textToMarkedString } from './markedTextUtil' ;
1111
12+ import * as cp from 'child_process' ;
1213import * as nls from 'vscode-nls' ;
1314const localize = nls . loadMessageBundle ( ) ;
1415
@@ -288,34 +289,34 @@ export class PackageJSONContribution implements IJSONContribution {
288289 }
289290
290291 private getInfo ( pack : string ) : Thenable < string [ ] > {
291-
292- const queryUrl = 'https://registry.npmjs.org/' + encodeURIComponent ( pack ) . replace ( / % 4 0 / g, '@' ) ;
293- return this . xhr ( {
294- url : queryUrl ,
295- agent : USER_AGENT
296- } ) . then ( ( success ) => {
297- try {
298- const obj = JSON . parse ( success . responseText ) ;
299- if ( obj ) {
292+ return new Promise ( ( resolve , reject ) => {
293+ const command = 'npm view ' + pack + ' description dist-tags.latest homepage' ;
294+ cp . exec ( command , ( error : object , stdout : string , stderr : string ) => {
295+ if ( error ) {
296+ return resolve ( [ ] ) ;
297+ }
298+ const lines = stdout . split ( '\n' ) ;
299+ if ( lines . length ) {
300+ const info : any = { } ;
301+ lines . forEach ( ( line ) => {
302+ const nameval = line . split ( ' = ' ) ;
303+ if ( nameval . length === 2 ) {
304+ /* tslint:disable:no-unexternalized-strings */
305+ const fq = nameval [ 1 ] . indexOf ( "'" ) ;
306+ const lq = nameval [ 1 ] . lastIndexOf ( "'" ) ;
307+ const val = nameval [ 1 ] . slice ( fq + 1 , lq ) . replace ( "\'" , "'" ) ;
308+ /* tslint:enable:no-unexternalized-strings */
309+ info [ nameval [ 0 ] ] = val ;
310+ }
311+ } ) ;
300312 const result : string [ ] = [ ] ;
301- if ( obj . description ) {
302- result . push ( obj . description ) ;
303- }
304- const latest = obj && obj [ 'dist-tags' ] && obj [ 'dist-tags' ] [ 'latest' ] ;
305- if ( latest ) {
306- result . push ( localize ( 'json.npm.version.hover' , 'Latest version: {0}' , latest ) ) ;
307- }
308- if ( obj . homepage ) {
309- result . push ( obj . homepage ) ;
310- }
311- return result ;
313+ result . push ( info . description || '' ) ;
314+ result . push ( info [ 'dist-tags.latest' ] ? localize ( 'json.npm.version.hover' , 'Latest version: {0}' , info [ 'dist-tags.latest' ] ) : '' ) ;
315+ result . push ( info . homepage || '' ) ;
316+ return resolve ( result ) ;
312317 }
313- } catch ( e ) {
314- // ignore
315- }
316- return [ ] ;
317- } , ( ) => {
318- return [ ] ;
318+ return resolve ( [ ] ) ;
319+ } ) ;
319320 } ) ;
320321 }
321322
0 commit comments