Repository navigation
Expand file tree
/
Copy pathship.mjs
More file actions
90 lines (83 loc) · 4.78 KB
/
Copy pathship.mjs
File metadata and controls
90 lines (83 loc) · 4.78 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
#!/usr/bin/env node
/**
* One command from a commit to a served, verified build.
*
* pnpm ship production studio (docker-compose.yml, service `studio`, port OCCLUDE_PORT or 4173)
* pnpm ship dev the isolated dev studio (docker-compose.yml + compose.3d.yml, service `dev`, port 5273);
* run it from the checkout that owns that compose project
* pnpm ship --push also push HEAD to origin (and to `dev` when on master) once the served stamp is verified
* pnpm ship dev --quick
* no gates, no image: build the studio on the host, copy dist into the running dev
* container, verify the stamp (~1 min). The docs are the last docs build; --docs
* rebuilds them too. For carrying on with a sketch, not for landing work.
*
* The Docker build IS the verification: the image only exists once the nine
* gates in check.mjs pass inside it. This script adds what was done by hand
* before: a traceable build stamp, the restart, and proof that the served
* bundle carries that stamp. A dirty tree still ships (the stamp gets a `+`),
* so a fix can be tried before it is committed; --push refuses a dirty tree.
*/
import { spawnSync } from 'node:child_process';
import { existsSync } from 'node:fs';
import { setTimeout as sleep } from 'node:timers/promises';
const args = process.argv.slice(2);
const target = args.find((a) => !a.startsWith('--')) ?? 'prod';
const push = args.includes('--push');
const quick = args.includes('--quick');
const docs = args.includes('--docs');
if (!['prod', 'dev'].includes(target)) { console.error(`usage: ship [prod|dev] [--push] | ship dev --quick [--docs]`); process.exit(2); }
if (quick && target !== 'dev') { console.error('ship: --quick is for dev only — prod ships through the gates'); process.exit(2); }
const sh = (cmd, argv, opts = {}) => {
const r = spawnSync(cmd, argv, { stdio: 'inherit', ...opts });
if (r.status !== 0) { console.error(`\nship: ${cmd} ${argv.join(' ')} failed (${r.status})`); process.exit(r.status ?? 1); }
};
const out = (cmd, argv) => spawnSync(cmd, argv, { encoding: 'utf8' }).stdout.trim();
const head = out('git', ['rev-parse', '--short', 'HEAD']);
const dirty = out('git', ['status', '--porcelain', '--untracked-files=no']) !== '';
const branch = out('git', ['rev-parse', '--abbrev-ref', 'HEAD']);
if (push && dirty) { console.error('ship: --push needs a clean tree (commit first)'); process.exit(2); }
const stamp = `${head}${dirty ? '+' : ''}${target === 'dev' ? '-dev' : ''}`;
const compose = target === 'dev'
? ['compose', '-p', 'occlude-3d', '-f', 'docker-compose.yml', '-f', 'compose.3d.yml', '--profile', 'dev']
: ['compose'];
const service = target === 'dev' ? 'dev' : 'studio';
const port = target === 'dev' ? 5273 : Number(process.env.OCCLUDE_PORT ?? 4173);
const env = { ...process.env, OCCLUDE_BUILD_STAMP: stamp };
console.log(`ship: ${target} ${stamp} (${branch}${dirty ? ', dirty' : ''})`);
const t0 = Date.now();
if (quick) {
// The server reads dist per request, so a copy needs no restart. A vite
// build empties dist, docs included; the docs come back from their own
// last build unless --docs (or no build yet) asks for a fresh one.
const studio = 'packages/occlude-studio';
const docsDist = 'packages/occlude-docs/dist';
sh('pnpm', ['--filter', 'occlude-studio', 'build'], { env });
if (docs || !existsSync(docsDist)) sh('pnpm', ['--filter', 'occlude-docs', 'build'], { env });
else sh('cp', ['-r', docsDist, `${studio}/dist/docs`]);
sh('docker', [...compose, 'exec', service, 'rm', '-rf', `/src/${studio}/dist`]);
sh('docker', [...compose, 'cp', `${studio}/dist`, `${service}:/src/${studio}/dist`]);
} else {
sh('docker', [...compose, 'build', service], { env });
sh('docker', [...compose, 'up', '-d', service], { env });
}
// The bundle names the stamp; the page names the bundle. Wait for both.
const base = `http://127.0.0.1:${port}`;
let served = '';
for (let i = 0; i < 40 && !served; i++) {
await sleep(1500);
try {
const html = await (await fetch(base, { signal: AbortSignal.timeout(3000) })).text();
const bundle = html.match(/\/assets\/main[^"]*\.js/)?.[0];
if (!bundle) continue;
const js = await (await fetch(base + bundle, { signal: AbortSignal.timeout(5000) })).text();
const m = js.match(new RegExp(`${stamp.replace(/[+.]/g, '\\$&')} \\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}`));
if (m) served = m[0];
} catch { /* not up yet */ }
}
if (!served) { console.error(`ship: ${base} did not serve stamp ${stamp} within 60 s`); process.exit(1); }
console.log(`ship: ${base} serves ${served} (${((Date.now() - t0) / 1000).toFixed(0)} s)`);
if (push) {
sh('git', ['push', 'origin', 'HEAD']);
if (branch === 'master') sh('git', ['push', 'origin', 'HEAD:dev']);
console.log(`ship: pushed ${head}`);
}