2022-02-14 10:19:20 +01:00
|
|
|
/**
|
|
|
|
|
* This file contains some utilities used from various build scripts in this directory.
|
|
|
|
|
*/
|
2023-04-19 13:58:49 +02:00
|
|
|
import fs from "node:fs/promises"
|
2023-04-20 17:14:30 +02:00
|
|
|
import path, { dirname } from "node:path"
|
|
|
|
|
import { fileURLToPath } from "node:url"
|
|
|
|
|
import stream from "node:stream"
|
2022-02-14 10:19:20 +01:00
|
|
|
|
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
|
|
|
|
|
|
|
|
/** global used by the measure() function to mark the start of measurement **/
|
|
|
|
|
var measureStartTime
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns tutanota app version (as in package.json).
|
2024-07-05 19:02:03 +02:00
|
|
|
* @returns {Promise<string>}
|
2022-02-14 10:19:20 +01:00
|
|
|
*/
|
2023-04-19 13:58:49 +02:00
|
|
|
export async function getTutanotaAppVersion() {
|
|
|
|
|
const packageJson = JSON.parse(await fs.readFile(path.join(__dirname, "..", "package.json"), "utf8"))
|
2022-03-02 18:21:01 +01:00
|
|
|
return packageJson.version.trim()
|
2022-02-14 10:19:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the elapsed time between the last and current call of measure().
|
|
|
|
|
* @returns {number}
|
|
|
|
|
*/
|
|
|
|
|
export function measure() {
|
|
|
|
|
if (!measureStartTime) {
|
|
|
|
|
measureStartTime = Date.now()
|
|
|
|
|
}
|
|
|
|
|
return (Date.now() - measureStartTime) / 1000
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the (absolute) path to the default dist directory/prefix.
|
|
|
|
|
* @returns {string}
|
|
|
|
|
*/
|
|
|
|
|
export function getDefaultDistDirectory() {
|
2023-10-05 10:51:49 +02:00
|
|
|
return path.resolve("build")
|
2022-02-14 10:19:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Throws if result has a value other than 0. **/
|
|
|
|
|
export function exitOnFail(result) {
|
|
|
|
|
if (result.status !== 0) {
|
|
|
|
|
throw new Error("error invoking process" + JSON.stringify(result))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-02 18:21:01 +01:00
|
|
|
/**
|
|
|
|
|
* Utility for writing to a logging function when a Writable is expected
|
|
|
|
|
*/
|
|
|
|
|
export class LogWriter extends stream.Writable {
|
|
|
|
|
/**
|
|
|
|
|
* @param logger {(string) => void}
|
|
|
|
|
*/
|
|
|
|
|
constructor(logger) {
|
|
|
|
|
super({
|
|
|
|
|
autoDestroy: true,
|
|
|
|
|
write(chunk, encoding, callback) {
|
|
|
|
|
logger(chunk.toString().trim())
|
|
|
|
|
callback()
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if a file exists and is a normal file
|
|
|
|
|
* @param filePath {string}
|
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
|
*/
|
|
|
|
|
export async function fileExists(filePath) {
|
2022-12-27 15:37:40 +01:00
|
|
|
return fs
|
|
|
|
|
.stat(filePath)
|
|
|
|
|
.then((stats) => stats.isFile())
|
|
|
|
|
.catch(() => false)
|
2022-03-02 18:21:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* There are various possibilities for how a given platform could be identified
|
|
|
|
|
* We need to make sure to be consistent at certain points, such as when caching files or processing CLI args
|
2024-07-05 19:02:03 +02:00
|
|
|
* @param platformName {string}
|
2022-03-02 18:21:01 +01:00
|
|
|
* @returns {"darwin"|"win32"|"linux"}
|
|
|
|
|
*/
|
|
|
|
|
export function getCanonicalPlatformName(platformName) {
|
|
|
|
|
switch (platformName) {
|
|
|
|
|
case "mac":
|
|
|
|
|
case "darwin":
|
|
|
|
|
return "darwin"
|
|
|
|
|
case "win":
|
|
|
|
|
case "win32":
|
|
|
|
|
return "win32"
|
|
|
|
|
case "linux":
|
|
|
|
|
return "linux"
|
|
|
|
|
default:
|
|
|
|
|
throw new Error(`Unknown platform name ${platformName}`)
|
|
|
|
|
}
|
2022-05-10 12:07:23 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-17 17:51:46 +01:00
|
|
|
/**
|
|
|
|
|
* Checks whether the combination of OS & architecture is supported by the build system
|
|
|
|
|
* @param platformName {"darwin"|"win32"|"linux"}
|
2023-11-20 08:37:27 +01:00
|
|
|
* @param architecture {"arm"|"arm64"|"ia32"|"mips"|"mipsel"|"ppc"|"ppc64"|"riscv64"|"s390"|"s390x"|"x64"|"universal"}
|
2023-11-17 17:51:46 +01:00
|
|
|
* @returns {boolean}
|
|
|
|
|
*/
|
|
|
|
|
export function checkArchitectureIsSupported(platformName, architecture) {
|
|
|
|
|
switch (architecture) {
|
|
|
|
|
case "x64":
|
|
|
|
|
return true
|
|
|
|
|
case "arm64":
|
2023-11-20 08:37:27 +01:00
|
|
|
case "universal":
|
2023-11-17 17:51:46 +01:00
|
|
|
return platformName === "darwin"
|
|
|
|
|
default:
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-10 12:07:23 +02:00
|
|
|
export async function runStep(name, cmd) {
|
|
|
|
|
const before = Date.now()
|
|
|
|
|
console.log("Build >", name)
|
|
|
|
|
await cmd()
|
|
|
|
|
console.log("Build >", name, "took", Date.now() - before, "ms")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function writeFile(targetFile, content) {
|
2022-12-27 15:37:40 +01:00
|
|
|
return fs.mkdir(path.dirname(targetFile), { recursive: true }).then(() => fs.writeFile(targetFile, content, "utf-8"))
|
2022-05-27 16:17:01 +02:00
|
|
|
}
|