All files / src/plugins/dev clay-preview.ts

29.11% Statements 46/158
46.53% Branches 47/101
25% Functions 7/28
29.8% Lines 45/151

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                      2x     2x                                                                   2x                                                           9x   9x 1x     8x   8x 1x     7x   7x   11x 1x     10x 10x 11x   11x 2x 8x 5x 1x     4x   3x       3x                                                   54x       61x   54x 1x     53x       53x       53x       53x   53x 10x 1x     9x     54x                   47x           47x 47x         47x                                                                                                       3x                                                                                                                             1x   1x                                                                                                                                                                                                                                                                                                                                                                                                         2x      
/**
 * Local Clay preview.
 *
 * Builds a face's real settings page (the same @rebble/clay the watch build uses, with its own
 * custom components and every normal item) into a plain HTML file you can open in a browser. Lets
 * you click around the config UI without loading it onto the watch after every change. It also
 * shows you the console error when the page refuses to render, which on the phone just looks like
 * a settings screen that never opens. That is the real reason it earns its keep.
 *
 * The page, its components, and its customClay come from what the face hands startPebbleApp in its
 * own src/pkjs/index.ts, so a component the face forgets to pass fails here the same way it fails
 * on the phone. A face whose appinfo lists providers gets its page narrowed to them, as the phone
 * narrows it. On a face built with a plugin define, the face's features start too, and their page
 * hooks change the page the way they do each time the phone opens it. With --plugins the phone seems
 * to have the Pebble app plugins a file lists, which is what such a face offers as a source.
 *
 * A plugin app's page is built the way the plugin app build builds it, from its src/config/index.ts. A
 * small stand-in for the Pebble app's bridge answers the page's load with the values a --settings file
 * holds, and shows what a Save would have sent in the browser's console, so the page loads and saves as
 * it does on the phone.
 *
 * Run from the unit that holds the face, through paf. USAGE below lists the options.
 *
 * Opens to: paf/plugins/dev/clay-preview.html, or the file --out names.
 *
 * A face's Save button does nothing here, since it talks to the phone. This is for eyeballing the page.
 */
import fs from 'node:fs';
import path from 'node:path';
import { facePaths, compile, fillEmit, writeTsconfig, type FacePaths } from '../../tools/pkjs/build-pkjs.ts';
import { displayNameOf, isPluginApp, PLUGIN_CONFIG_PAGE, readAppinfo, readDefines } from '../../tools/manifest/build-manifests.ts';
import { readPluginPage, renderPluginPage } from '../../tools/pkjs/plugin-page.ts';
import { readProviders } from '../../tools/manifest/providers.ts';
import type { ProviderLists } from '../../tools/manifest/providers.ts';
import { pluginKindsOn } from '../../tools/manifest/plugin-kinds.ts';
import { appinfoPath, faceDir, familyCoreDir } from '../../tools/shared/faces.ts';
import { ENGINE } from '../../tools/shared/paths.ts';
import { stubModuleLoad } from '../../ts/testing/module-load.ts';
import { ToolError, reportFailure } from '../../tools/shared/tool-error.ts';
import { isMainScript } from '../../tools/shared/entry.ts';
import { isObject } from '../../tools/shared/values.ts';
import { forgetCompiled, installHost, loadSettingsPage, messageKeysFrom, type PhonePlugins } from '../../tools/pkjs/settings-page.ts';
 
const OUT = path.join(import.meta.dirname, 'clay-preview.html');
 
/** What -h prints. */
const USAGE = `usage: paf tool <face> clay-preview [options]
 
Builds the face's settings page into paf/plugins/dev/clay-preview.html for a desktop browser, run
from the unit that holds the face.
 
  --watch             rebuild the page after every save until stopped, then refresh the browser
  --platform=<name>   open the page as if from that watch, emery by default
  --settings=<file>   open the page filled from a file of saved values, as a returning wearer
                      sees it, rather than as a fresh install. For a plugin app, the values the
                      plugin hands the page when it opens
  --plugins=<file>    open the page on a phone with the Pebble app plugins the file lists, and
                      with what each plugin source last saved
  --out=<file>        write the page to this file rather than clay-preview.html
  -h, --help          print this`;
 
/** What a preview was asked for. */
export type ClayPreviewArgs = {
  /** The face to preview, or empty when only the usage was asked for. */
  face: string;
  /** Whether to keep rebuilding after every save. */
  watch: boolean;
  /** The watch the page should think it was opened from. */
  platform: string;
  /** The file of saved values to fill the page from, or null for a fresh install. */
  settings: string | null;
  /** The file of plugins and their saved state, or null for a phone that lists none. */
  plugins: string | null;
  /** Where to write the page, or null for clay-preview.html beside this script. */
  out: string | null;
  /** Whether -h or --help asked for the usage. */
  help: boolean;
};
 
/** The flags that take a value after an equals sign, each with the example a refusal shows. */
const VALUE_FLAGS: Record<string, string> = {
  '--platform': '--platform=gabbro',
  '--settings': '--settings=saved.json',
  '--plugins': '--plugins=plugins.json',
  '--out': '--out=page.html',
};
 
/** The page a preview built, and the page before the features changed it. */
export type RenderedPage = {
  /** The page as the browser opens it. */
  html: string;
  /** The settings page as Clay built it from the face's own page, as JSON, with each theme picker's previews as the phone sends them. A face that lists providers has the providers' open pass run on it, as an open that falls back gets it. */
  unfolded: string;
  /** The settings page that opens, after the features' page hooks, as JSON, with each theme picker's previews as the phone sends them. */
  folded: string;
  /** The custom function's text, as Clay writes it into the page. */
  customFn: string;
};
 
/**
 * Reads what a preview was asked for.
 *
 * The face comes first, and the flags can follow in any order. -h or --help anywhere asks for the
 * usage and nothing else. Any other argument stops it, since a typo or `--settings saved.json` with a
 * space would otherwise preview a fresh install with no word that the flag was missed.
 *
 * @param args The command line after the script.
 * @return The face, the flags, and whether only the usage was asked for.
 */
export function parseClayPreviewArgs(args: string[]): ClayPreviewArgs {
  const parsed: ClayPreviewArgs = { face: '', watch: false, platform: 'emery', settings: null, plugins: null, out: null, help: false };
 
  if (args.includes('-h') || args.includes('--help')) {
    return { ...parsed, help: true };
  }
 
  const [face, ...rest] = args;
 
  if (!face || face.startsWith('-')) {
    throw new ToolError('no face given. Pass it first, as in paf tool my-face clay-preview --watch');
  }
 
  parsed.face = face;
 
  for (const arg of rest) {
    // paf hands a tool every argument as typed, so a -- marking the end of paf's own options is skipped
    if (arg === '--') {
      continue;
    }
 
    const equals = arg.indexOf('=');
    const flag = equals === -1 ? arg : arg.slice(0, equals);
    const value = equals === -1 ? '' : arg.slice(equals + 1);
 
    if (arg === '--watch') {
      parsed.watch = true;
    } else if (equals !== -1 && flag in VALUE_FLAGS) {
      if (!value) {
        throw new ToolError(`${flag} needs a value after the =, as in ${VALUE_FLAGS[flag]}`);
      }
 
      parsed[flag.slice(2) as 'platform' | 'settings' | 'plugins' | 'out'] = value;
    } else {
      throw new ToolError(`unknown argument: ${arg}. Run with --help for the options`);
    }
  }
 
  return parsed;
}
 
/**
 * Reads what a --plugins file says about the phone.
 *
 * The file is an object. `plugins` is the list Pebble.enumeratePlugins hands back, each entry as the
 * Pebble app lists a plugin. A current Pebble app always has the call, and with plugins switched off
 * it lists nothing, so an empty list is plugins off. Leaving it out is an older Pebble app with no
 * plugin calls at all. `state` holds what each plugin source last saved, by its category and
 * item, such as `weather/location`, which is what the settings page reads to pick its warnings.
 * `unreadable: true` is a phone whose list call throws, so the page cannot tell what is installed.
 *
 * Each state goes into localStorage under the key the plugin source saves it under, which is
 * `pluginSource:` and then the category and item. ts/plugin-sources/subscription.ts builds that key,
 * and the spec beside this file checks the two agree. The state itself is passed on as written, so a
 * file can hold a state the page has to turn away.
 *
 * Anything else stops the preview, since a misspelt field would otherwise open a page on a phone
 * the developer never asked for.
 *
 * @param parsed The file's contents, parsed.
 * @param file The file's name, which a refusal names.
 * @return The plugin list and the saved states, by their localStorage key.
 */
export function previewPlugins(parsed: unknown, file: string): PhonePlugins {
  Iif (!isObject(parsed)) {
    throw new ToolError(`--plugins=${file} has to hold an object, with plugins and state in it`);
  }
 
  const unknownField = Object.keys(parsed).find((name) => name !== 'plugins' && name !== 'state' && name !== 'unreadable');
 
  if (unknownField !== undefined) {
    throw new ToolError(`--plugins=${file} has a field called ${unknownField}. It takes plugins, state, and unreadable`);
  }
 
  Iif (parsed.unreadable !== undefined && typeof parsed.unreadable !== 'boolean') {
    throw new ToolError(`--plugins=${file} has an unreadable that is not true or false`);
  }
 
  Iif (parsed.plugins !== undefined && !Array.isArray(parsed.plugins)) {
    throw new ToolError(`--plugins=${file} has a plugins that is not a list. Leave it out for a phone without plugins`);
  }
 
  Iif (parsed.state !== undefined && !isObject(parsed.state)) {
    throw new ToolError(`--plugins=${file} has a state that is not an object of category/item to the saved state`);
  }
 
  const storage: Record<string, string> = {};
 
  Object.entries(parsed.state || {}).forEach(([source, saved]) => {
    if (!/^[^/]+\/[^/]+$/.test(source) || !isObject(saved)) {
      throw new ToolError(`--plugins=${file} has a state for ${source}. Each one is a category and item, such as weather/location, holding an object`);
    }
 
    storage[`pluginSource:${source}`] = JSON.stringify(saved);
  });
 
  return { plugins: Array.isArray(parsed.plugins) ? parsed.plugins : null, unreadable: parsed.unreadable === true, storage };
}
 
/**
 * Reads a --plugins file from disk.
 *
 * @param file The file's path.
 * @return The plugin list and the saved states, by their localStorage key.
 */
export function readPluginsFile(file: string): PhonePlugins {
  Iif (!fs.existsSync(file)) {
    throw new ToolError(`--plugins=${file} names no file`);
  }
 
  let parsed: unknown;
 
  try {
    parsed = JSON.parse(fs.readFileSync(file, 'utf8'));
  } catch (error) {
    throw new ToolError(`--plugins=${file} is not valid JSON. ${(error as Error).message}`);
  }
 
  return previewPlugins(parsed, file);
}
 
/** Pull the raw HTML out of the data url generateUrl hands back. */
function htmlFromDataUrl(url: string): string {
  const comma = url.indexOf(',');
  const head = url.slice(0, comma);
  const body = url.slice(comma + 1);
 
  return head.indexOf(';base64') !== -1
    ? Buffer.from(body, 'base64').toString('utf8')
    : decodeURIComponent(body);
}
 
/**
 * Fills the compiled emit/ the way the build does before the bundler reads it.
 *
 * That is the committed components, ical.js for a face that uses the calendar, and the entry files
 * written from the face's defines and providers, which decide whether its plugin code is there and
 * what its settings page offers. fillEmit runs them in the build's own order, with no other targets
 * to copy into.
 *
 * @param paths The paths for the face's sandbox.
 * @param defines The face's defines, from its appinfo.
 * @param providers The face's checked providers list, from readProviders. Left out, the face lists none.
 * @param appName The face's display name, which the page heading shows. Left out, the heading is the plain one.
 */
export function fillPreviewEmit(paths: FacePaths, defines: Record<string, number>, providers: ProviderLists = {}, appName: string | null = null): void {
  fillEmit(paths, [], defines, providers, appName);
}
 
/**
 * A face's defines, its checked providers list, and its display name, read from its appinfo alone. The preview builds no
 * watch app, so it never looks at the face's targets, and a face whose targets would not resolve still
 * previews. A providers list the build would refuse stops the preview the same way.
 */
function faceBuild(face: string): { defines: Record<string, number>; providers: ProviderLists; appName: string | null } {
  const appinfo = readAppinfo(face);
  const defines = readDefines(appinfo, face);
 
  return { defines, providers: readProviders(appinfo, face, defines), appName: displayNameOf(appinfo) };
}
 
/**
 * Whether the preview starts the face's features, which it does only for a face built with a plugin
 * define. Only such a face's features change the page, so every other face previews the page Clay
 * builds from the face's own config, with none of its features started.
 *
 * @param defines The face's defines, from its appinfo.
 * @return True when any plugin define is on.
 */
export function hasPluginDefines(defines: Record<string, number>): boolean {
  return pluginKindsOn(defines).length > 0;
}
 
/**
 * Builds the settings page the phone would open, from the face's compiled code.
 *
 * loadSettingsPage builds the page the way startPebbleApp does, narrowed to the providers the build
 * wrote, from what the face hands startPebbleApp. faceStart settles the saved settings a --settings
 * file gave, as the phone does at launch. With withFeatures the features start and their page hooks
 * change the page the way they do each time the wearer opens it. A sandbox with no compiled index.js
 * stops here, and so does a face whose start-up never calls startPebbleApp, since there is then no
 * settings page the phone would build. Each says which it is.
 *
 * @param args The face and the platform.
 * @param paths The paths for the face's sandbox, already compiled and filled.
 * @param messageKeys The face's message keys, as messageKeysFrom gives them.
 * @param withFeatures Whether to start the features and run their hooks, as hasPluginDefines decides.
 * @return The page, and the settings page before and after the features changed it.
 */
export function renderPage(args: Pick<ClayPreviewArgs, 'face' | 'platform'>, paths: FacePaths, messageKeys: Record<string, number>, withFeatures: boolean): RenderedPage {
  // the build compiles index.ts to index.js, so a sandbox without it never compiled the face's own
  // start-up, which is a different stop from a start-up that never calls startPebbleApp
  if (!fs.existsSync(path.join(paths.emitPkjs, 'index.js'))) {
    throw new ToolError(`${args.face}'s src/pkjs/index.ts compiled to no index.js in ${path.relative(process.cwd(), paths.emitPkjs)}, so there is no settings page to build. Check that tsc compiled the face`);
  }
 
  const loaded = loadSettingsPage(paths, messageKeys, withFeatures, { face: args.face });
 
  if (loaded === null) {
    throw new ToolError(`${args.face}'s src/pkjs/index.ts never calls startPebbleApp, which is where the phone builds the settings page`);
  }
 
  const { clay, built } = loaded;
 
  // Clay fills meta from the Pebble global, which the preview does not have
  // left alone the page carries activeWatchInfo: null, and any item with capabilities throws on it
  // filling it with --platform's watch is what lets the capability filter run off the phone, and it is
  // the watch each theme picker's previews are cut to
  clay.meta = {
    activeWatchInfo: { platform: args.platform, firmware: { major: 4, minor: 3, patch: 0 } },
    accountToken: '',
    watchToken: '',
    userData: {},
  };
 
  return {
    html: htmlFromDataUrl(loaded.url()),
    unfolded: JSON.stringify(loaded.forPhone(built.config)),
    folded: JSON.stringify(loaded.forPhone(clay.config)),
    customFn: String(clay.customFn),
  };
}
 
/**
 * A stand-in for the Pebble app's settings page bridge, for a plugin app's page in a desktop browser.
 * It says the plugin is ready, answers the load with the saved values, and answers a save by showing
 * what it would have sent in the console, saying on the page that nothing was saved. The values go in
 * as JSON with every `<` escaped, so none can close the script.
 *
 * @param saved The values the plugin hands the page when it opens.
 * @return The script to put at the top of the page.
 */
export function previewBridge(saved: Record<string, unknown>): string {
  const values = JSON.stringify(saved).replace(/</g, '\\u003c');
 
  return '<script>window.Pebble = {' +
    ' addEventListener: function (name, listener) { if (name === "ready") { setTimeout(function () { listener({ target: "plugin" }); }, 0); } },' +
    ' sendMessage: function (target, message) {' +
    ' if (message && message.type === "paf-settings-load") { return Promise.resolve({ settings: ' + values + ' }); }' +
    ' console.log("clay-preview: a save would have sent", JSON.stringify(message && message.settings));' +
    ' return Promise.resolve({ error: "this is a preview, so nothing was saved. The console shows what would have been" }); } };</script>';
}
 
/**
 * Builds a plugin app's settings page for a desktop browser, the way the plugin app build builds it,
 * with the preview bridge put in ahead of Clay's own script.
 *
 * @param face The plugin app.
 * @param settings A file of the values the plugin hands the page, or null for none saved.
 * @return The page as the browser opens it.
 */
export async function renderPluginPreview(face: string, settings: string | null): Promise<string> {
  if (readAppinfo(face).configPage !== PLUGIN_CONFIG_PAGE) {
    throw new ToolError(`${face} is a plugin app with no settings page, since its appinfo names no configPage`);
  }
 
  let saved: unknown = {};
 
  if (settings !== null) {
    if (!fs.existsSync(settings)) {
      throw new ToolError(`--settings=${settings} names no file`);
    }
 
    try {
      saved = JSON.parse(fs.readFileSync(settings, 'utf8'));
    } catch (error) {
      throw new ToolError(`--settings=${settings} is not JSON: ${(error as Error).message}`);
    }
 
    if (!isObject(saved)) {
      throw new ToolError(`--settings=${settings} has to hold an object of each setting's key and value`);
    }
  }
 
  const html = renderPluginPage(await readPluginPage(faceDir(face), face), face);
 
  if (!html.includes('<head>')) {
    throw new ToolError(`Clay's page for ${face} has no <head> for the preview bridge to go in`);
  }
 
  // a replacer, so a $ in a saved value goes in as written rather than as a replacement pattern
  return html.replace('<head>', () => '<head>' + previewBridge(saved as Record<string, unknown>));
}
 
/** Writes a plugin app's preview, and with --watch writes it again after every save under its src/. */
async function previewPluginApp(args: ClayPreviewArgs): Promise<void> {
  if (args.plugins !== null) {
    throw new ToolError(`${args.face} is a plugin app, so --plugins means nothing to its page. Use --settings for the values it opens on`);
  }
 
  const out = args.out === null ? OUT : path.resolve(args.out);
  const write = async (): Promise<void> => {
    const html = await renderPluginPreview(args.face, args.settings);
 
    fs.mkdirSync(path.dirname(out), { recursive: true });
    fs.writeFileSync(out, html);
    console.log('[' + new Date().toISOString().slice(11, 19) + '] wrote ' + out + ' (' + html.length + ' bytes) for ' + args.face);
  };
 
  await write();
 
  if (!args.watch) {
    return;
  }
 
  let timer: ReturnType<typeof setTimeout> | null = null;
 
  // the page config can import anything under src/, such as settings it shares with the plugin's script,
  // so the whole folder is watched. the framework's own page code is loaded once, so an edit to it
  // needs the preview started again
  console.log("watching the plugin app's src/ for changes. refresh the browser after each save. restart after editing the framework.");
  [path.join(faceDir(args.face), 'src')].filter((dir) => fs.existsSync(dir)).forEach((dir) => {
    fs.watch(dir, { recursive: true }, () => {
      if (timer) { clearTimeout(timer); }
 
      timer = setTimeout(() => {
        write().catch((error) => console.error('build failed: ' + (error as Error).message));
      }, 150);
    });
  });
}
 
/** Renders the settings page to the out file so a browser can open it. */
function build(args: ClayPreviewArgs, paths: FacePaths, messageKeys: Record<string, number>, defines: Record<string, number>): void {
  const out = args.out === null ? OUT : path.resolve(args.out);
  const page = renderPage(args, paths, messageKeys, hasPluginDefines(defines));
 
  fs.mkdirSync(path.dirname(out), { recursive: true });
  fs.writeFileSync(out, page.html);
 
  const stamp = new Date().toISOString().slice(11, 19);
 
  console.log('[' + stamp + '] wrote ' + out + ' (' + page.html.length + ' bytes) for ' + args.face);
}
 
/** Rebuilds the page after every save under the face's sources until stopped. */
function watch(args: ClayPreviewArgs, paths: FacePaths, messageKeys: Record<string, number>): void {
  // the face's page, the family core's shared sections, and the framework's ts, which the page
  // builders and the Clay components both come from
  const core = familyCoreDir(args.face);
  const watched = [
    paths.faceSrc,
    core ? path.join(core, 'pkjs') : '',
    path.join(ENGINE, 'ts'),
  ].filter((dir) => dir && fs.existsSync(dir));
 
  // fs.watch fires more than once per save and every pass shells out to a synchronous tsc, so
  // roll a burst of events into one rebuild
  let timer: ReturnType<typeof setTimeout> | null = null;
 
  const rebuild = (): void => {
    // node caches required modules, so drop the face's compiled ones before the next build
    forgetCompiled(paths);
 
    try {
      const { defines, providers, appName } = faceBuild(args.face);
 
      // no cleanEmit here: wiping emit/ would mean a cold tsc on every keystroke
      compile(paths);
      fillPreviewEmit(paths, defines, providers, appName);
      // each build starts the features again, so each one starts from the saved state the files gave
      installHost(args, args.plugins === null ? null : readPluginsFile(args.plugins));
      build(args, paths, messageKeys, defines);
    } catch (error) {
      console.error('build failed: ' + (error as Error).message);
    }
  };
 
  console.log('watching for changes. refresh the browser after each save.');
  console.log('note: edits under clay/builder need `paf gen ' + args.face + ' clay` first.');
  watched.forEach((dir) => {
    fs.watch(dir, { recursive: true }, () => {
      if (timer) { clearTimeout(timer); }
 
      timer = setTimeout(rebuild, 150);
    });
  });
}
 
function main(): void {
  let args: ClayPreviewArgs;
  let paths: FacePaths;
  let messageKeys: Record<string, number>;
 
  // a bad face, flag, or first build stops with the one line every tool gives, and tsc's own exit code
  try {
    args = parseClayPreviewArgs(process.argv.slice(2));
 
    if (args.help) {
      console.log(USAGE);
      return;
    }
 
    // a plugin app's page comes from its own config, not a face's start-up, and its preview is async
    if (isPluginApp(readAppinfo(args.face))) {
      previewPluginApp(args).catch(reportFailure);
      return;
    }
 
    paths = facePaths(args.face);
 
    // the page comes from the face's own src/pkjs/index.ts and what it hands startPebbleApp. a face
    // without one has nothing to preview, and going on would stop on a missing file with a stack that
    // reads as a bug here
    if (!fs.existsSync(path.join(paths.faceSrc, 'index.ts'))) {
      throw new ToolError(`${args.face} has no src/pkjs/index.ts of its own, which is where the phone builds the settings page`);
    }
 
    const plugins = args.plugins === null ? null : readPluginsFile(args.plugins);
    const keys = messageKeysFrom(appinfoPath(args.face));
    const { defines, providers, appName } = faceBuild(args.face);
 
    messageKeys = keys;
    stubModuleLoad((id) => (id === 'message_keys' ? keys : undefined));
    installHost(args, plugins);
    writeTsconfig(args.face, paths);
    compile(paths);
    // the face's start-up loads every feature it passes, the calendar one reads ical.js, the face's
    // defines decide which plugin code its features find and whether they start at all, and its
    // providers decide what its page offers
    fillPreviewEmit(paths, defines, providers, appName);
    build(args, paths, messageKeys, defines);
  } catch (error) {
    reportFailure(error);
    return;
  }
 
  if (args.watch) {
    watch(args, paths, messageKeys);
  }
}
 
Iif (isMainScript(import.meta)) {
  main();
}