| 
									
										
										
										
											2021-04-11 14:11:02 +02:00
										 |  |  | /* | 
					
						
							|  |  |  |  * This script attempt to automatically fix some basic issues when a theme from the custom generator is loaded | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | import {Utils} from "../Utils" | 
					
						
							|  |  |  | import {readFileSync, writeFileSync} from "fs"; | 
					
						
							|  |  |  | import SmallLicense from "../Models/smallLicense"; | 
					
						
							| 
									
										
										
										
											2021-06-22 14:21:32 +02:00
										 |  |  | import ScriptUtils from "./ScriptUtils"; | 
					
						
							|  |  |  | import AllImageProviders from "../Logic/ImageProviders/AllImageProviders"; | 
					
						
							| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  | import {LayoutConfigJson} from "../Models/ThemeConfig/Json/LayoutConfigJson"; | 
					
						
							|  |  |  | import LayerConfig from "../Models/ThemeConfig/LayerConfig"; | 
					
						
							| 
									
										
										
										
											2021-06-22 14:21:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | ScriptUtils.fixUtils() | 
					
						
							| 
									
										
										
										
											2021-04-11 14:11:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  | if (process.argv.length == 2) { | 
					
						
							| 
									
										
										
										
											2021-04-11 14:11:02 +02:00
										 |  |  |     console.log("USAGE: ts-node scripts/fixTheme <path to theme>") | 
					
						
							|  |  |  |     throw "No path specified" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const path = process.argv[2] | 
					
						
							| 
									
										
										
										
											2021-04-11 20:42:18 +02:00
										 |  |  | const dir = path.substring(0, path.lastIndexOf("/")) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-11 14:11:02 +02:00
										 |  |  | console.log("Fixing up ", path) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  | const themeConfigJson: LayoutConfigJson = JSON.parse(readFileSync(path, "UTF8")) | 
					
						
							| 
									
										
										
										
											2021-04-11 14:11:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  | const licenses: SmallLicense[] = [] | 
					
						
							| 
									
										
										
										
											2021-04-11 14:11:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  | const replacements: { source: string, destination: string }[] = [] | 
					
						
							| 
									
										
										
										
											2021-04-11 14:11:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | for (const layerConfigJson of themeConfigJson.layers) { | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |     if (typeof (layerConfigJson) === "string") { | 
					
						
							| 
									
										
										
										
											2021-04-11 14:11:02 +02:00
										 |  |  |         continue; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |     if (layerConfigJson["overpassTags"] !== undefined) { | 
					
						
							| 
									
										
										
										
											2021-04-11 14:11:02 +02:00
										 |  |  |         const tags = layerConfigJson["overpassTags"]; | 
					
						
							|  |  |  |         layerConfigJson["overpassTags"] = undefined; | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |         layerConfigJson["source"] = {osmTags: tags} | 
					
						
							| 
									
										
										
										
											2021-04-11 14:11:02 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     // @ts-ignore
 | 
					
						
							| 
									
										
										
										
											2021-09-13 01:21:47 +02:00
										 |  |  |     const layerConfig = new LayerConfig(layerConfigJson, "fix theme", true) | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |     const images: string[] = Array.from(layerConfig.ExtractImages()) | 
					
						
							| 
									
										
										
										
											2021-04-11 14:11:02 +02:00
										 |  |  |     const remoteImages = images.filter(img => img.startsWith("http")) | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-11 14:11:02 +02:00
										 |  |  |     for (const remoteImage of remoteImages) { | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-22 14:21:32 +02:00
										 |  |  |         const filename = remoteImage.substring(remoteImage.lastIndexOf("/")) | 
					
						
							|  |  |  |         ScriptUtils.DownloadFileTo(remoteImage, dir + "/" + filename) | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-11 14:11:02 +02:00
										 |  |  |         const imgPath = remoteImage.substring(remoteImage.lastIndexOf("/") + 1) | 
					
						
							| 
									
										
										
										
											2021-06-22 14:21:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         for (const attributionSrc of AllImageProviders.ImageAttributionSource) { | 
					
						
							|  |  |  |             try { | 
					
						
							|  |  |  |                 attributionSrc.GetAttributionFor(remoteImage).addCallbackAndRun(license => { | 
					
						
							|  |  |  |                     console.log("Downloaded an attribution!") | 
					
						
							|  |  |  |                     licenses.push({ | 
					
						
							|  |  |  |                         path: imgPath, | 
					
						
							|  |  |  |                         license: license?.license ?? "", | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |                         authors: Utils.NoNull([license?.artist]), | 
					
						
							| 
									
										
										
										
											2021-06-22 14:21:32 +02:00
										 |  |  |                         sources: [remoteImage] | 
					
						
							|  |  |  |                     }) | 
					
						
							|  |  |  |                 }) | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |             } catch (e) { | 
					
						
							| 
									
										
										
										
											2021-06-22 14:21:32 +02:00
										 |  |  |                 // Hush hush
 | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-11 20:42:18 +02:00
										 |  |  |         replacements.push({source: remoteImage, destination: `${dir}/${imgPath}`}) | 
					
						
							| 
									
										
										
										
											2021-04-11 14:11:02 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  | let fixedThemeJson = JSON.stringify(themeConfigJson, null, "  ") | 
					
						
							| 
									
										
										
										
											2021-04-11 14:11:02 +02:00
										 |  |  | for (const replacement of replacements) { | 
					
						
							|  |  |  |     fixedThemeJson = fixedThemeJson.replace(new RegExp(replacement.source, "g"), replacement.destination) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | writeFileSync(dir + "/generated.license_info.json", JSON.stringify(licenses, null, "  ")) | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  | writeFileSync(path + ".autofixed.json", fixedThemeJson) | 
					
						
							| 
									
										
										
										
											2021-04-11 14:15:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | console.log(`IMPORTANT:
 | 
					
						
							| 
									
										
										
										
											2021-06-22 14:21:32 +02:00
										 |  |  |  1) Copy generated.license_info.json over into license_info.json and add the missing attributions and authors | 
					
						
							|  |  |  |  2) Verify ${path}.autofixed.json as theme, and rename it to ${path}`)
 |