Skip to content

Commit

Permalink
[json] "Fetch Online Package Info" feature doesn't work when npm is n…
Browse files Browse the repository at this point in the history
…ot present. Fixes microsoft#77066
  • Loading branch information
aeschli committed Sep 30, 2019
1 parent 2de9a36 commit bae6f5f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 53 deletions.
4 changes: 2 additions & 2 deletions extensions/npm/src/features/jsonContributions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface IJSONContribution {
getDocumentSelector(): DocumentSelector;
getInfoContribution(fileName: string, location: Location): Thenable<MarkedString[] | null> | null;
collectPropertySuggestions(fileName: string, location: Location, currentWord: string, addValue: boolean, isLast: boolean, result: ISuggestionsCollector): Thenable<any> | null;
collectValueSuggestions(fileName: string, location: Location, result: ISuggestionsCollector): Thenable<any> | null;
collectValueSuggestions(fileName: string, location: Location, result: ISuggestionsCollector): Thenable<any>;
collectDefaultSuggestions(fileName: string, result: ISuggestionsCollector): Thenable<any>;
resolveSuggestion?(item: CompletionItem): Thenable<CompletionItem | null> | null;
}
Expand Down Expand Up @@ -164,4 +164,4 @@ export class JSONCompletionItemProvider implements CompletionItemProvider {
}
}

export const xhrDisabled = () => Promise.reject({ responseText: 'Use of online resources is disabled.' });
export const xhrDisabled = () => Promise.reject({ responseText: 'Use of online resources is disabled.' });
130 changes: 79 additions & 51 deletions extensions/npm/src/features/packageJSONContribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,46 +182,38 @@ export class PackageJSONContribution implements IJSONContribution {
return Promise.resolve(null);
}

public collectValueSuggestions(
_fileName: string,
location: Location,
result: ISuggestionsCollector
): Thenable<any> | null {
public async collectValueSuggestions(_fileName: string, location: Location, result: ISuggestionsCollector): Promise<any> {
if (!this.onlineEnabled()) {
return null;
}

if ((location.matches(['dependencies', '*']) || location.matches(['devDependencies', '*']) || location.matches(['optionalDependencies', '*']) || location.matches(['peerDependencies', '*']))) {
const currentKey = location.path[location.path.length - 1];
if (typeof currentKey === 'string') {
return this.npmView(currentKey).then(info => {
const latest = info.distTagsLatest;
if (latest) {
let name = JSON.stringify(latest);
let proposal = new CompletionItem(name);
proposal.kind = CompletionItemKind.Property;
proposal.insertText = name;
proposal.documentation = localize('json.npm.latestversion', 'The currently latest version of the package');
result.add(proposal);
const info = await this.fetchPackageInfo(currentKey);
if (info && info.distTagsLatest) {

name = JSON.stringify('^' + latest);
proposal = new CompletionItem(name);
proposal.kind = CompletionItemKind.Property;
proposal.insertText = name;
proposal.documentation = localize('json.npm.majorversion', 'Matches the most recent major version (1.x.x)');
result.add(proposal);
let name = JSON.stringify(info.distTagsLatest);
let proposal = new CompletionItem(name);
proposal.kind = CompletionItemKind.Property;
proposal.insertText = name;
proposal.documentation = localize('json.npm.latestversion', 'The currently latest version of the package');
result.add(proposal);

name = JSON.stringify('~' + latest);
proposal = new CompletionItem(name);
proposal.kind = CompletionItemKind.Property;
proposal.insertText = name;
proposal.documentation = localize('json.npm.minorversion', 'Matches the most recent minor version (1.2.x)');
result.add(proposal);
}
return 0;
}, () => {
return 0;
});
name = JSON.stringify('^' + info.distTagsLatest);
proposal = new CompletionItem(name);
proposal.kind = CompletionItemKind.Property;
proposal.insertText = name;
proposal.documentation = localize('json.npm.majorversion', 'Matches the most recent major version (1.x.x)');
result.add(proposal);

name = JSON.stringify('~' + info.distTagsLatest);
proposal = new CompletionItem(name);
proposal.kind = CompletionItemKind.Property;
proposal.insertText = name;
proposal.documentation = localize('json.npm.minorversion', 'Matches the most recent minor version (1.2.x)');
result.add(proposal);
}
}
}
return null;
Expand All @@ -243,39 +235,75 @@ export class PackageJSONContribution implements IJSONContribution {
return null;
}

private getInfo(pack: string): Thenable<string[]> {
return this.npmView(pack).then(info => {
private async getInfo(pack: string): Promise<string[]> {
let info = await this.fetchPackageInfo(pack);
if (info) {
const result: string[] = [];
result.push(info.description || '');
result.push(info.distTagsLatest ? localize('json.npm.version.hover', 'Latest version: {0}', info.distTagsLatest) : '');
result.push(info.homepage || '');
return result;
}, () => {
return [];
});
}

return [];
}

private async fetchPackageInfo(pack: string): Promise<ViewPackageInfo | undefined> {
let info = await this.npmView(pack);
if (!info) {
info = await this.npmjsView(pack);
}
return info;
}

private npmView(pack: string): Promise<ViewPackageInfo> {
return new Promise((resolve, reject) => {

private npmView(pack: string): Promise<ViewPackageInfo | undefined> {
return new Promise((resolve, _reject) => {
const command = 'npm view --json ' + pack + ' description dist-tags.latest homepage';
cp.exec(command, (error, stdout) => {
if (error) {
return reject();
}
try {
const content = JSON.parse(stdout);
resolve({
description: content['description'],
distTagsLatest: content['dist-tags.latest'],
homepage: content['homepage']
});
} catch (e) {
reject();
if (!error) {
try {
const content = JSON.parse(stdout);
resolve({
description: content['description'],
distTagsLatest: content['dist-tags.latest'],
homepage: content['homepage']
});
return;
} catch (e) {
// ignore
}
}
resolve(undefined);
});
});
}

private async npmjsView(pack: string): Promise<ViewPackageInfo | undefined> {
const queryUrl = 'https://registry.npmjs.org/' + encodeURIComponent(pack).replace(/%40/g, '@');
try {
const success = await this.xhr({
url: queryUrl,
agent: USER_AGENT
});
const obj = JSON.parse(success.responseText);
if (obj) {
const latest = obj && obj['dist-tags'] && obj['dist-tags']['latest'];
if (latest) {
return {
description: obj.description || '',
distTagsLatest: latest,
homepage: obj.homepage || ''
};
}
}
}
catch (e) {
//ignore
}
return undefined;
}

public getInfoContribution(_fileName: string, location: Location): Thenable<MarkedString[] | null> | null {
if ((location.matches(['dependencies', '*']) || location.matches(['devDependencies', '*']) || location.matches(['optionalDependencies', '*']) || location.matches(['peerDependencies', '*']))) {
const pack = location.path[location.path.length - 1];
Expand Down Expand Up @@ -327,4 +355,4 @@ interface ViewPackageInfo {
description: string;
distTagsLatest?: string;
homepage?: string;
}
}

0 comments on commit bae6f5f

Please sign in to comment.