mirror of
https://github.com/makecindy/cindy.git
synced 2026-12-04 03:49:43 +08:00
* perf(desktop): 裁剪跨平台预编译文件和打包冗余资源 Signed-off-by: Cindy <cindy@Cindys-Mac-mini.local> * perf(desktop): 去重运行依赖并移除发行包开发快照 Signed-off-by: Cindy <cindy@Cindys-Mac-mini.local> --------- Signed-off-by: Cindy <cindy@Cindys-Mac-mini.local> Co-authored-by: Cindy <cindy@Cindys-Mac-mini.local>
70 lines
2.7 KiB
TypeScript
70 lines
2.7 KiB
TypeScript
import * as fs from 'node:fs';
|
|
import * as path from 'node:path';
|
|
|
|
/** Prepare node-pty in the disposable package copy, never in the source checkout. */
|
|
export function preparePackagedNodePty(buildPath: string, platform: string, arch: string) {
|
|
const packageDir = path.join(buildPath, 'node_modules', 'node-pty');
|
|
// The pinned npm package ships macOS/Windows prebuilds; Linux still builds from source.
|
|
if (platform !== 'win32' && platform !== 'darwin') {
|
|
pruneOtherPrebuilds(packageDir);
|
|
return {
|
|
rebuild: true,
|
|
nativePath: path.join(packageDir, 'build', 'Release', 'pty.node'),
|
|
};
|
|
}
|
|
|
|
const prebuildDir = path.join(packageDir, 'prebuilds', `${platform}-${arch}`);
|
|
const required =
|
|
platform === 'win32'
|
|
? [
|
|
'pty.node',
|
|
'conpty.node',
|
|
'conpty_console_list.node',
|
|
'winpty-agent.exe',
|
|
'winpty.dll',
|
|
path.join('conpty', 'conpty.dll'),
|
|
path.join('conpty', 'OpenConsole.exe'),
|
|
]
|
|
: ['pty.node', 'spawn-helper'];
|
|
const missing = required.filter((file) => {
|
|
try {
|
|
const info = fs.statSync(path.join(prebuildDir, file));
|
|
return !info.isFile() || info.size === 0;
|
|
} catch {
|
|
return true;
|
|
}
|
|
});
|
|
if (missing.length) {
|
|
throw new Error(
|
|
`[forge:afterCopy] node-pty prebuild for ${platform}-${arch} is incomplete: ${missing.join(', ')}. ` +
|
|
'Reinstall workspace dependencies with pnpm install --force --frozen-lockfile, then package again.',
|
|
);
|
|
}
|
|
|
|
pruneOtherPrebuilds(packageDir, `${platform}-${arch}`);
|
|
|
|
// node-pty loads build/Release and build/Debug before prebuilds. Remove copied
|
|
// local bindings so a stale Node ABI or another architecture cannot shadow the target.
|
|
for (const variant of ['Release', 'Debug']) {
|
|
for (const file of ['pty.node', 'conpty.node', 'conpty_console_list.node']) {
|
|
fs.rmSync(path.join(packageDir, 'build', variant, file), { force: true });
|
|
}
|
|
}
|
|
if (platform === 'darwin') {
|
|
// pnpm can lose the executable bit while importing spawn-helper from its store.
|
|
fs.chmodSync(path.join(prebuildDir, 'spawn-helper'), 0o755);
|
|
}
|
|
return { rebuild: false, nativePath: path.join(prebuildDir, 'pty.node') };
|
|
}
|
|
|
|
function pruneOtherPrebuilds(packageDir: string, keep?: string): void {
|
|
const prebuilds = path.join(packageDir, 'prebuilds');
|
|
if (!fs.existsSync(prebuilds)) return;
|
|
for (const entry of fs.readdirSync(prebuilds, { withFileTypes: true })) {
|
|
// Preserve loose metadata/licenses. Only platform-specific directories are redundant.
|
|
if (entry.isDirectory() && entry.name !== keep) {
|
|
fs.rmSync(path.join(prebuilds, entry.name), { recursive: true, force: true });
|
|
}
|
|
}
|
|
}
|