Skip to content

Commit 12e287d

Browse files
author
Jonas Kemper
committed
implement npm view for version completions and hover suggestions
1 parent 6740eba commit 12e287d

1 file changed

Lines changed: 22 additions & 16 deletions

File tree

extensions/npm/src/features/packageJSONContribution.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ export class PackageJSONContribution implements IJSONContribution {
173173
url: queryUrl,
174174
agent: USER_AGENT
175175
}).then((success) => {
176+
console.log(success.responseText);
176177
if (success.status === 200) {
177178
try {
178179
const obj = JSON.parse(success.responseText);
@@ -230,14 +231,9 @@ export class PackageJSONContribution implements IJSONContribution {
230231
if ((location.matches(['dependencies', '*']) || location.matches(['devDependencies', '*']) || location.matches(['optionalDependencies', '*']) || location.matches(['peerDependencies', '*']))) {
231232
const currentKey = location.path[location.path.length - 1];
232233
if (typeof currentKey === 'string') {
233-
const queryUrl = 'https://registry.npmjs.org/' + encodeURIComponent(currentKey).replace(/%40/g, '@');
234-
return this.xhr({
235-
url: queryUrl,
236-
agent: USER_AGENT
237-
}).then((success) => {
234+
return this.npmView(currentKey).then(info => {
238235
try {
239-
const obj = JSON.parse(success.responseText);
240-
const latest = obj && obj['dist-tags'] && obj['dist-tags']['latest'];
236+
const latest = info && info['dist-tags.latest'];
241237
if (latest) {
242238
let name = JSON.stringify(latest);
243239
let proposal = new CompletionItem(name);
@@ -289,11 +285,26 @@ export class PackageJSONContribution implements IJSONContribution {
289285
}
290286

291287
private getInfo(pack: string): Thenable<string[]> {
292-
return new Promise((resolve, reject) => {
288+
return new Promise((resolve) => {
289+
return this.npmView(pack).then(info => {
290+
console.log(info);
291+
const result: string[] = [];
292+
result.push(info.description || '');
293+
result.push(info['dist-tags.latest'] ? localize('json.npm.version.hover', 'Latest version: {0}', info['dist-tags.latest']) : '');
294+
result.push(info.homepage || '');
295+
return resolve(result);
296+
}).catch(() => {
297+
return resolve([]);
298+
});
299+
});
300+
}
301+
302+
private npmView(pack: string): Promise<any> {
303+
return new Promise((resolve) => {
293304
const command = 'npm view ' + pack + ' description dist-tags.latest homepage';
294-
cp.exec(command, (error: object, stdout: string, stderr: string) => {
305+
cp.exec(command, (error, stdout) => {
295306
if (error) {
296-
return resolve([]);
307+
return resolve();
297308
}
298309
const lines = stdout.split('\n');
299310
if (lines.length) {
@@ -309,13 +320,8 @@ export class PackageJSONContribution implements IJSONContribution {
309320
info[nameval[0]] = val;
310321
}
311322
});
312-
const result: string[] = [];
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);
323+
return resolve(info);
317324
}
318-
return resolve([]);
319325
});
320326
});
321327
}

0 commit comments

Comments
 (0)