aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-client/scripts/sync-ui.js
blob: d0b6eeec1c5abc5f793bc3a1171f43cbb8819b49 (plain) (blame)
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
/**
 * Copy the interface into this package.
 *
 * `packages/meshbay-hub/src/meshbay_hub/static/` is the single source of the
 * interface, and it is *copied* here at build time rather than forked. A silent
 * fork is the only real way to end up maintaining the UI twice, so this script
 * refuses to leave a stale copy behind: it wipes the destination first, and CI
 * fails if running it changes anything that was committed.
 */

'use strict';

const fs = require('node:fs');
const path = require('node:path');

const SRC = path.resolve(__dirname, '..', '..', 'meshbay-hub', 'src',
                         'meshbay_hub', 'static');
const DEST = path.resolve(__dirname, '..', 'ui');

// index.html is ours: the hub builds its own with a /a/<hash>/ prefix for cache
// busting, which an application loading from disk does not need and must not
// have — the prefix would point at the hub.
const OURS = ['index.html'];

// sw.js has to sit at the root of the scope it serves, which it already does.
const SKIP = new Set(['webrtc-test.html']);

function copyTree(from, to) {
  fs.mkdirSync(to, { recursive: true });
  for (const entry of fs.readdirSync(from, { withFileTypes: true })) {
    if (SKIP.has(entry.name)) continue;
    const src = path.join(from, entry.name);
    const dst = path.join(to, entry.name);
    if (entry.isDirectory()) copyTree(src, dst);
    else fs.copyFileSync(src, dst);
  }
}

fs.rmSync(DEST, { recursive: true, force: true });
copyTree(SRC, DEST);
for (const name of OURS) {
  fs.copyFileSync(path.join(__dirname, name), path.join(DEST, name));
}

const count = fs.readdirSync(DEST).length;
console.log(`ui/ rebuilt from ${path.relative(process.cwd(), SRC)} (${count} entries)`);