-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild-electron-cli.cjs
More file actions
executable file
·177 lines (149 loc) · 5.97 KB
/
Copy pathbuild-electron-cli.cjs
File metadata and controls
executable file
·177 lines (149 loc) · 5.97 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
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env node
/**
* Build standalone Electron-based CLI binaries
* Creates truly standalone executables that don't require Node.js installation
*
* IMPORTANT: This script temporarily swaps package.json with package.cli.json
* during the build process, then restores the original package.json.
*
* When releasing a new version:
* 1. Update version in package.json
* 2. Update version in package.cli.json (keep in sync!)
* 3. Run this script to build CLI binaries
*/
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
console.log('Building Electron-based CLI binaries...\n');
// Ensure dist is built
if (!fs.existsSync('dist/cli.js')) {
console.log('Building TypeScript first...');
execSync('npm run build', { stdio: 'inherit' });
}
// Read package files
const originalPackage = fs.readFileSync('package.json', 'utf8');
const cliPackageContent = fs.readFileSync('package.cli.json', 'utf8');
// Parse and sync versions
const originalPkg = JSON.parse(originalPackage);
const cliPkg = JSON.parse(cliPackageContent);
if (originalPkg.version !== cliPkg.version) {
console.log(`⚠️ Version mismatch detected!`);
console.log(` package.json: ${originalPkg.version}`);
console.log(` package.cli.json: ${cliPkg.version}`);
console.log(` Syncing package.cli.json to ${originalPkg.version}...\n`);
cliPkg.version = originalPkg.version;
}
// Sync dependencies from main package.json to CLI package.json
if (originalPkg.dependencies) {
if (JSON.stringify(originalPkg.dependencies) !== JSON.stringify(cliPkg.dependencies)) {
console.log(`⚠️ Dependencies mismatch detected!`);
console.log(` Syncing dependencies from package.json to package.cli.json...\n`);
cliPkg.dependencies = originalPkg.dependencies;
}
}
// Save synced package.cli.json if changes were made
if (originalPkg.version !== JSON.parse(cliPackageContent).version ||
JSON.stringify(originalPkg.dependencies) !== JSON.stringify(JSON.parse(cliPackageContent).dependencies)) {
fs.writeFileSync('package.cli.json', JSON.stringify(cliPkg, null, 2) + '\n');
}
const cliPackage = JSON.stringify(cliPkg, null, 2);
try {
// Backup original package.json
fs.writeFileSync('package.json.backup', originalPackage);
// Use CLI package.json
fs.writeFileSync('package.json', cliPackage);
// Temporarily swap forge configs
if (fs.existsSync('forge.config.cjs')) {
fs.renameSync('forge.config.cjs', 'forge.config.cjs.gui-backup');
}
if (fs.existsSync('forge.config.cli.cjs')) {
fs.renameSync('forge.config.cli.cjs', 'forge.config.cjs');
}
// Build for current platform
const platform = process.platform;
const arch = process.arch;
console.log(`\nBuilding CLI for ${platform}-${arch}...`);
// Backup GUI builds if they exist
let guiBackupPath = null;
if (fs.existsSync('out')) {
guiBackupPath = 'out-gui-backup-' + Date.now();
console.log(`⚠️ Backing up existing GUI builds to ${guiBackupPath}...`);
fs.renameSync('out', guiBackupPath);
}
try {
execSync(`npx electron-forge package --platform=${platform} --arch=${arch}`, {
stdio: 'inherit'
});
execSync(`npx electron-forge make --platform=${platform} --arch=${arch}`, {
stdio: 'inherit'
});
// Move CLI output to out-cli directory
if (fs.existsSync('out')) {
if (fs.existsSync('out-cli')) {
fs.rmSync('out-cli', { recursive: true, force: true });
}
fs.renameSync('out', 'out-cli');
}
// Restore GUI builds
if (guiBackupPath && fs.existsSync(guiBackupPath)) {
console.log('✓ Restoring GUI builds...');
fs.renameSync(guiBackupPath, 'out');
}
} catch (error) {
// Restore GUI builds even on error
if (guiBackupPath && fs.existsSync(guiBackupPath)) {
console.log('⚠️ Restoring GUI builds after error...');
if (fs.existsSync('out')) {
fs.rmSync('out', { recursive: true, force: true });
}
fs.renameSync(guiBackupPath, 'out');
}
throw error;
}
// Rename release files to add -CLI suffix
const makeDir = path.join('out-cli', 'make');
if (fs.existsSync(makeDir)) {
const renameInDir = (dir) => {
if (!fs.existsSync(dir)) return;
const files = fs.readdirSync(dir, { recursive: true, withFileTypes: true });
files.forEach(file => {
if (file.isFile()) {
const oldPath = path.join(file.path || file.parentPath, file.name);
let newName = file.name;
// Handle different file types
if (file.name.endsWith('.zip') || file.name.endsWith('.exe')) {
// Add -CLI before the platform identifier
newName = file.name.replace(/(ESP32Tool)(-darwin|-linux|-win32)/, '$1-CLI$2');
} else if (file.name.endsWith('.deb')) {
// esp32tool_1.2.0_amd64.deb -> esp32tool-cli_1.2.0_amd64.deb
newName = file.name.replace(/^esp32tool_/, 'esp32tool-cli_');
} else if (file.name.endsWith('.rpm')) {
// esp32tool-1.2.0-1.x86_64.rpm -> esp32tool-cli-1.2.0-1.x86_64.rpm
newName = file.name.replace(/^esp32tool-/, 'esp32tool-cli-');
}
const newPath = path.join(file.path || file.parentPath, newName);
if (oldPath !== newPath) {
fs.renameSync(oldPath, newPath);
console.log(`Renamed: ${file.name} -> ${newName}`);
}
}
});
};
renameInDir(makeDir);
}
console.log('\n✓ CLI binaries built successfully!');
console.log('Output: out-cli/make/');
} finally {
// Restore forge configs
if (fs.existsSync('forge.config.cjs')) {
fs.renameSync('forge.config.cjs', 'forge.config.cli.cjs');
}
if (fs.existsSync('forge.config.cjs.gui-backup')) {
fs.renameSync('forge.config.cjs.gui-backup', 'forge.config.cjs');
}
// Restore original package.json
fs.writeFileSync('package.json', originalPackage);
if (fs.existsSync('package.json.backup')) {
fs.unlinkSync('package.json.backup');
}
}