-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathbuild.mts
More file actions
846 lines (750 loc) · 23.2 KB
/
build.mts
File metadata and controls
846 lines (750 loc) · 23.2 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
/* max-file-lines: legitimate — tracks one cohesive module domain; splitting would scatter tightly coupled helpers. */
/**
* Comprehensive build script with intelligent caching.
*
* Builds packages in the correct order:
*
* 1. CLI package (TypeScript compilation and bundling)
* 2. SEA binary for current platform (only with --force)
*
* Note: Yoga WASM and node-smol binaries are downloaded from socket-btm during
* CLI build.
*
* Usage: pnpm run build # Smart build (skips unchanged) pnpm run build --force
* # Force rebuild all + SEA for current platform pnpm run build:sea # Build SEA
* binaries for all platforms pnpm run build --target <name> # Build specific
* target pnpm run build --targets <t1,t2,...> # Build multiple targets pnpm run
* build --platforms # Build all platform binaries pnpm run build --platforms
* --parallel # Build platforms in parallel pnpm run build --help # Show this
* help.
*/
import crypto from 'node:crypto'
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
import path from 'node:path'
import process from 'node:process'
import { fileURLToPath } from 'node:url'
import fg from 'fast-glob'
import colors from 'yoctocolors-cjs'
import { WIN32 } from '@socketsecurity/lib-stable/constants/platform'
import { getDefaultLogger } from '@socketsecurity/lib-stable/logger/default'
import { spawn } from '@socketsecurity/lib-stable/process/spawn/child'
import { CHECKPOINTS } from '../packages/build-infra/lib/constants.mts'
import {
PLATFORM_TARGETS,
formatPlatformTarget,
parsePlatformTarget,
} from '../packages/build-infra/lib/platform-targets.mts'
import { runPipelineCli } from '../packages/build-infra/lib/build-pipeline.mts'
const logger = getDefaultLogger()
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const rootDir = path.resolve(__dirname, '..')
const TARGET_PACKAGES: Record<string, string> = {
__proto__: undefined as unknown as string,
all: './packages/**',
cli: '@socketsecurity/cli',
'cli-sentry': '@socketsecurity/cli-with-sentry',
node: '@socketbin/node-smol-builder-builder',
sea: '@socketbin/node-sea-builder-builder',
socket: 'socket',
}
interface BuildPackageConfig {
name: string
filter: string
outputCheck: string
/**
* Glob patterns (repo-relative) whose file contents contribute to the build
* signature. A change to any matching file invalidates the cache and forces a
* rebuild.
*/
inputs: string[]
}
interface BuildResult {
success: boolean
skipped: boolean
}
interface BuildTargetResult {
duration: string
success: boolean
target: string
}
interface ParsedArgs {
arch: string | undefined
buildArgs: string[]
force: boolean
help: boolean
parallel: boolean
platform: string | undefined
platforms: boolean
target: string | undefined
targets: string[]
}
/**
* Build configuration for each package in the default build order.
*/
const BUILD_PACKAGES: BuildPackageConfig[] = [
{
name: 'CLI Package',
filter: '@socketsecurity/cli',
outputCheck: 'packages/cli/dist/index.js',
inputs: [
'packages/cli/.config/**/*.{mts,ts,json}',
'packages/cli/scripts/**/*.{mts,ts}',
'packages/cli/src/**/*.{mts,ts,cts,json}',
'packages/cli/package.json',
'packages/cli/tsconfig.json',
'packages/build-infra/lib/**/*.{mts,ts}',
'packages/build-infra/package.json',
'pnpm-lock.yaml',
'.node-version',
],
},
]
/**
* Build SEA binary for current platform.
*/
async function buildCurrentPlatformSea(): Promise<{ success: boolean }> {
const { arch, platform } = await import('node:os')
const currentPlatform = platform()
const currentArch = arch()
logger.log('')
logger.log(
`${colors.cyan('→')} Building SEA binary for ${currentPlatform}-${currentArch}...`,
)
const startTime = Date.now()
const result = await spawn(
'pnpm',
[
'--filter',
'@socketsecurity/cli',
'run',
'build:sea',
`--platform=${currentPlatform}`,
`--arch=${currentArch}`,
],
{
shell: WIN32,
stdio: 'inherit',
},
)
const duration = ((Date.now() - startTime) / 1000).toFixed(1)
if (result.code !== 0) {
logger.log(`${colors.red('✗')} SEA build failed (${duration}s)`)
return { success: false }
}
logger.log(`${colors.green('✓')} SEA binary built (${duration}s)`)
return { success: true }
}
/**
* Build a single package.
*/
async function buildPackage(
pkg: BuildPackageConfig,
force: boolean,
): Promise<BuildResult> {
const skip = !needsBuild(pkg, force)
if (skip) {
logger.log(
`${colors.cyan('→')} ${pkg.name}: ${colors.gray('skipped (up to date)')}`,
)
return { success: true, skipped: true }
}
logger.log(`${colors.cyan('→')} ${pkg.name}: ${colors.blue('building...')}`)
const buildScript = force ? 'build:force' : 'build'
const args = ['--filter', pkg.filter, 'run', buildScript]
const startTime = Date.now()
const result = await spawn('pnpm', args, {
shell: WIN32,
stdio: 'inherit',
})
const duration = ((Date.now() - startTime) / 1000).toFixed(1)
if (result.code !== 0) {
logger.log(
`${colors.red('✗')} ${pkg.name}: ${colors.red('failed')} (${duration}s)`,
)
return { success: false, skipped: false }
}
logger.log(
`${colors.green('✓')} ${pkg.name}: ${colors.green('built')} (${duration}s)`,
)
try {
writeSignature(pkg, computeBuildSignature(pkg))
} catch (e) {
logger.warn(
`Could not write build signature for ${pkg.name}: ${e instanceof Error ? e.message : String(e)}`,
)
}
return { success: true, skipped: false }
}
/**
* Build SEA binary for a specific platform.
*/
async function buildPlatformSea(
platform: string,
arch: string,
libc: string | undefined,
): Promise<{ success: boolean }> {
const targetName = formatPlatformTarget(platform, arch, libc)
logger.log('')
logger.log(`${colors.cyan('→')} Building SEA binary for ${targetName}...`)
const seaArgs = [
'--filter',
'@socketsecurity/cli',
'run',
'build:sea',
`--platform=${platform}`,
`--arch=${arch}`,
]
if (libc) {
seaArgs.push(`--libc=${libc}`)
}
const startTime = Date.now()
const result = await spawn('pnpm', seaArgs, {
shell: WIN32,
stdio: 'inherit',
})
const duration = ((Date.now() - startTime) / 1000).toFixed(1)
if (result.code !== 0) {
logger.log(
`${colors.red('✗')} SEA build for ${targetName} failed (${duration}s)`,
)
return { success: false }
}
logger.log(
`${colors.green('✓')} SEA binary for ${targetName} built (${duration}s)`,
)
return { success: true }
}
/**
* Build a single target (for parallel/sequential builds).
*/
export async function buildTarget(
target: string,
buildArgs: string[],
): Promise<BuildTargetResult> {
const startTime = Date.now()
logger.log(`${colors.cyan('→')} [${target}] Starting build...`)
// Check if this is a platform target (e.g., darwin-arm64).
const platformInfo = parsePlatformTarget(target)
if (platformInfo) {
const seaArgs = [
'--filter',
'@socketsecurity/cli',
'run',
'build:sea',
`--platform=${platformInfo.platform}`,
`--arch=${platformInfo.arch}`,
]
if (platformInfo.libc) {
seaArgs.push(`--libc=${platformInfo.libc}`)
}
const result = await spawn('pnpm', seaArgs, {
shell: WIN32,
stdio: 'pipe',
})
const duration = ((Date.now() - startTime) / 1000).toFixed(1)
if (result.code === 0) {
logger.log(
`${colors.green('✓')} [${target}] Build succeeded (${duration}s)`,
)
return { duration, success: true, target }
}
logger.error(`${colors.red('✗')} [${target}] Build failed (${duration}s)`)
if (result.stderr) {
logger.error(`${colors.red('✗')} [${target}] Error output:`)
logger.error(result.stderr)
}
return { duration, success: false, target }
}
// Not a platform target, use pnpm filter for package builds.
const packageFilter = TARGET_PACKAGES[target]
if (!packageFilter) {
throw new Error(`Unknown build target: ${target}`)
}
const pnpmArgs = ['--filter', packageFilter, 'run', 'build', ...buildArgs]
const result = await spawn('pnpm', pnpmArgs, {
shell: WIN32,
stdio: 'pipe',
})
const duration = ((Date.now() - startTime) / 1000).toFixed(1)
if (result.code === 0) {
logger.log(
`${colors.green('✓')} [${target}] Build succeeded (${duration}s)`,
)
return { duration, success: true, target }
}
logger.error(`${colors.red('✗')} [${target}] Build failed (${duration}s)`)
if (result.stderr) {
logger.error(`${colors.red('✗')} [${target}] Error output:`)
logger.error(result.stderr)
}
return { duration, success: false, target }
}
/**
* Compute a SHA-256 signature over the contents of files matched by the
* package's input globs. Files are sorted to keep the hash deterministic.
*/
function computeBuildSignature(pkg: BuildPackageConfig): string {
const files = fg.sync(pkg.inputs, {
cwd: rootDir,
onlyFiles: true,
dot: true,
absolute: true,
})
files.sort()
const hash = createHash('sha256')
for (let i = 0, { length } = files; i < length; i += 1) {
const file = files[i]
const relative = path.relative(rootDir, file)
hash.update(relative)
hash.update('\0')
hash.update(readFileSync(file))
hash.update('\0')
}
return hash.digest('hex')
}
/**
* Check if a package needs to be built. Returns true if build is needed, false
* if can skip.
*
* Rebuild triggers: 1. --force 2. Missing build output 3. Missing signature
* sidecar 4. Current input signature differs from the recorded one.
*/
function needsBuild(pkg: BuildPackageConfig, force: boolean): boolean {
if (force) {
return true
}
const outputPath = path.join(rootDir, pkg.outputCheck)
if (!existsSync(outputPath)) {
return true
}
const stored = readSignature(pkg)
if (!stored) {
return true
}
return computeBuildSignature(pkg) !== stored
}
/**
* Parse command line arguments.
*/
export function parseArgs(): ParsedArgs {
const args = process.argv.slice(2)
let target: string | undefined
let targets: string[] = []
let platforms = false
let parallel = false
let force = false
let help = false
let platform: string | undefined
let arch: string | undefined
const buildArgs: string[] = []
for (let i = 0; i < args.length; i++) {
const arg = args[i]!
if (arg === '--target' && i + 1 < args.length) {
target = args[++i]
} else if (arg === '--targets' && i + 1 < args.length) {
targets = args[++i]!.split(',').map(t => t.trim())
} else if (arg === '--platform' && i + 1 < args.length) {
platform = args[++i]
} else if (arg.startsWith('--platform=')) {
platform = arg.split('=')[1]
} else if (arg === '--arch' && i + 1 < args.length) {
arch = args[++i]
} else if (arg.startsWith('--arch=')) {
arch = arg.split('=')[1]
} else if (arg === '--platforms') {
platforms = true
} else if (arg === '--parallel') {
parallel = true
} else if (arg === '--force') {
force = true
} else if (arg === '--help' || arg === '-h') {
help = true
} else {
buildArgs.push(arg)
}
}
// If --platform and --arch are provided, combine them into target.
if (platform && arch) {
target = `${platform}-${arch}`
}
return {
arch,
buildArgs,
force,
help,
parallel,
platform,
platforms,
target,
targets,
}
}
function readSignature(pkg: BuildPackageConfig): string | null {
const file = signaturePath(pkg)
if (!existsSync(file)) {
return undefined
}
return readFileSync(file, 'utf8').trim()
}
/**
* Run multiple targeted builds in parallel.
*/
async function runParallelBuilds(
targetsToBuild: string[],
buildArgs: string[],
): Promise<void> {
logger.log('')
logger.log('='.repeat(60))
logger.log(
`${colors.blue('Building ' + targetsToBuild.length + ' targets in parallel')}`,
)
logger.log('='.repeat(60))
logger.log('')
logger.log(`Targets: ${targetsToBuild.join(', ')}`)
logger.log('')
// Check if any targets are platform targets that need CLI built first.
const hasPlatformTargets = targetsToBuild.some(
t => parsePlatformTarget(t) !== null,
)
if (hasPlatformTargets) {
const cliOutputPath = path.join(rootDir, 'packages/cli/dist/index.js')
if (!existsSync(cliOutputPath)) {
logger.log(`${colors.cyan('→')} Building CLI first...`)
const cliResult = await buildPackage(BUILD_PACKAGES[0], false)
if (!cliResult.success) {
process.exitCode = 1
return
}
logger.log('')
}
}
const startTime = Date.now()
const results = await Promise.allSettled(
targetsToBuild.map(target => buildTarget(target, buildArgs)),
)
const totalDuration = ((Date.now() - startTime) / 1000).toFixed(1)
logger.log('')
logger.log('='.repeat(60))
logger.log(`${colors.blue('Build Summary')}`)
logger.log('='.repeat(60))
logger.log('')
const successful = results.filter(
r => r.status === 'fulfilled' && r.value.success,
).length
const failed = results.filter(
r =>
r.status === 'rejected' || (r.status === 'fulfilled' && !r.value.success),
).length
logger.log(`${colors.green('Succeeded:')} ${successful}`)
logger.log(`${colors.red('Failed:')} ${failed}`)
logger.log(`${colors.blue('Total:')} ${totalDuration}s`)
logger.log('')
if (failed > 0) {
logger.log(`${colors.red('✗')} One or more builds failed`)
logger.log('')
process.exitCode = 1
return
}
logger.log(`${colors.green('✓')} All builds completed successfully`)
logger.log('')
}
/**
* Run multiple targeted builds sequentially.
*/
async function runSequentialBuilds(
targetsToBuild: string[],
buildArgs: string[],
): Promise<void> {
logger.log('')
logger.log('='.repeat(60))
logger.log(
`${colors.blue('Building ' + targetsToBuild.length + ' targets sequentially')}`,
)
logger.log('='.repeat(60))
logger.log('')
logger.log(`Targets: ${targetsToBuild.join(', ')}`)
logger.log('')
// Check if any targets are platform targets that need CLI built first.
const hasPlatformTargets = targetsToBuild.some(
t => parsePlatformTarget(t) !== null,
)
if (hasPlatformTargets) {
const cliOutputPath = path.join(rootDir, 'packages/cli/dist/index.js')
if (!existsSync(cliOutputPath)) {
logger.log(`${colors.cyan('→')} Building CLI first...`)
const cliResult = await buildPackage(BUILD_PACKAGES[0], false)
if (!cliResult.success) {
process.exitCode = 1
return
}
logger.log('')
}
}
const startTime = Date.now()
const results = []
for (let i = 0, { length } = targetsToBuild; i < length; i += 1) {
const target = targetsToBuild[i]
const result = await buildTarget(target, buildArgs)
results.push(result)
if (!result.success) {
break
}
}
const totalDuration = ((Date.now() - startTime) / 1000).toFixed(1)
logger.log('')
logger.log('='.repeat(60))
logger.log(`${colors.blue('Build Summary')}`)
logger.log('='.repeat(60))
logger.log('')
const successful = results.filter(r => r.success).length
const failed = results.filter(r => !r.success).length
logger.log(`${colors.green('Succeeded:')} ${successful}`)
logger.log(`${colors.red('Failed:')} ${failed}`)
logger.log(`${colors.blue('Total:')} ${totalDuration}s`)
logger.log('')
if (failed > 0) {
logger.log(
`${colors.red('✗')} Build failed at target: ${results.find(r => !r.success)?.target}`,
)
logger.log('')
process.exitCode = 1
return
}
logger.log(`${colors.green('✓')} All builds completed successfully`)
logger.log('')
}
/**
* Run the default smart build — now orchestrated via the shared build-pipeline
* (same system socket-btm/ultrathink/socket-tui/sdxgen use).
*
* Stages: CLI build @socketsecurity/cli via pnpm --filter (the existing
* buildPackage helper handles signature check + skip-on-cached inside the
* workspace; the orchestrator's shouldRun layer complements with a
* content-hashed cache key) SEA build SEA binary for current platform (only
* when --force/--prod) FINALIZED verify expected outputs exist.
*
* Inherits CLI flags from runPipelineCli: --force, --clean, --clean-stage,
* --from-stage, --cache-key, --prod, --dev.
*/
async function runSmartBuild(force: boolean): Promise<void> {
// The orchestrator reads --force/--clean/... off process.argv itself; we
// only pass `force` here so the SEA stage knows whether to run.
const cliPkg = BUILD_PACKAGES[0]!
const cliOutputPath = path.join(rootDir, cliPkg.outputCheck)
await runPipelineCli({
packageRoot: rootDir,
packageName: 'cli',
resolvePlatformArch: async () => 'universal',
getBuildPaths: (mode: string) => ({
buildDir: path.join(rootDir, 'build', mode),
}),
getOutputFiles: () => [cliOutputPath],
stages: [
{
name: CHECKPOINTS.CLI,
sourcePaths: fg.sync(cliPkg.inputs, {
cwd: rootDir,
onlyFiles: true,
dot: true,
absolute: true,
}),
run: async () => {
const result = await buildPackage(cliPkg, force)
if (!result.success) {
throw new Error(`${cliPkg.name} build failed`)
}
return {
smokeTest: async () => {
if (!existsSync(cliOutputPath)) {
throw new Error(`CLI output missing: ${cliOutputPath}`)
}
},
}
},
},
// SEA stage only runs when --force / --prod is set (matches the
// historical behavior of runSmartBuild — plain `pnpm build` stops
// after CLI, `pnpm build --force` also builds SEA for the current
// platform). The skip predicate runs before shouldRun(), so the
// stage is transparently absent on a normal dev build.
{
name: CHECKPOINTS.SEA,
skip: ctx => !force && ctx.buildMode !== 'prod',
// Hash the CLI output into this stage's cache key. Without it,
// shouldRun() only sees external-tools.json + package.json, so a
// CLI rebuild that leaves those files untouched would skip SEA
// and leave a stale binary built against the previous dist/index.js.
sourcePaths: [cliOutputPath],
run: async () => {
const seaResult = await buildCurrentPlatformSea()
if (!seaResult.success) {
throw new Error('SEA binary build failed')
}
return {}
},
},
{
name: CHECKPOINTS.FINALIZED,
run: async () => ({
smokeTest: async () => {
if (!existsSync(cliOutputPath)) {
throw new Error(`CLI output missing: ${cliOutputPath}`)
}
},
}),
},
],
})
}
/**
* Run a targeted build for a specific package or platform.
*/
async function runTargetedBuild(
target: string,
buildArgs: string[],
): Promise<void> {
// Check if this is a platform target (e.g., darwin-arm64).
const platformInfo = parsePlatformTarget(target)
if (platformInfo) {
// Ensure CLI is built first.
const cliOutputPath = path.join(rootDir, 'packages/cli/dist/index.js')
if (!existsSync(cliOutputPath)) {
logger.log(`${colors.cyan('→')} Building CLI first...`)
const cliResult = await buildPackage(BUILD_PACKAGES[0], false)
if (!cliResult.success) {
process.exitCode = 1
return
}
}
// Build SEA for the specified platform.
const result = await buildPlatformSea(
platformInfo.platform,
platformInfo.arch,
platformInfo.libc,
)
process.exitCode = result.success ? 0 : 1
return
}
// Not a platform target, use pnpm filter for package builds.
const packageFilter = TARGET_PACKAGES[target]
if (!packageFilter) {
logger.error(`Unknown build target: ${target}`)
logger.error(
`Available targets: ${Object.keys(TARGET_PACKAGES).join(', ')}`,
)
process.exitCode = 1
return
}
const pnpmArgs = ['--filter', packageFilter, 'run', 'build', ...buildArgs]
const result = await spawn('pnpm', pnpmArgs, {
shell: WIN32,
stdio: 'inherit',
})
process.exitCode = result.code ?? 1
}
/**
* Display help message.
*/
export function showHelp(): void {
logger.log('')
logger.log(`${colors.blue('Socket CLI Build System')}`)
logger.log('')
logger.log('Usage:')
logger.log(
' pnpm run build # Smart build (skips unchanged)',
)
logger.log(
' pnpm run build --force # Force rebuild all + SEA for current platform',
)
logger.log(
' pnpm run build:sea # Build SEA binaries for all platforms',
)
logger.log(
' pnpm run build --target <name> # Build specific target',
)
logger.log(
' pnpm run build --platform <p> --arch <a> # Build specific platform/arch',
)
logger.log(
' pnpm run build --targets <t1,t2,...> # Build multiple targets',
)
logger.log(
' pnpm run build --platforms # Build all platform binaries',
)
logger.log(
' pnpm run build --platforms --parallel # Build platforms in parallel',
)
logger.log(' pnpm run build --help # Show this help')
logger.log('')
logger.log('Default Build Order:')
logger.log(' 1. CLI Package (TypeScript compilation + bundling)')
logger.log(' 2. SEA Binary for current platform (only with --force)')
logger.log('')
logger.log(
'Note: Yoga WASM and node-smol binaries are downloaded from socket-btm',
)
logger.log(' All pre-built binaries are cached in ~/.socket/ directory')
logger.log('')
logger.log('Platform Targets:')
for (let i = 0, { length } = PLATFORM_TARGETS; i < length; i += 1) {
const target = PLATFORM_TARGETS[i]
logger.log(` ${target}`)
}
logger.log('')
logger.log('Other Available Targets:')
for (const target of Object.keys(TARGET_PACKAGES).sort()) {
if (!PLATFORM_TARGETS.includes(target)) {
logger.log(` ${target}`)
}
}
logger.log('')
}
/**
* Path to the sidecar signature file written alongside the build output.
*/
function signaturePath(pkg: BuildPackageConfig): string {
return path.join(rootDir, `${pkg.outputCheck}.build-signature`)
}
function writeSignature(
pkg: BuildPackageConfig,
signature: string,
): void {
writeFileSync(signaturePath(pkg), `${signature}\n`, 'utf8')
}
/**
* Main build function.
*/
async function main(): Promise<void> {
const opts = parseArgs()
if (opts.help) {
showHelp()
return
}
// Handle platforms build.
if (opts.platforms) {
const buildFn = opts.parallel ? runParallelBuilds : runSequentialBuilds
await buildFn(PLATFORM_TARGETS, opts.buildArgs)
return
}
// Handle multiple targets.
if (opts.targets.length > 0) {
const buildFn = opts.parallel ? runParallelBuilds : runSequentialBuilds
await buildFn(opts.targets, opts.buildArgs)
return
}
// Handle single target.
if (opts.target) {
await runTargetedBuild(opts.target, opts.buildArgs)
return
}
// Otherwise, run the smart build with caching.
await runSmartBuild(opts.force)
}
main().catch((e: unknown) => {
const message = e instanceof Error ? e.message : String(e)
logger.error('')
logger.error(`${colors.red('✗')} Unexpected error: ${message}`)
logger.error('')
process.exitCode = 1
})