diff --git a/package.json b/package.json index 25ed7797..4d74aaa8 100644 --- a/package.json +++ b/package.json @@ -70,6 +70,7 @@ "make-zip": "web-ext build --source-dir build --overwrite-dest", "pretty": "bash scripts/pretty.sh", "run": "web-ext run -s build/ -u 'paste.to'", + "runwithprofile": "node scripts/runwithprofile.js", "test": "yarn run build && web-ext build --source-dir ./build --overwrite-dest && jest --silent", "update-buildsystem": "rm -rf src/node_modules; yarn run clean", "watch": "echo 'watch is broken, use build instead'; exit 0;", diff --git a/scripts/runwithprofile.js b/scripts/runwithprofile.js new file mode 100644 index 00000000..50857727 --- /dev/null +++ b/scripts/runwithprofile.js @@ -0,0 +1,45 @@ +#!/usr/bin/env node + +const { spawnSync } = require("child_process") +const fs = require("fs") +const os = require("os") +const path = require("path") + +const firefoxDir = + process.platform === "win32" + ? path.join(process.env.APPDATA || os.homedir(), "Mozilla", "Firefox") + : process.platform === "darwin" + ? path.join(os.homedir(), "Library", "Application Support", "Firefox") + : path.join(os.homedir(), ".mozilla", "firefox") + +function findDefaultProfile() { + const sections = [] + let section + for (const line of fs.readFileSync(path.join(firefoxDir, "profiles.ini"), "utf8").split(/\r?\n/)) { + const header = line.trim().match(/^\[([^\]]+)\]$/) + if (header) { + section = { name: header[1] } + sections.push(section) + } else if (section) { + const separator = line.indexOf("=") + if (separator > 0) section[line.slice(0, separator)] = line.slice(separator + 1) + } + } + const profilePath = + sections.find(section => section.name.startsWith("Profile") && section.Name === "default-release")?.Path ?? + sections.find(section => section.name.startsWith("Install") && section.Default)?.Default ?? + sections.find(section => section.name.startsWith("Profile") && section.Default === "1")?.Path + if (!profilePath) throw new Error(`No default profile found in ${firefoxDir}`) + return path.isAbsolute(profilePath) ? profilePath : path.resolve(firefoxDir, profilePath) +} + +try { + const profile = process.argv[2] || findDefaultProfile() + const command = process.platform === "win32" ? "web-ext.cmd" : "web-ext" + const result = spawnSync(command, ["run", "--source-dir", "build/", "--firefox", "deved", "--pre-install", "--firefox-profile", profile, "--no-reload", "--pref", "browser.startup.page=3", "--pref", "browser.sessionstore.resume_from_crash=true"], { stdio: "inherit" }) + if (result.error) throw result.error + process.exitCode = result.status ?? 1 +} catch (error) { + console.error(`runwithprofile: ${error.message}`) + process.exitCode = 1 +}