-
Notifications
You must be signed in to change notification settings - Fork 720
/
Copy pathupdate-3rd-party.ts
152 lines (117 loc) · 4.72 KB
/
update-3rd-party.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
import * as path from 'path';
import * as fs from 'fs';
import fetch from 'node-fetch';
import { compile } from 'json-schema-to-typescript';
type Transform = {
pattern: string;
replacement: string;
} | ((content: string, location: string) => Promise<string> | string);
const cleanSchemaObject = (obj: any) => {
if (!obj || typeof obj !== 'object') {
return;
}
// Disallow additional properties
if (obj.properties && !('additionalProperties' in obj)) {
obj.additionalProperties = false;
}
// Make definition refs local
if (obj && obj.$ref && obj.$ref.startsWith('https://')) {
obj.$ref = `#${obj.$ref.split('#')[1]}`;
}
Object.values(obj).forEach(cleanSchemaObject);
};
/**
* Flatten remotely referenced schemas into a single, combined schema.
* Handles merging `properties` and `definitions` from a root `allOf`.
*/
const inlineRemoteRefs = async (json: any, seenRefs = new Set<string>()) => {
if (json.allOf) {
for (const entry of json.allOf) {
if (entry.$ref && entry.$ref.startsWith('https://')) {
if (seenRefs.has(entry.$ref)) {
throw new Error(`Cyclic reference detected: ${entry.$ref}`);
}
seenRefs.add(entry.$ref);
try {
const res = await fetch(entry.$ref);
if (!res.ok) {
throw new Error(`Failed to fetch ${entry.$ref}: ${res.statusText}`);
}
const refJson = await res.json();
json.properties = { ...json.properties, ...refJson.properties };
json.definitions = { ...json.definitions, ...refJson.definitions };
} catch (err) {
throw new Error(`Error fetching or parsing ${entry.$ref}: ${err.message}`);
}
}
}
delete json.allOf;
}
};
/**
* Prepare the manifest schema for use as a single file.
* Also generate associated TypeScript interfaces.
*/
const prepManifestSchema = async (content: string, location: string): Promise<string> => {
const schema = JSON.parse(content);
schema.title = 'Manifest';
await inlineRemoteRefs(schema);
cleanSchemaObject(schema);
const ts = await compile(schema, 'Manifest');
await fs.promises.writeFile(location.replace('.json', '.ts'), `/* eslint-disable */\n${ts}`);
return JSON.stringify(schema, null, 4);
};
/**
* ajv 7 doesn't support property id. It uses $id.
*/
const replaceId = (content: string, location: string): string => {
const schema = JSON.parse(content);
if (!('$id' in schema)) {
schema.$id = schema.id;
}
if ('id' in schema) {
delete schema.id;
}
return JSON.stringify(schema, null, 4);
};
const downloadFile = async (downloadURL: string, downloadLocation: string, transforms: Transform[] = []) => {
const res = await fetch(downloadURL);
if (res.body && (res.body as any).message) {
throw new Error((res.body as any).message);
}
let body = await res.text();
for (const transform of transforms) {
if (typeof transform === 'function') {
body = await transform(body, downloadLocation);
} else {
body = body.replace(transform.pattern, transform.replacement);
}
}
fs.writeFileSync(downloadLocation, body, 'utf-8'); // eslint-disable-line no-sync
};
const resources = new Map([
['packages/hint-performance-budget/src/connections.ini', 'https://raw.githubusercontent.com/WPO-Foundation/webpagetest/master/www/settings/connectivity.ini.sample'],
['packages/hint-no-vulnerable-javascript-libraries/src/snyk-snapshot.json', 'https://snyk.io/partners/api/v2/vulndb/clientside.json'],
['packages/parser-manifest/src/schema.json', 'https://json.schemastore.org/web-manifest-combined'],
['packages/parser-typescript-config/src/schema.json', 'https://json.schemastore.org/tsconfig']
]);
// AJV uses draft-07 and otherwise tests break
const replaceDraft04 = { pattern: 'draft-04', replacement: 'draft-07' };
const resourceTransforms = new Map([
['packages/parser-manifest/src/schema.json', [replaceDraft04, prepManifestSchema, replaceId]],
['packages/parser-typescript-config/src/schema.json', [replaceDraft04, replaceId]]
]);
const updateEverything = async () => {
for (const [route, uri] of resources) {
const message = `Updating ${route}`;
console.log(message);
try {
const transform = resourceTransforms.get(route);
await downloadFile(uri, path.normalize(route), transform);
} catch (e) {
console.error(`Error downloading ${uri}`, e);
throw e;
}
}
};
updateEverything();