Repository navigation
Expand file tree
/
Copy pathscripts.js
More file actions
553 lines (469 loc) · 51.6 KB
/
Copy pathscripts.js
File metadata and controls
553 lines (469 loc) · 51.6 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
import {execFile, execFileSync, spawnSync} from 'node:child_process'
import {copyFileSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync} from 'node:fs'
import {homedir, tmpdir} from 'node:os'
import {dirname, join} from 'node:path'
import {fileURLToPath} from 'node:url'
import {Time, saySize4, tickToIso} from '@zootella/ergo'//small helpers from the ergo project, the same in every language fuji is written in
import {buildStep, hashSha256, readSha256} from './build-log.js'
/*
The publishing pipeline for every workspace, in one file at the monorepo root. Everything the package.json scripts do to publish is here, reached by a verb, like hash or upload-installer.
**This file publishes; it does not build.** The desktop workspace builds with build.js, which runs tauri, with dmg.js and win-setup.js, which make the installers tauri doesn't, and with notarize.js for the mac's release; the linux workspace builds in containers; and each then calls in here to stage, hash and send — which is why hashing and uploading exist once rather than once per workspace. `linux/build.js` is the other half of that split and knows nothing about publishing.
Two machines publish fuji. Windows sends the exe. The mac sends the dmg it built natively and the four linux packages it built in docker. So a command means the same thing everywhere while doing different work underneath: `pnpm hash` is one package in desktop on windows and four in linux on the mac, and nobody has to remember which computer they are sitting at. What differs is passed as --source by the workspace that asked.
One file rather than one per workspace, because the facts worth keeping in one place cross the workspace boundary. The published name is the clearest case: the build writes Fuji_0.1.0_aarch64.dmg, which is right for a build directory and wrong for a download link, so hash stages it as fuji.dmg and a link in a page survives the next version. That name is written by the build side and shipped by the site side, and when these were separate scripts each spelled it out for itself. The targets table below is now the only place any of it is said.
Living at the root also settles the working-directory question by force rather than by discipline. Every workspace calls this file, each from its own folder, so nothing here can be relative to wherever node started; every path is built from this file's own location. The scripts this replaced each had their own answer to that, every one of them correct only because pnpm happened to run it from the right place.
Its one companion at the root is build-log.js, which keeps the build log and says times the way the pipeline says them. That is a file of its own so the scripts that build can import it, dmg.js, win-setup.js, notarize.js and linux/build.js, while this file stays something that is only ever run, never imported.
*/
//every path is built from this file's own location, never from the working directory, because every workspace calls this file, each from its own folder
const root = fileURLToPath(new URL('.', import.meta.url))
const configurationFile = join(root, 'desktop/src-tauri/tauri.conf.json')//the file that named the bundle
const manifestFile = join(root, 'desktop/src-tauri/Cargo.toml') //and the crate's, which named the executable
const bundled = join(root, 'desktop/src-tauri/target/release/bundle') //where tauri leaves what it built
const staged = { //where hash puts a package and its sidecar, and where upload looks for them
bundle: join(root, 'desktop/release'), //the dmg and the exe, copied out from under tauri's versioned name
linux: join(root, 'linux/release'), //the four the containers made, already sitting where they were written
}
const icons = join(root, 'desktop/src-tauri/icons') //committed artwork, generated rather than drawn
const loginKeychain = join(homedir(), 'Library/Keychains/login.keychain-db')//where the signing identity lives, on every mac that builds an installer
const built = join(root, 'site/docs/.vitepress/dist') //what vitepress builds
function readStem() {//the first name in Cargo.toml, which is the package's, found with a pattern, since one line of toml needs no parser
let found = readFileSync(manifestFile, 'utf8').match(/^name\s*=\s*"([^"]+)"/m)
if (!found) throw new Error('Cargo.toml names no package: ' + manifestFile)
return found[1]
}
//the product's two names, read before anything is named, from the two files brand.js reads them from for the page; the README's section The two names says which goes where. brandName is productName in tauri.conf.json, the name people read, which tauri names every build with. brandStem is the crate's name in Cargo.toml, the stem of the executable's name, which every published name begins with
const brandName = JSON.parse(readFileSync(configurationFile, 'utf8')).productName
const brandStem = readStem()
/*
Every artifact fuji publishes, and the one place any of it is said.
This was a table of three platforms keyed by process.platform until the linux workspace arrived, and the change matters more than it looks. A platform table quietly assumed that the machine running a command is the machine that made the file — true when each operating system built its own installer, and false the moment a mac started building linux packages in docker. So the key is now the artifact rather than the computer, and which machine can make a thing is a separate question asked by whatMachineMakes below.
source says where the built file is found: 'bundle' is the desktop workspace's build output, under tauri's bundle folder, 'linux' is what the containers left in linux/release.
publish is the name the file takes on the server, and sidecar is the name of the json beside it. They are the same string throughout, and each is brandStem followed by the format, so the published name is the executable's stem the way fuji.exe is fuji's.
The rule for a published name: **every linux package states its architecture, and none carries a version.** macOS and Windows ship one architecture each by decision rather than by accident — there is no intel build and no ARM windows build — so fuji.dmg and fuji.exe need no token, and both names are already published and linked. Linux is where architectures multiply, so every name there says which machine it is for, including the formats that have only one build today. That costs a few characters and means no name ever has to change when an aarch64 flatpak or an ARM rpm turns up.
That decision stands. A versioned filename says what it is when it is sitting in somebody's downloads folder, and costs more than it is worth: a link anyone posts pins that version forever, so it either serves stale software or 404s on the next release, and neither failure announces itself. A stable name is overwritten in place, so every link ever shared keeps handing people the current build. What version a download is belongs on the page, which reads it from the sidecar.
*/
const targets = {
//the two an operating system builds for itself
'dmg': {source: 'bundle', folder: 'dmg', suffix: '.dmg', publish: `${brandStem}.dmg`, sidecar: `${brandStem}.dmg`},
'exe': {source: 'bundle', folder: 'win-setup', suffix: '-setup.exe', publish: `${brandStem}.exe`, sidecar: `${brandStem}.exe`},
//the four the linux workspace builds in containers, and every one names its architecture. giving the ARM deb the bare name fuji.deb is the tempting mistake here, since it is the raspberry pi link: beside fuji.amd64.deb an unadorned name reads like the ordinary choice while being the rarer one, which is a trap laid for the majority. no bare linux name also means none has to be renamed, and no link broken, when a second architecture of some format turns up
'deb-arm64': {source: 'linux', match: /_(arm64)\.deb$/, publish: `${brandStem}.arm64.deb`, sidecar: `${brandStem}.arm64.deb`},
'deb-x64': {source: 'linux', match: /_(amd64)\.deb$/, publish: `${brandStem}.amd64.deb`, sidecar: `${brandStem}.amd64.deb`},
'rpm-x64': {source: 'linux', match: /\.(x86_64)\.rpm$/, publish: `${brandStem}.x86_64.rpm`, sidecar: `${brandStem}.x86_64.rpm`},
'flatpak-x64': {source: 'linux', match: /_(x86_64)\.flatpak$/, publish: `${brandStem}.x86_64.flatpak`, sidecar: `${brandStem}.x86_64.flatpak`},
}
/*
pnpm installer signs the app inside the dmg with a certificate of fuji's own, asked for by one line: signingIdentity "Fuji Desktop" under bundle.macOS in tauri.conf.json. That file cannot hold a comment, so the line is explained here, beside the pipeline that ships what it produces. The dmg fuji publishes is signed with a Developer ID and notarized instead, by pnpm notarize, and this is the fallback kept working beside it, so a release never has to depend on Apple, and a mac with no Apple developer account builds an installer all the same.
Without a signing identity, tauri skips signing, and the app leaves with only the stamp the linker puts on every arm64 executable: nothing seals the bundle, Info.plist is not bound, and codesign --verify reports "code has no resources but signature indicates they must be present". A browser quarantines whatever it downloads, and at the first launch of a quarantined app Gatekeeper reads a signature that fails to verify as corruption. The dialog says "Fuji is damaged and can't be opened. You should move it to the Trash", under a caution triangle, with no button that proceeds. That is what a presenter met on a Mac Studio running Tahoe on 2026-09-23, and it reproduced on the Sequoia mini the next morning from a Chrome download. It had never shown up in development because nothing there quarantines: a dmg built here and dragged in, or fetched with curl, carries no quarantine attribute, and Gatekeeper never looks.
With an identity, tauri runs codesign over every library in the bundle, the executable and then the bundle, without the hardened runtime here: hardenedRuntime is off beside the identity in tauri.conf.json, because the runtime's library validation loads only libraries signed under the app's own Team ID, a self-signed identity has none, and a fallback build carrying libmpv's tree died at launch with it on; pnpm notarize turns it back on for its own build, where the Developer ID gives the app and every library one Team ID and notarization requires it. The seal verifies, so Gatekeeper can read what the app is, an unnotarized app from no known developer, and shows the dialog it has for that: "Apple could not verify Fuji is free of malware", with Done and Move to Trash, and for about an hour afterwards an Open Anyway button under Privacy & Security in System Settings. So this moves fuji from a dialog that calls the file broken and offers no way in to the one every unnotarized app gets, which has a way through. Only a Developer ID certificate from Apple and notarization take that dialog away, which is what pnpm notarize does; Gatekeeper trusts no other certificate, so to it fuji's own reads exactly as the identity "-" did, an ad-hoc seal with nobody's name on it. Tauri does try to notarize after signing, finds no credentials, and logs a warning — expected in every pnpm installer build, since notarizing is pnpm notarize's.
What the certificate changes is who macOS thinks fuji is from one release to the next. A seal records a designated requirement, the rule a later copy must meet to count as the same program, and macOS keeps each permission the user grants, like access to the Downloads folder, against that rule. An ad-hoc seal's rule is the hash of that one build, so every release was a stranger, and asked for the Downloads folder again. A certificate's rule is the bundle identifier signed by that certificate, which every release meets, so a permission given once stays given. A Developer ID's rule names Apple's team rather than one certificate, so moving the published release from Fuji Desktop to the Developer ID cost each user one permission, once, and going back to the fallback would cost one more.
So the original is made once, ever, on the mac that publishes, which signs the fallback with it, and a new computer takes over publishing by importing it. Every other mac that builds an installer makes one of its own, once, and that costs nobody anything: those builds never reach a user, macOS keeps a permission against the certificate rather than its name, and Gatekeeper trusts no self-signed certificate, so a Fuji Desktop anyone can make carries no authority past the mac that made it. Either way it's self-signed, and holds the name Fuji Desktop and nothing else: no email, no organization, no place, because each signed app carries it where anyone can read it with codesign -dvvv. It's state on the mac rather than in this repository, the key and certificate in the login keychain, where codesign finds them by name and signs without the certificate being trusted. From the desktop folder:
pnpm certificate say whether this mac has the identity
pnpm certificate make ~/Desktop/fuji-code-signing.p12 make one for this mac, and a password-protected backup
pnpm certificate import ~/Desktop/fuji-code-signing.p12 bring one back from its backup, like the original onto a new mac that publishes
Make writes the backup to keep somewhere safe off the mac, with its password, which it insists on, since macOS can't import a backup without one, and import is how a new computer takes over publishing without users being asked again. pnpm installer looks for the identity before it builds anything, and names make when it's missing. Either way, the first pnpm installer afterward stops at a dialog asking for the mac's login password so codesign can use the key; Always Allow there is what lets every later build sign without asking. A lost backup of the original means a new certificate, which costs each user one more round of permission prompts and nothing worse. A fork names its own identity here and in tauri.conf.json and makes its own certificate, or goes back to "-".
Windows is untouched by this and has the same story in its own words: an installer with no certificate meets SmartScreen's "Windows protected your PC", and Run anyway sits behind More info. Neither dialog is about the bytes; the sidecar's hash is. Version 0.1.0 was published unsealed on 2026-09-22, sealed after, and notarized from 2026-10-09, and only the sidecar's date tells the three apart.
*/
/*
Which targets this computer stages and sends. Two machines publish fuji and no others: the mac makes its own dmg and, through docker, every linux package; windows makes the exe.
Linux is absent, and its absence is the simpler answer rather than an omission. Somebody can clone this repository on ubuntu or raspberry pi os and run pnpm installer in the desktop workspace, and they will get a package built for the machine they are sitting at — that is development, and it works. What they cannot do is stage and upload it, because a published package comes from the mac where all four are built together against one base image and one lockfile. Teaching this file a third place to look for a built file, so that a borrowed linux box could publish one package out of four, would buy a case nobody has and cost a branch in every function below.
*/
const machines = {
darwin: ['dmg', 'deb-arm64', 'deb-x64', 'rpm-x64', 'flatpak-x64'],
win32: ['exe'],
}
function whatMachineMakes() {
let found = machines[process.platform]
if (!found) throw new Error(
`${brandName} does not publish from ${process.platform}. The dmg and every linux package are staged on the mac, ` +
`the exe on windows. Building here for your own use is a different thing and works: pnpm installer in desktop.`)
return found
}
function readTarget(name) {
let found = targets[name]
if (!found) throw new Error(`no such target: ${name} — say one of ${Object.keys(targets).join(', ')}`)
return found
}
/*
Which targets a command acts on, decided in one place because hash and upload must agree.
Named targets win and are an instruction: asking for one that is not built is an error. With none named it is everything this machine makes, narrowed by --source to the workspace that asked — which is what lets `pnpm hash` mean "the dmg" in desktop and "the four packages" in linux while being the same word and the same code. Run from the root with neither, it means everything this computer makes.
*/
function chosenTargets() {
let args = process.argv.slice(3)
let named = args.filter(a => !a.startsWith('-'))
if (named.length) return {names: named, demanded: true}
let source = (args.find(a => a.startsWith('--source=')) || '').split('=')[1]
let names = whatMachineMakes()
if (source) names = names.filter(name => readTarget(name).source == source)
return {names, demanded: false}
}
//where a graphical file manager gets pointed, per platform rather than per target
const openers = {darwin: 'open', win32: 'explorer', linux: 'xdg-open'}
let server//the destination, filled by readServer before either upload runs
//open the graphical file manager on this platform's finished installer, so it can be double-clicked the way a person who downloaded it would. that is a different and stronger test than starting a built binary in place: an installer has a first-run experience — the publisher warning, where the application ends up, the shortcut it leaves — and none of that happens otherwise
function reveal() {
let name = process.argv[3] || whatMachineMakes()[0]
let target = readTarget(name)
//the folder, not the file: a filename carries the version and would need editing every release
let folder = target.source == 'bundle' ? join(bundled, target.folder) : staged.linux
if (!existsSync(folder)) throw new Error('nothing built yet at ' + folder + ', run pnpm installer first')
console.log('opening ' + folder)
//an absolute path, because explorer resolves a relative one against its own working directory rather than ours. and explorer answers 1 even when it did open the window, so its exit means nothing
execFile(openers[process.platform], [folder], error => {
if (error && process.platform != 'win32') console.error('could not open the file manager: ' + error.message)
})
}
//put what pnpm installer just built onto this machine the way a person would from the download, launching nothing: on the mac a running copy is asked to quit, or ended if it won't, and the app replaces the one in Applications, and on windows setup installs it per user, closing a running copy first as it does for anyone. pnpm installer-install reaches this, since pnpm keeps the name install for itself
function install() {
if (process.platform == 'darwin') {
let app = `${brandName}.app`
let from = join(bundled, 'macos', app), to = join('/Applications', app)
if (!existsSync(from)) throw new Error('nothing built yet at ' + from + ', run pnpm installer first')
let identifier = JSON.parse(readFileSync(configurationFile, 'utf8')).identifier
if (existsSync(to)) {//never remove an app that isn't this one
let there = spawnSync('/usr/libexec/PlistBuddy', ['-c', 'Print :CFBundleIdentifier', join(to, 'Contents/Info.plist')], {encoding: 'utf8'}).stdout.trim()
if (there != identifier) throw new Error(`${to} is not ${brandName}, its identifier is ${there || 'unreadable'}, so it was left alone`)
}
let pattern = join(to, 'Contents/MacOS') + '/'
let running = () => spawnSync('pgrep', ['-f', pattern]).status == 0
let wait = duration => { let until = Date.now() + duration; while (running() && Date.now() < until) spawnSync('sleep', ['0.2']) }
if (running()) {//asked to quit as its own Quit does, so it saves its settings and log, and ended by force if it hasn't after a few seconds, because a step after this one has to find the new build, never the old one still running
console.log(`asking ${brandName} to quit`)
spawnSync('osascript', ['-e', `tell application id "${identifier}" to quit`])//the first time, macOS may ask whether this terminal may control the app
wait(4*Time.second)
if (running()) { console.log(`ending ${brandName}, which did not quit`); spawnSync('pkill', ['-KILL', '-f', pattern]); wait(2*Time.second) }
if (running()) throw new Error(`${brandName} is still running and could not be ended`)
}
buildStep('install', app, '', () => {
rmSync(to, {recursive: true, force: true})//the old copy whole rather than copied over, so a file an earlier build had and this one dropped can't linger inside the sealed bundle
execFileSync('ditto', [from, to])//which keeps the bundle's links, attributes and seal, as a drag in finder does
let executable = `Contents/MacOS/${brandStem}`
if (!readFileSync(join(from, executable)).equals(readFileSync(join(to, executable)))) throw new Error(`${to} does not hold the build just made`)
return {value: '-', words: 'into ' + to}
})
} else if (process.platform == 'win32') {
let built = findBuilt(readTarget('exe'), readVersion())
if (!built) throw new Error('nothing built yet in ' + join(bundled, readTarget('exe').folder) + ', run pnpm installer first')
buildStep('install', built.file, '', () => {
execFileSync(join(built.folder, built.file), [], {stdio: 'inherit'})
return {value: '-', words: 'per user'}
})
} else {
throw new Error(`nothing installs on ${process.platform} this way; install the package pnpm installer made with the distribution's own tools`)
}
}
function readVersion() {//the version comes from the same file that named the bundle, so the two cannot disagree
let version = JSON.parse(readFileSync(configurationFile, 'utf8')).version
if (!version) throw new Error('tauri.conf.json has no version')
return version
}
//find the one file a target describes, and say which architecture it turned out to be. read from the filename rather than from the machine running this, so every number describes the file that exists
function findBuilt(target, version) {
if (target.source == 'bundle') {
let folder = join(bundled, target.folder)
if (!existsSync(folder)) return false
let prefix = `${brandName}_${version}_`//tauri and dmg.js name a build brandName, version, architecture
let names = readdirSync(folder).filter(n => n.startsWith(prefix) && n.endsWith(target.suffix))
if (names.length > 1) throw new Error(`expected one ${prefix}*${target.suffix} in ${folder}, found ${names.length}: ${names.join(', ')}`)
if (!names.length) return false
return {folder, file: names[0], arch: names[0].slice(prefix.length, names[0].length - target.suffix.length)}
}
//a container wrote this one, under whatever name tauri or flatpak chose. the regex does two jobs and the parentheses are not decoration: the group is where the architecture comes from, so a sidecar describes the file that exists rather than repeating something this table already believed
let folder = staged.linux
if (!existsSync(folder)) return false
//the containers write here and hash stages here too, so by the second run a published copy is sitting beside the build it came from — and fuji.x86_64.rpm matches the same pattern Fuji-0.1.0-1.x86_64.rpm does. Skipping every published name is what makes hash repeatable rather than a once-per-clean thing
let published = new Set(Object.values(targets).map(t => t.publish))
let names = readdirSync(folder).filter(n => target.match.test(n) && !published.has(n))
if (names.length > 1) throw new Error(`expected one ${target.match} in ${folder}, found ${names.length}: ${names.join(', ')}`)
if (!names.length) return false
return {folder, file: names[0], arch: names[0].match(target.match)[1]}
}
/*
Stage what this machine built and write a sidecar beside each, building nothing.
Two things a sidecar must not lie about, and each is read rather than assumed: the version comes from tauri.conf.json, the same file that named the bundle; the architecture comes out of the built file's own name, so it describes the file that exists rather than the computer that ran this.
Named with no arguments this means "everything this machine makes", which is one thing on windows and five on the mac. A target that has not been built yet is reported and skipped rather than thrown on, because staging a dmg should not fail merely because nobody has run the linux containers today — but a target asked for by name is an instruction, so that one throws.
*/
async function hash() {
let {names, demanded} = chosenTargets()
let version = readVersion()
for (let name of names) await hashOne(name, version, demanded)
}
async function hashOne(name, version, demanded) {
let target = readTarget(name)
let built = findBuilt(target, version)
if (!built) {
if (demanded) throw new Error(`${name}: nothing built to stage — run the build that makes it first`)
console.log(`skipped ${name} — nothing built yet`)
return false
}
let stage = staged[target.source]
mkdirSync(stage, {recursive: true})
let published = target.publish
let destination = join(stage, published)
let {notarized} = await buildStep('hash', published, `${built.file}, ${version} ${built.arch}`, async () => {
let libmpv = await readLibrary(name, target, built)//the record of the media library inside, checked before anything is copied, so a refusal stages nothing
//copy first, then measure what landed, so every number describes the file the site will ship. a container's output is already sitting in the staging folder under its published name, and copying a file onto itself is both pointless and an error
if (join(built.folder, built.file) != destination) copyFileSync(join(built.folder, built.file), destination)
let bytes = readFileSync(destination)//whole file into memory; a package is a few megabytes and streaming would buy nothing
let hashed = Date.now()//the moment of hashing, kept twice in the sidecar: as ergo's tick in tick, for a program, and as its iso text in date, for a person, so nothing ever reads a date back out of text
let sidecar = {
file: published,
version,
arch: built.arch,
size: bytes.length,
sha256: await hashSha256(bytes),
tick: hashed,
date: tickToIso(hashed),
libmpv,//the record whole, which readLibrary describes
}
writeFileSync(join(stage, target.sidecar + '.json'), JSON.stringify(sidecar, null, '\t') + '\n')
/*
hash, size, filename — and the hash whole, because a cropped hash cannot check a download. No leading verb: the command is called hash, so saying "staged" on every line is a word that carries nothing, and the lines can be counted by looking at them rather than being totalled underneath. Nothing is padded: every package fuji builds comes to two digits of megabytes, so saySize4 writes each size the same width and the columns line up on their own. If that ever stops being true the columns drift a little, which costs less than machinery to prevent it.
*/
console.log(`${sidecar.sha256} ${saySize4(sidecar.size)} ${published}`)
let notarized = name != 'dmg' || isNotarized(destination)
return {value: sidecar.sha256, words: name == 'dmg' ? (notarized ? 'notarized' : 'not notarized') : '', notarized}
})
if (!notarized) warnUnnotarized(published)//after the step's last line, so it is the final thing on the screen
return true
}
/*
The media library inside a package, from the record the recipe writes beside the library it built, desktop/libmpv/build.py: the library's name, size and hash, the day it was built, the hash of the recipe that built it, the tools it was built with, and every source the recipe fetched with its hash. The sidecar carries the record whole, under libmpv, so the repository says of every release, in one tracked file, which library it carried and where each part of that library came from, and a release is traced from its own hash back to the exact sources long after the recipe has moved on.
Four checks keep the record true, and each refuses rather than warns, since the fix is a command that takes seconds when nothing has changed. The recipe checked out must be the recipe that built the library: the record holds the recipe's hash, and a library built by an older one would otherwise ship, without a word, under a commit whose recipe never made it. On the mac and windows, where the library sits in binaries between builds, it must still be the one the record describes, hashed again here, since either could have been replaced. And on every platform the package must be newer than the record beside it, by the two files' times, since a package carries whatever library was there when it was built: an installer carries what was in binaries when tauri ran, and a linux package what the container took from linux/cache, whose record the container writes out just before the packages; so a pnpm library after the packages were made, which writes a newer record, means building them again before hash accepts them, as does a recipe run on the mac with nothing to rebuild, which still writes the record afresh. The containers take the library from linux/cache, where pnpm library put it beside its record, and each writes the record out under its architecture's name; the rpm and the flatpak carry the amd64 library. And the library must have an entry in its platform's library file at the root, which only the command that compiles it writes, so no release carries a library the ledger does not know.
*/
async function readLibrary(name, target, built) {
let bundle = target.source == 'bundle'
let folder = bundle ? join(root, 'desktop/src-tauri/binaries') : staged.linux
let record = join(folder, bundle ? 'libmpv.json' : `libmpv.${built.arch == 'x86_64' ? 'amd64' : built.arch}.json`)
let again = bundle ? 'pnpm library and then the installer, in desktop' : 'pnpm library and then pnpm build, in linux'
if (!existsSync(record)) throw new Error(`${name}: no record of the media library at ${record} — run ${again}, then hash`)
let library = JSON.parse(readFileSync(record, 'utf8'))
let recipe = await readSha256(join(root, 'desktop/libmpv/build.py'))
if (library.recipe != recipe) throw new Error(`${name}: the media library was built by a recipe other than desktop/libmpv/build.py as checked out — run ${again}, then hash`)
if (bundle) {
let sha256 = await readSha256(join(folder, library.file))
if (sha256 != library.sha256) throw new Error(`${name}: ${library.file} in binaries is not the library its record describes — run ${again}, then hash`)
}
if (statSync(record).mtimeMs > statSync(join(built.folder, built.file)).mtimeMs) throw new Error(`${name}: the media library's record is newer than ${built.file}, which carries the library before it — run ${bundle ? 'the installer' : 'pnpm build'}, then hash`)
let ledger = {dmg: 'library-mac.txt', exe: 'library-win.txt', 'deb-arm64': 'library-arm.txt'}[name] || 'library-amd.txt'
if (!existsSync(join(root, ledger)) || !readFileSync(join(root, ledger), 'utf8').includes(library.sha256)) throw new Error(`${name}: ${library.file} has no entry in ${ledger} — pnpm library writes one when it compiles the library, so run it, then ${bundle ? 'the installer' : 'pnpm build'}, then hash`)
return library
}
//whether a dmg carries the notarization ticket pnpm notarize staples to it. one without is either the fallback signed with Fuji Desktop alone, or a pnpm installer build staged by mistake, and only the person publishing knows which
function isNotarized(path) {
return spawnSync('xcrun', ['stapler', 'validate', path], {stdio: 'ignore'}).status == 0
}
function warnUnnotarized(file) {//loud, and never a refusal, because the fallback, the pnpm installer build, ships exactly this
console.log(`\n🍎👮🚓👮♀️⛓️ ${file} is not notarized: a downloaded copy meets the dialog saying Apple could not verify ${brandName} is free of malware`)
console.log(`🍎👮🚓👮♀️⛓️ pnpm notarize and then pnpm hash put a notarized one in its place, unless this is the Fuji Desktop fallback\n`)
}
/*
Both uploads ship finished files and do no thinking about them: the site was built by vitepress, the installer and its sidecar by hash above, on the machine that could build them.
Two destinations, and the difference between them is the reason there are two. The site goes as an account that administers the server, over rsync, which mirrors with --delete so a file dropped from the build leaves the server too. The installers go as a second account with no shell at all: chrooted to the hostname's directory and able to speak only the SFTP file protocol. That way a compromise of the machine that builds an installer could overwrite the installers and their sidecars and nothing else. rsync is unavailable to such an account by construction, since rsync works by running a program on the far side — scp is what remains, and since OpenSSH 9.0 scp transfers over SFTP anyway, so the two agree on the wire.
The server answers one hostname from two directories: a request that misses the site's directory falls through to the downloads directory. That is why an installer sits at the apex, like fujidesktop.app/fuji.exe, rather than under a downloads path, and why a site deploy has no way to reach an installer.
## Running this against your own server
The destination lives in .env at the monorepo root, which is gitignored, so a fresh clone will not have one and readServer will say which values are missing and stop. Write it yourself — seven values and no logic:
DEPLOY_HOST=files.example.com
DEPLOY_PORT=22
DEPLOY_SITE_USER=deploy
DEPLOY_SITE_PATH=/var/www/example.com/site/
DEPLOY_FILES_USER=upload
DEPLOY_FILES_PATH=/downloads/
DEPLOY_FILES_KEY=/home/you/.ssh/upload_ed25519
On an EC2 instance or a DigitalOcean droplet the ordinary account is something like ubuntu or deploy, the host can be an IP like 203.0.113.10, and the port is 22.
The two paths look nothing alike, which is the one thing here that surprises people. DEPLOY_SITE_PATH is absolute because that account sees the whole filesystem. DEPLOY_FILES_PATH is short because the account that uses it is chrooted by sshd, so its own directory is its filesystem root and the path has to be written as that account sees it. Give it the full path `ls` would show you elsewhere and scp fails with no such file or directory — correctly, since inside the chroot there is no such path, and it costs a confusing half hour to work out why. Using one ordinary unchrooted account for both works and is the simplest thing that runs; splitting them, so the machine that builds an installer holds credentials that cannot administer anything, earns the extra setup once a release is public.
Keep the trailing slashes: rsync ignores one on a destination, but scp given a path that does not exist yet will write a file by that name, and the slash turns that into an error instead. DEPLOY_FILES_KEY names the installer account's own key rather than letting ssh offer whatever it finds, which matters with two accounts, since the default identity is the administrative one.
The file sits at the monorepo root rather than inside a workspace. Vite reads a .env inside the workspace it builds and copies any VITE_-prefixed value into the client bundle, which would publish these values on the website itself. At the root it is outside what Vite looks at, so that hazard stops existing rather than becoming something to remember — which is also the reason not to move it down.
Node does not read .env on its own; the package.json scripts pass --env-file-if-exists, which is why these run as pnpm upload rather than node scripts.js. Run it the second way, or leave the file unwritten, and the values arrive empty, which readServer catches and explains. The tolerant spelling of the flag matters: plain --env-file makes node refuse to start when the file is absent, printing one line and never reaching that message.
*/
function readServer() {//gather the destination from the environment, naming whatever the scripts did not fill
let required = [
'DEPLOY_HOST', 'DEPLOY_PORT',
'DEPLOY_SITE_USER', 'DEPLOY_SITE_PATH',
'DEPLOY_FILES_USER', 'DEPLOY_FILES_PATH', 'DEPLOY_FILES_KEY',
]
let missing = required.filter(name => !process.env[name])
if (missing.length > 0) throw new Error(`.env at the monorepo root is missing ${missing.join(', ')} — see the essay in scripts.js, and run this as pnpm rather than node, so the env file is passed`)
return {
host: process.env.DEPLOY_HOST,
port: process.env.DEPLOY_PORT,
siteUser: process.env.DEPLOY_SITE_USER,//administers the server; ships the site over rsync
sitePath: process.env.DEPLOY_SITE_PATH,
filesUser: process.env.DEPLOY_FILES_USER,//no shell, chrooted, sftp only; ships installers over scp
filesPath: process.env.DEPLOY_FILES_PATH,
filesKey: process.env.DEPLOY_FILES_KEY,//that account's own key, so an installer upload never offers the admin one
}
}
function uploadSite() {//ship the built site, mirroring so a file dropped from the build leaves the server too
if (process.platform == 'win32') throw new Error('the site ships from the Mac — windows has no rsync, and this would fail with a spawn error rather than a sentence')
if (!existsSync(built)) throw new Error('no build to upload, run pnpm build first: ' + built)
//refuse to ship a sidecar from inside the site. the server answers one hostname from two directories and checks the site's first, so a fuji.*.json in this build would shadow the real one in downloads and pin the download page to whatever hash it holds until the next deploy. one gets here by sitting in docs/public, which a retired fixtures script used to copy them into for local development — the files are gitignored, so a machine that ran it still has them and no other machine can tell
let shadowing = readdirSync(built).filter(name => name.startsWith(`${brandStem}.`) && name.endsWith('.json'))
if (shadowing.length > 0) throw new Error(`${shadowing.join(', ')} would ship inside the site and shadow the real sidecar on the server — delete site/docs/public/${brandStem}.*.json on this machine, then build again`)
server = readServer()
//--delete is load-bearing: without it every previous deploy's hashed assets accumulate on the server forever. it is also the reason the installers live in a directory this cannot reach
buildStep('upload', 'site', '', () => {
execFileSync('rsync', [
'-avz', '--delete',
built + '/',//trailing slash: copy the contents, not the directory itself
`${server.siteUser}@${server.host}:${server.sitePath}`,
'-e', `ssh -p ${server.port}`,//rsync spells the port inside the ssh command, lowercase -p
], {stdio: 'inherit'})
return {value: '-', words: 'the site, mirrored'}
})
}
//Ship what this machine staged: every package it makes, and the sidecar beside each. Named with no arguments this is one file on windows and five on the mac, and the same word means "this machine's work" in both places — which is the whole reason a command is not named per platform.
async function uploadInstaller() {
let {names, demanded} = chosenTargets()
let version = readVersion()
//gather and check everything before asking for credentials, so a missing .env is never what hides a stale sidecar, and a half-finished release is never half uploaded
let sending = []
for (let name of names) {
let target = readTarget(name)
let stage = staged[target.source]
let sidecarName = target.sidecar + '.json'
if (!existsSync(join(stage, sidecarName))) {
if (demanded) throw new Error(`${name}: no sidecar staged — run pnpm hash on this machine first`)
console.log(`skipped ${name.padEnd(12)} not staged`)
continue
}
let sidecar = JSON.parse(readFileSync(join(stage, sidecarName), 'utf8'))
await checkSidecar(name, stage, sidecar, sidecarName, version)
sending.push({name, stage, file: sidecar.file, sidecarName, sha256: sidecar.sha256})
}
if (!sending.length) throw new Error('nothing staged to upload — run pnpm hash first')
server = readServer()
reportTransport()
//the package before its sidecar, every time, so the page never fetches a hash for a file still arriving
for (let one of sending) buildStep('upload', one.file, '', () => {
send(one.stage, one.file); send(one.stage, one.sidecarName)
return {value: one.sha256, words: 'and ' + one.sidecarName}
})
console.log(`sent ${sending.length} package${sending.length == 1 ? '' : 's'} and ${sending.length} sidecar${sending.length == 1 ? '' : 's'}`)
for (let one of sending) if (one.name == 'dmg' && !isNotarized(join(one.stage, one.file))) warnUnnotarized(one.file)//last, so it is the final thing on the screen
}
function send(folder, name) {//copy one file into the downloads directory as the restricted account
//run from the staging directory and name the file bare. a windows absolute path contains the colon scp uses to split host from path, and a bare filename makes that question stop existing
execFileSync('scp', [
'-P', server.port,//scp spells the port capital -P, unlike ssh and rsync
'-o', 'IdentitiesOnly=yes',//offer only the key named below; without this, keys loaded in ssh-agent are offered too, and can go first, so the admin key could be tried
'-i', server.filesKey,//with IdentitiesOnly above, the admin key is never tried
name,
`${server.filesUser}@${server.host}:${server.filesPath}`,
], {stdio: 'inherit', cwd: folder})
}
//catch a sidecar left from a previous release, which would otherwise publish a hash describing a file nobody can download. it cannot see a stale build — a sidecar truthfully describing a package compiled before the last source change passes — and it defends against nobody who can write here
async function checkSidecar(name, stage, sidecar, sidecarName, version) {
if (!existsSync(join(stage, sidecar.file))) throw new Error(`${name}: ${sidecarName} names ${sidecar.file}, which is not beside it`)
let bytes = readFileSync(join(stage, sidecar.file))
let sha256 = await hashSha256(bytes)
if (sidecar.version != version) throw new Error(`${name}: sidecar says version ${sidecar.version}, tauri.conf.json says ${version}; re-run pnpm hash`)
if (sidecar.size != bytes.length) throw new Error(`${name}: sidecar says ${sidecar.size} bytes, the file is ${bytes.length}; re-run pnpm hash`)
if (sidecar.sha256 != sha256) throw new Error(`${name}: sidecar hash does not describe this file; re-run pnpm hash`)
if (!sidecar.libmpv) throw new Error(`${name}: sidecar carries no record of the media library inside; re-run pnpm hash`)
console.log(`checked ${sidecar.sha256} ${saySize4(bytes.length)} ${sidecar.file} ${sidecar.version} ${sidecar.arch}`)
}
//say which scp is about to run. windows carries two openssh installs, and which one a script gets depends on the shell it was launched from; they differ in how they judge a private key's permissions, so this is the first thing anyone will want to know when authentication fails. diagnostic only, so it never throws — a missing scp should be reported by the transfer, not by this
function reportTransport() {
try {
let found = execFileSync(process.platform == 'win32' ? 'where.exe' : 'which', ['scp'], {encoding: 'utf8'}).trim().split('\n')[0]
console.log('using ' + found)
} catch (e) {
console.log('using could not locate scp')//say so and carry on; the transfer below reports the real failure
}
}
//the three tauri icon runs stay in package.json, where pnpm is what puts the tauri cli on the path; this is the part after them. each run writes a folder of its own, and these are the files that have to come out from under a generated name and sit where tauri.conf.json and the windows manifest expect them, and then the Tahoe icon, compiled. they are committed artifacts, because a tauri cli upgrade does not refresh them and only a run of this does
function iconsCollect() {
let copies = [
['.mac/icon.icns', 'mac/icon.icns'], //the dock icon, inset to apple's grid, kept where the shared run cannot overwrite it
['.tile/Square284x284Logo.png', 'tile/tile-medium.png'], //the start menu tile, renamed because the manifest attribute names the size and the file is just an asset
['.tile/Square142x142Logo.png', 'tile/tile-small.png'],
]
for (let [from, to] of copies) {
mkdirSync(dirname(join(icons, to)), {recursive: true})
copyFileSync(join(icons, from), join(icons, to))
console.log('copied ' + to)
}
iconsCompile()
}
//the icon Tahoe and later show, compiled from AppIcon.icon into mac/Assets.car by actool, which comes only with Xcode. the mac keeps the command line tools as its active developer folder, so this reaches Xcode for the one command through DEVELOPER_DIR, and Assets.car is committed, so no build needs Xcode, only a change to this icon. anywhere without Xcode, windows included, it says so and leaves the committed file as it is. the README in the icons folder has the rest
function iconsCompile() {
let xcode = '/Applications/Xcode.app/Contents/Developer'//where Xcode installs itself
if (process.platform != 'darwin' || !existsSync(xcode)) { console.log('skipped mac/Assets.car, which only Xcode on a Mac can compile; the committed one stands'); return }
let out = mkdtempSync(join(tmpdir(), 'actool-'))//actool writes three files and only Assets.car is wanted: its own icns falls short of fuji's, and its plist says what Info.plist already does
let report = execFileSync('xcrun', ['actool', join(icons, 'AppIcon.icon'), '--compile', out, '--platform', 'macosx', '--target-device', 'mac', '--minimum-deployment-target', '11.0',
'--app-icon', 'AppIcon',//the folder's name without .icon, or actool makes nothing and says nothing
'--output-partial-info-plist', join(out, 'partial.plist'),
], {env: {...process.env, DEVELOPER_DIR: xcode}, encoding: 'utf8'})//actool answers in a plist on standard output, its errors included, and exits 0 even when it makes nothing
if (!existsSync(join(out, 'Assets.car'))) throw new Error(`actool made no Assets.car from AppIcon.icon, and reported: ${report.trim()}`)
copyFileSync(join(out, 'Assets.car'), join(icons, 'mac/Assets.car'))
rmSync(out, {recursive: true, force: true})
console.log('compiled mac/Assets.car')
}
//the signing identity on this mac, which the signing essay above explains: with nothing after it, say whether the login keychain holds it; make writes a new certificate for this mac and its backup; import brings a backup in, like the original onto a new mac that publishes. Either way the identity goes in through the backup, so the first run proves the backup works
function certificate() {
let [action, backup] = process.argv.slice(3)
let name = JSON.parse(readFileSync(configurationFile, 'utf8')).bundle.macOS.signingIdentity//the identity tauri signs with, and the certificate's whole subject
if (action == 'ready') return _certificateReady(name)
if (process.platform != 'darwin') throw new Error('a signing certificate is the mac\'s alone; nothing on this platform signs with one')
if (!name || name == '-') throw new Error('tauri.conf.json signs ad hoc, with signingIdentity "-", so there is no certificate to look for')
let found = _certificateFind(name)
if (found) {
console.log(`found ${found} "${name}" in the login keychain`)
if (action) throw new Error(`the login keychain already holds "${name}", so ${action} would only make a second identity by that name`)
return
}
if (!action) { console.log(`missing no identity named "${name}" in the login keychain; pnpm certificate make <backup.p12> makes one for this mac, and on the mac that publishes, pnpm certificate import <backup.p12> brings in the original`); return }
if (!backup) throw new Error(`say where the backup is: pnpm certificate ${action} <backup.p12>`)
if (action == 'make') _certificateMake(name, backup)
else if (action != 'import') throw new Error('say which: make or import')
execFileSync('security', ['import', backup, '-k', loginKeychain, '-T', '/usr/bin/codesign'], {stdio: 'inherit'})//no -P, so macOS asks for the backup's password in its own dialog; -T names codesign in the key's access list, though macOS still asks once, at the first signing, as the essay says
let made = _certificateFind(name)
if (!made) throw new Error(`imported ${backup}, but the login keychain still has no identity named "${name}"; the backup holds a different one`)
console.log(`ready ${made} "${name}" in the login keychain`)
}
function _certificateReady(name) {//pnpm installer's first step: on a mac, stop before building anything if the identity tauri will sign with is missing, naming the command that makes one, where codesign would say only that it found none, at the end of the build. Elsewhere nothing signs, and an ad hoc seal needs no identity
if (process.platform != 'darwin' || !name || name == '-') return
if (!_certificateFind(name)) throw new Error(`no identity named "${name}" in the login keychain, which pnpm installer signs with — pnpm certificate make <backup.p12> makes one for this mac, once, and then pnpm installer again`)
}
function _certificateMake(name, backup) {//a new key and certificate, written only into the password-protected backup; the loose key lives a moment in a private folder and is gone either way
if (existsSync(backup)) throw new Error(`${backup} is already there, and may be the only copy of the original; if make wrote it and the import didn't finish, pnpm certificate import ${backup} picks up from there, and otherwise choose another path`)
let folder = mkdtempSync(join(tmpdir(), 'certificate-'))//the user's own temporary folder, readable only by them
try {
let key = join(folder, 'key.pem'), certificate = join(folder, 'certificate.pem')
execFileSync('/usr/bin/openssl', ['req', '-x509', '-newkey', 'rsa:2048', '-nodes', '-keyout', key, '-out', certificate,//macos's own libressl rather than whatever openssl is first on the path: its backup is one security imports, where openssl 3's fails as a wrong password
'-days', '7300',//twenty years, since codesign won't sign with an expired certificate, and a new one is a new identity
'-subj', `/CN=${name}`,//the name and nothing else, since every signed app carries this certificate where anyone can read it
'-addext', 'keyUsage=critical,digitalSignature', '-addext', 'extendedKeyUsage=critical,codeSigning', '-addext', 'basicConstraints=critical,CA:false',//good for signing code and nothing else
], {stdio: ['ignore', 'ignore', 'inherit']})
console.log(`made "${name}"; now a password for the backup, twice`)
execFileSync('/usr/bin/openssl', ['pkcs12', '-export', '-inkey', key, '-in', certificate, '-name', name, '-out', backup], {stdio: 'inherit'})//openssl asks for the password itself, so it never passes through here
if (spawnSync('/usr/bin/openssl', ['pkcs12', '-in', backup, '-noout', '-passin', 'pass:'], {stdio: 'ignore'}).status == 0) {//the backup opens with no password, which openssl allows and macOS's import refuses, its dialog taking no empty answer
rmSync(backup)
throw new Error('the backup needs a password, since macOS cannot import one without; run make again and give it one')
}
console.log(`wrote ${backup}; keep it, and its password, somewhere safe off this mac`)
} finally {
rmSync(folder, {recursive: true, force: true})
}
}
function _certificateFind(name) {//the sha-1 of the code signing identity named exactly name in the search list, or false; find-identity lists untrusted ones too, which a self-signed certificate always is, and codesign signs with them all the same
let listed = execFileSync('security', ['find-identity', '-p', 'codesigning'], {encoding: 'utf8'})
for (let [, hash, named] of listed.matchAll(/\d+\) ([0-9A-F]{40}) "(.*)"/g)) if (named == name) return hash
return false
}
//the verb package.json passes. these are spelled out rather than shared, because the script names a person types differ between the two workspaces on purpose — pnpm upload means the installer in desktop and the site in site — and this file should never have to guess which one called it
const commands = {
'reveal': reveal,
'install': install,
'hash': hash,
'upload-installer': uploadInstaller,
'upload-site': uploadSite,
'icons-collect': iconsCollect,
'certificate': certificate,
}
async function main() {//one gate in, one gate out
let command = commands[process.argv[2]]
if (!command) throw new Error('say which: ' + Object.keys(commands).join(', '))
await command()
}
main().catch(e => { console.error('🚧 Error:', e); process.exitCode = 1 })