feat: add extension packing script and update build script
All checks were successful
Deploy to Production / deploy (push) Successful in 14s
All checks were successful
Deploy to Production / deploy (push) Successful in 14s
This commit is contained in:
6
.gitignore
vendored
6
.gitignore
vendored
@@ -27,3 +27,9 @@ dev-dist
|
||||
extension-release.zip
|
||||
*.zip
|
||||
tools-app-extension-*.zip
|
||||
tools-app-extension-*.crx
|
||||
|
||||
# Extension keys and builds
|
||||
*.pem
|
||||
*.crx
|
||||
scripts/*.pub
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
{
|
||||
"name": "tools-app",
|
||||
"private": true,
|
||||
"version": "0.6.16",
|
||||
"version": "0.6.17",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "vite build",
|
||||
"preview": "vite preview",
|
||||
"pack-extension": "node scripts/pack_crx.js",
|
||||
"postinstall": "mkdir -p public/wasm && cp node_modules/zxing-wasm/dist/reader/zxing_reader.wasm public/wasm/"
|
||||
},
|
||||
"dependencies": {
|
||||
|
||||
@@ -6,9 +6,14 @@ import zipfile
|
||||
# Configuration
|
||||
SOURCE_DIR = "extension"
|
||||
BUILD_DIR = "dist-extension"
|
||||
OUTPUT_ZIP = "extension-release.zip"
|
||||
MANIFEST_FILE = "manifest.json"
|
||||
|
||||
# Read version to create dynamic zip name
|
||||
with open(os.path.join(SOURCE_DIR, MANIFEST_FILE), "r") as f:
|
||||
source_manifest = json.load(f)
|
||||
version = source_manifest.get("version", "unknown")
|
||||
OUTPUT_ZIP = f"tools-app-extension-v{version}.zip"
|
||||
|
||||
# Remove build directory if exists
|
||||
if os.path.exists(BUILD_DIR):
|
||||
shutil.rmtree(BUILD_DIR)
|
||||
|
||||
110
scripts/pack_crx.js
Normal file
110
scripts/pack_crx.js
Normal file
@@ -0,0 +1,110 @@
|
||||
import { execSync } from 'child_process';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
function findKey() {
|
||||
// 1. Check if key provided via CLI
|
||||
if (process.argv[3]) return process.argv[3];
|
||||
|
||||
// 2. Check local project directory (gitignored)
|
||||
const localKey = path.join(process.cwd(), 'scripts', 'key.pem');
|
||||
if (fs.existsSync(localKey)) return localKey;
|
||||
|
||||
// 3. Check common SSH key locations in ~/.ssh
|
||||
const sshDir = path.join(process.env.HOME, '.ssh');
|
||||
const commonKeys = ['id_rsa', 'id_ecdsa', 'id_ed25519'];
|
||||
|
||||
for (const keyName of commonKeys) {
|
||||
const fullPath = path.join(sshDir, keyName);
|
||||
if (fs.existsSync(fullPath)) return fullPath;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
const KEY_PATH = findKey();
|
||||
const INPUT_SOURCE = process.argv[2] || path.join(process.cwd(), 'extension');
|
||||
const TEMP_DIR = path.join(process.cwd(), 'temp_extension_build');
|
||||
|
||||
function pack() {
|
||||
console.log('📦 Packing extension to CRX...');
|
||||
|
||||
if (!KEY_PATH) {
|
||||
console.error('❌ Error: No private key found.');
|
||||
console.log('Tried local scripts/key.pem and common ~/.ssh/id_* keys.');
|
||||
console.log('You can provide a path manually: npm run pack-extension <source> <path/to/key>');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
let extensionDir = INPUT_SOURCE;
|
||||
let isTemp = false;
|
||||
|
||||
try {
|
||||
// If input is a zip file, unzip it first
|
||||
if (INPUT_SOURCE.endsWith('.zip')) {
|
||||
console.log('🤐 Unzipping extension...');
|
||||
if (fs.existsSync(TEMP_DIR)) fs.rmSync(TEMP_DIR, { recursive: true, force: true });
|
||||
fs.mkdirSync(TEMP_DIR);
|
||||
execSync(`unzip -o "${INPUT_SOURCE}" -d "${TEMP_DIR}"`, { stdio: 'pipe' });
|
||||
extensionDir = TEMP_DIR;
|
||||
isTemp = true;
|
||||
}
|
||||
|
||||
// Resolve actual extension directory (handle subdirs in zip)
|
||||
if (!fs.existsSync(path.join(extensionDir, 'manifest.json'))) {
|
||||
const subdirs = fs.readdirSync(extensionDir).filter(f => fs.statSync(path.join(extensionDir, f)).isDirectory());
|
||||
if (subdirs.length === 1 && fs.existsSync(path.join(extensionDir, subdirs[0], 'manifest.json'))) {
|
||||
extensionDir = path.join(extensionDir, subdirs[0]);
|
||||
console.log(`📂 Found manifest in subdirectory: ${extensionDir}`);
|
||||
} else {
|
||||
console.error(`❌ Error: manifest.json not found in ${extensionDir}`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Determine output filename
|
||||
let outputName;
|
||||
if (INPUT_SOURCE.endsWith('.zip')) {
|
||||
outputName = path.basename(INPUT_SOURCE).replace('.zip', '.crx');
|
||||
} else {
|
||||
const manifest = JSON.parse(fs.readFileSync(path.join(extensionDir, 'manifest.json'), 'utf8'));
|
||||
outputName = `tools-app-extension-v${manifest.version}.crx`;
|
||||
}
|
||||
const outputFull = path.join(process.cwd(), outputName);
|
||||
|
||||
// Get version for logging
|
||||
const manifest = JSON.parse(fs.readFileSync(path.join(extensionDir, 'manifest.json'), 'utf8'));
|
||||
const version = manifest.version;
|
||||
|
||||
console.log(`🔑 Using key: ${KEY_PATH}`);
|
||||
console.log(`📂 Source: ${extensionDir} (v${version})`);
|
||||
console.log(`🚀 Running crx3 via npx...`);
|
||||
|
||||
// Command: npx -y crx3 --key <key> --crx <output> <dir>
|
||||
execSync(`npx -y crx3 --key "${KEY_PATH}" --crx "${outputFull}" "${extensionDir}"`, {
|
||||
stdio: 'inherit'
|
||||
});
|
||||
|
||||
// Cleanup any file that crx3 might have created automatically based on temp dir name
|
||||
const unintendedFile = extensionDir + '.crx';
|
||||
if (fs.existsSync(unintendedFile) && unintendedFile !== outputFull) {
|
||||
fs.unlinkSync(unintendedFile);
|
||||
}
|
||||
|
||||
console.log(`\n✅ Success! Extension packed to: ${outputFull}`);
|
||||
} catch (error) {
|
||||
console.error('\n❌ Failed to pack extension.');
|
||||
if (error.message.includes('algorithm')) {
|
||||
console.error('⚠️ Note: Chrome CRX format requires RSA or ECDSA (P-256) keys.');
|
||||
} else {
|
||||
console.error('Error details:', error.message);
|
||||
}
|
||||
process.exit(1);
|
||||
} finally {
|
||||
if (isTemp && fs.existsSync(TEMP_DIR)) {
|
||||
fs.rmSync(TEMP_DIR, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pack();
|
||||
Reference in New Issue
Block a user