153 lines
4.9 KiB
JavaScript
Executable File
153 lines
4.9 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
/**
|
|
* IWA Build Script
|
|
* Bundles and signs the web app for Chrome Isolated Web Apps
|
|
*/
|
|
|
|
import { execSync } from 'child_process';
|
|
import fs from 'fs';
|
|
|
|
const DIST_DIR = './dist';
|
|
const WBN_OUTPUT = `${DIST_DIR}/dist.wbn`;
|
|
const SWBN_OUTPUT = `${DIST_DIR}/dist.swbn`;
|
|
const PRIVATE_KEY_PATH = './iwa_private_key.pem';
|
|
const PUBLIC_KEY_PATH = './iwa_public_key.pem';
|
|
const INFO_FILE = `${DIST_DIR}/iwa-info.txt`;
|
|
|
|
function log(message) {
|
|
console.log(`[IWA-Build] ${message}`);
|
|
}
|
|
|
|
if (process.env.IWA !== '1') {
|
|
log('This script runs as part of `npm run build:iwa`. Exiting.');
|
|
process.exit(0);
|
|
}
|
|
|
|
log('Starting IWA build...');
|
|
log(`Working directory: ${DIST_DIR}`);
|
|
|
|
if (!fs.existsSync(DIST_DIR)) {
|
|
throw new Error('Vite build failed. dist/ directory not found.');
|
|
}
|
|
|
|
const files = fs.readdirSync(DIST_DIR).sort();
|
|
log(`Found ${files.length} files in dist/:`);
|
|
|
|
// Generate keys if they don't exist
|
|
if (!fs.existsSync(PRIVATE_KEY_PATH)) {
|
|
log('\nGenerating new signing keys...');
|
|
try {
|
|
execSync('openssl genpkey -algorithm Ed25519 -out ' + PRIVATE_KEY_PATH, {
|
|
stdio: 'inherit',
|
|
env: { ...process.env, OPENSSL_CONF: '' }
|
|
});
|
|
log('Private key created:', PRIVATE_KEY_PATH);
|
|
} catch (err) {
|
|
throw new Error(`Failed to generate private key: ${err.message}`);
|
|
}
|
|
|
|
const privateKeyContent = fs.readFileSync(PRIVATE_KEY_PATH, 'utf8');
|
|
try {
|
|
execSync(`openssl pkey -in ${PRIVATE_KEY_PATH} -pubout -out ${PUBLIC_KEY_PATH}`, {
|
|
stdio: 'inherit',
|
|
env: { ...process.env, OPENSSL_CONF: '' }
|
|
});
|
|
log('Public key created:', PUBLIC_KEY_PATH);
|
|
} catch (err) {
|
|
throw new Error(`Failed to extract public key: ${err.message}`);
|
|
}
|
|
} else {
|
|
log('\nUsing existing signing keys');
|
|
}
|
|
|
|
const publicKeyContent = fs.readFileSync(PUBLIC_KEY_PATH, 'utf8');
|
|
log('\nPublic Key (IWA Identity):');
|
|
console.log(publicKeyContent.trim());
|
|
|
|
let passphrase = process.env.WEB_BUNDLE_SIGNING_PASSPHRASE;
|
|
if (!passphrase) {
|
|
const isInteractive = !process.env.CI && !process.pipes?.length && process.stdin.isTTY;
|
|
|
|
if (isInteractive) {
|
|
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
log('Enter passphrase to encrypt the signing key (or press Enter for unencrypted):');
|
|
rl.on('close', () => { console.log('\n[Cancelled] Build cancelled.'); process.exit(0); });
|
|
rl.question('', answer => { rl.close(); passphrase = answer; });
|
|
} else {
|
|
log('[Non-interactive mode] Using unencrypted key');
|
|
passphrase = '';
|
|
}
|
|
}
|
|
|
|
log('\n=== Bundling into unsigned web bundle ===');
|
|
try {
|
|
execSync(`node node_modules/wbn/bin/wbn.js --dir ${DIST_DIR} -o ${WBN_OUTPUT}`, { stdio: 'inherit' });
|
|
log('Unsigned bundle created:', WBN_OUTPUT);
|
|
const wbnStats = fs.statSync(WBN_OUTPUT);
|
|
log(`Size: ${(wbnStats.size / 1024).toFixed(2)} KB`);
|
|
} catch (err) {
|
|
throw new Error(`Failed to bundle: ${err.message}`);
|
|
}
|
|
|
|
// Sign the bundle if passphrase provided, or always sign (unencrypted)
|
|
if (passphrase !== undefined) {
|
|
log('\n=== Signing web bundle ===');
|
|
try {
|
|
let signArgs = `node node_modules/wbn-sign/bin/wbn-sign.js sign ${WBN_OUTPUT} ${PRIVATE_KEY_PATH} -o ${SWBN_OUTPUT}`;
|
|
if (passphrase && passphrase.length > 0) {
|
|
execSync(signArgs + ' -p "' + passphrase + '"', { stdio: 'inherit' });
|
|
} else {
|
|
execSync(signArgs, { stdio: 'inherit' });
|
|
}
|
|
log('Signed bundle created:', SWBN_OUTPUT);
|
|
const swbnStats = fs.statSync(SWBN_OUTPUT);
|
|
log(`Size: ${(swbnStats.size / 1024).toFixed(2)} KB`);
|
|
} catch (err) {
|
|
throw new Error(`Failed to sign bundle: ${err.message}`);
|
|
}
|
|
} else {
|
|
log('[No signing] Skipping signature generation');
|
|
}
|
|
|
|
// Extract bundle ID from the SWBN file or use public key
|
|
let bundleId = publicKeyContent.trim().replace(/-----[\s\S]*------/g, '').trim();
|
|
bundleId = bundleId.replace(/\s+/g, '');
|
|
const origin = `isolated-app://${bundleId}/`;
|
|
|
|
log('\n=== IWA Information ===');
|
|
console.log(`Web Bundle ID: ${bundleId}`);
|
|
console.log(`IWA Origin: ${origin}`);
|
|
log('Save these values for Chrome Web App Internals');
|
|
|
|
// Write to info file
|
|
const infoContent = `Web Bundle ID: ${bundleId}
|
|
IWA Origin: ${origin}
|
|
|
|
To install your IWA:
|
|
1. Open Chrome (v120+) and enable: chrome://flags/#enable-isolated-web-app-dev-mode
|
|
2. Go to chrome://web-app-internals
|
|
3. Click "Install IWA from Signed Web Bundle"
|
|
4. Browse to the dist.swbn file
|
|
|
|
Alternatively, use Dev Mode Proxy for development:
|
|
3. Click "Install IWA with Dev Mode Proxy"
|
|
4. Enter URL: file://${DIST_DIR}/
|
|
|
|
For testing without signing (unsigned bundle):
|
|
- Use: ${WBN_OUTPUT}
|
|
`;
|
|
fs.writeFileSync(INFO_FILE, infoContent);
|
|
log(`Info written to: ${INFO_FILE}`);
|
|
|
|
log('\n=== Build Complete ===');
|
|
log(`Output files:`);
|
|
console.log(` - ${SWBN_OUTPUT} (signed web bundle)`);
|
|
if (fs.existsSync(WBN_OUTPUT)) {
|
|
console.log(` - ${WBN_OUTPUT} (unsigned web bundle)`);
|
|
}
|
|
console.log(` - ${PRIVATE_KEY_PATH} (private key - keep secure!)`);
|
|
console.log(` - ${PUBLIC_KEY_PATH} (public key - IWA identity)`);
|
|
|
|
log('\nIWA is ready to install!');
|