@xlinklight/browser-extension-tools 1.0.0-rc.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/LICENSE +21 -21
- package/README.md +33 -33
- package/{assets → dist/assets}/shared/css/side-panel.css +94 -94
- 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
|
@@ -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 {};
|
package/dist/src/cli.js
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { existsSync, lstatSync, readdirSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { isMissingFile } from "./fs-error.js";
|
|
5
|
+
export const defaultDevUrl = "http://localhost:3000";
|
|
6
|
+
export const chromeExtensionsUrl = "chrome://extensions/";
|
|
7
|
+
export const devReloadClientFileName = "link-light-dev-reload-client.js";
|
|
8
|
+
export const devSidePanelReloadClientFileName = "link-light-dev-side-panel-reload-client.js";
|
|
9
|
+
export const defaultChromeForTestingFallbackBuildId = "149.0.7827.22";
|
|
10
|
+
export const chromeForTestingOfficialBaseUrl = "https://storage.googleapis.com/chrome-for-testing-public";
|
|
11
|
+
export const profileStateFileName = ".link-light-extension-dev-state.json";
|
|
12
|
+
export function getSharedAssetsRoot() {
|
|
13
|
+
return path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../assets/shared");
|
|
14
|
+
}
|
|
15
|
+
function within(root, target) {
|
|
16
|
+
const relative = path.relative(path.resolve(root), path.resolve(target));
|
|
17
|
+
return relative !== "" && relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative);
|
|
18
|
+
}
|
|
19
|
+
export function assertUnlinkedPath(target) {
|
|
20
|
+
for (let current = path.resolve(target);; current = path.dirname(current)) {
|
|
21
|
+
let entry;
|
|
22
|
+
try {
|
|
23
|
+
entry = lstatSync(current);
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
if (!isMissingFile(error))
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
if (entry?.isSymbolicLink())
|
|
30
|
+
throw new Error(`LINKED_EXTENSION_PATH ${current}`);
|
|
31
|
+
if (current === path.dirname(current))
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
return path.resolve(target);
|
|
35
|
+
}
|
|
36
|
+
export function assertExtensionRoot(rootDir, extensionRoot) {
|
|
37
|
+
if (typeof extensionRoot !== "string" || !extensionRoot)
|
|
38
|
+
throw new Error("EXTENSION_ROOT_REQUIRED");
|
|
39
|
+
const root = path.resolve(rootDir);
|
|
40
|
+
const target = path.resolve(root, extensionRoot);
|
|
41
|
+
if (!within(root, target))
|
|
42
|
+
throw new Error("EXTENSION_ROOT_SCOPE");
|
|
43
|
+
assertUnlinkedPath(target);
|
|
44
|
+
if (!lstatSync(target).isDirectory())
|
|
45
|
+
throw new Error("EXTENSION_ROOT_DIRECTORY_REQUIRED");
|
|
46
|
+
return target;
|
|
47
|
+
}
|
|
48
|
+
export function assertExtensionTree(directory) {
|
|
49
|
+
const entry = lstatSync(assertUnlinkedPath(directory));
|
|
50
|
+
if (!entry.isDirectory())
|
|
51
|
+
throw new Error("EXTENSION_DIRECTORY_REQUIRED");
|
|
52
|
+
for (const name of readdirSync(directory)) {
|
|
53
|
+
if ([".git", "node_modules", "shared"].includes(name))
|
|
54
|
+
throw new Error(`UNEXPECTED_EXTENSION_ENTRY ${name}`);
|
|
55
|
+
const source = path.join(directory, name);
|
|
56
|
+
const stat = lstatSync(source);
|
|
57
|
+
if (stat.isSymbolicLink())
|
|
58
|
+
throw new Error(`LINKED_EXTENSION_PATH ${source}`);
|
|
59
|
+
if (stat.isDirectory())
|
|
60
|
+
assertExtensionTree(source);
|
|
61
|
+
else if (!stat.isFile())
|
|
62
|
+
throw new Error(`UNSUPPORTED_EXTENSION_ENTRY ${source}`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
export function assertProjectOutput(rootDir, target) {
|
|
66
|
+
const root = path.resolve(rootDir);
|
|
67
|
+
const output = path.resolve(root, target);
|
|
68
|
+
const top = path.relative(root, output).split(path.sep)[0];
|
|
69
|
+
if (!within(root, output) || ![".tmp", "dist"].includes(top) || output === path.join(root, top))
|
|
70
|
+
throw new Error("EXTENSION_OUTPUT_SCOPE");
|
|
71
|
+
assertUnlinkedPath(output);
|
|
72
|
+
if (existsSync(output) && lstatSync(output).isFile() && lstatSync(output).nlink > 1)
|
|
73
|
+
throw new Error("SHARED_EXTENSION_OUTPUT_FILE");
|
|
74
|
+
return output;
|
|
75
|
+
}
|
|
76
|
+
export function assertSeparateExtensionOutput(source, output) {
|
|
77
|
+
if (path.relative(source, output) === "" || within(source, output) || within(output, source))
|
|
78
|
+
throw new Error("EXTENSION_SOURCE_OUTPUT_OVERLAP");
|
|
79
|
+
}
|
|
80
|
+
export function getArgValue(argv, name) {
|
|
81
|
+
const prefix = `${name}=`;
|
|
82
|
+
return argv.find((arg) => arg.startsWith(prefix))?.slice(prefix.length);
|
|
83
|
+
}
|
|
84
|
+
export function parseAppUrl(value) {
|
|
85
|
+
let parsed;
|
|
86
|
+
try {
|
|
87
|
+
parsed = new URL(value);
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
throw new Error(`Invalid app URL: ${value}`);
|
|
91
|
+
}
|
|
92
|
+
if (!["http:", "https:"].includes(parsed.protocol) || parsed.username || parsed.password)
|
|
93
|
+
throw new Error(`Invalid app URL: ${value}`);
|
|
94
|
+
if (parsed.pathname !== "/" || parsed.search || parsed.hash)
|
|
95
|
+
throw new Error("App URL must be an origin without path, search, or hash.");
|
|
96
|
+
return parsed.origin;
|
|
97
|
+
}
|
|
98
|
+
export function getRequestedAppUrl(argv, env = process.env) {
|
|
99
|
+
return parseAppUrl(getArgValue(argv, "--app-url") || env.LINK_LIGHT_EXTENSION_APP_URL || defaultDevUrl);
|
|
100
|
+
}
|
|
101
|
+
export function assertValidExtensionName(name) {
|
|
102
|
+
if (typeof name !== "string" || !/^[a-z][a-z0-9-]{0,79}$/.test(name))
|
|
103
|
+
throw new Error("Extension name must contain only lowercase letters, numbers, and hyphens.");
|
|
104
|
+
}
|
|
105
|
+
export function getRequestedExtension(argv) {
|
|
106
|
+
const name = getArgValue(argv, "--extension");
|
|
107
|
+
if (!name)
|
|
108
|
+
throw new Error("EXTENSION_SELECTION_REQUIRED");
|
|
109
|
+
assertValidExtensionName(name);
|
|
110
|
+
return name;
|
|
111
|
+
}
|
|
112
|
+
export function discoverExtensionNames(extensionRoot) {
|
|
113
|
+
assertUnlinkedPath(extensionRoot);
|
|
114
|
+
return readdirSync(extensionRoot, { withFileTypes: true })
|
|
115
|
+
.filter(entry => entry.isDirectory() && existsSync(path.join(extensionRoot, entry.name, "manifest.json")))
|
|
116
|
+
.map(entry => entry.name).toSorted();
|
|
117
|
+
}
|
|
118
|
+
export function getRequestedExtensions(argv, rootDir = process.cwd()) {
|
|
119
|
+
const extensionRoot = assertExtensionRoot(rootDir, getArgValue(argv, "--extension-root"));
|
|
120
|
+
const multiple = getArgValue(argv, "--extensions");
|
|
121
|
+
const single = getArgValue(argv, "--extension");
|
|
122
|
+
if (multiple && single)
|
|
123
|
+
throw new Error("Use either --extension or --extensions, not both.");
|
|
124
|
+
const names = multiple === "all" ? discoverExtensionNames(extensionRoot)
|
|
125
|
+
: multiple ? multiple.split(",").map(value => value.trim()).filter(Boolean) : [getRequestedExtension(argv)];
|
|
126
|
+
if (!names.length)
|
|
127
|
+
throw new Error("No Chrome extensions found under the explicit extension root.");
|
|
128
|
+
if (new Set(names).size !== names.length)
|
|
129
|
+
throw new Error("DUPLICATE_EXTENSION_SELECTION");
|
|
130
|
+
for (const name of names) {
|
|
131
|
+
assertValidExtensionName(name);
|
|
132
|
+
const source = path.join(extensionRoot, name);
|
|
133
|
+
assertExtensionTree(source);
|
|
134
|
+
if (!lstatSync(path.join(source, "manifest.json")).isFile())
|
|
135
|
+
throw new Error("EXTENSION_MANIFEST_REQUIRED");
|
|
136
|
+
}
|
|
137
|
+
return names;
|
|
138
|
+
}
|
|
139
|
+
export function getRequestedBrowser(argv) {
|
|
140
|
+
const browser = getArgValue(argv, "--browser");
|
|
141
|
+
if (!browser)
|
|
142
|
+
return null;
|
|
143
|
+
if (browser === "chrome")
|
|
144
|
+
return browser;
|
|
145
|
+
throw new Error(`Unsupported browser "${browser}". Supported values: chrome.`);
|
|
146
|
+
}
|
|
147
|
+
export function getProjectPaths(rootDir, extensionName, extensionRoot) {
|
|
148
|
+
const outputRoot = path.join(path.resolve(rootDir), ".tmp", "browser-extensions");
|
|
149
|
+
const browser = { chromeForTestingDir: path.join(outputRoot, "chrome-for-testing") };
|
|
150
|
+
if (extensionName === undefined)
|
|
151
|
+
return browser;
|
|
152
|
+
assertValidExtensionName(extensionName);
|
|
153
|
+
const sourceRoot = assertExtensionRoot(rootDir, extensionRoot);
|
|
154
|
+
const devExtensionDir = path.join(outputRoot, extensionName, "unpacked");
|
|
155
|
+
const extensionDir = path.join(sourceRoot, extensionName);
|
|
156
|
+
assertSeparateExtensionOutput(extensionDir, devExtensionDir);
|
|
157
|
+
return {
|
|
158
|
+
...browser,
|
|
159
|
+
devExtensionDir,
|
|
160
|
+
extensionDir,
|
|
161
|
+
profileDir: path.join(outputRoot, "profiles", extensionName),
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
export function getRelevantExtensionFiles(rootDir, extensionName, extensionRoot) {
|
|
165
|
+
const { extensionDir } = getProjectPaths(rootDir, extensionName, extensionRoot);
|
|
166
|
+
assertExtensionTree(extensionDir);
|
|
167
|
+
return [...readdirSync(extensionDir).toSorted(), "shared"];
|
|
168
|
+
}
|
|
169
|
+
export function getRelevantExtensionSourcePath(rootDir, extensionName, fileName, extensionRoot) {
|
|
170
|
+
if (fileName === "shared")
|
|
171
|
+
return getSharedAssetsRoot();
|
|
172
|
+
if (path.basename(fileName) !== fileName)
|
|
173
|
+
throw new Error("EXTENSION_ENTRY_SCOPE");
|
|
174
|
+
return path.join(getProjectPaths(rootDir, extensionName, extensionRoot).extensionDir, fileName);
|
|
175
|
+
}
|
|
176
|
+
export function getExistingRelevantExtensionSourcePaths(rootDir, extensionName, extensionRoot) {
|
|
177
|
+
return getRelevantExtensionFiles(rootDir, extensionName, extensionRoot)
|
|
178
|
+
.map(file => getRelevantExtensionSourcePath(rootDir, extensionName, file, extensionRoot));
|
|
179
|
+
}
|
|
180
|
+
export function getProfileDirForExtensions(rootDir, extensionNames, extensionRoot) {
|
|
181
|
+
if (!Array.isArray(extensionNames) || !extensionNames.length)
|
|
182
|
+
throw new Error("EXTENSION_SELECTION_REQUIRED");
|
|
183
|
+
if (extensionNames.length === 1)
|
|
184
|
+
return getProjectPaths(rootDir, extensionNames[0], extensionRoot).profileDir;
|
|
185
|
+
return path.join(path.resolve(rootDir), ".tmp", "browser-extensions", "profiles", "multiple");
|
|
186
|
+
}
|
|
187
|
+
export function getChromeLaunchExtensionDirs({ extensionNames, rootDir, extensionRoot }) {
|
|
188
|
+
return extensionNames.map(name => getProjectPaths(rootDir, name, extensionRoot).devExtensionDir);
|
|
189
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { runChromeExtensionDev } from "./run.js";
|
|
3
|
+
try {
|
|
4
|
+
const controller = await runChromeExtensionDev();
|
|
5
|
+
if (controller.keepAlive)
|
|
6
|
+
await controller.waitForShutdown();
|
|
7
|
+
}
|
|
8
|
+
catch (error) {
|
|
9
|
+
console.error(error instanceof Error ? error.message : error);
|
|
10
|
+
process.exitCode = 1;
|
|
11
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { installChromeForTestingForDev } from "./chrome-for-testing.js";
|
|
2
|
+
import { createDevReloadServer } from "./reload-server.js";
|
|
3
|
+
import { createExtensionFileWatcher } from "./watcher.js";
|
|
4
|
+
interface PrepareOptions {
|
|
5
|
+
appUrl?: string;
|
|
6
|
+
extensionName: string;
|
|
7
|
+
extensionRoot?: string;
|
|
8
|
+
reloadPort?: number;
|
|
9
|
+
signal?: AbortSignal;
|
|
10
|
+
rootDir?: string;
|
|
11
|
+
}
|
|
12
|
+
interface RunDevOptions {
|
|
13
|
+
argv?: string[];
|
|
14
|
+
env?: NodeJS.ProcessEnv;
|
|
15
|
+
rootDir?: string;
|
|
16
|
+
platform?: NodeJS.Platform;
|
|
17
|
+
signal?: AbortSignal;
|
|
18
|
+
logger?: (message: string) => void;
|
|
19
|
+
installRunner?: NonNullable<Parameters<typeof installChromeForTestingForDev>[0]>["installRunner"];
|
|
20
|
+
chromeInstaller?: typeof installChromeForTestingForDev;
|
|
21
|
+
reloadServerFactory?: typeof createDevReloadServer;
|
|
22
|
+
watcherFactory?: Parameters<typeof createExtensionFileWatcher>[0]["watcherFactory"];
|
|
23
|
+
runner?: (command: string, args: string[], options: {
|
|
24
|
+
detached: boolean;
|
|
25
|
+
stdio: "ignore";
|
|
26
|
+
windowsHide: boolean;
|
|
27
|
+
}) => {
|
|
28
|
+
unref(): void;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export declare function readFileIfExists(filePath: string): Promise<string>;
|
|
32
|
+
export declare function copyExtensionEntry(sourcePath: string, targetPath: string): Promise<void>;
|
|
33
|
+
export declare function addUniqueItems<T>(items: readonly T[] | undefined, additions: readonly T[]): T[];
|
|
34
|
+
export declare function buildDevReloadClient({ appOrigin, reloadPort }: {
|
|
35
|
+
appOrigin: string;
|
|
36
|
+
reloadPort: number;
|
|
37
|
+
}): string;
|
|
38
|
+
export declare function buildDevSidePanelReloadClient({ buildLabel, reloadPort }: {
|
|
39
|
+
buildLabel: string;
|
|
40
|
+
reloadPort: number;
|
|
41
|
+
}): string;
|
|
42
|
+
export declare function prepareDevExtension({ appUrl, extensionName, extensionRoot, reloadPort, signal, rootDir, }: PrepareOptions): Promise<{
|
|
43
|
+
buildLabel: string;
|
|
44
|
+
devExtensionDir: string;
|
|
45
|
+
}>;
|
|
46
|
+
export declare function runChromeExtensionDev({ argv, env, installRunner, chromeInstaller, reloadServerFactory, signal, logger, runner, rootDir, platform, watcherFactory, }?: RunDevOptions): Promise<{
|
|
47
|
+
close: () => Promise<void>;
|
|
48
|
+
keepAlive: boolean;
|
|
49
|
+
waitForShutdown: () => Promise<void>;
|
|
50
|
+
}>;
|
|
51
|
+
export {};
|