Repository navigation
Expand file tree
/
Copy pathstyle.js
More file actions
346 lines (305 loc) · 10.8 KB
/
Copy pathstyle.js
File metadata and controls
346 lines (305 loc) · 10.8 KB
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
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env node
'use strict';
//
// Produces the published basemap styles from the raw OpenMapTiles recompose.
//
// The raw recompose is the cartographic reference: the light output is a
// no-op of its layer tree, differing only in publication fields. The dark
// output starts from that light clone and applies themes/dark/theme.json
// strictly through scripts/lib/theme.js, so only the allowlisted paint colour
// properties change.
//
// Usage:
// node scripts/style.js write build/style/style-dark.json and style-light.json
// node scripts/style.js --validate verify the generated outputs against the raw style
// node scripts/style.js --help show this message
const fs = require('node:fs');
const path = require('node:path');
const { applyTheme, loadTheme } = require('./lib/theme');
const ROOT = path.resolve(__dirname, '..');
const RAW_STYLE = path.join('build', 'style', 'style.json');
const OUTPUT_DIR = path.join('build', 'style');
const DARK_OUTPUT = 'style-dark.json';
const LIGHT_OUTPUT = 'style-light.json';
const THEME_PATH = path.join('themes', 'dark', 'theme.json');
const LOCK_PATH = path.join('vendor', 'openmaptiles.lock.json');
const GLYPHS = 'https://www.ogis.org/basemap/fonts/{fontstack}/{range}.pbf';
const TILE_URL = 'https://tiles.openfreemap.org/planet';
const ATTRIBUTION =
'<a href="https://openfreemap.org" target="_blank">OpenFreeMap</a> ' +
'<a href="https://www.openmaptiles.org/" target="_blank">© OpenMapTiles</a> ' +
'Data from <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a>';
const DARK_SPRITE = 'https://www.ogis.org/basemap/sprite';
const LIGHT_SPRITE = 'https://www.ogis.org/basemap/sprite-light';
const THEMES = Object.freeze({
dark: Object.freeze({ name: 'Basemap Dark', sprite: DARK_SPRITE }),
light: Object.freeze({ name: 'Basemap Light', sprite: LIGHT_SPRITE })
});
function isPlainObject(value) {
return value !== null && typeof value === 'object' && !Array.isArray(value);
}
function clone(value) {
return JSON.parse(JSON.stringify(value));
}
function readJSON(filePath) {
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
}
function readUpstreamCommit(root = ROOT) {
const lock = readJSON(path.join(root, LOCK_PATH));
if (!isPlainObject(lock) || typeof lock.commit !== 'string' || !lock.commit.trim()) {
throw new Error(`Upstream lock ${LOCK_PATH} has no commit SHA`);
}
return lock.commit;
}
function styleMetadata(rawStyle, theme, commit) {
const metadata = isPlainObject(rawStyle.metadata) ? clone(rawStyle.metadata) : {};
metadata['ogis:theme'] = theme;
metadata['ogis:upstreamCommit'] = commit;
return metadata;
}
function applyPublicationFields(style, { theme, commit }) {
const definition = THEMES[theme];
if (!definition) throw new Error(`Unknown output theme: ${theme}`);
const output = clone(style);
output.name = definition.name;
output.sprite = definition.sprite;
output.glyphs = GLYPHS;
if (!isPlainObject(output.sources) || !isPlainObject(output.sources.openmaptiles)) {
throw new Error('Raw style has no sources.openmaptiles object');
}
output.sources.openmaptiles.url = TILE_URL;
output.sources.openmaptiles.attribution = ATTRIBUTION;
output.metadata = styleMetadata(style, theme, commit);
return output;
}
function createLightStyle(rawStyle, { commit }) {
if (!isPlainObject(rawStyle)) throw new Error('Raw style must be a JSON object');
return applyPublicationFields(rawStyle, { theme: 'light', commit });
}
function createDarkStyle(lightStyle, theme, { commit }) {
if (!isPlainObject(lightStyle)) throw new Error('Light style must be a JSON object');
if (!theme) throw new Error('A dark theme is required');
const darkStyle = applyTheme(lightStyle, theme);
darkStyle.name = THEMES.dark.name;
darkStyle.sprite = THEMES.dark.sprite;
darkStyle.metadata = {
...(isPlainObject(darkStyle.metadata) ? darkStyle.metadata : {}),
'ogis:theme': 'dark',
'ogis:upstreamCommit': commit
};
return darkStyle;
}
function mutablePaths() {
return [
['name'],
['sprite'],
['glyphs'],
['metadata'],
['sources', 'openmaptiles', 'url'],
['sources', 'openmaptiles', 'attribution']
];
}
function deletePath(target, keys) {
let cursor = target;
for (let index = 0; index < keys.length - 1; index += 1) {
cursor = cursor[keys[index]];
if (!isPlainObject(cursor)) return;
}
delete cursor[keys[keys.length - 1]];
}
function stripMutableFields(style) {
const stripped = clone(style);
for (const keys of mutablePaths()) deletePath(stripped, keys);
return stripped;
}
function stripIdentity(style) {
const stripped = clone(style);
delete stripped.name;
delete stripped.metadata;
delete stripped.sprite;
delete stripped.layers;
return stripped;
}
function assertEqual(actual, expected, message) {
if (JSON.stringify(actual) !== JSON.stringify(expected)) {
throw new Error(message);
}
}
function validateStyles({ rawStyle, darkStyle, lightStyle, theme, commit }) {
if (!isPlainObject(rawStyle)) throw new Error('Raw style must be a JSON object');
if (!isPlainObject(lightStyle)) throw new Error('Light style must be a JSON object');
if (!isPlainObject(darkStyle)) throw new Error('Dark style must be a JSON object');
assertEqual(lightStyle.layers, rawStyle.layers, 'Light layers differ from the raw recompose');
assertEqual(
stripMutableFields(lightStyle),
stripMutableFields(rawStyle),
'Light style changed a field outside the publication allowlist'
);
assertEqual(lightStyle.name, THEMES.light.name, 'Light style name is not Basemap Light');
assertEqual(lightStyle.sprite, THEMES.light.sprite, 'Light style sprite is not the light sprite');
assertEqual(lightStyle.glyphs, GLYPHS, 'Light style glyphs URL is wrong');
assertEqual(
lightStyle.sources.openmaptiles.url,
TILE_URL,
'Light style tile URL is wrong'
);
assertEqual(
lightStyle.sources.openmaptiles.attribution,
ATTRIBUTION,
'Light style attribution is wrong'
);
assertEqual(
lightStyle.metadata && lightStyle.metadata['ogis:theme'],
'light',
'Light style metadata is missing ogis:theme=light'
);
assertEqual(
lightStyle.metadata && lightStyle.metadata['ogis:upstreamCommit'],
commit,
'Light style metadata is missing the upstream commit'
);
assertEqual(darkStyle.name, THEMES.dark.name, 'Dark style name is not Basemap Dark');
assertEqual(darkStyle.sprite, THEMES.dark.sprite, 'Dark style sprite is not the dark sprite');
assertEqual(darkStyle.glyphs, GLYPHS, 'Dark style glyphs URL is wrong');
assertEqual(
darkStyle.sources.openmaptiles.url,
TILE_URL,
'Dark style tile URL is wrong'
);
assertEqual(
darkStyle.sources.openmaptiles.attribution,
ATTRIBUTION,
'Dark style attribution is wrong'
);
assertEqual(
darkStyle.metadata && darkStyle.metadata['ogis:theme'],
'dark',
'Dark style metadata is missing ogis:theme=dark'
);
assertEqual(
darkStyle.metadata && darkStyle.metadata['ogis:upstreamCommit'],
commit,
'Dark style metadata is missing the upstream commit'
);
assertEqual(
darkStyle.layers,
applyTheme(lightStyle, theme).layers,
'Dark style is not the light style with strict theme transformations applied'
);
assertEqual(
stripIdentity(darkStyle),
stripIdentity(lightStyle),
'Dark style changed a field other than its name and metadata'
);
return true;
}
function buildStyles({
root = ROOT,
rawPath = path.join(root, RAW_STYLE),
themePath = path.join(root, THEME_PATH),
commit = readUpstreamCommit(root),
theme
} = {}) {
const rawStyle = readJSON(rawPath);
const compiledTheme = theme || loadTheme(themePath);
const lightStyle = createLightStyle(rawStyle, { commit });
const darkStyle = createDarkStyle(lightStyle, compiledTheme, { commit });
validateStyles({ rawStyle, darkStyle, lightStyle, theme: compiledTheme, commit });
return { rawStyle, lightStyle, darkStyle, commit };
}
function writeJSON(filePath, value) {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
fs.writeFileSync(filePath, `${JSON.stringify(value, null, 2)}\n`);
}
function writeStyles({ root = ROOT, outputDir = path.join(root, OUTPUT_DIR), styles } = {}) {
const built = styles || buildStyles({ root });
const darkPath = path.join(outputDir, DARK_OUTPUT);
const lightPath = path.join(outputDir, LIGHT_OUTPUT);
writeJSON(darkPath, built.darkStyle);
writeJSON(lightPath, built.lightStyle);
return { darkPath, lightPath, ...built };
}
function readStyles({ root = ROOT, outputDir = path.join(root, OUTPUT_DIR) } = {}) {
return {
rawStyle: readJSON(path.join(root, RAW_STYLE)),
darkStyle: readJSON(path.join(outputDir, DARK_OUTPUT)),
lightStyle: readJSON(path.join(outputDir, LIGHT_OUTPUT)),
theme: loadTheme(path.join(root, THEME_PATH)),
commit: readUpstreamCommit(root)
};
}
function validateOutputs(options = {}) {
const root = options.root || ROOT;
const outputDir = options.outputDir || path.join(root, OUTPUT_DIR);
return validateStyles(readStyles({ root, outputDir }));
}
function usage() {
return `Usage: node scripts/style.js [options]
Produces the published basemap styles from ${RAW_STYLE}:
${path.join(OUTPUT_DIR, LIGHT_OUTPUT)} cartographic no-op of the raw recompose
${path.join(OUTPUT_DIR, DARK_OUTPUT)} light style with strict dark theme colours
Options:
--validate verify generated styles against the raw recompose
--help, -h show this message`;
}
function main(argv = process.argv.slice(2)) {
const known = new Set(['--validate', '--help', '-h']);
const unknown = argv.filter((argument) => !known.has(argument));
if (unknown.length) {
console.error(`Unknown argument: ${unknown[0]}`);
console.error(usage());
process.exitCode = 2;
return;
}
if (argv.includes('--help') || argv.includes('-h')) {
console.log(usage());
return;
}
try {
if (argv.includes('--validate')) {
validateOutputs();
console.log('Style outputs validated: light is a no-op, dark is strictly themed.');
return;
}
const result = writeStyles();
console.log(
`Wrote ${path.relative(ROOT, result.lightPath)} (${result.lightStyle.layers.length} layers)`
);
console.log(
`Wrote ${path.relative(ROOT, result.darkPath)} (${result.darkStyle.layers.length} layers)`
);
console.log(`Upstream commit ${result.commit}`);
} catch (error) {
console.error(`Style generation failed: ${error.message}`);
process.exitCode = 1;
}
}
if (require.main === module) {
main();
}
module.exports = {
ATTRIBUTION,
DARK_OUTPUT,
DARK_SPRITE,
GLYPHS,
LIGHT_OUTPUT,
LIGHT_SPRITE,
OUTPUT_DIR,
RAW_STYLE,
ROOT,
THEMES,
TILE_URL,
buildStyles,
createDarkStyle,
createLightStyle,
deletePath,
mutablePaths,
readStyles,
readUpstreamCommit,
stripIdentity,
stripMutableFields,
usage,
validateOutputs,
validateStyles,
writeStyles
};