/
host-rules-migration.ts
57 lines (48 loc) · 1.35 KB
/
host-rules-migration.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
import is from '@sindresorhus/is';
import type { HostRule } from '../../../types';
import { AbstractMigration } from '../base/abstract-migration';
export class HostRulesMigration extends AbstractMigration {
override readonly propertyName = 'hostRules';
override run(value: Record<string, unknown>[]): void {
const newHostRules: HostRule[] = [];
for (const hostRule of value) {
const newRule: any = {};
for (const [key, value] of Object.entries(hostRule)) {
if (key === 'platform') {
if (is.string(value)) {
newRule.hostType ??= value;
}
continue;
}
if (key === 'matchHost') {
if (is.string(value)) {
newRule.matchHost ??= massageUrl(value);
}
continue;
}
if (
key === 'endpoint' ||
key === 'host' ||
key === 'baseUrl' ||
key === 'hostName' ||
key === 'domainName'
) {
if (is.string(value)) {
newRule.matchHost ??= massageUrl(value);
}
continue;
}
newRule[key] = value;
}
newHostRules.push(newRule);
}
this.rewrite(newHostRules);
}
}
function massageUrl(url: string): string {
if (!url.includes('://') && url.includes('/')) {
return 'https://' + url;
} else {
return url;
}
}