mirror of
https://github.com/hanzoai/dev.git
synced 2026-10-25 03:32:32 +00:00
fix: reduce npm package size by downloading binaries on install
- Add postinstall script to download platform-specific binaries from GitHub releases - Update package.json to exclude binaries from npm package (only include JS wrappers) - Modify release workflow to upload binaries as GitHub release assets - Fixes 'Payload Too Large' error when publishing to npm (was 219MB, now <1MB)
This commit is contained in:
@@ -112,21 +112,21 @@ jobs:
|
||||
with:
|
||||
path: artifacts/
|
||||
|
||||
- name: Prepare binaries for npm
|
||||
- name: Prepare release assets
|
||||
run: |
|
||||
# Create bin directory in codex-cli
|
||||
mkdir -p codex-cli/bin
|
||||
# Create a directory for all release assets
|
||||
mkdir -p release-assets
|
||||
|
||||
# Copy all binaries to the bin directory
|
||||
# Copy all binaries to release-assets for GitHub Release
|
||||
for dir in artifacts/binaries-*; do
|
||||
cp $dir/* codex-cli/bin/
|
||||
cp $dir/* release-assets/
|
||||
done
|
||||
|
||||
# Make binaries executable
|
||||
chmod +x codex-cli/bin/*
|
||||
chmod +x release-assets/*
|
||||
|
||||
# List binaries for verification
|
||||
ls -la codex-cli/bin/
|
||||
# List assets for verification
|
||||
ls -la release-assets/
|
||||
|
||||
- name: Bump version
|
||||
id: version
|
||||
@@ -222,6 +222,6 @@ jobs:
|
||||
coder
|
||||
```
|
||||
files: |
|
||||
artifacts/binaries-*/*
|
||||
release-assets/*
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GH_PAT || secrets.GITHUB_TOKEN }}
|
||||
@@ -11,9 +11,14 @@
|
||||
"node": ">=20"
|
||||
},
|
||||
"files": [
|
||||
"bin",
|
||||
"bin/coder.js",
|
||||
"bin/codex.js",
|
||||
"postinstall.js",
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"postinstall": "node postinstall.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/just-every/coder.git"
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { existsSync, mkdirSync, createWriteStream, chmodSync } from 'fs';
|
||||
import { join, dirname } from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { get } from 'https';
|
||||
import { platform, arch } from 'os';
|
||||
import { execSync } from 'child_process';
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
// Map Node.js platform/arch to Rust target triples
|
||||
function getTargetTriple() {
|
||||
const platformMap = {
|
||||
'darwin': 'apple-darwin',
|
||||
'linux': 'unknown-linux-musl', // Default to musl for better compatibility
|
||||
'win32': 'pc-windows-msvc'
|
||||
};
|
||||
|
||||
const archMap = {
|
||||
'x64': 'x86_64',
|
||||
'arm64': 'aarch64'
|
||||
};
|
||||
|
||||
const rustArch = archMap[arch()] || arch();
|
||||
const rustPlatform = platformMap[platform()] || platform();
|
||||
|
||||
return `${rustArch}-${rustPlatform}`;
|
||||
}
|
||||
|
||||
async function downloadBinary(url, dest) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const file = createWriteStream(dest);
|
||||
|
||||
get(url, (response) => {
|
||||
if (response.statusCode === 302 || response.statusCode === 301) {
|
||||
// Follow redirect
|
||||
get(response.headers.location, (redirectResponse) => {
|
||||
redirectResponse.pipe(file);
|
||||
file.on('finish', () => {
|
||||
file.close();
|
||||
resolve();
|
||||
});
|
||||
}).on('error', reject);
|
||||
} else if (response.statusCode === 200) {
|
||||
response.pipe(file);
|
||||
file.on('finish', () => {
|
||||
file.close();
|
||||
resolve();
|
||||
});
|
||||
} else {
|
||||
reject(new Error(`Failed to download: ${response.statusCode}`));
|
||||
}
|
||||
}).on('error', reject);
|
||||
});
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const targetTriple = getTargetTriple();
|
||||
const isWindows = platform() === 'win32';
|
||||
const binaryExt = isWindows ? '.exe' : '';
|
||||
|
||||
const binDir = join(__dirname, 'bin');
|
||||
if (!existsSync(binDir)) {
|
||||
mkdirSync(binDir, { recursive: true });
|
||||
}
|
||||
|
||||
// Get package version
|
||||
const packageJson = await import('./package.json', { assert: { type: 'json' } });
|
||||
const version = packageJson.default.version;
|
||||
|
||||
// Binary names to download
|
||||
const binaries = ['coder', 'coder-tui', 'coder-exec'];
|
||||
|
||||
console.log(`Installing @just-every/coder v${version} for ${targetTriple}...`);
|
||||
|
||||
for (const binary of binaries) {
|
||||
const binaryName = `${binary}-${targetTriple}${binaryExt}`;
|
||||
const localPath = join(binDir, binaryName);
|
||||
|
||||
// Skip if already exists
|
||||
if (existsSync(localPath)) {
|
||||
console.log(`✓ ${binaryName} already exists`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const downloadUrl = `https://github.com/just-every/coder/releases/download/v${version}/${binaryName}`;
|
||||
|
||||
console.log(`Downloading ${binaryName}...`);
|
||||
try {
|
||||
await downloadBinary(downloadUrl, localPath);
|
||||
|
||||
// Make executable on Unix-like systems
|
||||
if (!isWindows) {
|
||||
chmodSync(localPath, '755');
|
||||
}
|
||||
|
||||
console.log(`✓ Downloaded ${binaryName}`);
|
||||
} catch (error) {
|
||||
console.error(`✗ Failed to download ${binaryName}: ${error.message}`);
|
||||
console.error(` URL: ${downloadUrl}`);
|
||||
// Continue with other binaries even if one fails
|
||||
}
|
||||
}
|
||||
|
||||
// Create platform-specific symlink/copy for main binary
|
||||
const mainBinary = `coder-${targetTriple}${binaryExt}`;
|
||||
const mainBinaryPath = join(binDir, mainBinary);
|
||||
|
||||
if (existsSync(mainBinaryPath)) {
|
||||
console.log('Setting up main coder binary...');
|
||||
|
||||
// On Windows, we can't use symlinks easily, so update the JS wrapper
|
||||
// On Unix, the JS wrapper will find the correct binary
|
||||
console.log('✓ Installation complete!');
|
||||
} else {
|
||||
console.warn('⚠ Main coder binary not found. You may need to build from source.');
|
||||
}
|
||||
}
|
||||
|
||||
main().catch(error => {
|
||||
console.error('Installation failed:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user