@xlinklight/browser-extension-tools 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/shared/js/chrome-runtime.js +79 -0
- package/dist/assets/shared/js/extension-account-menu.js +212 -0
- package/dist/assets/shared/js/extension-i18n.js +35 -0
- package/dist/assets/shared/js/extension-session-store.js +40 -0
- package/dist/assets/shared/js/extension-tabs.js +27 -0
- package/dist/assets/shared/js/link-light-api-client.js +30 -0
- package/dist/assets/shared/js/side-panel-account.js +14 -0
- package/dist/assets/shared/js/side-panel-api.js +13 -0
- package/dist/assets/shared/js/side-panel-bootstrap.js +97 -0
- package/dist/assets/shared/js/side-panel-dom.js +14 -0
- package/dist/assets/shared/js/side-panel-i18n.js +34 -0
- package/dist/assets/shared/js/side-panel-import-preview.js +7 -0
- package/dist/assets/shared/js/side-panel-status.js +12 -0
- package/dist/assets/shared/js/side-panel-tabs.js +29 -0
- package/dist/assets/shared/js/side-panel-translations.js +30 -0
- package/dist/src/chrome-discovery.d.ts +57 -0
- package/dist/src/chrome-discovery.js +178 -0
- package/dist/src/chrome-for-testing.d.ts +66 -0
- package/dist/src/chrome-for-testing.js +157 -0
- package/dist/src/chrome-install-worker.d.ts +1 -0
- package/dist/src/chrome-install-worker.js +8 -0
- package/dist/src/cli.d.ts +41 -0
- package/dist/src/cli.js +189 -0
- package/dist/src/dev-cli.d.ts +2 -0
- package/dist/src/dev-cli.js +11 -0
- package/dist/src/dev-extension.d.ts +51 -0
- package/dist/src/dev-extension.js +307 -0
- package/dist/src/fs-error.d.ts +1 -0
- package/dist/src/fs-error.js +3 -0
- package/dist/src/package-cli.d.ts +2 -0
- package/dist/src/package-cli.js +9 -0
- package/dist/src/package.d.ts +15 -0
- package/dist/src/package.js +203 -0
- package/dist/src/profile.d.ts +34 -0
- package/dist/src/profile.js +105 -0
- package/dist/src/reload-server.d.ts +14 -0
- package/dist/src/reload-server.js +96 -0
- package/{src/run.mjs → dist/src/run.d.ts} +7 -7
- package/dist/src/run.js +7 -0
- package/dist/src/watcher.d.ts +17 -0
- package/dist/src/watcher.js +30 -0
- package/package.json +33 -11
- package/assets/shared/js/chrome-runtime.js +0 -87
- package/assets/shared/js/extension-account-menu.js +0 -239
- package/assets/shared/js/extension-i18n.js +0 -42
- package/assets/shared/js/extension-session-store.js +0 -42
- package/assets/shared/js/extension-tabs.js +0 -23
- package/assets/shared/js/link-light-api-client.js +0 -31
- package/assets/shared/js/side-panel-account.js +0 -14
- package/assets/shared/js/side-panel-api.js +0 -13
- package/assets/shared/js/side-panel-bootstrap.js +0 -127
- package/assets/shared/js/side-panel-dom.js +0 -15
- package/assets/shared/js/side-panel-i18n.js +0 -37
- package/assets/shared/js/side-panel-import-preview.js +0 -7
- package/assets/shared/js/side-panel-status.js +0 -13
- package/assets/shared/js/side-panel-tabs.js +0 -35
- package/assets/shared/js/side-panel-translations.js +0 -41
- package/src/chrome-discovery.mjs +0 -231
- package/src/chrome-for-testing.mjs +0 -203
- package/src/chrome-install-worker.mjs +0 -8
- package/src/cli.mjs +0 -171
- package/src/dev-cli.mjs +0 -9
- package/src/dev-extension.mjs +0 -350
- package/src/package-cli.mjs +0 -7
- package/src/package.mjs +0 -223
- package/src/profile.mjs +0 -144
- package/src/reload-server.mjs +0 -108
- package/src/watcher.mjs +0 -45
- /package/{assets → dist/assets}/shared/css/side-panel.css +0 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(function initializeLinkLightSidePanelTranslations(global) {
|
|
3
|
+
function applySidePanelTranslations({ documentRef = global.document, t, locale, titleKey = "extensionName", languageInput, languagePreference, safetyNote, onAfterStaticTranslations, }) {
|
|
4
|
+
documentRef.documentElement.lang = locale;
|
|
5
|
+
documentRef.title = t(titleKey);
|
|
6
|
+
for (const element of documentRef.querySelectorAll("[data-i18n]")) {
|
|
7
|
+
element.textContent = t(element.dataset.i18n);
|
|
8
|
+
}
|
|
9
|
+
for (const element of documentRef.querySelectorAll("[data-i18n-placeholder]")) {
|
|
10
|
+
element.setAttribute("placeholder", t(element.dataset.i18nPlaceholder));
|
|
11
|
+
}
|
|
12
|
+
for (const element of documentRef.querySelectorAll("[data-i18n-aria-label]")) {
|
|
13
|
+
element.setAttribute("aria-label", t(element.dataset.i18nAriaLabel));
|
|
14
|
+
}
|
|
15
|
+
for (const element of documentRef.querySelectorAll("[data-i18n-title]")) {
|
|
16
|
+
const values = {};
|
|
17
|
+
if (element.dataset.i18nTitleFeatureKey) {
|
|
18
|
+
values.feature = t(element.dataset.i18nTitleFeatureKey);
|
|
19
|
+
}
|
|
20
|
+
element.setAttribute("title", t(element.dataset.i18nTitle, values));
|
|
21
|
+
}
|
|
22
|
+
if (safetyNote)
|
|
23
|
+
safetyNote.textContent = t("safetyNote");
|
|
24
|
+
if (languageInput)
|
|
25
|
+
languageInput.value = languagePreference;
|
|
26
|
+
if (typeof onAfterStaticTranslations === "function")
|
|
27
|
+
onAfterStaticTranslations();
|
|
28
|
+
}
|
|
29
|
+
global.LinkLightSidePanelTranslations = { applySidePanelTranslations };
|
|
30
|
+
})(globalThis);
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
interface ChromeLaunchOptions {
|
|
3
|
+
argv?: string[];
|
|
4
|
+
env?: NodeJS.ProcessEnv;
|
|
5
|
+
existsSync?: typeof existsSync;
|
|
6
|
+
platform?: NodeJS.Platform;
|
|
7
|
+
rootDir?: string;
|
|
8
|
+
extensionRoot?: string;
|
|
9
|
+
extensionName?: string;
|
|
10
|
+
extensionNames?: string[];
|
|
11
|
+
appUrl?: string;
|
|
12
|
+
profileDir?: string;
|
|
13
|
+
}
|
|
14
|
+
export declare function normalizeChromeExtensionPath(extensionPath: string, platform: NodeJS.Platform): string;
|
|
15
|
+
export declare function getChromeExtensionIdForPath(extensionPath: string, { platform }?: {
|
|
16
|
+
platform?: NodeJS.Platform | undefined;
|
|
17
|
+
}): string;
|
|
18
|
+
export declare function getChromeExecutableName(platform: NodeJS.Platform): "chrome" | "chrome.exe" | "Google Chrome for Testing";
|
|
19
|
+
export declare function findExecutableBelow(directory: string, executableName: string): string | null;
|
|
20
|
+
export declare function findLocalChromeForTesting({ rootDir, platform, exists, }?: {
|
|
21
|
+
rootDir?: string | undefined;
|
|
22
|
+
platform?: NodeJS.Platform | undefined;
|
|
23
|
+
exists?: typeof existsSync | undefined;
|
|
24
|
+
}): string | null;
|
|
25
|
+
export declare function isLikelyGoogleChromeOrChromeForTesting(chromePath: string): boolean;
|
|
26
|
+
export declare function candidateChromePaths({ browser, env, platform }: {
|
|
27
|
+
browser: 'chrome' | null;
|
|
28
|
+
env: NodeJS.ProcessEnv;
|
|
29
|
+
platform: NodeJS.Platform;
|
|
30
|
+
}): string[];
|
|
31
|
+
export declare function findChromePath({ argv, env, exists, platform, rootDir, }?: {
|
|
32
|
+
argv?: string[] | undefined;
|
|
33
|
+
env?: NodeJS.ProcessEnv | undefined;
|
|
34
|
+
exists?: typeof existsSync | undefined;
|
|
35
|
+
platform?: NodeJS.Platform | undefined;
|
|
36
|
+
rootDir?: string | undefined;
|
|
37
|
+
}): string;
|
|
38
|
+
export declare function isLikelyBrandedChrome(chromePath: string): boolean;
|
|
39
|
+
export declare function buildChromeLaunchConfig({ argv, env, existsSync: exists, platform, rootDir, extensionRoot, extensionName, extensionNames, appUrl, profileDir, }?: ChromeLaunchOptions): {
|
|
40
|
+
autoLoadSupported: false;
|
|
41
|
+
chromePath: string;
|
|
42
|
+
launchExtensionDirs: string[];
|
|
43
|
+
args: string[];
|
|
44
|
+
manualLoadPath: string;
|
|
45
|
+
resetProfile: boolean;
|
|
46
|
+
unsupportedAutoLoadReason: string;
|
|
47
|
+
} | {
|
|
48
|
+
autoLoadSupported: true;
|
|
49
|
+
chromePath: string;
|
|
50
|
+
launchExtensionDirs: string[];
|
|
51
|
+
args: string[];
|
|
52
|
+
resetProfile: boolean;
|
|
53
|
+
manualLoadPath?: undefined;
|
|
54
|
+
unsupportedAutoLoadReason?: undefined;
|
|
55
|
+
};
|
|
56
|
+
export declare function samePath(left: string, right: string): boolean;
|
|
57
|
+
export {};
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { existsSync, readdirSync, statSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { isMissingFile } from "./fs-error.js";
|
|
5
|
+
import { chromeExtensionsUrl, getArgValue, getChromeLaunchExtensionDirs, getProfileDirForExtensions, getProjectPaths, getRequestedAppUrl, getRequestedBrowser, getRequestedExtensions } from "./cli.js";
|
|
6
|
+
export function normalizeChromeExtensionPath(extensionPath, platform) {
|
|
7
|
+
const pathApi = platform === "win32" ? path.win32 : path.posix;
|
|
8
|
+
const resolvedPath = pathApi.resolve(extensionPath);
|
|
9
|
+
if (platform !== "win32") {
|
|
10
|
+
return resolvedPath;
|
|
11
|
+
}
|
|
12
|
+
return resolvedPath.replace(/^([a-z]):/, (_, driveLetter) => `${driveLetter.toUpperCase()}:`);
|
|
13
|
+
}
|
|
14
|
+
export function getChromeExtensionIdForPath(extensionPath, { platform = process.platform } = {}) {
|
|
15
|
+
const normalizedPath = normalizeChromeExtensionPath(extensionPath, platform);
|
|
16
|
+
const pathBytes = platform === "win32" ? Buffer.from(normalizedPath, "utf16le") : Buffer.from(normalizedPath);
|
|
17
|
+
const digest = createHash("sha256").update(pathBytes).digest("hex").slice(0, 32);
|
|
18
|
+
return digest.replace(/[0-9a-f]/g, (character) => "abcdefghijklmnop"[Number.parseInt(character, 16)]);
|
|
19
|
+
}
|
|
20
|
+
export function getChromeExecutableName(platform) {
|
|
21
|
+
if (platform === "win32")
|
|
22
|
+
return "chrome.exe";
|
|
23
|
+
if (platform === "darwin")
|
|
24
|
+
return "Google Chrome for Testing";
|
|
25
|
+
return "chrome";
|
|
26
|
+
}
|
|
27
|
+
export function findExecutableBelow(directory, executableName) {
|
|
28
|
+
const entries = readdirSync(directory, { withFileTypes: true });
|
|
29
|
+
for (const entry of entries) {
|
|
30
|
+
const entryPath = path.join(directory, entry.name);
|
|
31
|
+
if (entry.isFile() && entry.name === executableName) {
|
|
32
|
+
return entryPath;
|
|
33
|
+
}
|
|
34
|
+
if (entry.isDirectory()) {
|
|
35
|
+
const foundPath = findExecutableBelow(entryPath, executableName);
|
|
36
|
+
if (foundPath)
|
|
37
|
+
return foundPath;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
export function findLocalChromeForTesting({ rootDir = process.cwd(), platform = process.platform, exists = existsSync, } = {}) {
|
|
43
|
+
const { chromeForTestingDir } = getProjectPaths(rootDir);
|
|
44
|
+
if (!exists(chromeForTestingDir)) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
if (!statSync(chromeForTestingDir).isDirectory()) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
return findExecutableBelow(chromeForTestingDir, getChromeExecutableName(platform));
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
if (isMissingFile(error)) {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export function isLikelyGoogleChromeOrChromeForTesting(chromePath) {
|
|
61
|
+
const normalizedPath = chromePath.toLowerCase().replaceAll("\\", "/");
|
|
62
|
+
return (normalizedPath.includes("chrome for testing") ||
|
|
63
|
+
normalizedPath.includes("chrome-for-testing") ||
|
|
64
|
+
normalizedPath.includes("chrome-win64") ||
|
|
65
|
+
normalizedPath.includes("/google/chrome/application/chrome.exe") ||
|
|
66
|
+
normalizedPath.endsWith("/google chrome"));
|
|
67
|
+
}
|
|
68
|
+
export function candidateChromePaths({ browser, env, platform }) {
|
|
69
|
+
if (platform === "win32") {
|
|
70
|
+
if (browser === "chrome") {
|
|
71
|
+
return [
|
|
72
|
+
env.CHROME_FOR_TESTING_PATH,
|
|
73
|
+
env.LOCALAPPDATA && path.join(env.LOCALAPPDATA, "Google", "Chrome", "Application", "chrome.exe"),
|
|
74
|
+
env.PROGRAMFILES && path.join(env.PROGRAMFILES, "Google", "Chrome", "Application", "chrome.exe"),
|
|
75
|
+
env["PROGRAMFILES(X86)"] && path.join(env["PROGRAMFILES(X86)"], "Google", "Chrome", "Application", "chrome.exe"),
|
|
76
|
+
String.raw `C:\Program Files\Google\Chrome\Application\chrome.exe`,
|
|
77
|
+
String.raw `C:\Program Files (x86)\Google\Chrome\Application\chrome.exe`,
|
|
78
|
+
].filter((candidate) => Boolean(candidate));
|
|
79
|
+
}
|
|
80
|
+
return [
|
|
81
|
+
env.CHROME_FOR_TESTING_PATH,
|
|
82
|
+
env.LOCALAPPDATA && path.join(env.LOCALAPPDATA, "Chromium", "Application", "chrome.exe"),
|
|
83
|
+
env.PROGRAMFILES && path.join(env.PROGRAMFILES, "Chromium", "Application", "chrome.exe"),
|
|
84
|
+
env["PROGRAMFILES(X86)"] && path.join(env["PROGRAMFILES(X86)"], "Chromium", "Application", "chrome.exe"),
|
|
85
|
+
env.LOCALAPPDATA && path.join(env.LOCALAPPDATA, "Microsoft", "Edge", "Application", "msedge.exe"),
|
|
86
|
+
env.PROGRAMFILES && path.join(env.PROGRAMFILES, "Microsoft", "Edge", "Application", "msedge.exe"),
|
|
87
|
+
env["PROGRAMFILES(X86)"] && path.join(env["PROGRAMFILES(X86)"], "Microsoft", "Edge", "Application", "msedge.exe"),
|
|
88
|
+
env.LOCALAPPDATA && path.join(env.LOCALAPPDATA, "Google", "Chrome", "Application", "chrome.exe"),
|
|
89
|
+
env.PROGRAMFILES && path.join(env.PROGRAMFILES, "Google", "Chrome", "Application", "chrome.exe"),
|
|
90
|
+
env["PROGRAMFILES(X86)"] && path.join(env["PROGRAMFILES(X86)"], "Google", "Chrome", "Application", "chrome.exe"),
|
|
91
|
+
String.raw `C:\Program Files\Microsoft\Edge\Application\msedge.exe`,
|
|
92
|
+
String.raw `C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe`,
|
|
93
|
+
String.raw `C:\Program Files\Google\Chrome\Application\chrome.exe`,
|
|
94
|
+
String.raw `C:\Program Files (x86)\Google\Chrome\Application\chrome.exe`,
|
|
95
|
+
].filter((candidate) => Boolean(candidate));
|
|
96
|
+
}
|
|
97
|
+
if (platform === "darwin") {
|
|
98
|
+
return ["/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"];
|
|
99
|
+
}
|
|
100
|
+
if (browser === "chrome") {
|
|
101
|
+
return ["/usr/bin/google-chrome", "/usr/bin/google-chrome-stable"];
|
|
102
|
+
}
|
|
103
|
+
return ["/usr/bin/chromium", "/snap/bin/chromium", "/usr/bin/microsoft-edge", "/usr/bin/google-chrome", "/usr/bin/google-chrome-stable"];
|
|
104
|
+
}
|
|
105
|
+
export function findChromePath({ argv = process.argv.slice(2), env = process.env, exists = existsSync, platform = process.platform, rootDir = process.cwd(), } = {}) {
|
|
106
|
+
const requestedBrowser = getRequestedBrowser(argv);
|
|
107
|
+
const explicitChromePath = getArgValue(argv, "--chrome-path") || env.CHROME_FOR_TESTING_PATH;
|
|
108
|
+
if (explicitChromePath?.trim()) {
|
|
109
|
+
return explicitChromePath.trim();
|
|
110
|
+
}
|
|
111
|
+
const localChromeForTestingPath = findLocalChromeForTesting({ rootDir, platform, exists });
|
|
112
|
+
if (localChromeForTestingPath) {
|
|
113
|
+
return localChromeForTestingPath;
|
|
114
|
+
}
|
|
115
|
+
if (env.CHROME_PATH?.trim() &&
|
|
116
|
+
(!requestedBrowser || isLikelyGoogleChromeOrChromeForTesting(env.CHROME_PATH.trim()))) {
|
|
117
|
+
return env.CHROME_PATH.trim();
|
|
118
|
+
}
|
|
119
|
+
const foundPath = candidateChromePaths({ browser: requestedBrowser, env, platform }).find((candidate) => exists(candidate));
|
|
120
|
+
if (foundPath) {
|
|
121
|
+
return foundPath;
|
|
122
|
+
}
|
|
123
|
+
throw new Error("Chrome executable was not found. Set CHROME_PATH or pass --chrome-path=<path-to-chrome>.");
|
|
124
|
+
}
|
|
125
|
+
export function isLikelyBrandedChrome(chromePath) {
|
|
126
|
+
const normalizedPath = chromePath.toLowerCase().replaceAll("\\", "/");
|
|
127
|
+
if (normalizedPath.includes("chrome for testing") || normalizedPath.includes("chrome-for-testing")) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
if (normalizedPath.includes("chrome-win64") || normalizedPath.includes("chromium") || normalizedPath.includes("/microsoft/edge/")) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
return normalizedPath.includes("/google/chrome/application/chrome.exe") || normalizedPath.endsWith("/google chrome");
|
|
134
|
+
}
|
|
135
|
+
export function buildChromeLaunchConfig({ argv = process.argv.slice(2), env = process.env, existsSync: exists = existsSync, platform = process.platform, rootDir = process.cwd(), extensionRoot = getArgValue(argv, "--extension-root"), extensionName, extensionNames = extensionName ? [extensionName] : getRequestedExtensions(argv, rootDir), appUrl = getRequestedAppUrl(argv, env), profileDir = getProfileDirForExtensions(rootDir, extensionNames, extensionRoot), } = {}) {
|
|
136
|
+
const launchExtensionDirs = getChromeLaunchExtensionDirs({ extensionNames, rootDir, extensionRoot });
|
|
137
|
+
const launchExtensionArg = launchExtensionDirs.join(",");
|
|
138
|
+
const url = getArgValue(argv, "--url") || appUrl;
|
|
139
|
+
const chromePath = findChromePath({ argv, env, exists, platform, rootDir });
|
|
140
|
+
const autoLoadSupported = argv.includes("--allow-branded-chrome") || !isLikelyBrandedChrome(chromePath);
|
|
141
|
+
const baseArgs = [
|
|
142
|
+
`--user-data-dir=${profileDir}`,
|
|
143
|
+
"--new-window",
|
|
144
|
+
"--no-first-run",
|
|
145
|
+
"--no-default-browser-check",
|
|
146
|
+
];
|
|
147
|
+
if (!autoLoadSupported) {
|
|
148
|
+
return {
|
|
149
|
+
autoLoadSupported,
|
|
150
|
+
chromePath,
|
|
151
|
+
launchExtensionDirs,
|
|
152
|
+
args: [...baseArgs, chromeExtensionsUrl, url],
|
|
153
|
+
manualLoadPath: launchExtensionDirs.join(", "),
|
|
154
|
+
resetProfile: argv.includes("--reset-profile"),
|
|
155
|
+
unsupportedAutoLoadReason: "Automatic unpacked extension loading is disabled in current branded Chrome builds. Use Chrome for Testing for one-command loading, or load the unpacked extension manually.",
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
autoLoadSupported,
|
|
160
|
+
chromePath,
|
|
161
|
+
launchExtensionDirs,
|
|
162
|
+
args: [
|
|
163
|
+
...baseArgs,
|
|
164
|
+
`--load-extension=${launchExtensionArg}`,
|
|
165
|
+
`--disable-extensions-except=${launchExtensionArg}`,
|
|
166
|
+
chromeExtensionsUrl,
|
|
167
|
+
url,
|
|
168
|
+
],
|
|
169
|
+
resetProfile: argv.includes("--reset-profile"),
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
export function samePath(left, right) {
|
|
173
|
+
const resolvedLeft = path.resolve(left);
|
|
174
|
+
const resolvedRight = path.resolve(right);
|
|
175
|
+
return process.platform === "win32"
|
|
176
|
+
? resolvedLeft.toLowerCase() === resolvedRight.toLowerCase()
|
|
177
|
+
: resolvedLeft === resolvedRight;
|
|
178
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { StdioOptions } from "node:child_process";
|
|
2
|
+
import type { CommandRunner } from "@openge/forge-command-runner";
|
|
3
|
+
interface InstallResult {
|
|
4
|
+
error?: Error;
|
|
5
|
+
stdout?: string | Uint8Array | null;
|
|
6
|
+
stderr?: string | Uint8Array | null;
|
|
7
|
+
status?: number | null;
|
|
8
|
+
}
|
|
9
|
+
interface InstallOptions {
|
|
10
|
+
env?: NodeJS.ProcessEnv;
|
|
11
|
+
rootDir?: string;
|
|
12
|
+
platform?: NodeJS.Platform;
|
|
13
|
+
maxAttempts?: number;
|
|
14
|
+
logger?: (message: string) => void;
|
|
15
|
+
stdio?: StdioOptions;
|
|
16
|
+
installRunner?: (command: string, args: string[], options: {
|
|
17
|
+
stdio: StdioOptions;
|
|
18
|
+
shell: boolean;
|
|
19
|
+
}) => InstallResult;
|
|
20
|
+
}
|
|
21
|
+
interface DevInstallOptions extends InstallOptions {
|
|
22
|
+
signal?: AbortSignal;
|
|
23
|
+
commandRunner?: Pick<ReturnType<typeof createInstallerCommandRunner>, "run">;
|
|
24
|
+
}
|
|
25
|
+
declare function createInstallerCommandRunner(platform: NodeJS.Platform): CommandRunner;
|
|
26
|
+
export declare function trimOutput(value: string, maxLength?: number): string;
|
|
27
|
+
export declare function stringifyProcessOutput(value: unknown): string;
|
|
28
|
+
export declare function getProcessOutputErrorReason(result: InstallResult): string;
|
|
29
|
+
export declare function formatChromeForTestingInstallFailure(result: InstallResult): string;
|
|
30
|
+
export declare function getPackageRunner({ env, platform }: {
|
|
31
|
+
env: NodeJS.ProcessEnv;
|
|
32
|
+
platform: NodeJS.Platform;
|
|
33
|
+
}): {
|
|
34
|
+
command: string;
|
|
35
|
+
argsPrefix: string[];
|
|
36
|
+
};
|
|
37
|
+
export declare function getChromeForTestingInstallStep({ env, platform, rootDir, }?: {
|
|
38
|
+
env?: NodeJS.ProcessEnv | undefined;
|
|
39
|
+
platform?: NodeJS.Platform | undefined;
|
|
40
|
+
rootDir?: string | undefined;
|
|
41
|
+
}): {
|
|
42
|
+
command: string;
|
|
43
|
+
args: string[];
|
|
44
|
+
};
|
|
45
|
+
export declare function getChromeForTestingFallbackBuildId(env?: NodeJS.ProcessEnv): string;
|
|
46
|
+
export declare function getChromeForTestingDirectInstallStep({ env, platform, rootDir, }?: {
|
|
47
|
+
env?: NodeJS.ProcessEnv | undefined;
|
|
48
|
+
platform?: NodeJS.Platform | undefined;
|
|
49
|
+
rootDir?: string | undefined;
|
|
50
|
+
}): {
|
|
51
|
+
command: string;
|
|
52
|
+
args: string[];
|
|
53
|
+
route: string;
|
|
54
|
+
} | null;
|
|
55
|
+
export declare function getChromeForTestingInstallSteps({ env, platform, rootDir, }?: {
|
|
56
|
+
env?: NodeJS.ProcessEnv | undefined;
|
|
57
|
+
platform?: NodeJS.Platform | undefined;
|
|
58
|
+
rootDir?: string | undefined;
|
|
59
|
+
}): {
|
|
60
|
+
command: string;
|
|
61
|
+
args: string[];
|
|
62
|
+
route: string;
|
|
63
|
+
}[];
|
|
64
|
+
export declare function installChromeForTesting({ env, installRunner, logger, maxAttempts, platform, rootDir, stdio, }?: InstallOptions): void;
|
|
65
|
+
export declare function installChromeForTestingForDev({ env, logger, maxAttempts, platform, rootDir, signal, commandRunner, ...options }?: DevInstallOptions): Promise<void>;
|
|
66
|
+
export {};
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { createNodeCommandRunner } from "@openge/forge-command-runner/node";
|
|
5
|
+
import { createPosixProcessControl, createWindowsProcessControl } from "@openge/forge-process-control-node";
|
|
6
|
+
import { chromeForTestingOfficialBaseUrl, defaultChromeForTestingFallbackBuildId, getProjectPaths } from "./cli.js";
|
|
7
|
+
function createInstallerCommandRunner(platform) {
|
|
8
|
+
return createNodeCommandRunner({
|
|
9
|
+
platform: platform === "win32" ? "win32" : "posix",
|
|
10
|
+
processControl: platform === "win32" ? createWindowsProcessControl({ listenerBackend: "powershell" }) : createPosixProcessControl({ listenerBackend: "lsof" }),
|
|
11
|
+
terminationPolicy: { mode: platform === "win32" ? "force" : "graceful" },
|
|
12
|
+
identityAcquisition: { timeoutMs: platform === "win32" ? 8_000 : 2_000, pollIntervalMs: 20 },
|
|
13
|
+
terminationTimeoutMs: platform === "win32" ? 15_000 : 5_000,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
export function trimOutput(value, maxLength = 2400) {
|
|
17
|
+
const normalized = value.trim();
|
|
18
|
+
if (normalized.length <= maxLength)
|
|
19
|
+
return normalized;
|
|
20
|
+
return normalized.slice(-maxLength);
|
|
21
|
+
}
|
|
22
|
+
export function stringifyProcessOutput(value) {
|
|
23
|
+
if (!value)
|
|
24
|
+
return "";
|
|
25
|
+
return Buffer.isBuffer(value) ? value.toString("utf8") : String(value);
|
|
26
|
+
}
|
|
27
|
+
export function getProcessOutputErrorReason(result) {
|
|
28
|
+
const output = [result.stderr, result.stdout].map(stringifyProcessOutput).filter(Boolean).join("\n");
|
|
29
|
+
const errorMatch = output.match(/\bError:\s*([^\r\n]+)/);
|
|
30
|
+
return errorMatch?.[1]?.trim() || "";
|
|
31
|
+
}
|
|
32
|
+
export function formatChromeForTestingInstallFailure(result) {
|
|
33
|
+
if (result.error)
|
|
34
|
+
return result.error.message;
|
|
35
|
+
const reason = getProcessOutputErrorReason(result);
|
|
36
|
+
if (typeof result.status === "number" && result.status !== 0) {
|
|
37
|
+
return `exit code ${result.status}${reason ? `: ${reason}` : ""}`;
|
|
38
|
+
}
|
|
39
|
+
return reason || "unknown error";
|
|
40
|
+
}
|
|
41
|
+
export function getPackageRunner({ env, platform }) {
|
|
42
|
+
const userAgent = env.npm_config_user_agent ?? "";
|
|
43
|
+
const execPath = env.npm_execpath ?? "";
|
|
44
|
+
const commandSuffix = platform === "win32" ? ".cmd" : "";
|
|
45
|
+
if (userAgent.startsWith("pnpm/") || /[\\/]pnpm(?:\.cjs|\.js|\.cmd)?$/i.test(execPath)) {
|
|
46
|
+
return { command: `pnpm${commandSuffix}`, argsPrefix: ["dlx"] };
|
|
47
|
+
}
|
|
48
|
+
return { command: `npx${commandSuffix}`, argsPrefix: [] };
|
|
49
|
+
}
|
|
50
|
+
export function getChromeForTestingInstallStep({ env = process.env, platform = process.platform, rootDir = process.cwd(), } = {}) {
|
|
51
|
+
const { chromeForTestingDir } = getProjectPaths(rootDir);
|
|
52
|
+
const packageRunner = getPackageRunner({ env, platform });
|
|
53
|
+
return {
|
|
54
|
+
command: packageRunner.command,
|
|
55
|
+
args: [
|
|
56
|
+
...packageRunner.argsPrefix,
|
|
57
|
+
"@puppeteer/browsers",
|
|
58
|
+
"install",
|
|
59
|
+
"chrome@stable",
|
|
60
|
+
"--path",
|
|
61
|
+
chromeForTestingDir,
|
|
62
|
+
],
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
export function getChromeForTestingFallbackBuildId(env = process.env) {
|
|
66
|
+
return env.LINK_LIGHT_CHROME_FOR_TESTING_BUILD_ID?.trim() || defaultChromeForTestingFallbackBuildId;
|
|
67
|
+
}
|
|
68
|
+
export function getChromeForTestingDirectInstallStep({ env = process.env, platform = process.platform, rootDir = process.cwd(), } = {}) {
|
|
69
|
+
if (platform !== "win32")
|
|
70
|
+
return null;
|
|
71
|
+
const { chromeForTestingDir } = getProjectPaths(rootDir);
|
|
72
|
+
const packageRunner = getPackageRunner({ env, platform });
|
|
73
|
+
const buildId = getChromeForTestingFallbackBuildId(env);
|
|
74
|
+
return {
|
|
75
|
+
command: packageRunner.command,
|
|
76
|
+
args: [
|
|
77
|
+
...packageRunner.argsPrefix,
|
|
78
|
+
"@puppeteer/browsers",
|
|
79
|
+
"install",
|
|
80
|
+
`chrome@${buildId}`,
|
|
81
|
+
"--platform",
|
|
82
|
+
"win64",
|
|
83
|
+
"--base-url",
|
|
84
|
+
chromeForTestingOfficialBaseUrl,
|
|
85
|
+
"--path",
|
|
86
|
+
chromeForTestingDir,
|
|
87
|
+
],
|
|
88
|
+
route: "official exact build fallback",
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
export function getChromeForTestingInstallSteps({ env = process.env, platform = process.platform, rootDir = process.cwd(), } = {}) {
|
|
92
|
+
return [
|
|
93
|
+
{ ...getChromeForTestingInstallStep({ env, platform, rootDir }), route: "stable channel" },
|
|
94
|
+
getChromeForTestingDirectInstallStep({ env, platform, rootDir }),
|
|
95
|
+
].filter((step) => step !== null);
|
|
96
|
+
}
|
|
97
|
+
export function installChromeForTesting({ env = process.env, installRunner = spawnSync, logger = console.log, maxAttempts = 3, platform = process.platform, rootDir = process.cwd(), stdio = "inherit", } = {}) {
|
|
98
|
+
const steps = getChromeForTestingInstallSteps({ env, platform, rootDir });
|
|
99
|
+
let lastFailure = null;
|
|
100
|
+
for (const step of steps) {
|
|
101
|
+
logger(`Using Chrome for Testing install route: ${step.route}.`);
|
|
102
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
|
|
103
|
+
const attemptLabel = maxAttempts > 1 ? ` (${attempt}/${maxAttempts})` : "";
|
|
104
|
+
logger(`Installing Chrome for Testing${attemptLabel} with: ${[step.command, ...step.args].join(" ")}`);
|
|
105
|
+
const result = installRunner(step.command, step.args, {
|
|
106
|
+
stdio,
|
|
107
|
+
shell: platform === "win32",
|
|
108
|
+
});
|
|
109
|
+
if (result.error) {
|
|
110
|
+
const failureMessage = formatChromeForTestingInstallFailure(result);
|
|
111
|
+
lastFailure = failureMessage;
|
|
112
|
+
if (attempt === maxAttempts) {
|
|
113
|
+
logger(`Chrome for Testing install route "${step.route}" failed with ${failureMessage}.`);
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
logger(`Chrome for Testing install failed with ${failureMessage}. Retrying.`);
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (typeof result.status === "number" && result.status !== 0) {
|
|
120
|
+
const failureMessage = formatChromeForTestingInstallFailure(result);
|
|
121
|
+
lastFailure = failureMessage;
|
|
122
|
+
if (attempt === maxAttempts) {
|
|
123
|
+
logger(`Chrome for Testing install route "${step.route}" failed with ${failureMessage}.`);
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
logger(`Chrome for Testing install failed with ${failureMessage}. Retrying.`);
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
throw new Error(`Chrome for Testing install failed with ${lastFailure || "unknown error"}`);
|
|
133
|
+
}
|
|
134
|
+
// 同步安装器保留原有重试策略;主 dev 在独立 Node 进程中等待它,确保可以取消整个安装树。
|
|
135
|
+
export async function installChromeForTestingForDev({ env = process.env, logger = console.log, maxAttempts = 3, platform = process.platform, rootDir = process.cwd(), signal, commandRunner = createInstallerCommandRunner(platform), ...options } = {}) {
|
|
136
|
+
if (!signal)
|
|
137
|
+
return installChromeForTesting({ ...options, env, logger, maxAttempts, platform, rootDir });
|
|
138
|
+
signal.throwIfAborted();
|
|
139
|
+
const result = await commandRunner.run({
|
|
140
|
+
command: process.execPath,
|
|
141
|
+
args: [path.join(path.dirname(fileURLToPath(import.meta.url)), "chrome-install-worker.js"), String(maxAttempts)],
|
|
142
|
+
cwd: rootDir,
|
|
143
|
+
signal,
|
|
144
|
+
environment: { mode: "replace", values: Object.fromEntries(Object.entries(env).filter((pair) => pair[1] !== undefined)) },
|
|
145
|
+
output: { mode: "capture", maxBytesPerStream: 1024 * 1024, overflow: "truncate" },
|
|
146
|
+
});
|
|
147
|
+
for (const bytes of [result.stdout, result.stderr]) {
|
|
148
|
+
if (bytes?.length)
|
|
149
|
+
logger(Buffer.from(bytes).toString("utf8"));
|
|
150
|
+
}
|
|
151
|
+
if (result.diagnostics.length)
|
|
152
|
+
throw new Error(result.diagnostics.map(item => item.code).join(", "));
|
|
153
|
+
signal.throwIfAborted();
|
|
154
|
+
if (result.terminationReason !== "exit" || result.exitCode !== 0) {
|
|
155
|
+
throw new Error("Chrome for Testing install command ended: " + result.terminationReason + " (" + result.exitCode + ").");
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { installChromeForTesting } from "./chrome-for-testing.js";
|
|
2
|
+
try {
|
|
3
|
+
installChromeForTesting({ maxAttempts: Number(process.argv[2]), stdio: "inherit" });
|
|
4
|
+
}
|
|
5
|
+
catch (error) {
|
|
6
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
7
|
+
process.exitCode = 1;
|
|
8
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export declare const defaultDevUrl = "http://localhost:3000";
|
|
2
|
+
export declare const chromeExtensionsUrl = "chrome://extensions/";
|
|
3
|
+
export declare const devReloadClientFileName = "link-light-dev-reload-client.js";
|
|
4
|
+
export declare const devSidePanelReloadClientFileName = "link-light-dev-side-panel-reload-client.js";
|
|
5
|
+
export declare const defaultChromeForTestingFallbackBuildId = "149.0.7827.22";
|
|
6
|
+
export declare const chromeForTestingOfficialBaseUrl = "https://storage.googleapis.com/chrome-for-testing-public";
|
|
7
|
+
export declare const profileStateFileName = ".link-light-extension-dev-state.json";
|
|
8
|
+
export declare function getSharedAssetsRoot(): string;
|
|
9
|
+
export declare function assertUnlinkedPath(target: string): string;
|
|
10
|
+
export declare function assertExtensionRoot(rootDir: string, extensionRoot: string | undefined): string;
|
|
11
|
+
export declare function assertExtensionTree(directory: string): void;
|
|
12
|
+
export declare function assertProjectOutput(rootDir: string, target: string): string;
|
|
13
|
+
export declare function assertSeparateExtensionOutput(source: string, output: string): void;
|
|
14
|
+
export declare function getArgValue(argv: readonly string[], name: string): string | undefined;
|
|
15
|
+
export declare function parseAppUrl(value: string): string;
|
|
16
|
+
export declare function getRequestedAppUrl(argv: readonly string[], env?: NodeJS.ProcessEnv): string;
|
|
17
|
+
export declare function assertValidExtensionName(name: string): void;
|
|
18
|
+
export declare function getRequestedExtension(argv: readonly string[]): string;
|
|
19
|
+
export declare function discoverExtensionNames(extensionRoot: string): string[];
|
|
20
|
+
export declare function getRequestedExtensions(argv: readonly string[], rootDir?: string): string[];
|
|
21
|
+
export declare function getRequestedBrowser(argv: readonly string[]): "chrome" | null;
|
|
22
|
+
interface BrowserPaths {
|
|
23
|
+
chromeForTestingDir: string;
|
|
24
|
+
}
|
|
25
|
+
interface ExtensionPaths extends BrowserPaths {
|
|
26
|
+
devExtensionDir: string;
|
|
27
|
+
extensionDir: string;
|
|
28
|
+
profileDir: string;
|
|
29
|
+
}
|
|
30
|
+
export declare function getProjectPaths(rootDir: string): BrowserPaths;
|
|
31
|
+
export declare function getProjectPaths(rootDir: string, extensionName: string, extensionRoot?: string): ExtensionPaths;
|
|
32
|
+
export declare function getRelevantExtensionFiles(rootDir: string, extensionName: string, extensionRoot: string | undefined): string[];
|
|
33
|
+
export declare function getRelevantExtensionSourcePath(rootDir: string, extensionName: string, fileName: string, extensionRoot: string | undefined): string;
|
|
34
|
+
export declare function getExistingRelevantExtensionSourcePaths(rootDir: string, extensionName: string, extensionRoot: string | undefined): string[];
|
|
35
|
+
export declare function getProfileDirForExtensions(rootDir: string, extensionNames: readonly string[], extensionRoot: string | undefined): string;
|
|
36
|
+
export declare function getChromeLaunchExtensionDirs({ extensionNames, rootDir, extensionRoot }: {
|
|
37
|
+
extensionNames: readonly string[];
|
|
38
|
+
rootDir: string;
|
|
39
|
+
extensionRoot?: string;
|
|
40
|
+
}): string[];
|
|
41
|
+
export {};
|