-
Notifications
You must be signed in to change notification settings - Fork 3
/
check_update.ts
336 lines (293 loc) · 9.64 KB
/
check_update.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import {
DENO_SLACK_API,
DENO_SLACK_HOOKS,
DENO_SLACK_SDK,
} from "./libraries.ts";
import { getProtocolInterface, JSONValue } from "./deps.ts";
import { getJSON, isNewSemverRelease } from "./utilities.ts";
const IMPORT_MAP_SDKS = [DENO_SLACK_SDK, DENO_SLACK_API];
const SLACK_JSON_SDKS = [
DENO_SLACK_HOOKS, // should be the only one needed now that the get-hooks hook is supported
];
interface CheckUpdateResponse {
name: string;
releases: Release[];
message?: string;
url?: string;
error?: {
message: string;
} | null;
}
interface VersionMap {
[key: string]: Release;
}
export interface Release {
name: string;
current?: string;
latest?: string;
update?: boolean;
breaking?: boolean;
message?: string;
url?: string;
error?: {
message: string;
} | null;
}
interface InaccessibleFile {
name: string;
error: Error;
}
export const checkForSDKUpdates = async () => {
const { versionMap, inaccessibleFiles } = await createVersionMap();
const updateResp = createUpdateResp(versionMap, inaccessibleFiles);
return updateResp;
};
/**
* createVersionMap creates an object that contains each dependency,
* featuring information about the current and latest versions, as well
* as if breaking changes are present and if any errors occurred during
* version retrieval.
*/
export async function createVersionMap(): Promise<
{ versionMap: VersionMap; inaccessibleFiles: InaccessibleFile[] }
> {
const { versionMap, inaccessibleFiles } = await readProjectDependencies();
// Check each dependency for updates, classify update as breaking or not,
// craft message with information retrieved, and note any error that occurred.
for (const [sdk, value] of Object.entries(versionMap)) {
if (value) {
const current = versionMap[sdk].current || "";
let latest = "", error = null;
try {
latest = await fetchLatestModuleVersion(sdk);
} catch (err) {
error = err;
}
const update = (!!current && !!latest) &&
isNewSemverRelease(current, latest);
const breaking = hasBreakingChange(current, latest);
versionMap[sdk] = {
...versionMap[sdk],
latest,
update,
breaking,
error,
};
}
}
return { versionMap, inaccessibleFiles };
}
/** readProjectDependencies cycles through supported project
* dependency files and maps them to the versionMap that contains
* each dependency's update information.
*/
export async function readProjectDependencies(): Promise<
{ versionMap: VersionMap; inaccessibleFiles: InaccessibleFile[] }
> {
const cwd = Deno.cwd();
const versionMap: VersionMap = {};
const { dependencyFiles, inaccessibleDenoFiles } =
await gatherDependencyFiles(cwd);
const inaccessibleFiles = [...inaccessibleDenoFiles];
for (const [fileName, depKey] of dependencyFiles) {
try {
const fileJSON = await getJSON(`${cwd}/${fileName}`);
const fileDependencies = extractDependencies(fileJSON, depKey);
// For each dependency found, compare to SDK-related dependency
// list and, if known, update the versionMap with version information
for (const [_, val] of fileDependencies) {
for (const sdk of [...IMPORT_MAP_SDKS, ...SLACK_JSON_SDKS]) {
if (val.includes(sdk)) {
versionMap[sdk] = {
name: sdk,
current: extractVersion(val),
};
}
}
}
} catch (err) {
inaccessibleFiles.push({ name: fileName, error: err });
}
}
return { versionMap, inaccessibleFiles };
}
/**
* gatherDependencyFiles rounds up all SDK-supported dependency files, as well
* as those dependency files referenced in deno.json or deno.jsonc, and returns
* an array of arrays made up of filename and dependency key pairs.
*/
export async function gatherDependencyFiles(
cwd: string,
): Promise<
{
dependencyFiles: [string, "imports" | "hooks"][];
inaccessibleDenoFiles: InaccessibleFile[];
}
> {
const dependencyFiles: [string, "imports" | "hooks"][] = [
["slack.json", "hooks"],
["slack.jsonc", "hooks"],
];
// Parse deno.* files for `importMap` dependency file
const { denoJSONDepFiles, inaccessibleDenoFiles } =
await getDenoImportMapFiles(cwd);
dependencyFiles.push(...denoJSONDepFiles);
return { dependencyFiles, inaccessibleDenoFiles };
}
/**
* getDenoImportMapFiles cycles through supported deno.* files and,
* if an `importMap` key is found, returns an array of arrays made up
* of filename and dependency key pairs.
*
* ex: [["import_map.json", "imports"], ["custom_map.json", "imports"]]
*/
export async function getDenoImportMapFiles(
cwd: string,
): Promise<
{
denoJSONDepFiles: [string, "imports"][];
inaccessibleDenoFiles: InaccessibleFile[];
}
> {
const denoJSONFiles = ["deno.json", "deno.jsonc"];
const denoJSONDepFiles: [string, "imports"][] = [];
const inaccessibleDenoFiles: InaccessibleFile[] = [];
for (const fileName of denoJSONFiles) {
try {
const denoJSON = await getJSON(`${cwd}/${fileName}`);
const jsonIsParsable = denoJSON && typeof denoJSON === "object" &&
!Array.isArray(denoJSON) && denoJSON.importMap;
if (jsonIsParsable) {
denoJSONDepFiles.push([`${denoJSON.importMap}`, "imports"]);
}
} catch (err) {
inaccessibleDenoFiles.push({ name: fileName, error: err });
}
}
return { denoJSONDepFiles, inaccessibleDenoFiles };
}
/**
* extractDependencies accepts the contents of a JSON file and a
* top-level, file-specific key within that file that corresponds to
* recognized project dependencies. If found, returns an array of key,
* value pairs that make use of the dependencies.
*/
export function extractDependencies(
json: JSONValue,
key: string,
): [string, string][] {
// Determine if the JSON passed is an object
const jsonIsParsable = json !== null && typeof json === "object" &&
!Array.isArray(json);
if (jsonIsParsable) {
const dependencyMap = json[key];
return dependencyMap ? Object.entries(dependencyMap) : [];
}
return [];
}
/** fetchLatestModuleVersion retrieves the published metadata.json that
* contains all releases and returns the latest published version
*/
export async function fetchLatestModuleVersion(
moduleName: string,
): Promise<string> {
try {
const res = await fetch("https://api.slack.com/slackcli/metadata.json");
const jsonData = await res.json();
const hypenatedModuleName = moduleName.replaceAll("_", "-");
return jsonData[hypenatedModuleName].releases[0].version;
} catch (err) {
throw new Error(err);
}
}
/**
* extractVersion takes in a URL formatted string, searches for a version,
* and, if version is found, returns that version.
*
* Example input: https://deno.land/x/deno_slack_sdk@2.6.0/
*/
export function extractVersion(str: string): string {
const at = str.indexOf("@");
// Doesn't contain an @ version
if (at === -1) return "";
const slash = str.indexOf("/", at);
const version = slash < at
? str.substring(at + 1)
: str.substring(at + 1, slash);
return version;
}
/**
* hasBreakingChange determines whether or not there is a
* major version difference of greater or equal to 1 between the current
* and latest version.
*/
export function hasBreakingChange(current: string, latest: string): boolean {
const currMajor = current.split(".")[0];
const latestMajor = latest.split(".")[0];
return +latestMajor - +currMajor >= 1;
}
/**
* createUpdateResp creates and returns an CheckUpdateResponse object
* that contains information about a collection of release dependencies
* in the shape of an object that the CLI expects to consume
*/
export function createUpdateResp(
versionMap: VersionMap,
inaccessibleFiles: InaccessibleFile[],
): CheckUpdateResponse {
const name = "the Slack SDK";
const releases = [];
const message = "";
const url = "https://api.slack.com/automation/changelog";
const fileErrorMsg = createFileErrorMsg(inaccessibleFiles);
let error = null;
let errorMsg = "";
// Output information for each dependency
for (const sdk of Object.values(versionMap)) {
// Dependency has an update OR the fetch of update failed
if (sdk) {
releases.push(sdk);
// Add the dependency that failed to be fetched to the top-level error message
if (sdk.error && sdk.error.message) {
errorMsg += errorMsg
? `, ${sdk.name}`
: `An error occurred fetching updates for the following packages: ${sdk.name}`;
}
}
}
// If there were issues accessing dependency files, append error message(s)
if (inaccessibleFiles.length) {
errorMsg += errorMsg ? `\n\n ${fileErrorMsg}` : fileErrorMsg;
}
if (errorMsg) error = { message: errorMsg };
return {
name,
message,
releases,
url,
error,
};
}
/**
* createFileErrorMsg creates and returns an error message that
* contains lightly formatted information about the dependency
* files that were found but otherwise inaccessible/unreadable.
*/
export function createFileErrorMsg(
inaccessibleFiles: InaccessibleFile[],
): string {
let fileErrorMsg = "";
// There were issues with reading some of the files that were found
for (const file of inaccessibleFiles) {
// Skip surfacing error to user if supported file was merely not found
if (file.error.cause instanceof Deno.errors.NotFound) continue;
fileErrorMsg += fileErrorMsg
? `\n ${file.name}: ${file.error.message}`
: `An error occurred while reading the following files: \n\n ${file.name}: ${file.error.message}`;
}
return fileErrorMsg;
}
if (import.meta.main) {
const protocol = getProtocolInterface(Deno.args);
protocol.respond(JSON.stringify(await checkForSDKUpdates()));
}