Skip to content

Commit 30254a8

Browse files
author
atompkins
committed
Use npm view to populate info box
1 parent a6d1949 commit 30254a8

1 file changed

Lines changed: 27 additions & 26 deletions

File tree

extensions/npm/src/features/packageJSONContribution.ts

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { XHRRequest } from 'request-light';
1010
import { Location } from 'jsonc-parser';
1111
import { textToMarkedString } from './markedTextUtil';
1212

13+
import * as cp from 'child_process';
1314
import * as nls from 'vscode-nls';
1415
const 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

Comments
 (0)