| 
									
										
										
										
											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" | 
					
						
							|  |  |  | Utils.runningFromConsole = true; | 
					
						
							| 
									
										
										
										
											2021-06-22 14:21:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-11 14:11:02 +02:00
										 |  |  | import {readFileSync, writeFileSync} from "fs"; | 
					
						
							|  |  |  | import {LayoutConfigJson} from "../Customizations/JSON/LayoutConfigJson"; | 
					
						
							|  |  |  | import LayerConfig from "../Customizations/JSON/LayerConfig"; | 
					
						
							|  |  |  | import SmallLicense from "../Models/smallLicense"; | 
					
						
							| 
									
										
										
										
											2021-06-22 03:16:45 +02:00
										 |  |  | import AllKnownLayers from "../Customizations/AllKnownLayers"; | 
					
						
							| 
									
										
										
										
											2021-06-22 14:21:32 +02:00
										 |  |  | import ScriptUtils from "./ScriptUtils"; | 
					
						
							|  |  |  | import AllImageProviders from "../Logic/ImageProviders/AllImageProviders"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ScriptUtils.fixUtils() | 
					
						
							| 
									
										
										
										
											2021-04-11 14:11:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | if(process.argv.length == 2){ | 
					
						
							|  |  |  |     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) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const themeConfigJson : LayoutConfigJson = JSON.parse(readFileSync(path, "UTF8")) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const licenses : SmallLicense[] = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const replacements: {source: string, destination: string}[] = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | for (const layerConfigJson of themeConfigJson.layers) { | 
					
						
							|  |  |  |     if(typeof (layerConfigJson) === "string"){ | 
					
						
							|  |  |  |         continue; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if(layerConfigJson["overpassTags"] !== undefined){ | 
					
						
							|  |  |  |         const tags = layerConfigJson["overpassTags"]; | 
					
						
							|  |  |  |         layerConfigJson["overpassTags"] = undefined; | 
					
						
							|  |  |  |         layerConfigJson["source"] = { osmTags : tags} | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // @ts-ignore
 | 
					
						
							| 
									
										
										
										
											2021-06-22 03:16:45 +02:00
										 |  |  |     const layerConfig = new LayerConfig(layerConfigJson, AllKnownLayers.sharedUnits, "fix theme",true) | 
					
						
							| 
									
										
										
										
											2021-04-11 14:11:02 +02:00
										 |  |  |     const images : string[] = Array.from(layerConfig.ExtractImages()) | 
					
						
							|  |  |  |     const remoteImages = images.filter(img => img.startsWith("http")) | 
					
						
							| 
									
										
										
										
											2021-06-22 14:21:32 +02:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2021-04-11 14:11:02 +02:00
										 |  |  |     for (const remoteImage of remoteImages) { | 
					
						
							| 
									
										
										
										
											2021-06-22 14:21:32 +02:00
										 |  |  |          | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         const filename = remoteImage.substring(remoteImage.lastIndexOf("/")) | 
					
						
							|  |  |  |         ScriptUtils.DownloadFileTo(remoteImage, dir + "/" + filename) | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |          | 
					
						
							| 
									
										
										
										
											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 ?? "", | 
					
						
							|  |  |  |                         authors:Utils.NoNull([license?.artist]), | 
					
						
							|  |  |  |                         sources: [remoteImage] | 
					
						
							|  |  |  |                     }) | 
					
						
							|  |  |  |                 }) | 
					
						
							|  |  |  |             }catch(e){ | 
					
						
							|  |  |  |                 // Hush hush
 | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |          | 
					
						
							| 
									
										
										
										
											2021-04-11 20:42:18 +02:00
										 |  |  |         replacements.push({source: remoteImage, destination: `${dir}/${imgPath}`}) | 
					
						
							| 
									
										
										
										
											2021-04-11 14:11:02 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let fixedThemeJson = JSON.stringify(themeConfigJson, null , "  ") | 
					
						
							|  |  |  | 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, "  ")) | 
					
						
							|  |  |  | 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}`)
 |