-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathdub-api.ts
125 lines (113 loc) · 3.95 KB
/
dub-api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import * as vscode from "vscode";
import * as fs from "fs";
import * as path from "path";
import { reqJson } from "./util";
import { AxiosError } from "axios";
function dubAPI() {
return reqJson("https://code.dlang.org/");
}
export function searchDubPackages(query: string): Thenable<any[]> {
return dubAPI().get("/api/packages/search?q=" + encodeURIComponent(query))
.then((body) => {
return body.data;
}).catch((e: AxiosError) => {
throw e.response ? "No packages found" : e;
});
}
export function listPackages(): Thenable<any[]> {
return dubAPI().get("/packages/index.json")
.then((body) => {
return body.data;
}).catch((e: AxiosError) => {
throw e.response ? "No packages found" : e;
});
}
var packageCache: any;
var packageCacheDate = new Date(0);
export function listPackageOptions(): Thenable<vscode.QuickPickItem[]> {
if (new Date().getTime() - packageCacheDate.getTime() < 15 * 60 * 1000)
return Promise.resolve(packageCache);
return dubAPI().get<{ name: string, description: string, version: string }[]>("/api/packages/search").then((body) => {
var ret: vscode.QuickPickItem[] = [];
body.data.forEach(element => {
ret.push({
label: element.name,
description: element.version,
detail: element.description
})
});
packageCache = ret;
packageCacheDate = new Date();
return ret;
}).catch((e: AxiosError) => {
throw e.response ? "No packages found" : e;
});
}
export function getPackageInfo(pkg: string): Thenable<any> {
return dubAPI().get("/api/packages/" + encodeURIComponent(pkg) + "/info")
.then((body) => {
return body.data;
}).catch((e: AxiosError) => {
throw e.response ? "No packages found" : e;
});
}
export function getLatestPackageInfo(pkg: string): Thenable<{ description?: string; version?: string; subPackages?: string[], readme?: string, readmeMarkdown?: boolean, license?: string, copyright?: string }> {
return dubAPI().get<any>("/api/packages/" + encodeURIComponent(pkg) + "/latest/info")
.then((body) => {
var json = body.data;
var subPackages: string[] = [];
if (json.info.subPackages)
json.info.subPackages.forEach((pkg: any) => {
subPackages.push(pkg.name);
});
return {
version: json.version,
description: json.info.description,
license: json.info.license,
copyright: json.info.copyright,
subPackages: subPackages,
readme: json.readme,
readmeMarkdown: json.readmeMarkdown
};
});
}
export function autoCompletePath(fileName: string, key: string, currentValue: string, addResult: (v: vscode.CompletionItem) => any): Thenable<any> {
let folderOnly = ["path", "targetPath", "sourcePaths", "stringImportPaths", "importPaths"].indexOf(key) != -1;
let fileRegex = ["copyFiles"].indexOf(key) != -1 ? null : /\.di?$/i;
return new Promise((resolve, reject) => {
if (currentValue != "") {
let end = currentValue.lastIndexOf('/');
if (end != -1)
currentValue = currentValue.substr(0, end);
}
let dir = path.join(path.dirname(fileName), currentValue);
fs.readdir(dir, { withFileTypes: true }, (err, files) => {
if (err)
return reject(err);
files.forEach(file => {
if (file.name[0] == '.')
return;
if (folderOnly && !file.isDirectory())
return;
if (!file.isDirectory() && fileRegex && !fileRegex.exec(file.name))
return;
let kind: vscode.CompletionItemKind = vscode.CompletionItemKind.Text;
if (file.isSymbolicLink())
kind = vscode.CompletionItemKind.Reference;
else if (file.isDirectory())
kind = vscode.CompletionItemKind.Folder;
else if (file.isFile())
kind = vscode.CompletionItemKind.File;
let value = path.join(currentValue, file.name).replace(/\\/g, '/');
if (file.isDirectory() && !folderOnly)
value += "/";
value = JSON.stringify(value);
let item = new vscode.CompletionItem(value, kind);
if (file.isDirectory())
item.insertText = new vscode.SnippetString(value.slice(0, -1) + "${0}\"");
addResult(item);
});
resolve(null);
});
});
}