@xlinklight/browser-extension-tools 1.0.0-rc.0 → 1.0.0
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/shared/css/side-panel.css +94 -94
- package/assets/shared/js/chrome-runtime.js +87 -87
- package/assets/shared/js/extension-account-menu.js +239 -239
- package/assets/shared/js/extension-i18n.js +42 -42
- package/assets/shared/js/extension-session-store.js +42 -42
- package/assets/shared/js/extension-tabs.js +23 -23
- package/assets/shared/js/link-light-api-client.js +31 -31
- package/assets/shared/js/side-panel-account.js +14 -14
- package/assets/shared/js/side-panel-api.js +13 -13
- package/assets/shared/js/side-panel-bootstrap.js +127 -127
- package/assets/shared/js/side-panel-dom.js +15 -15
- package/assets/shared/js/side-panel-i18n.js +37 -37
- package/assets/shared/js/side-panel-import-preview.js +7 -7
- package/assets/shared/js/side-panel-status.js +13 -13
- package/assets/shared/js/side-panel-tabs.js +35 -35
- package/assets/shared/js/side-panel-translations.js +41 -41
- package/package.json +1 -1
- package/src/chrome-discovery.mjs +231 -231
- package/src/chrome-for-testing.mjs +203 -203
- package/src/chrome-install-worker.mjs +8 -8
- package/src/cli.mjs +171 -171
- package/src/dev-cli.mjs +9 -9
- package/src/dev-extension.mjs +350 -350
- package/src/package-cli.mjs +7 -7
- package/src/package.mjs +223 -223
- package/src/profile.mjs +144 -144
- package/src/reload-server.mjs +108 -108
- package/src/run.mjs +7 -7
- package/src/watcher.mjs +45 -45
package/src/package-cli.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import { packageChromeExtension } from "./package.mjs";
|
|
3
|
-
try { await packageChromeExtension(); }
|
|
4
|
-
catch (error) {
|
|
5
|
-
console.error(error instanceof Error ? error.message : error);
|
|
6
|
-
process.exitCode = 1;
|
|
7
|
-
}
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { packageChromeExtension } from "./package.mjs";
|
|
3
|
+
try { await packageChromeExtension(); }
|
|
4
|
+
catch (error) {
|
|
5
|
+
console.error(error instanceof Error ? error.message : error);
|
|
6
|
+
process.exitCode = 1;
|
|
7
|
+
}
|
package/src/package.mjs
CHANGED
|
@@ -1,223 +1,223 @@
|
|
|
1
|
-
import { cp, mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
|
|
4
|
-
import { calculateFileIntegritySync } from "@openge/forge-artifact-integrity/node";
|
|
5
|
-
|
|
6
|
-
import { assertExtensionRoot, assertExtensionTree, assertProjectOutput, assertSeparateExtensionOutput, assertValidExtensionName, getArgValue, getRequestedExtensions, getSharedAssetsRoot } from "./cli.mjs";
|
|
7
|
-
|
|
8
|
-
function requireString(config, key) {
|
|
9
|
-
const value = config[key];
|
|
10
|
-
if (typeof value !== "string" || value.trim().length === 0) {
|
|
11
|
-
throw new Error(`Missing extension.${key}`);
|
|
12
|
-
}
|
|
13
|
-
return value.trim();
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function normalizeHttpsOrigin(value) {
|
|
17
|
-
let url;
|
|
18
|
-
try {
|
|
19
|
-
url = new URL(value);
|
|
20
|
-
} catch {
|
|
21
|
-
throw new Error("extension.origin must be an https:// origin");
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
if (url.protocol !== "https:" || url.username || url.password || url.pathname !== "/" || url.search || url.hash) {
|
|
25
|
-
throw new Error("extension.origin must be an https:// origin");
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return url.origin;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function validatePackageConfig(config, rootDir) {
|
|
32
|
-
if (!config || typeof config !== "object" || Array.isArray(config)) throw new Error("Extension config must contain a JSON object");
|
|
33
|
-
return {
|
|
34
|
-
origin: normalizeHttpsOrigin(requireString(config, "origin")),
|
|
35
|
-
pluginToken: requireString(config, "pluginToken"),
|
|
36
|
-
outputDir: assertProjectOutput(rootDir, requireString(config, "outputDir")),
|
|
37
|
-
zipFile: assertProjectOutput(rootDir, requireString(config, "zipFile")),
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function configForExtension(config, extensionName, count) {
|
|
42
|
-
if (count === 1) return config;
|
|
43
|
-
return { ...config, outputDir: path.join(config.outputDir, extensionName), zipFile: path.join(path.dirname(config.zipFile), `${path.basename(config.zipFile, ".zip")}-${extensionName}.zip`) };
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async function copyExtensionEntries(outputDir, extensionDir) {
|
|
47
|
-
await rm(outputDir, { recursive: true, force: true });
|
|
48
|
-
await mkdir(outputDir, { recursive: true });
|
|
49
|
-
|
|
50
|
-
for (const entry of [...(await readdir(extensionDir)).toSorted(), "shared"]) {
|
|
51
|
-
if (entry === "manifest.json" || entry === "link-light-extension-config.js") continue;
|
|
52
|
-
const sourcePath = entry === "shared" ? getSharedAssetsRoot() : path.join(extensionDir, entry);
|
|
53
|
-
const targetPath = path.join(outputDir, entry);
|
|
54
|
-
await cp(sourcePath, targetPath, { recursive: true });
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
async function writePackagedManifest({ origin, outputDir, extensionDir }) {
|
|
59
|
-
const manifest = JSON.parse(await readFile(path.join(extensionDir, "manifest.json"), "utf8"));
|
|
60
|
-
const packagedManifest = {
|
|
61
|
-
...manifest,
|
|
62
|
-
host_permissions: [`${origin}/*`],
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
await writeFile(path.join(outputDir, "manifest.json"), `${JSON.stringify(packagedManifest, null, 2)}\n`, "utf8");
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
async function writePackagedConfig({ origin, outputDir, pluginToken }) {
|
|
69
|
-
const payload = JSON.stringify({ origin, pluginToken });
|
|
70
|
-
await writeFile(path.join(outputDir, "link-light-extension-config.js"), `globalThis.LINK_LIGHT_CONFIG = ${payload};\n`, "utf8");
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const crcTable = Array.from({ length: 256 }, (_unused, index) => {
|
|
74
|
-
let value = index;
|
|
75
|
-
for (let bit = 0; bit < 8; bit += 1) {
|
|
76
|
-
value = value & 1 ? 0xedb88320 ^ (value >>> 1) : value >>> 1;
|
|
77
|
-
}
|
|
78
|
-
return value >>> 0;
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
function crc32(buffer) {
|
|
82
|
-
let value = 0xffffffff;
|
|
83
|
-
for (const byte of buffer) {
|
|
84
|
-
value = crcTable[(value ^ byte) & 0xff] ^ (value >>> 8);
|
|
85
|
-
}
|
|
86
|
-
return (value ^ 0xffffffff) >>> 0;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function writeUInt16(value) {
|
|
90
|
-
const buffer = Buffer.alloc(2);
|
|
91
|
-
buffer.writeUInt16LE(value);
|
|
92
|
-
return buffer;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function writeUInt32(value) {
|
|
96
|
-
const buffer = Buffer.alloc(4);
|
|
97
|
-
buffer.writeUInt32LE(value);
|
|
98
|
-
return buffer;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
async function listFiles(directory, baseDir = directory) {
|
|
102
|
-
const entries = await readdir(directory, { withFileTypes: true });
|
|
103
|
-
const files = [];
|
|
104
|
-
|
|
105
|
-
for (const entry of entries.toSorted((left, right) => left.name.localeCompare(right.name))) {
|
|
106
|
-
const entryPath = path.join(directory, entry.name);
|
|
107
|
-
if (entry.isDirectory()) {
|
|
108
|
-
files.push(...(await listFiles(entryPath, baseDir)));
|
|
109
|
-
continue;
|
|
110
|
-
}
|
|
111
|
-
if (entry.isFile()) {
|
|
112
|
-
files.push({
|
|
113
|
-
absolutePath: entryPath,
|
|
114
|
-
zipPath: path.relative(baseDir, entryPath).replaceAll(path.sep, "/"),
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return files;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
async function writeZipFile({ outputDir, zipFile }) {
|
|
123
|
-
const files = await listFiles(outputDir);
|
|
124
|
-
const localParts = [];
|
|
125
|
-
const centralParts = [];
|
|
126
|
-
let offset = 0;
|
|
127
|
-
|
|
128
|
-
for (const file of files) {
|
|
129
|
-
const data = await readFile(file.absolutePath);
|
|
130
|
-
const name = Buffer.from(file.zipPath);
|
|
131
|
-
const checksum = crc32(data);
|
|
132
|
-
const localHeader = Buffer.concat([
|
|
133
|
-
writeUInt32(0x04034b50),
|
|
134
|
-
writeUInt16(20),
|
|
135
|
-
writeUInt16(0),
|
|
136
|
-
writeUInt16(0),
|
|
137
|
-
writeUInt16(0),
|
|
138
|
-
writeUInt16(0x0021),
|
|
139
|
-
writeUInt32(checksum),
|
|
140
|
-
writeUInt32(data.length),
|
|
141
|
-
writeUInt32(data.length),
|
|
142
|
-
writeUInt16(name.length),
|
|
143
|
-
writeUInt16(0),
|
|
144
|
-
name,
|
|
145
|
-
]);
|
|
146
|
-
const centralHeader = Buffer.concat([
|
|
147
|
-
writeUInt32(0x02014b50),
|
|
148
|
-
writeUInt16(20),
|
|
149
|
-
writeUInt16(20),
|
|
150
|
-
writeUInt16(0),
|
|
151
|
-
writeUInt16(0),
|
|
152
|
-
writeUInt16(0),
|
|
153
|
-
writeUInt16(0x0021),
|
|
154
|
-
writeUInt32(checksum),
|
|
155
|
-
writeUInt32(data.length),
|
|
156
|
-
writeUInt32(data.length),
|
|
157
|
-
writeUInt16(name.length),
|
|
158
|
-
writeUInt16(0),
|
|
159
|
-
writeUInt16(0),
|
|
160
|
-
writeUInt16(0),
|
|
161
|
-
writeUInt16(0),
|
|
162
|
-
writeUInt32(0),
|
|
163
|
-
writeUInt32(offset),
|
|
164
|
-
name,
|
|
165
|
-
]);
|
|
166
|
-
|
|
167
|
-
localParts.push(localHeader, data);
|
|
168
|
-
centralParts.push(centralHeader);
|
|
169
|
-
offset += localHeader.length + data.length;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
const centralDirectory = Buffer.concat(centralParts);
|
|
173
|
-
const endOfCentralDirectory = Buffer.concat([
|
|
174
|
-
writeUInt32(0x06054b50),
|
|
175
|
-
writeUInt16(0),
|
|
176
|
-
writeUInt16(0),
|
|
177
|
-
writeUInt16(files.length),
|
|
178
|
-
writeUInt16(files.length),
|
|
179
|
-
writeUInt32(centralDirectory.length),
|
|
180
|
-
writeUInt32(offset),
|
|
181
|
-
writeUInt16(0),
|
|
182
|
-
]);
|
|
183
|
-
|
|
184
|
-
await mkdir(path.dirname(zipFile), { recursive: true });
|
|
185
|
-
await writeFile(zipFile, Buffer.concat([...localParts, centralDirectory, endOfCentralDirectory]));
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
async function packageSingleChromeExtension(config, extensionName, extensionRoot, rootDir, logger) {
|
|
189
|
-
const extensionDir = path.join(extensionRoot, extensionName);
|
|
190
|
-
await copyExtensionEntries(config.outputDir, extensionDir);
|
|
191
|
-
await writePackagedManifest({ ...config, extensionDir });
|
|
192
|
-
await writePackagedConfig(config);
|
|
193
|
-
await writeZipFile(config);
|
|
194
|
-
|
|
195
|
-
const digest = calculateFileIntegritySync(config.zipFile).sha256.slice(0, 12);
|
|
196
|
-
logger(`Packaged Link Light Chrome extension ${extensionName}: ${path.relative(rootDir, config.zipFile)} (${digest})`);
|
|
197
|
-
return { extensionName, outputDir: config.outputDir, zipFile: config.zipFile, digest };
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
export async function packageChromeExtension({ argv = process.argv.slice(2), rootDir = process.cwd(), extensionRoot, extensionNames, config, logger = console.log } = {}) {
|
|
201
|
-
extensionRoot = assertExtensionRoot(rootDir, extensionRoot ?? getArgValue(argv, "--extension-root"));
|
|
202
|
-
extensionNames ??= getRequestedExtensions(argv, rootDir);
|
|
203
|
-
if (!Array.isArray(extensionNames) || !extensionNames.length || new Set(extensionNames).size !== extensionNames.length) throw new Error("EXTENSION_SELECTION_REQUIRED");
|
|
204
|
-
for (const extensionName of extensionNames) {
|
|
205
|
-
assertValidExtensionName(extensionName);
|
|
206
|
-
assertExtensionTree(path.join(extensionRoot, extensionName));
|
|
207
|
-
}
|
|
208
|
-
config ??= JSON.parse(await readFile(path.join(rootDir, "config", "local", "extension.local.json"), "utf8"));
|
|
209
|
-
const validated = validatePackageConfig(config, rootDir);
|
|
210
|
-
for (const name of extensionNames) {
|
|
211
|
-
const source = path.join(extensionRoot, name);
|
|
212
|
-
assertSeparateExtensionOutput(source, validated.outputDir);
|
|
213
|
-
assertSeparateExtensionOutput(source, validated.zipFile);
|
|
214
|
-
}
|
|
215
|
-
if (validated.zipFile === validated.outputDir || validated.zipFile.startsWith(validated.outputDir + path.sep)) throw new Error("EXTENSION_OUTPUT_OVERLAP");
|
|
216
|
-
const results = [];
|
|
217
|
-
for (const name of extensionNames) {
|
|
218
|
-
const selected = configForExtension(validated, name, extensionNames.length);
|
|
219
|
-
assertProjectOutput(rootDir, selected.outputDir); assertProjectOutput(rootDir, selected.zipFile);
|
|
220
|
-
results.push(await packageSingleChromeExtension(selected, name, extensionRoot, rootDir, logger));
|
|
221
|
-
}
|
|
222
|
-
return results;
|
|
223
|
-
}
|
|
1
|
+
import { cp, mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
import { calculateFileIntegritySync } from "@openge/forge-artifact-integrity/node";
|
|
5
|
+
|
|
6
|
+
import { assertExtensionRoot, assertExtensionTree, assertProjectOutput, assertSeparateExtensionOutput, assertValidExtensionName, getArgValue, getRequestedExtensions, getSharedAssetsRoot } from "./cli.mjs";
|
|
7
|
+
|
|
8
|
+
function requireString(config, key) {
|
|
9
|
+
const value = config[key];
|
|
10
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
11
|
+
throw new Error(`Missing extension.${key}`);
|
|
12
|
+
}
|
|
13
|
+
return value.trim();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function normalizeHttpsOrigin(value) {
|
|
17
|
+
let url;
|
|
18
|
+
try {
|
|
19
|
+
url = new URL(value);
|
|
20
|
+
} catch {
|
|
21
|
+
throw new Error("extension.origin must be an https:// origin");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (url.protocol !== "https:" || url.username || url.password || url.pathname !== "/" || url.search || url.hash) {
|
|
25
|
+
throw new Error("extension.origin must be an https:// origin");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return url.origin;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function validatePackageConfig(config, rootDir) {
|
|
32
|
+
if (!config || typeof config !== "object" || Array.isArray(config)) throw new Error("Extension config must contain a JSON object");
|
|
33
|
+
return {
|
|
34
|
+
origin: normalizeHttpsOrigin(requireString(config, "origin")),
|
|
35
|
+
pluginToken: requireString(config, "pluginToken"),
|
|
36
|
+
outputDir: assertProjectOutput(rootDir, requireString(config, "outputDir")),
|
|
37
|
+
zipFile: assertProjectOutput(rootDir, requireString(config, "zipFile")),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function configForExtension(config, extensionName, count) {
|
|
42
|
+
if (count === 1) return config;
|
|
43
|
+
return { ...config, outputDir: path.join(config.outputDir, extensionName), zipFile: path.join(path.dirname(config.zipFile), `${path.basename(config.zipFile, ".zip")}-${extensionName}.zip`) };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function copyExtensionEntries(outputDir, extensionDir) {
|
|
47
|
+
await rm(outputDir, { recursive: true, force: true });
|
|
48
|
+
await mkdir(outputDir, { recursive: true });
|
|
49
|
+
|
|
50
|
+
for (const entry of [...(await readdir(extensionDir)).toSorted(), "shared"]) {
|
|
51
|
+
if (entry === "manifest.json" || entry === "link-light-extension-config.js") continue;
|
|
52
|
+
const sourcePath = entry === "shared" ? getSharedAssetsRoot() : path.join(extensionDir, entry);
|
|
53
|
+
const targetPath = path.join(outputDir, entry);
|
|
54
|
+
await cp(sourcePath, targetPath, { recursive: true });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function writePackagedManifest({ origin, outputDir, extensionDir }) {
|
|
59
|
+
const manifest = JSON.parse(await readFile(path.join(extensionDir, "manifest.json"), "utf8"));
|
|
60
|
+
const packagedManifest = {
|
|
61
|
+
...manifest,
|
|
62
|
+
host_permissions: [`${origin}/*`],
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
await writeFile(path.join(outputDir, "manifest.json"), `${JSON.stringify(packagedManifest, null, 2)}\n`, "utf8");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async function writePackagedConfig({ origin, outputDir, pluginToken }) {
|
|
69
|
+
const payload = JSON.stringify({ origin, pluginToken });
|
|
70
|
+
await writeFile(path.join(outputDir, "link-light-extension-config.js"), `globalThis.LINK_LIGHT_CONFIG = ${payload};\n`, "utf8");
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const crcTable = Array.from({ length: 256 }, (_unused, index) => {
|
|
74
|
+
let value = index;
|
|
75
|
+
for (let bit = 0; bit < 8; bit += 1) {
|
|
76
|
+
value = value & 1 ? 0xedb88320 ^ (value >>> 1) : value >>> 1;
|
|
77
|
+
}
|
|
78
|
+
return value >>> 0;
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
function crc32(buffer) {
|
|
82
|
+
let value = 0xffffffff;
|
|
83
|
+
for (const byte of buffer) {
|
|
84
|
+
value = crcTable[(value ^ byte) & 0xff] ^ (value >>> 8);
|
|
85
|
+
}
|
|
86
|
+
return (value ^ 0xffffffff) >>> 0;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function writeUInt16(value) {
|
|
90
|
+
const buffer = Buffer.alloc(2);
|
|
91
|
+
buffer.writeUInt16LE(value);
|
|
92
|
+
return buffer;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function writeUInt32(value) {
|
|
96
|
+
const buffer = Buffer.alloc(4);
|
|
97
|
+
buffer.writeUInt32LE(value);
|
|
98
|
+
return buffer;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async function listFiles(directory, baseDir = directory) {
|
|
102
|
+
const entries = await readdir(directory, { withFileTypes: true });
|
|
103
|
+
const files = [];
|
|
104
|
+
|
|
105
|
+
for (const entry of entries.toSorted((left, right) => left.name.localeCompare(right.name))) {
|
|
106
|
+
const entryPath = path.join(directory, entry.name);
|
|
107
|
+
if (entry.isDirectory()) {
|
|
108
|
+
files.push(...(await listFiles(entryPath, baseDir)));
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (entry.isFile()) {
|
|
112
|
+
files.push({
|
|
113
|
+
absolutePath: entryPath,
|
|
114
|
+
zipPath: path.relative(baseDir, entryPath).replaceAll(path.sep, "/"),
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return files;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async function writeZipFile({ outputDir, zipFile }) {
|
|
123
|
+
const files = await listFiles(outputDir);
|
|
124
|
+
const localParts = [];
|
|
125
|
+
const centralParts = [];
|
|
126
|
+
let offset = 0;
|
|
127
|
+
|
|
128
|
+
for (const file of files) {
|
|
129
|
+
const data = await readFile(file.absolutePath);
|
|
130
|
+
const name = Buffer.from(file.zipPath);
|
|
131
|
+
const checksum = crc32(data);
|
|
132
|
+
const localHeader = Buffer.concat([
|
|
133
|
+
writeUInt32(0x04034b50),
|
|
134
|
+
writeUInt16(20),
|
|
135
|
+
writeUInt16(0),
|
|
136
|
+
writeUInt16(0),
|
|
137
|
+
writeUInt16(0),
|
|
138
|
+
writeUInt16(0x0021),
|
|
139
|
+
writeUInt32(checksum),
|
|
140
|
+
writeUInt32(data.length),
|
|
141
|
+
writeUInt32(data.length),
|
|
142
|
+
writeUInt16(name.length),
|
|
143
|
+
writeUInt16(0),
|
|
144
|
+
name,
|
|
145
|
+
]);
|
|
146
|
+
const centralHeader = Buffer.concat([
|
|
147
|
+
writeUInt32(0x02014b50),
|
|
148
|
+
writeUInt16(20),
|
|
149
|
+
writeUInt16(20),
|
|
150
|
+
writeUInt16(0),
|
|
151
|
+
writeUInt16(0),
|
|
152
|
+
writeUInt16(0),
|
|
153
|
+
writeUInt16(0x0021),
|
|
154
|
+
writeUInt32(checksum),
|
|
155
|
+
writeUInt32(data.length),
|
|
156
|
+
writeUInt32(data.length),
|
|
157
|
+
writeUInt16(name.length),
|
|
158
|
+
writeUInt16(0),
|
|
159
|
+
writeUInt16(0),
|
|
160
|
+
writeUInt16(0),
|
|
161
|
+
writeUInt16(0),
|
|
162
|
+
writeUInt32(0),
|
|
163
|
+
writeUInt32(offset),
|
|
164
|
+
name,
|
|
165
|
+
]);
|
|
166
|
+
|
|
167
|
+
localParts.push(localHeader, data);
|
|
168
|
+
centralParts.push(centralHeader);
|
|
169
|
+
offset += localHeader.length + data.length;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const centralDirectory = Buffer.concat(centralParts);
|
|
173
|
+
const endOfCentralDirectory = Buffer.concat([
|
|
174
|
+
writeUInt32(0x06054b50),
|
|
175
|
+
writeUInt16(0),
|
|
176
|
+
writeUInt16(0),
|
|
177
|
+
writeUInt16(files.length),
|
|
178
|
+
writeUInt16(files.length),
|
|
179
|
+
writeUInt32(centralDirectory.length),
|
|
180
|
+
writeUInt32(offset),
|
|
181
|
+
writeUInt16(0),
|
|
182
|
+
]);
|
|
183
|
+
|
|
184
|
+
await mkdir(path.dirname(zipFile), { recursive: true });
|
|
185
|
+
await writeFile(zipFile, Buffer.concat([...localParts, centralDirectory, endOfCentralDirectory]));
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async function packageSingleChromeExtension(config, extensionName, extensionRoot, rootDir, logger) {
|
|
189
|
+
const extensionDir = path.join(extensionRoot, extensionName);
|
|
190
|
+
await copyExtensionEntries(config.outputDir, extensionDir);
|
|
191
|
+
await writePackagedManifest({ ...config, extensionDir });
|
|
192
|
+
await writePackagedConfig(config);
|
|
193
|
+
await writeZipFile(config);
|
|
194
|
+
|
|
195
|
+
const digest = calculateFileIntegritySync(config.zipFile).sha256.slice(0, 12);
|
|
196
|
+
logger(`Packaged Link Light Chrome extension ${extensionName}: ${path.relative(rootDir, config.zipFile)} (${digest})`);
|
|
197
|
+
return { extensionName, outputDir: config.outputDir, zipFile: config.zipFile, digest };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export async function packageChromeExtension({ argv = process.argv.slice(2), rootDir = process.cwd(), extensionRoot, extensionNames, config, logger = console.log } = {}) {
|
|
201
|
+
extensionRoot = assertExtensionRoot(rootDir, extensionRoot ?? getArgValue(argv, "--extension-root"));
|
|
202
|
+
extensionNames ??= getRequestedExtensions(argv, rootDir);
|
|
203
|
+
if (!Array.isArray(extensionNames) || !extensionNames.length || new Set(extensionNames).size !== extensionNames.length) throw new Error("EXTENSION_SELECTION_REQUIRED");
|
|
204
|
+
for (const extensionName of extensionNames) {
|
|
205
|
+
assertValidExtensionName(extensionName);
|
|
206
|
+
assertExtensionTree(path.join(extensionRoot, extensionName));
|
|
207
|
+
}
|
|
208
|
+
config ??= JSON.parse(await readFile(path.join(rootDir, "config", "local", "extension.local.json"), "utf8"));
|
|
209
|
+
const validated = validatePackageConfig(config, rootDir);
|
|
210
|
+
for (const name of extensionNames) {
|
|
211
|
+
const source = path.join(extensionRoot, name);
|
|
212
|
+
assertSeparateExtensionOutput(source, validated.outputDir);
|
|
213
|
+
assertSeparateExtensionOutput(source, validated.zipFile);
|
|
214
|
+
}
|
|
215
|
+
if (validated.zipFile === validated.outputDir || validated.zipFile.startsWith(validated.outputDir + path.sep)) throw new Error("EXTENSION_OUTPUT_OVERLAP");
|
|
216
|
+
const results = [];
|
|
217
|
+
for (const name of extensionNames) {
|
|
218
|
+
const selected = configForExtension(validated, name, extensionNames.length);
|
|
219
|
+
assertProjectOutput(rootDir, selected.outputDir); assertProjectOutput(rootDir, selected.zipFile);
|
|
220
|
+
results.push(await packageSingleChromeExtension(selected, name, extensionRoot, rootDir, logger));
|
|
221
|
+
}
|
|
222
|
+
return results;
|
|
223
|
+
}
|