Repository navigation
Expand file tree
/
Copy pathbuild.ts
More file actions
135 lines (126 loc) · 4.26 KB
/
Copy pathbuild.ts
File metadata and controls
135 lines (126 loc) · 4.26 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
import { execFileSync } from 'node:child_process'
import { cpSync, existsSync, mkdirSync, rmSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { build } from 'esbuild'
import { identityForOrigin } from './channels'
const watch = process.argv.includes('--watch')
// Optional build-time default server origin (pre-release shares pointed at a
// non-prod environment): SIM_DESKTOP_DEFAULT_ORIGIN=https://www.dev.sim.ai.
// Baked into the bundle so it applies to fresh installs with no settings —
// unlike the SIM_DESKTOP_ORIGIN env var, which only affects terminal-launched
// processes. Official builds leave it unset (default https://www.sim.ai).
const bakedDefaultOrigin = process.env.SIM_DESKTOP_DEFAULT_ORIGIN ?? ''
if (
bakedDefaultOrigin &&
!/^https:\/\/[^\s/]+$/.test(bakedDefaultOrigin) &&
!/^http:\/\/(localhost|127\.0\.0\.1)(:\d+)?$/.test(bakedDefaultOrigin)
) {
console.error(
`SIM_DESKTOP_DEFAULT_ORIGIN must be a bare https origin or http://localhost (got "${bakedDefaultOrigin}")`
)
process.exit(1)
}
if (bakedDefaultOrigin) {
console.log(`• Baking default server origin: ${bakedDefaultOrigin}`)
}
const appIcon = identityForOrigin(bakedDefaultOrigin).icon
const generatedIcon = 'build/generated-icon.icon'
rmSync(generatedIcon, { force: true, recursive: true })
cpSync(appIcon, generatedIcon, { recursive: true })
console.log(`• Selecting desktop icon: ${appIcon}`)
function compileNativeHelpSearch(): void {
const outputDirectory = 'dist/native'
rmSync(outputDirectory, { force: true, recursive: true })
if (process.platform !== 'darwin') return
const nodeExecutable = execFileSync('node', ['-p', 'process.execPath'], {
encoding: 'utf8',
}).trim()
const nodeIncludeDirectory = join(dirname(nodeExecutable), '..', 'include', 'node')
const nodeApiHeader = join(nodeIncludeDirectory, 'node_api.h')
if (!existsSync(nodeApiHeader)) {
throw new Error(`Could not find Node-API headers at ${nodeApiHeader}`)
}
mkdirSync(outputDirectory, { recursive: true })
execFileSync(
'xcrun',
[
'clang++',
'-std=c++17',
'-DNAPI_VERSION=8',
'-fobjc-arc',
'-fblocks',
'-bundle',
'-undefined',
'dynamic_lookup',
'-mmacosx-version-min=12.0',
'-arch',
'arm64',
'-arch',
'x86_64',
'-I',
nodeIncludeDirectory,
'-framework',
'AppKit',
'-framework',
'Foundation',
'-o',
join(outputDirectory, 'help-search.node'),
'native/help-search.mm',
],
{ stdio: 'inherit' }
)
console.log('• Compiled native macOS documentation Help search')
}
const common = {
bundle: true,
platform: 'node' as const,
format: 'cjs' as const,
target: 'node22',
sourcemap: true,
// node-pty resolves a prebuilt .node binary at runtime, so it must stay
// external and be loaded from node_modules rather than inlined here.
external: ['electron', '@lydell/node-pty'],
tsconfig: 'tsconfig.json',
logLevel: 'info' as const,
define: {
'process.env.SIM_DESKTOP_DEFAULT_ORIGIN': JSON.stringify(bakedDefaultOrigin),
},
}
async function run(): Promise<void> {
compileNativeHelpSearch()
if (watch) {
const { context } = await import('esbuild')
const mainCtx = await context({
...common,
entryPoints: ['src/main/index.ts'],
outfile: 'dist/main.cjs',
})
const preloadCtx = await context({
...common,
entryPoints: ['src/preload/index.ts'],
outfile: 'dist/preload.cjs',
})
// Separate from the main-window preload: this one is injected into
// untrusted pages in the built-in browser and must stay minimal.
const browserPreloadCtx = await context({
...common,
entryPoints: ['src/preload/browser/index.ts'],
outfile: 'dist/browser-preload.cjs',
})
await Promise.all([mainCtx.watch(), preloadCtx.watch(), browserPreloadCtx.watch()])
return
}
await Promise.all([
build({ ...common, entryPoints: ['src/main/index.ts'], outfile: 'dist/main.cjs' }),
build({ ...common, entryPoints: ['src/preload/index.ts'], outfile: 'dist/preload.cjs' }),
build({
...common,
entryPoints: ['src/preload/browser/index.ts'],
outfile: 'dist/browser-preload.cjs',
}),
])
}
run().catch((error) => {
console.error(error)
process.exit(1)
})