forked from MapComplete/MapComplete
		
	Add script to extract a layer from a theme
This commit is contained in:
		
							parent
							
								
									4af622575d
								
							
						
					
					
						commit
						fa50e52afc
					
				
					 2 changed files with 53 additions and 16 deletions
				
			
		
							
								
								
									
										53
									
								
								scripts/extractLayer.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								scripts/extractLayer.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,53 @@ | ||||||
|  | import {LayoutConfigJson} from "../Models/ThemeConfig/Json/LayoutConfigJson"; | ||||||
|  | import {LayerConfigJson} from "../Models/ThemeConfig/Json/LayerConfigJson"; | ||||||
|  | import {existsSync, mkdirSync, readFileSync, writeFileSync} from "fs"; | ||||||
|  | 
 | ||||||
|  | function main(args: string[]){ | ||||||
|  |     if(args.length === 0){ | ||||||
|  |         console.log("Extracts an inline layer from a theme and places it in it's own layer directory.") | ||||||
|  |         console.log("USAGE: ts-node scripts/extractLayerFromTheme.ts <themeid> <layerid>") | ||||||
|  |         console.log("(Invoke with only the themename to see which layers can be extracted)") | ||||||
|  |     } | ||||||
|  |     const themeId = args[0] | ||||||
|  |     const layerId = args[1] | ||||||
|  | 
 | ||||||
|  |     const themePath = "./assets/themes/"+themeId+"/"+themeId+".json" | ||||||
|  |     const contents = <LayoutConfigJson> JSON.parse(readFileSync(themePath, "UTF-8")) | ||||||
|  |     const layers = <LayerConfigJson[]> contents.layers.filter(l => { | ||||||
|  |         if(typeof l === "string"){ | ||||||
|  |             return false | ||||||
|  |         } | ||||||
|  |         if(l["override"] !== undefined){ | ||||||
|  |             return false | ||||||
|  |         } | ||||||
|  |         return true | ||||||
|  |     }) | ||||||
|  | 
 | ||||||
|  |     if(layers.length === 0){ | ||||||
|  |         console.log("No layers can be extracted from this theme. The "+contents.layers.length+" layers are already substituted layers") | ||||||
|  |         return | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const layerConfig = layers.find(l => l.id === layerId) | ||||||
|  |     if(layerId === undefined || layerConfig === undefined){ | ||||||
|  |         if(layerConfig === undefined){ | ||||||
|  |             console.error( "Layer "+layerId+" not found as inline layer") | ||||||
|  |         } | ||||||
|  |         console.log("Layers available for extraction are:") | ||||||
|  |         console.log(layers.map(l => l.id).join("\n")) | ||||||
|  |         return | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     const dir = "./assets/layers/"+layerId | ||||||
|  |     if(!existsSync(dir)){ | ||||||
|  |         mkdirSync(dir) | ||||||
|  |     } | ||||||
|  |     writeFileSync(dir+"/"+layerId+".json", JSON.stringify(layerConfig, null, "  ")) | ||||||
|  | 
 | ||||||
|  |     const index = contents.layers.findIndex(l => l["id"] === layerId) | ||||||
|  |     contents.layers[index] = layerId | ||||||
|  |     writeFileSync(themePath, JSON.stringify(contents, null, "  ")) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | main(process.argv.slice(2)) | ||||||
|  | @ -57,22 +57,6 @@ function addArticleToPresets(layerConfig: {presets?: {title: any}[]}){ | ||||||
|     //*/
 |     //*/
 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function extractInlineLayer(theme: LayoutConfigJson){ |  | ||||||
|     for (let i = 0; i < theme.layers.length; i++){ |  | ||||||
|         const layer = theme.layers[i]; |  | ||||||
|         if(typeof layer === "string"){ |  | ||||||
|             continue |  | ||||||
|         } |  | ||||||
|         if(layer["override"] !== undefined){ |  | ||||||
|             continue |  | ||||||
|         } |  | ||||||
|         const l = <LayerConfigJson> layer |  | ||||||
|         mkdirSync("./assets/layers/"+l.id) |  | ||||||
|         writeFileSync("./assets/layers/"+l.id+"/"+l.id+".json", JSON.stringify(l, null, "    ")) |  | ||||||
|         theme.layers[i] = l.id |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| const layerFiles = ScriptUtils.getLayerFiles(); | const layerFiles = ScriptUtils.getLayerFiles(); | ||||||
| for (const layerFile of layerFiles) { | for (const layerFile of layerFiles) { | ||||||
|     try { |     try { | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue