Package hover: Show when last published (#177634)

This commit is contained in:
Christof Marti
2023-03-20 09:41:52 +01:00
committed by GitHub
parent 18703b5872
commit 7a4092f1b0
2 changed files with 213 additions and 7 deletions

View File

@@ -10,6 +10,7 @@ import { Location } from 'jsonc-parser';
import * as cp from 'child_process';
import { dirname } from 'path';
import { fromNow } from './date';
const LIMIT = 40;
@@ -215,14 +216,14 @@ export class PackageJSONContribution implements IJSONContribution {
return null;
}
private getDocumentation(description: string | undefined, version: string | undefined, homepage: string | undefined): MarkdownString {
private getDocumentation(description: string | undefined, version: string | undefined, time: string | undefined, homepage: string | undefined): MarkdownString {
const str = new MarkdownString();
if (description) {
str.appendText(description);
}
if (version) {
str.appendText('\n\n');
str.appendText(l10n.t("Latest version: {0}", version));
str.appendText(time ? l10n.t("Latest version: {0} published {1}", version, fromNow(Date.parse(time), true, true)) : l10n.t("Latest version: {0}", version));
}
if (homepage) {
str.appendText('\n\n');
@@ -241,7 +242,7 @@ export class PackageJSONContribution implements IJSONContribution {
return this.fetchPackageInfo(name, resource).then(info => {
if (info) {
item.documentation = this.getDocumentation(info.description, info.version, info.homepage);
item.documentation = this.getDocumentation(info.description, info.version, info.time, info.homepage);
return item;
}
return null;
@@ -283,15 +284,17 @@ export class PackageJSONContribution implements IJSONContribution {
private npmView(npmCommandPath: string, pack: string, resource: Uri | undefined): Promise<ViewPackageInfo | undefined> {
return new Promise((resolve, _reject) => {
const args = ['view', '--json', pack, 'description', 'dist-tags.latest', 'homepage', 'version'];
const args = ['view', '--json', pack, 'description', 'dist-tags.latest', 'homepage', 'version', 'time'];
const cwd = resource && resource.scheme === 'file' ? dirname(resource.fsPath) : undefined;
cp.execFile(npmCommandPath, args, { cwd }, (error, stdout) => {
if (!error) {
try {
const content = JSON.parse(stdout);
const version = content['dist-tags.latest'] || content['version'];
resolve({
description: content['description'],
version: content['dist-tags.latest'] || content['version'],
version,
time: content.time?.[version],
homepage: content['homepage']
});
return;
@@ -316,6 +319,7 @@ export class PackageJSONContribution implements IJSONContribution {
return {
description: obj.description || '',
version,
time: obj.time?.[version],
homepage: obj.homepage || ''
};
}
@@ -334,7 +338,7 @@ export class PackageJSONContribution implements IJSONContribution {
if (typeof pack === 'string') {
return this.fetchPackageInfo(pack, resource).then(info => {
if (info) {
return [this.getDocumentation(info.description, info.version, info.homepage)];
return [this.getDocumentation(info.description, info.version, info.time, info.homepage)];
}
return null;
});
@@ -363,7 +367,7 @@ export class PackageJSONContribution implements IJSONContribution {
proposal.kind = CompletionItemKind.Property;
proposal.insertText = insertText;
proposal.filterText = JSON.stringify(name);
proposal.documentation = this.getDocumentation(pack.description, pack.version, pack?.links?.homepage);
proposal.documentation = this.getDocumentation(pack.description, pack.version, undefined, pack?.links?.homepage);
collector.add(proposal);
}
}
@@ -379,5 +383,6 @@ interface SearchPackageInfo {
interface ViewPackageInfo {
description: string;
version?: string;
time?: string;
homepage?: string;
}