2026-03-14 10:04:12 -06:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
/**
|
|
|
|
|
* link-workspace-packages.cjs
|
|
|
|
|
*
|
2026-04-18 12:27:06 +02:00
|
|
|
* Creates node_modules/@singularity-forge/* symlinks pointing to shipped
|
|
|
|
|
* packages/* directories.
|
2026-03-14 10:04:12 -06:00
|
|
|
*
|
|
|
|
|
* During development, npm workspaces creates these automatically. But in the
|
|
|
|
|
* published tarball, workspace packages are shipped under packages/ (via the
|
2026-04-15 22:56:33 +02:00
|
|
|
* "files" field) and the @singularity-forge/* imports in compiled code need node_modules/@singularity-forge/*
|
2026-03-14 10:04:12 -06:00
|
|
|
* to resolve. This script bridges the gap.
|
|
|
|
|
*
|
2026-04-15 22:56:33 +02:00
|
|
|
* Runs as part of postinstall (before any ESM code that imports @singularity-forge/*).
|
2026-03-17 10:35:57 -05:00
|
|
|
*
|
|
|
|
|
* On Windows without Developer Mode or administrator rights, creating symlinks
|
|
|
|
|
* (even NTFS junctions) can fail with EPERM. In that case we fall back to
|
|
|
|
|
* cpSync (directory copy) which works universally.
|
2026-03-14 10:04:12 -06:00
|
|
|
*/
|
2026-03-17 10:35:57 -05:00
|
|
|
const { existsSync, mkdirSync, symlinkSync, cpSync, lstatSync, readlinkSync, unlinkSync } = require('fs')
|
2026-03-14 10:04:12 -06:00
|
|
|
const { resolve, join } = require('path')
|
|
|
|
|
|
|
|
|
|
const root = resolve(__dirname, '..')
|
|
|
|
|
const packagesDir = join(root, 'packages')
|
2026-04-18 12:27:06 +02:00
|
|
|
const scope = '@singularity-forge'
|
|
|
|
|
const scopeDir = join(root, 'node_modules', scope)
|
2026-03-14 10:04:12 -06:00
|
|
|
|
2026-04-18 12:27:06 +02:00
|
|
|
// Directory names under packages/ that should be linked as @singularity-forge/<dir>
|
|
|
|
|
const packageDirs = [
|
|
|
|
|
'native',
|
|
|
|
|
'pi-agent-core',
|
|
|
|
|
'pi-ai',
|
|
|
|
|
'pi-coding-agent',
|
|
|
|
|
'pi-tui',
|
|
|
|
|
'rpc-client',
|
|
|
|
|
'mcp-server',
|
|
|
|
|
]
|
2026-03-14 10:04:12 -06:00
|
|
|
|
2026-04-18 12:27:06 +02:00
|
|
|
if (!existsSync(scopeDir)) {
|
|
|
|
|
mkdirSync(scopeDir, { recursive: true })
|
2026-03-14 10:04:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let linked = 0
|
2026-03-17 10:35:57 -05:00
|
|
|
let copied = 0
|
2026-04-18 12:27:06 +02:00
|
|
|
for (const dir of packageDirs) {
|
2026-03-14 10:04:12 -06:00
|
|
|
const source = join(packagesDir, dir)
|
2026-04-18 12:27:06 +02:00
|
|
|
const target = join(scopeDir, dir)
|
2026-03-14 10:04:12 -06:00
|
|
|
|
|
|
|
|
if (!existsSync(source)) continue
|
|
|
|
|
|
|
|
|
|
// Skip if already correctly linked or is a real directory (bundled)
|
|
|
|
|
if (existsSync(target)) {
|
|
|
|
|
try {
|
|
|
|
|
const stat = lstatSync(target)
|
|
|
|
|
if (stat.isSymbolicLink()) {
|
|
|
|
|
const linkTarget = readlinkSync(target)
|
2026-04-09 18:29:38 -05:00
|
|
|
if (resolve(join(scopeDir, linkTarget)) === source || linkTarget === source) {
|
2026-03-14 10:04:12 -06:00
|
|
|
continue // Already correct
|
|
|
|
|
}
|
|
|
|
|
unlinkSync(target) // Wrong target, relink
|
|
|
|
|
} else {
|
2026-03-17 10:35:57 -05:00
|
|
|
continue // Real directory (e.g., copied or from bundleDependencies), don't touch
|
2026-03-14 10:04:12 -06:00
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 10:35:57 -05:00
|
|
|
let symlinkOk = false
|
2026-03-14 10:04:12 -06:00
|
|
|
try {
|
|
|
|
|
symlinkSync(source, target, 'junction') // junction works on Windows too
|
2026-03-17 10:35:57 -05:00
|
|
|
symlinkOk = true
|
2026-03-14 10:04:12 -06:00
|
|
|
linked++
|
|
|
|
|
} catch {
|
2026-03-17 10:35:57 -05:00
|
|
|
// Symlink failed — common on Windows without Developer Mode or admin rights.
|
|
|
|
|
// Fall back to a directory copy so the package is still resolvable.
|
2026-03-14 10:04:12 -06:00
|
|
|
}
|
|
|
|
|
|
2026-03-17 10:35:57 -05:00
|
|
|
if (!symlinkOk) {
|
|
|
|
|
try {
|
|
|
|
|
cpSync(source, target, { recursive: true })
|
|
|
|
|
copied++
|
|
|
|
|
} catch {
|
|
|
|
|
// Non-fatal — loader.ts will emit a clearer error if resolution still fails
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-14 10:04:12 -06:00
|
|
|
}
|
2026-03-17 10:35:57 -05:00
|
|
|
|
|
|
|
|
if (linked > 0) process.stderr.write(` Linked ${linked} workspace package${linked !== 1 ? 's' : ''}\n`)
|
|
|
|
|
if (copied > 0) process.stderr.write(` Copied ${copied} workspace package${copied !== 1 ? 's' : ''} (symlinks unavailable)\n`)
|
2026-04-28 21:10:17 +02:00
|
|
|
|
2026-04-30 20:21:12 +02:00
|
|
|
// Platform-specific native engine packages live under rust-engine/npm/<suffix>/, not packages/.
|
2026-04-28 21:10:17 +02:00
|
|
|
// Wire them into node_modules/@singularity-forge/ so native.ts can require() them without
|
|
|
|
|
// a registry install. Only link platforms where the binary (forge_engine.node) is present.
|
|
|
|
|
const nativeNpmDir = join(root, 'native', 'npm')
|
|
|
|
|
const engineSuffixes = ['darwin-arm64', 'darwin-x64', 'linux-x64-gnu', 'linux-arm64-gnu', 'win32-x64-msvc']
|
|
|
|
|
for (const suffix of engineSuffixes) {
|
|
|
|
|
const source = join(nativeNpmDir, suffix)
|
|
|
|
|
const binaryPath = join(source, 'forge_engine.node')
|
|
|
|
|
if (!existsSync(source) || !existsSync(binaryPath)) continue
|
|
|
|
|
|
|
|
|
|
const target = join(scopeDir, `engine-${suffix}`)
|
|
|
|
|
if (existsSync(target)) {
|
|
|
|
|
try {
|
|
|
|
|
const stat = lstatSync(target)
|
|
|
|
|
if (stat.isSymbolicLink()) {
|
|
|
|
|
const linkTarget = readlinkSync(target)
|
|
|
|
|
if (resolve(join(scopeDir, linkTarget)) === source || linkTarget === source) continue
|
|
|
|
|
unlinkSync(target)
|
|
|
|
|
} else {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
symlinkSync(source, target, 'junction')
|
|
|
|
|
process.stderr.write(` Linked native engine: @singularity-forge/engine-${suffix}\n`)
|
|
|
|
|
} catch {
|
|
|
|
|
try { cpSync(source, target, { recursive: true }) } catch { /* non-fatal */ }
|
|
|
|
|
}
|
|
|
|
|
}
|