Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

scripts/e2e_android.mjs

Raw
#!/usr/bin/env node
import { spawnSync } from "node:child_process";
import { createHash } from "node:crypto";
import { mkdirSync } from "node:fs";
import { homedir } from "node:os";
import { dirname, posix } from "node:path";
import process from "node:process";
import { fileURLToPath, pathToFileURL } from "node:url";

const ROOT = dirname(dirname(fileURLToPath(import.meta.url)));
const DISTRO = "debian";
const LINUX_MISE = "/usr/local/bin/mise";

export function linuxModulesDir(root, home = homedir()) {
	const key = createHash("sha256").update(root).digest("hex").slice(0, 12);
	return posix.join(home, ".cache", "pi-ext", "e2e-linux", key, "node_modules");
}

export function prootArgs(root, modulesDir) {
	return [
		"login",
		"--bind",
		`${modulesDir}:${posix.join(root, "node_modules")}`,
		"--work-dir",
		root,
		DISTRO,
		"--",
		"env",
		"CI=true",
		LINUX_MISE,
		"run",
		"e2e",
	];
}

export function main() {
	if (process.platform !== "android") {
		console.error("scripts/e2e_android.mjs runs only on Android");
		return 2;
	}

	const modulesDir = linuxModulesDir(ROOT);
	mkdirSync(modulesDir, { recursive: true });
	const result = spawnSync("proot-distro", prootArgs(ROOT, modulesDir), {
		stdio: "inherit",
	});
	if (result.error) {
		console.error(`Could not start Debian PRoot: ${result.error.message}`);
		return 1;
	}
	if (result.status === 127) {
		console.error(`Debian requires Linux mise at ${LINUX_MISE}`);
	}
	return result.status ?? 1;
}

if (
	process.argv[1] &&
	import.meta.url === pathToFileURL(process.argv[1]).href
) {
	process.exitCode = main();
}