mapcomplete/scripts/generateLayouts.ts

225 lines
7.5 KiB
TypeScript
Raw Permalink Normal View History

2020-11-11 15:28:49 +00:00
// We HAVE to mark this while importing
2021-01-06 01:21:50 +00:00
import {Utils} from "../Utils";
Utils.runningFromConsole = true;
2020-11-11 15:28:49 +00:00
2021-01-06 01:21:50 +00:00
import LayoutConfig from "../Customizations/JSON/LayoutConfig";
import {existsSync, mkdirSync, readFileSync, writeFile, writeFileSync} from "fs";
2020-11-17 01:22:48 +00:00
import Locale from "../UI/i18n/Locale";
import Translations from "../UI/i18n/Translations";
import {Translation} from "../UI/i18n/Translation";
import Constants from "../Models/Constants";
2021-04-10 13:01:28 +00:00
import * as all_known_layouts from "../assets/generated/known_layers_and_themes.json"
import {LayoutConfigJson} from "../Customizations/JSON/LayoutConfigJson";
2021-03-17 13:17:33 +00:00
const sharp = require('sharp');
2021-01-06 01:21:50 +00:00
function enc(str: string): string {
return encodeURIComponent(str.toLowerCase());
}
const alreadyWritten = []
async function createIcon(iconPath: string, size: number) {
let name = iconPath.split(".").slice(0, -1).join(".");
2020-11-17 15:29:51 +00:00
if (name.startsWith("./")) {
name = name.substr(2)
}
const newname = `${name}${size}.png`
2020-11-06 02:17:27 +00:00
.replace(/\//g, "_")
.replace("assets_", "assets/generated/");
if (alreadyWritten.indexOf(newname) >= 0) {
return newname;
}
alreadyWritten.push(newname);
try {
readFileSync(newname);
return newname; // File already exists - nothing to do
} catch (e) {
// Errors are normal here if this file exists
}
try {
// We already read to file, in order to crash here if the file is not found
2020-11-06 02:17:27 +00:00
readFileSync(iconPath);
let img = await sharp(iconPath)
let resized = await img.resize(size)
await resized.toFile(newname)
} catch (e) {
console.error("Could not read icon", iconPath, "due to", e)
}
return newname;
2020-07-25 16:00:08 +00:00
}
async function createManifest(layout: LayoutConfig, relativePath: string) {
const name = layout.id;
2021-03-16 19:03:19 +00:00
Translation.forcedLanguage = "en"
const icons = [];
2020-08-22 01:15:42 +00:00
let icon = layout.icon;
2020-11-06 02:17:27 +00:00
if (icon.endsWith(".svg") || icon.startsWith("<svg") || icon.startsWith("<?xml")) {
// This is an svg. Lets create the needed pngs!
2020-11-06 02:17:27 +00:00
let path = layout.icon;
if (layout.icon.startsWith("<")) {
// THis is already the svg
2020-11-17 15:29:51 +00:00
path = "./assets/generated/" + layout.id + "_logo.svg"
2020-11-06 02:17:27 +00:00
writeFileSync(path, layout.icon)
}
const sizes = [72, 96, 120, 128, 144, 152, 180, 192, 384, 512];
for (const size of sizes) {
const name = await createIcon(path, size);
icons.push({
src: name,
sizes: size + "x" + size,
type: "image/png"
})
}
icons.push({
2020-11-06 02:17:27 +00:00
src: path,
sizes: "513x513",
type: "image/svg"
})
} else {
2020-11-06 02:17:27 +00:00
console.log(icon)
throw "Icon is not an svg for " + layout.id
}
const ogTitle = Translations.W(layout.title).InnerRender();
2020-08-22 01:15:42 +00:00
const ogDescr = Translations.W(layout.description ?? "").InnerRender();
2021-03-17 13:17:33 +00:00
return {
name: name,
short_name: ogTitle,
start_url: `${relativePath}/${layout.id.toLowerCase()}.html`,
display: "standalone",
background_color: "#fff",
description: ogDescr,
orientation: "portrait-primary, landscape-primary",
icons: icons
2021-03-17 13:17:33 +00:00
};
}
const template = readFileSync("index.html", "utf8");
2021-03-17 13:17:33 +00:00
async function createLandingPage(layout: LayoutConfig, manifest) {
2020-11-11 15:23:49 +00:00
Locale.language.setData(layout.language[0]);
const ogTitle = Translations.W(layout.title)?.InnerRender();
const ogDescr = Translations.W(layout.shortDescription ?? "Easily add and edit geodata with OpenStreetMap")?.InnerRender();
const ogImage = layout.socialImage;
let customCss = "";
if (layout.customCss !== undefined && layout.customCss !== "") {
try {
const cssContent = readFileSync(layout.customCss);
customCss = "<style>" + cssContent + "</style>";
} catch (e) {
customCss = `<link rel='stylesheet' href="${layout.customCss}"/>`
}
}
const og = `
2021-04-07 18:28:13 +00:00
<meta property="og:image" content="${ogImage ?? './assets/SocialImage.png'}">
<meta property="og:title" content="${ogTitle}">
<meta property="og:description" content="${ogDescr}">`
2020-11-06 02:17:27 +00:00
let icon = layout.icon;
if (icon.startsWith("<?xml") || icon.startsWith("<svg")) {
// This already is an svg
2020-11-17 15:29:51 +00:00
icon = `./assets/generated/${layout.id}_icon.svg`
2020-11-06 02:17:27 +00:00
writeFileSync(icon, layout.icon);
}
2021-03-17 13:17:33 +00:00
const apple_icons = []
for (const icon of manifest.icons) {
if(icon.type !== "image/png"){
continue;
}
apple_icons.push(`<link rel="apple-touch-icon" sizes="${icon.sizes}" href="${icon.src}">`)
}
2021-03-16 19:03:19 +00:00
let themeSpecific = [
`<title>${ogTitle}</title>`,
`<link rel="manifest" href="${enc(layout.id)}.webmanifest">`,
og,
customCss,
`<link rel="icon" href="assets/svg/add.svg" sizes="any" type="image/svg+xml">`,
`<link rel="icon" href="${icon}" sizes="any" type="image/svg+xml">`,
2021-03-17 13:17:33 +00:00
...apple_icons
2021-03-16 19:03:19 +00:00
].join("\n")
let output = template
.replace("Loading MapComplete, hang on...", `Loading MapComplete theme <i>${ogTitle}</i>...`)
2021-03-16 19:03:19 +00:00
.replace(/<!-- THEME-SPECIFIC -->.*<!-- THEME-SPECIFIC-END-->/s, themeSpecific);
try {
output = output
2020-11-06 02:17:27 +00:00
.replace(/<!-- DECORATION 0 START -->.*<!-- DECORATION 0 END -->/s, `<img src='${icon}' width="100%" height="100%">`)
.replace(/<!-- DECORATION 1 START -->.*<!-- DECORATION 1 END -->/s, `<img src='${icon}' width="100%" height="100%">`);
} catch (e) {
console.warn("Error while applying logo: ", e)
}
return output;
}
2020-11-17 15:29:51 +00:00
const generatedDir = "./assets/generated";
if (!existsSync(generatedDir)) {
2020-11-17 15:29:51 +00:00
mkdirSync(generatedDir)
}
const blacklist = ["", "test", ".", "..", "manifest", "index", "land", "preferences", "account", "openstreetmap", "custom"]
// @ts-ignore
2021-04-10 13:01:28 +00:00
const all : LayoutConfigJson[] = all_known_layouts.themes;
2020-09-17 17:06:32 +00:00
2021-04-10 13:01:28 +00:00
for (const i in all) {
const layoutConfigJson : LayoutConfigJson = all[i]
const layout = new LayoutConfig(layoutConfigJson, true, "generating layouts")
const layoutName = layout.id
if (blacklist.indexOf(layoutName.toLowerCase()) >= 0) {
console.log(`Skipping a layout with name${layoutName}, it is on the blacklist`);
continue;
}
const err = err => {
if (err !== null) {
console.log("Could not write manifest for ", layoutName, " because ", err)
}
};
createManifest(layout, "").then(manifObj => {
2021-02-28 22:46:47 +00:00
const manif = JSON.stringify(manifObj, undefined, 2);
const manifestLocation = encodeURIComponent(layout.id.toLowerCase()) + ".webmanifest";
writeFile(manifestLocation, manif, err);
2021-03-17 13:17:33 +00:00
// Create a landing page for the given theme
createLandingPage(layout, manifObj).then(landing => {
writeFile(enc(layout.id) + ".html", landing, err)
});
})
2021-03-17 13:17:33 +00:00
}
2020-10-25 16:26:15 +00:00
2021-03-16 19:03:19 +00:00
createManifest(new LayoutConfig({
2021-02-27 23:49:26 +00:00
icon: "./assets/svg/mapcomplete_logo.svg",
id: "index",
language: "en",
layers: [],
maintainer: "Pieter Vander Vennet",
startLat: 0,
startLon: 0,
startZoom: 0,
title: "MapComplete",
version: Constants.vNumber,
2021-03-16 19:03:19 +00:00
description: "MapComplete as a map viewer and editor which show thematic POI based on OpenStreetMap"
}), "").then(manifObj => {
const manif = JSON.stringify(manifObj, undefined, 2);
writeFileSync("index.manifest", manif)
})
2020-08-22 01:15:42 +00:00
console.log("Counting all translations")
2020-07-31 15:11:44 +00:00
Translations.CountTranslations();
2020-09-17 17:06:32 +00:00
console.log("All done!");