tutanota/buildSrc/createHtml.js

55 lines
1.8 KiB
JavaScript
Raw Normal View History

/**
* Utility to create the HTML landing page for the app.
*/
import fs from "fs-extra"
import { renderHtml } from "./LaunchHtml.js"
import { mkdir } from "node:fs/promises"
import path from "node:path"
/**
*
* @param env Object with the following keys:
* mode: String of value "App" or "Desktop" or "Browser"
* dist: Boolean
* staticUrl: String defining app base url for non-production environments and the native clients.
* versionNumber: String containing the app's version number
2024-07-30 10:57:18 +02:00
* @param app App to be built, defaults to mail app {String}
* @returns {Promise<Awaited<void>[]>}
*/
2024-07-30 10:57:18 +02:00
export async function createHtml(env, app = "mail") {
let jsFileName
let htmlFileName
2024-07-30 10:57:18 +02:00
const buildDir = app === "calendar" ? "build-calendar-app" : "build"
switch (env.mode) {
case "App":
jsFileName = "index-app.js"
htmlFileName = "index-app.html"
break
case "Browser":
jsFileName = "index.js"
htmlFileName = "index.html"
break
case "Desktop":
jsFileName = "index-desktop.js"
htmlFileName = "index-desktop.html"
}
// We need to import bluebird early as it Promise must be replaced before any of our code is executed
const imports = [{ src: "polyfill.js" }, { src: jsFileName }]
let indexTemplate = await fs.readFile("./buildSrc/index.template.js", "utf8")
2024-07-30 10:57:18 +02:00
if (app === "calendar") indexTemplate = indexTemplate.replaceAll("app.js", "calendar-app.js")
const index = `window.whitelabelCustomizations = null
window.env = ${JSON.stringify(env, null, 2)}
${indexTemplate}`
2024-07-30 10:57:18 +02:00
return Promise.all([
_writeFile(`./${buildDir}/${jsFileName}`, index),
renderHtml(imports, env).then((content) => _writeFile(`./${buildDir}/${htmlFileName}`, content)),
2024-07-30 10:57:18 +02:00
])
}
async function _writeFile(targetFile, content) {
await mkdir(path.dirname(targetFile), { recursive: true })
2022-12-27 15:37:40 +01:00
await fs.writeFile(targetFile, content, "utf-8")
}