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
|
// Exports defined here are re-exported through urls.js,
// so they're generally imported from '#urls'.
import {readFile} from 'node:fs/promises';
import * as path from 'node:path';
import {fileURLToPath} from 'node:url';
import yaml from 'js-yaml';
import {annotateError, annotateErrorWithFile, openAggregate} from '#aggregate';
import {empty, typeAppearance, withEntries} from '#sugar';
export const DEFAULT_URL_SPEC_FILE = 'urls-default.yaml';
export const internalDefaultURLSpecFile =
path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
DEFAULT_URL_SPEC_FILE);
function processStringToken(key, token) {
const oops = appearance =>
new Error(
`Expected ${key} to be a string or an array of strings, ` +
`got ${appearance}`);
if (typeof token === 'string') {
return token;
} else if (Array.isArray(token)) {
if (empty(token)) {
throw oops(`empty array`);
} else if (token.every(item => typeof item !== 'string')) {
throw oops(`array of non-strings`);
} else if (token.some(item => typeof item !== 'string')) {
throw oops(`array of mixed strings and non-strings`);
} else {
return token.join('');
}
} else {
throw oops(typeAppearance(token));
}
}
// Mutates, so don't even think about reusing the original representation.
function processObjectToken(key, token) {
const oops = appearance =>
new Error(
`Expected ${key} to be an object or an array of objects, ` +
`got ${appearance}`);
const looksLikeObject = value =>
typeof value === 'object' &&
value !== null &&
!Array.isArray(value);
if (looksLikeObject(token)) {
return token;
} else if (Array.isArray(token)) {
if (empty(token)) {
throw oops(`empty array`);
} else if (token.every(item => !looksLikeObject(item))) {
throw oops(`array of non-objects`);
} else if (token.some(item => !looksLikeObject(item))) {
throw oops(`array of mixed objects and non-objects`);
} else {
return Object.assign(...token);
}
}
}
function makeProcessToken(aggregate) {
return (object, key, processFn) => {
if (key in object) {
const value = aggregate.call(processFn, key, object[key]);
if (value === null) {
delete object[key];
} else {
object[key] = value;
}
}
};
}
export function processGroupSpec(groupKey, groupSpec) {
const aggregate =
openAggregate({message: `Errors procsesing group "${groupKey}"`});
const processToken = makeProcessToken(aggregate);
processToken(groupSpec, 'prefix', processStringToken);
processToken(groupSpec, 'paths', processObjectToken);
return {aggregate, result: groupSpec};
}
export function processURLSpec(sourceSpec) {
const aggregate =
openAggregate({message: `Errors processing URL spec`});
sourceSpec ??= {};
const urlSpec = structuredClone(sourceSpec);
delete urlSpec.yamlAliases;
delete urlSpec.localizedWithBaseDirectory;
aggregate.nest({message: `Errors processing groups`}, groupsAggregate => {
Object.assign(urlSpec,
withEntries(urlSpec, entries =>
entries.map(([groupKey, groupSpec]) => [
groupKey,
groupsAggregate.receive(
processGroupSpec(groupKey, groupSpec)),
])));
});
switch (sourceSpec.localizedWithBaseDirectory) {
case '<auto>': {
if (!urlSpec.localized) {
aggregate.push(new Error(
`Not ready for 'localizedWithBaseDirectory' group, ` +
`'localized' not available`));
} else if (!urlSpec.localized.paths) {
aggregate.push(new Error(
`Not ready for 'localizedWithBaseDirectory' group, ` +
`'localized' group's paths not available`));
}
break;
}
case undefined:
break;
default:
aggregate.push(new Error(
`Expected 'localizedWithBaseDirectory' group to have value '<auto>' ` +
`or not be set`));
break;
}
return {aggregate, result: urlSpec};
}
export function applyURLSpecOverriding(overrideSpec, baseSpec) {
const aggregate = openAggregate({message: `Errors applying URL spec`});
for (const [groupKey, overrideGroupSpec] of Object.entries(overrideSpec)) {
const baseGroupSpec = baseSpec[groupKey];
if (!baseGroupSpec) {
aggregate.push(new Error(`Group key "${groupKey}" not available on base spec`));
continue;
}
if (overrideGroupSpec.prefix) {
baseGroupSpec.prefix = overrideGroupSpec.prefix;
}
if (overrideGroupSpec.paths) {
for (const [pathKey, overridePathValue] of Object.entries(overrideGroupSpec.paths)) {
if (!baseGroupSpec.paths[pathKey]) {
aggregate.push(new Error(`Path key "${groupKey}.${pathKey}" not available on base spec`));
continue;
}
baseGroupSpec.paths[pathKey] = overridePathValue;
}
}
}
return {aggregate};
}
export function applyLocalizedWithBaseDirectory(urlSpec) {
const paths =
withEntries(urlSpec.localized.paths, entries =>
entries.map(([key, path]) => [key, '<>/' + path]));
urlSpec.localizedWithBaseDirectory =
Object.assign(
structuredClone(urlSpec.localized),
{paths});
}
export async function processURLSpecFromFile(file) {
let contents;
try {
contents = await readFile(file, 'utf-8');
} catch (caughtError) {
throw annotateError(
new Error(`Failed to read URL spec file`, {cause: caughtError}),
error => annotateErrorWithFile(error, file));
}
let sourceSpec;
let parseLanguage;
try {
if (path.extname(file) === '.yaml') {
parseLanguage = 'YAML';
sourceSpec = yaml.load(contents);
} else {
parseLanguage = 'JSON';
sourceSpec = JSON.parse(contents);
}
} catch (caughtError) {
throw annotateError(
new Error(`Failed to parse URL spec file as valid ${parseLanguage}`, {cause: caughtError}),
error => annotateErrorWithFile(error, file));
}
try {
return processURLSpec(sourceSpec);
} catch (caughtError) {
throw annotateErrorWithFile(caughtError, file);
}
}
|