-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
165 lines (136 loc) Β· 4.92 KB
/
build.js
File metadata and controls
165 lines (136 loc) Β· 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env node
/**
* Build script for creating standalone swarm-cli executables
* Uses @yao-pkg/pkg to bundle Node.js + swarm-cli into single binaries
*/
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const BUILD_DIR = path.join(__dirname, 'build');
const VERSION_FILE = path.join(BUILD_DIR, 'VERSION.txt');
// Target configurations
const TARGETS = {
linux: 'node22-linux-x64',
macos: 'node22-macos-x64',
windows: 'node22-win-x64',
'linux-arm': 'node22-linux-arm64',
'macos-arm': 'node22-macos-arm64'
};
function getSwarmCliVersion() {
try {
const swarmCliPkgPath = path.join(__dirname, 'node_modules', '@ethersphere', 'swarm-cli', 'package.json');
const swarmCliPkg = JSON.parse(fs.readFileSync(swarmCliPkgPath, 'utf8'));
return swarmCliPkg.version;
} catch (error) {
return 'unknown';
}
}
function checkDependencies() {
console.log('π¦ Checking dependencies...');
if (!fs.existsSync('node_modules')) {
console.error('β Error: node_modules not found');
console.error('Please install dependencies first: pnpm install\n');
process.exit(1);
}
if (!fs.existsSync('node_modules/@ethersphere/swarm-cli')) {
console.error('β Error: @ethersphere/swarm-cli not found');
console.error('Please install dependencies first: pnpm install\n');
process.exit(1);
}
if (!fs.existsSync('node_modules/@yao-pkg')) {
console.error('β Error: @yao-pkg/pkg not found');
console.error('Please install dependencies first: pnpm install\n');
process.exit(1);
}
console.log('β All dependencies found\n');
}
function writeVersionInfo(swarmCliVersion, buildDate) {
const versionInfo = {
swarmCliVersion,
buildDate,
nodeVersion: process.version,
platform: process.platform,
arch: process.arch
};
const versionText = `Swarm CLI Binary
=================
Bundled swarm-cli version: ${swarmCliVersion}
Build date: ${buildDate}
Node.js version: ${process.version}
Built on: ${process.platform}-${process.arch}
To check version in binary, run:
./swarm-cli-linux --bundled-version
`;
fs.writeFileSync(VERSION_FILE, versionText);
fs.writeFileSync(VERSION_FILE.replace('.txt', '.json'), JSON.stringify(versionInfo, null, 2));
console.log(`β Version info written to ${VERSION_FILE}\n`);
}
function buildTarget(targetName, targetSpec) {
console.log(`π¨ Building for ${targetName} (${targetSpec})...`);
// Create build directory
if (!fs.existsSync(BUILD_DIR)) {
fs.mkdirSync(BUILD_DIR, { recursive: true });
}
// For Windows, pkg automatically adds .exe extension
const isWindows = targetName.includes('win');
const outputPath = path.join(BUILD_DIR, `swarm-cli-${targetName}`);
const finalPath = isWindows ? outputPath + '.exe' : outputPath;
try {
execSync(
`npx @yao-pkg/pkg entry.js --target ${targetSpec} --output ${outputPath} --compress GZip`,
{ stdio: 'inherit' }
);
// pkg automatically adds .exe for Windows targets, so no rename needed
console.log(`β Built: ${finalPath}\n`);
return true;
} catch (error) {
console.error(`β Failed to build ${targetName}:`, error.message);
return false;
}
}
function main() {
const args = process.argv.slice(2);
const requestedTarget = args[0] || 'linux';
console.log('π Swarm CLI Builder\n');
console.log('================================\n');
// Check dependencies
checkDependencies();
// Get version info
const swarmCliVersion = getSwarmCliVersion();
const buildDate = new Date().toISOString();
console.log(`π Build Information:`);
console.log(` Swarm CLI version: ${swarmCliVersion}`);
console.log(` Build date: ${buildDate}`);
console.log(` Node.js version: ${process.version}\n`);
// Build
console.log('π¨ Starting build process...\n');
if (requestedTarget === 'all') {
console.log('Building for all platforms...\n');
let successCount = 0;
for (const [name, spec] of Object.entries(TARGETS)) {
if (buildTarget(name, spec)) {
successCount++;
}
}
console.log(`\nβ Built ${successCount}/${Object.keys(TARGETS).length} targets successfully`);
} else if (TARGETS[requestedTarget]) {
buildTarget(requestedTarget, TARGETS[requestedTarget]);
} else {
console.error(`Unknown target: ${requestedTarget}`);
console.log('\nAvailable targets:');
console.log(' - all (build all platforms)');
Object.keys(TARGETS).forEach(name => {
console.log(` - ${name}`);
});
process.exit(1);
}
// Write version info
writeVersionInfo(swarmCliVersion, buildDate);
console.log('\nβ
Build complete!');
console.log(`\nBinaries are in: ${BUILD_DIR}`);
console.log(`\nπ Version info: ${VERSION_FILE}`);
console.log('\nTo test the binary:');
console.log(` ${path.join(BUILD_DIR, 'swarm-cli-linux')} --help`);
console.log(` ${path.join(BUILD_DIR, 'swarm-cli-linux')} --bundled-version`);
}
main();