All files / src/tools/manifest build-manifests.ts

66.84% Statements 125/187
73.79% Branches 107/145
61.36% Functions 27/44
67.24% Lines 117/174

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 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957                                                                                                                                          10x 10x       10x   10x     10x     10x     10x     10x             10x                                                                                                                                                 53x 1x     52x 36x     16x       16x   16x 15x 1x     61x 2x     12x 1x     11x 2x     9x     10x                           96x 96x   480x 622x   176x 5x                                     74x 74x 74x                                   44x 7x   7x     37x 1x     36x     3x   3x 1x     3x   2x 1x     2x     33x 1x     32x                           15x   15x 7x   4x 1x     3x     14x                             11x                                                                   127x                               17x 187x   17x 7x     10x 1x     9x 1x     8x 1x     7x 1x     6x                                 2x   2x                                                                     16x                               4x                                                                                                                 32x       32x 5x     25x 25x 25x 25x   25x   25x                                             20x 3x     11x   11x 11x   11x                                                                                                                     3x   3x 5x 7x   7x 1x     6x 1x     5x       1x                                                                                 37x   37x 5x     31x   31x                             33x   33x                                                             10x                                                                                                                                                                           10x                                   11x 11x   7x                     9x                   8x                                                                                                                                         10x      
/**
 * Generate targets/<target>/package.json from a face's pebble.appinfo.json.
 *
 * pebble.appinfo.json holds the Pebble appinfo (uuid, messageKeys, the whole resource
 * list) plus the per-face build identity: the release name, the watchface flag, and (for
 * an app) which bundled icon is the launcher menu icon. The author/version come from the
 * root package.json, so there is one place for each fact.
 *
 * A face can declare either one build target inline or a `targets` map naming several, so
 * one source face can produce more than one .pbw. Each target builds to its own staging
 * sandbox targets/<target name>/, so there is one manifest per target rather than per face.
 * The manifest is a gitignored build input written as plain JSON. Nobody reads it by hand.
 * `pebble build` needs package.json to exist before it runs, so each build regenerates it
 * via tools/build.ts. A file whose contents would not change is left alone, so its mtime does too.
 *
 * The manifest's message keys are the face's own, then the ones its features and its plugin defines
 * need, which feature-keys.ts works out, so a face never lists the keys a feature brings. The SDK
 * version, multi JS, and the capabilities the features and the settings page need are filled in the
 * same way. The features come from the face's compiled phone code, so tools/build.ts runs prepareFace
 * before the phone build, reads the features after it, and hands both to writeSandboxes.
 *
 * A face whose keys, with those added, hold only part of a key group the watch reads as a unit, such
 * as the weather extras, is refused here too, before any sandbox is written. key-groups.ts holds the
 * groups.
 *
 * A face can also set C defines for its own build, such as a raised ENGINE_SLOTS_MAX. They are checked
 * here and carried into the manifest, where the waf build turns each one into a -D flag. The dev
 * plugin's switches join them, as dev-switches.ts covers, and a dev build also gets the header
 * dev-header.ts writes into each sandbox's page/ folder. A plugin
 * define, such as PLUGIN_WEATHER, brings the message keys its kind needs, and the keys only a plugin
 * fills, such as the music keys, are refused without their define. A face's
 * providers list, which names what it offers for each kind of data, is checked here too, against its
 * keys and its defines, and stays in the appinfo rather than the manifest.
 *
 * The manifest also carries textDefines, the C defines the build writes as text rather than taking from
 * the face. They are the defaults of the plugin source choices the watch keeps, worked out from the
 * face's providers list, and the waf build turns each into a -D flag holding a string.
 *
 * The manifest also lists the sites and phone features the face uses, as usesPermissions. permissions.ts
 * works them out from the face's message keys, its providers list, and its capabilities.
 *
 * A plugin app is a face whose appinfo sets "projectType": "plugin". It is a Pebble app plugin with no
 * watch code, so its appinfo holds the SDK's plugin block and its own usesPermissions rather than
 * message keys, resources, or defines, and both go into the manifest as written. Its sandbox gets the
 * SDK's plugin wscript in place of the framework's. It has no features and no keys, so none of the keys
 * or the fields above are filled in for it, and its appinfo goes in as it is written.
 *
 * Usage: node tools/manifest/build-manifests.ts --targets <face>
 */
import fs from 'node:fs';
import path from 'node:path';
import { appinfoPath, faceRelative, familyCoreFor, familyNameFor, listFaceNames } from '../shared/faces.ts';
import { writeIfChanged } from '../shared/files.ts';
import { isObject } from '../shared/values.ts';
import { ENGINE, ENGINE_REL, WORKSPACE } from '../shared/paths.ts';
import { ToolError, reportFailure } from '../shared/tool-error.ts';
import { isMainScript } from '../shared/entry.ts';
import { renderDevHeader, writeDevHeader } from './dev-header.ts';
import { devDefines, isDevDefine, unitDevSwitches, type DevSwitches } from './dev-switches.ts';
import { thumbnailFiles } from './dev-walks.ts';
import { checkKeyGroups } from './key-groups.ts';
import { NO_START, fillAppinfo, type FaceStartFacts } from './feature-keys.ts';
import { keyNames } from './message-keys.ts';
import { checkOwnPermissions, permissionWarnings, permissionsFor, readOwn } from './permissions.ts';
import { PLUGIN_KINDS, pluginKindsOn } from './plugin-kinds.ts';
import { checkProviderKeys, checkSelectKeys, pluginSourceDefaults, providerWarnings, readProviders } from './providers.ts';
import type { Permission } from './permissions.ts';
import type { ProviderLists } from './providers.ts';
 
const ROOT = WORKSPACE;
const ROOT_PKG = path.join(ROOT, 'package.json');
// every target's wscript comes from one template with only its folders filled in, so it is
// generated rather than committed per face. a sandbox missing its wscript makes `pebble build`
// report "This project is very outdated" instead of anything useful
const WSCRIPT_TEMPLATE = path.join(ENGINE, 'waf', 'wscript.template');
// a plugin app builds with the SDK's own plugin rules, which name no folders, so it is copied as it is
const PLUGIN_WSCRIPT = path.join(ENGINE, 'waf', 'plugin-wscript.template');
 
/** The appinfo projectType that makes a face a plugin app, spelled as the Pebble SDK spells it. */
const PLUGIN_PROJECT_TYPE = 'plugin';
 
/** The projectType `pebble new-project` writes for a watchface or a watchapp, which builds as a face. */
const NATIVE_PROJECT_TYPE = 'native';
 
/** The script name the build writes a plugin app's code to, which the SDK reads from the plugin block. */
export const PLUGIN_SCRIPT = 'plugin.js';
 
/** The settings page the build writes into a plugin app's pbw, and the only `configPage` a plugin app can name. */
export const PLUGIN_CONFIG_PAGE = 'config.html';
 
/**
 * The appinfo fields only a face with watch code uses. A plugin app refuses each one, since the SDK
 * builds a plugin with no platforms and fills in its own resources, so the field would only sit in the
 * pbw meaning nothing, and a face moved to a plugin app would keep keys it never reads.
 */
const WATCH_ONLY_FIELDS = ['messageKeys', 'resources', 'defines', 'textDefines', 'providers', 'targets', 'watchapp', 'watchface', 'menuIcon', 'targetPlatforms', 'enableMultiJS'];
 
/**
 * One entry in pebble.appinfo.json's resources.media: a bitmap or font the SDK
 * packs. generate-icons.ts rewrites the bitmap rows, so it reads this shape too.
 */
export type MediaEntry = { type: string; name: string; file?: string; menuIcon?: boolean; [key: string]: unknown };
 
/**
 * The per-face build identity: the bits that make a manifest a watchface or a watchapp.
 *
 * A target shares the face's uuid unless it names its own. Sharing one means the watch holds only
 * one of the face's targets at a time and they share the phone's saved settings. A uuid of its own
 * lets a target be installed beside the others, with settings of its own.
 */
export type Target = { name: string; watchface: boolean; menuIcon?: string; uuid?: string };
 
/**
 * The shared Pebble fields common to every face, plus the C defines the face sets for its own build.
 * This is what building a manifest reads. The per-face build identity (name/watchface/menuIcon)
 * rides alongside these in the file but is split out into a Target before buildManifest sees it.
 * The SDK version, multi JS, and the capabilities can be left out, since fillAppinfo fills them in.
 */
export type SharedAppinfo = {
  displayName: string;
  uuid: string;
  sdkVersion?: string;
  enableMultiJS?: boolean;
  targetPlatforms: string[];
  capabilities?: unknown;
  messageKeys: unknown;
  resources: { media: MediaEntry[] };
  defines?: unknown;
  textDefines?: unknown;
  usesPermissions?: unknown;
  providers?: unknown;
};
 
/**
 * A face's build identity is either a single target inlined at the top level (the common case:
 * one .pbw per face) or a targets map naming several, such as a face that ships a watchface and a
 * watchapp from one source.
 */
type TargetsMap = Record<string, Target>;
 
/**
 * The whole appinfo file: the shared Pebble fields plus this face's build identity and its
 * own version. Faces version independently (each keeps its own CHANGELOG.md), so the version
 * lives here rather than in the root package.json. A face declares its identity one of two ways:
 * the single-target fields (name/watchface/menuIcon) inline, or a `targets` map for many.
 */
export type Appinfo = SharedAppinfo & Partial<Target> & { version?: string; targets?: TargetsMap; projectType?: unknown; configPage?: unknown; plugin?: unknown };
 
/** The C defines a face sets for its own build, each a macro name and a whole number. */
export type Defines = Record<string, number>;
 
/** The C defines the build writes as text, each a macro name and the string it holds. */
export type TextDefines = Record<string, string>;
 
/**
 * The C defines a face sets for its own build, checked so each one is safe to hand the compiler.
 *
 * Every define goes straight onto the compiler's command line, so only a macro name and a whole
 * number get through. A name the build sets itself, a HAS_ feature switch, BUILD_WATCHAPP, a plugin
 * source default, or a dev switch, is turned away too, since those follow from the face's message keys,
 * resources, targets, providers list, and the dev switches. So is a textDefines block in the
 * appinfo, since the build writes the manifest's own from the providers list.
 *
 * @param config The face's appinfo.
 * @param face The face the appinfo belongs to, which a mistake in it names.
 * @return The defines, empty when the face sets none.
 */
export function readDefines(config: SharedAppinfo, face: string): Defines {
  if (config.textDefines !== undefined) {
    throw new ToolError(`${face}'s appinfo sets textDefines, which the build writes itself from the providers list. Take it out of the appinfo, and list the provider the face should default to first in providers instead`);
  }
 
  if (config.defines === undefined) {
    return {};
  }
 
  Iif (typeof config.defines !== 'object' || config.defines === null || Array.isArray(config.defines)) {
    throw new ToolError(`${face}'s appinfo defines has to be a map of macro name to whole number`);
  }
 
  const defines: Defines = {};
 
  for (const [name, value] of Object.entries(config.defines)) {
    if (!/^[A-Z][A-Z0-9_]*$/.test(name)) {
      throw new ToolError(`${face}'s appinfo defines ${name}, which is not a macro name such as ENGINE_SLOTS_MAX`);
    }
 
    if (/^HAS_/.test(name) || name === 'BUILD_WATCHAPP' || PLUGIN_KINDS.some((kind) => kind.defaultDefine === name)) {
      throw new ToolError(`${face}'s appinfo defines ${name}, which the build sets itself`);
    }
 
    if (isDevDefine(name)) {
      throw new ToolError(`${face}'s appinfo defines ${name}, which the build sets itself from the dev switches. Take it out of the appinfo, and build with paf build ${face} --dev for dev mode`);
    }
 
    if (typeof value !== 'number' || !Number.isInteger(value)) {
      throw new ToolError(`${face}'s appinfo defines ${name} as ${JSON.stringify(value)}, which is not a whole number`);
    }
 
    defines[name] = value;
  }
 
  return defines;
}
 
/**
 * Refuses the keys only a plugin fills without that plugin's define, since nothing on the phone would
 * ever answer them, so the mistake shows at build time rather than as a panel that stays empty.
 *
 * The keys a define that is on needs are added by fillAppinfo, so this asks for none of them.
 *
 * @param defines The face's defines, from readDefines.
 * @param messageKeys The message keys the manifest carries.
 * @param face The face the appinfo belongs to, which the refusal names.
 */
export function checkPluginKeys(defines: Defines, messageKeys: unknown, face: string): void {
  const declared = keyNames(messageKeys);
  const kindsOn = pluginKindsOn(defines);
 
  for (const kind of PLUGIN_KINDS.filter((known) => known.pluginOnly && !kindsOn.includes(known))) {
    const stray = [...kind.keys, ...(kind.optionalKeys || [])].filter((key) => declared.has(key));
 
    if (stray.length > 0) {
      throw new ToolError(`${face}'s appinfo declares ${stray.join(', ')} without ${kind.define}, and only that plugin fills them. Add "${kind.define}": 1 to its defines, or take the keys out of messageKeys`);
    }
  }
}
 
/**
 * Runs every check against the message keys the manifest carries, the face's own and the ones
 * fillAppinfo added.
 *
 * It refuses a key group declared in part or without the keys it is read beside, a providers list the
 * keys cannot back, and the keys only a plugin fills without its define. checkAppinfo runs it for the
 * build, and the dev plugin's page check runs it for each scenario, so both refuse the same faces.
 *
 * @param messageKeys The message keys the manifest carries.
 * @param face The face the appinfo belongs to, which a refusal names.
 * @param defines The face's defines, from readDefines.
 * @param providers The face's providers list, from readProviders.
 */
export function checkMessageKeys(messageKeys: unknown, face: string, defines: Defines, providers: ProviderLists): void {
  checkKeyGroups(messageKeys, face);
  checkProviderKeys(providers, messageKeys, face);
  checkPluginKeys(defines, messageKeys, face);
}
 
/**
 * The build targets a face declares, as a flat list. A `targets` map wins. Otherwise the inline
 * single target is the whole list. One source face can produce several .pbw
 * targets, each with its own sandbox under targets/<target name>/.
 *
 * Every path that learns a face's targets comes through here, so this is also where a plugin app is
 * told apart from a face. A plugin app is checked by checkPluginApp and has the one target its appinfo
 * names. Any projectType other than plugin or native is refused, native being what `pebble new-project`
 * writes for a face, so a misspelled plugin app does not build as a face.
 *
 * @param config The parsed appinfo to read the target or targets from.
 * @param face The face the appinfo belongs to, which a mistake in it names.
 * @return Every build target this face declares.
 */
export function resolveTargets(config: Appinfo, face: string): Target[] {
  if (isPluginApp(config)) {
    checkPluginApp(config, face);
 
    return [{ name: config.name as string, watchface: false }];
  }
 
  if (config.projectType !== undefined && config.projectType !== NATIVE_PROJECT_TYPE) {
    throw new ToolError(`${face}'s appinfo sets projectType ${JSON.stringify(config.projectType)}, which the build does not know. Set it to "${PLUGIN_PROJECT_TYPE}" for a plugin app, or to "${NATIVE_PROJECT_TYPE}" or leave it out for a watchface or watchapp`);
  }
 
  if (config.targets) {
    // an empty map or a target with no name reached the sandbox step as targets/undefined, or as
    // a TypeError that named nothing
    const targets = Object.entries(config.targets);
 
    if (targets.length === 0) {
      throw new ToolError(`${face}'s appinfo declares an empty targets map`);
    }
 
    const unnamed = targets.find(([, target]) => !target || !target.name);
 
    if (unnamed) {
      throw new ToolError(`${face}'s appinfo target "${unnamed[0]}" has no name`);
    }
 
    return targets.map(([, target]) => target);
  }
 
  if (!config.name) {
    throw new ToolError(`${face}'s appinfo declares neither a targets map nor a top-level name`);
  }
 
  return [{ name: config.name, watchface: config.watchface ?? false, menuIcon: config.menuIcon }];
}
 
/** The facts each manifest copies out of the root package.json. */
type RootPkg = { author: string; version: string };
 
/**
 * Builds one target's media list, marking its menu icon if it declares one.
 *
 * @param config The shared appinfo fields, including the media list to copy.
 * @param target The build target, checked for a menuIcon to mark.
 * @return The target's own copy of the media list.
 */
export function buildMedia(config: SharedAppinfo, target: Target): MediaEntry[] {
  const media: MediaEntry[] = JSON.parse(JSON.stringify(config.resources.media));
 
  if (target.menuIcon) {
    const entry = media.find((item) => item.name === target.menuIcon);
 
    if (!entry) {
      throw new ToolError(`target ${target.name}'s menuIcon ${target.menuIcon} is not in the media list`);
    }
 
    entry.menuIcon = true;
  }
 
  return media;
}
 
/**
 * Builds one target's whole package.json manifest from the shared config and the root package.
 *
 * @param config The shared appinfo fields.
 * @param rootPkg The author and version to copy in from the root package.json.
 * @param target The build target this manifest is for.
 * @param defines The C defines the face sets for its own build.
 * @param permissions The usesPermissions list, from permissionsFor.
 * @param textDefines The C defines the build writes as text, from pluginSourceDefaults.
 * @return The finished manifest, ready to write out as package.json.
 */
export function buildManifest(config: SharedAppinfo, rootPkg: RootPkg, target: Target, defines: Defines = {}, permissions: Permission[] = [], textDefines: TextDefines = {}) {
  return {
    name: target.name,
    author: rootPkg.author,
    version: rootPkg.version,
    private: true,
    pebble: {
      displayName: config.displayName,
      // every target shares the face's uuid unless it names its own, so a face's targets install
      // as one app with one set of phone settings, and installing one replaces the other
      uuid: target.uuid || config.uuid,
      sdkVersion: config.sdkVersion,
      enableMultiJS: config.enableMultiJS,
      targetPlatforms: config.targetPlatforms,
      watchapp: { watchface: target.watchface },
      capabilities: config.capabilities,
      messageKeys: config.messageKeys,
      resources: { media: buildMedia(config, target) },
      // left out when there are none, so a face with no phone-side fetches gets no empty list
      ...(permissions.length > 0 ? { usesPermissions: permissions } : {}),
    },
    // outside the pebble block, which the SDK reads. left out when there are none, so a face that
    // sets none gets a manifest with no defines key
    ...(Object.keys(defines).length > 0 ? { defines: defines } : {}),
    ...(Object.keys(textDefines).length > 0 ? { textDefines: textDefines } : {}),
  };
}
 
/**
 * Whether a face is a plugin app, known by `"projectType": "plugin"` in its appinfo.
 *
 * @param config The face's appinfo.
 * @return True for a plugin app.
 */
export function isPluginApp(config: { projectType?: unknown }): boolean {
  return config.projectType === PLUGIN_PROJECT_TYPE;
}
 
/**
 * Refuses a plugin app's appinfo the build cannot turn into a working plugin.
 *
 * A plugin app has no watch code, so a field only watch code uses is refused, naming the field. It
 * needs a name, which its sandbox is named after, and the SDK's plugin block, whose own shape the SDK
 * and the Pebble app check. The build writes the plugin's script as plugin.js and its settings page as
 * config.html, so a plugin block naming another script, or a settings page by another name, is refused
 * rather than built into a pbw missing it. Its permissions are checked the way a face's own are.
 *
 * @param config The face's appinfo.
 * @param face The face the appinfo belongs to, which a refusal names.
 */
export function checkPluginApp(config: Appinfo, face: string): void {
  const record = config as unknown as Record<string, unknown>;
  const watchOnly = WATCH_ONLY_FIELDS.filter((field) => record[field] !== undefined);
 
  if (watchOnly.length > 0) {
    throw new ToolError(`${face} is a plugin app, which has no watch code, so its appinfo cannot set ${watchOnly.join(', ')}. Take ${watchOnly.length === 1 ? 'it' : 'them'} out`);
  }
 
  if (typeof config.name !== 'string' || !config.name) {
    throw new ToolError(`${face} is a plugin app whose appinfo has no name`);
  }
 
  if (!isObject(config.plugin)) {
    throw new ToolError(`${face} is a plugin app whose appinfo has no plugin block. Add "plugin": { "description": "...", "sources": [...] }, as the Pebble SDK describes it`);
  }
 
  if (config.plugin.script !== undefined && config.plugin.script !== PLUGIN_SCRIPT) {
    throw new ToolError(`${face}'s plugin block names its script ${JSON.stringify(config.plugin.script)}, but the build writes it as ${PLUGIN_SCRIPT}. Take script out of the plugin block`);
  }
 
  if (config.configPage !== undefined && config.configPage !== PLUGIN_CONFIG_PAGE) {
    throw new ToolError(`${face}'s appinfo names its settings page ${JSON.stringify(config.configPage)}, but the build writes it as ${PLUGIN_CONFIG_PAGE}. Set configPage to "${PLUGIN_CONFIG_PAGE}", or take it out for a plugin app with no settings page`);
  }
 
  readOwn(config.usesPermissions, face);
}
 
/**
 * Builds a plugin app's package.json manifest.
 *
 * The SDK reads everything about a plugin from its pebble block, projectType included. usesPermissions
 * and the plugin block go in as the appinfo writes them, since each permission's reason and required
 * flag, and each source's callerPermissions, are the Pebble app's to read and the framework's types
 * would drop them.
 *
 * @param config The plugin app's appinfo, checked by checkPluginApp.
 * @param rootPkg The author, and the version the plugin app builds as.
 * @param permissions The usesPermissions list, from checkAppinfo, as the appinfo writes it.
 * @return The finished manifest, ready to write out as package.json.
 */
export function buildPluginManifest(config: Appinfo, rootPkg: RootPkg, permissions: Permission[] = []) {
  const plugin = { ...(isObject(config.plugin) ? config.plugin : {}), script: PLUGIN_SCRIPT };
 
  return {
    name: config.name,
    author: rootPkg.author,
    version: rootPkg.version,
    private: true,
    pebble: {
      displayName: config.displayName,
      uuid: config.uuid,
      sdkVersion: config.sdkVersion,
      projectType: PLUGIN_PROJECT_TYPE,
      ...(config.capabilities !== undefined ? { capabilities: config.capabilities } : {}),
      ...(config.configPage !== undefined ? { configPage: config.configPage } : {}),
      ...(permissions.length > 0 ? { usesPermissions: permissions } : {}),
      plugin,
    },
  };
}
 
/**
 * Where one sandbox's sources sit, each folder relative to the repo root with forward slashes, plus the
 * family's name and what the target installs as.
 */
export interface SandboxDirs {
  engine: string;     // the framework, such as paf
  face: string;       // the face, such as face-a in a family, or . for a face on its own
  familyCore: string; // the face's family core, which is core in a family, or empty for none
  family: string;     // the family's name, such as my-family, or empty for none
  watchface: boolean; // true for a face target, false for an app one, which builds with -DBUILD_WATCHAPP
}
 
/**
 * A value as it goes inside the template's single-quoted Python strings. A backslash or a quote in a
 * folder name, as in Andrew's Faces, would otherwise end the string and break the wscript.
 */
function pythonQuoted(value: string): string {
  return value.replace(/\\/g, '\\\\').replace(/'/g, "\\'");
}
 
/**
 * Fills the wscript template in with where one sandbox's sources sit.
 *
 * The build runs from inside targets/<target>/, and everything it needs about where the framework, the
 * face and its family core are is written into the wscript here, so waf never goes looking for them.
 * The watchface flag rides along, since it decides whether the target compiles with
 * -DBUILD_WATCHAPP, and it is spelled the way Python reads a boolean.
 *
 * @param template The wscript template's text.
 * @param dirs The folders to fill in, and what the target installs as.
 * @return The finished wscript.
 */
export function fillWscript(template: string, dirs: SandboxDirs): string {
  return template
    .split('{{ENGINE_DIR}}').join(pythonQuoted(dirs.engine))
    .split('{{FACE_DIR}}').join(pythonQuoted(dirs.face))
    .split('{{FAMILY_CORE_DIR}}').join(pythonQuoted(dirs.familyCore))
    .split('{{FAMILY}}').join(pythonQuoted(dirs.family))
    .split('{{WATCHFACE}}').join(dirs.watchface ? 'True' : 'False');
}
 
/**
 * The version a face builds as. The face's own version wins, and the root package.json is the fallback.
 * The release check reads it from here too, so a tag is held to the version the pbw is built with.
 *
 * @param config The face's appinfo.
 * @param rootPkg The root package.json.
 * @return The version, or undefined when neither file has one.
 */
export function faceVersion(config: { version?: string }, rootPkg: { version?: string }): string | undefined {
  return config.version || rootPkg.version;
}
 
/** What the build reads and checks in a face's appinfo before the phone build, none of which needs the features. */
export interface AppinfoPrep extends FaceBuildInfo {
  /** The C defines from --dev and the unit's paf.dev.config.json, from devDefines. */
  devDefines: Defines;
  /** The face's switches for a dev build, from unitDevSwitches, or null for a build without --dev. */
  devSwitches: DevSwitches | null;
}
 
/** What the build reads and checks in a face's appinfo once, before it writes any target's sandbox. */
export interface CheckedAppinfo extends AppinfoPrep {
  /** The usesPermissions list, from permissionsFor. */
  permissions: Permission[];
  /** The C defines the build writes as text, from pluginSourceDefaults. */
  textDefines: TextDefines;
  /** What the build warns about and carries on past, from providerWarnings and permissionWarnings. */
  warnings: string[];
}
 
/**
 * Reads and checks the parts of a face's appinfo that do not need the features it passes.
 *
 * It stops on a define, a providers list, a usesPermissions entry, a part of a key group no feature or
 * define completes, half of the settings pair, which a kept plugin source choice brings whole but a
 * face may not list in part, a plugin only list beside its select key, the keys only a plugin fills without
 * its define, a dev build in a unit without the dev plugin, or a paf.dev.config.json the build cannot
 * read. None of these needs the keys the features bring, so the
 * build runs it before the phone build, and a refusal here leaves nothing written. A plugin's keys and
 * a select key are a define's alone, never a feature's, so the face's own keys give the same answer as
 * the whole list. checkAppinfo runs the key checks again on the whole list.
 *
 * @param config The face's appinfo.
 * @param face The face the appinfo belongs to, which a refusal names.
 * @param unit The unit's folder, whose paf.dev.config.json holds the dev switches.
 * @param dev Whether the build was asked for with --dev, which turns dev mode on.
 * @return The targets, defines, providers, and dev defines.
 */
export function prepareAppinfo(config: Appinfo, face: string, unit: string = ROOT, dev: boolean = false): AppinfoPrep {
  const info = faceBuildInfo(config, face);
 
  // a plugin app has no keys, defines, providers, or dev switches, since it has no watch code. buildOne
  // stops a dev build of one, or passes over it in all --dev
  if (isPluginApp(config)) {
    return { ...info, devDefines: {}, devSwitches: null };
  }
 
  checkOwnPermissions(config.usesPermissions, face, info.providers);
  checkKeyGroups(config.messageKeys, face, true);
  checkSelectKeys(info.providers, config.messageKeys, face);
  checkPluginKeys(info.defines, config.messageKeys, face);
 
  const devSwitches = unitDevSwitches(unit, face, dev);
 
  return { ...info, devDefines: devDefines(devSwitches), devSwitches };
}
 
/**
 * Reads and checks everything in a face's appinfo that every one of its targets shares.
 *
 * It stops on anything prepareAppinfo stops on, and on a key group declared in part or without the
 * keys it is read beside, a providers list the keys cannot back, or a usesPermissions entry the face
 * cannot build with. Every target gets the same answer, so it runs once per face. The checks against
 * the message keys need the keys the face's features bring, so the build runs them after the phone
 * build. A refusal at that point leaves each target's freshly compiled emit/ and its tsconfig.pkjs.json
 * behind, beside whatever package.json, wscript, and page/settings_page.h an earlier build wrote. The
 * header's fingerprint no longer matches the new emit/, so a pebble build run there by hand stops.
 *
 * @param config The face's appinfo, with the keys and the fields fillAppinfo fills in.
 * @param face The face the appinfo belongs to, which a refusal or a warning names.
 * @param unit The unit's folder, whose paf.dev.config.json holds the dev switches.
 * @param dev Whether the build was asked for with --dev, which turns dev mode on.
 * @param prep What prepareAppinfo already read and checked, dev switches included. Left out, it is read here.
 * @return The targets, defines, providers, permissions, text defines, and dev defines every target builds with, and the warnings to print.
 */
export function checkAppinfo(config: Appinfo, face: string, unit: string = ROOT, dev: boolean = false, prep: AppinfoPrep = prepareAppinfo(config, face, unit, dev)): CheckedAppinfo {
  // a plugin app has no keys, defines, or providers, and its permissions go in as it writes them
  if (isPluginApp(config)) {
    return { ...prep, permissions: readOwn(config.usesPermissions, face), textDefines: {}, warnings: [] };
  }
 
  checkMessageKeys(config.messageKeys, face, prep.defines, prep.providers);
 
  const permissions = permissionsFor(config.messageKeys, config.usesPermissions, face, prep.defines, prep.providers, config.capabilities);
  const textDefines = pluginSourceDefaults(prep.defines, prep.providers);
 
  return { ...prep, permissions, textDefines, warnings: [...providerWarnings(prep.providers, face), ...permissionWarnings(config.messageKeys, face, prep.providers)] };
}
 
/**
 * Writes one target's sandbox: targets/<target name>/{package.json,wscript}.
 *
 * @return The sandbox's absolute path.
 */
function writeTarget(face: string, config: Appinfo, rootPkg: RootPkg, target: Target, checked: CheckedAppinfo, devHeader: string | null): string {
  // the root package.json still owns the author
  const version = faceVersion(config, rootPkg) as string;
  const pluginApp = isPluginApp(config);
  // the dev switches are the face's, so every target of one face builds with the same ones
  const defines = { ...checked.defines, ...checked.devDefines };
  const manifest = pluginApp
    ? buildPluginManifest(config, { author: rootPkg.author, version }, checked.permissions)
    : buildManifest(config, { author: rootPkg.author, version }, target, defines, checked.permissions, checked.textDefines);
 
  const outDir = path.join(ROOT, 'targets', target.name);
 
  fs.mkdirSync(outDir, { recursive: true });
  writeIfChanged(path.join(outDir, 'package.json'), JSON.stringify(manifest, null, 2) + '\n');
  writeIfChanged(path.join(outDir, 'wscript'), pluginApp ? fs.readFileSync(PLUGIN_WSCRIPT, 'utf8') : faceWscript(face, target));
  writeDevHeader(outDir, devHeader);
 
  // stdout is kept for the sandbox paths the bare face prints, so the note goes to stderr
  console.error(`Sandbox targets/${target.name} is ready (source ${pluginApp ? `plugin app ${face} ${version}` : `face ${face} ${version}, watchface=${target.watchface}`}).`);
  return outDir;
}
 
/**
 * A face target's wscript. The waf entry point has to exist before `pebble build` runs in its sandbox.
 * The sandbox is named after the target, but its sources are the face's, and one face can feed several
 * targets, so the wscript is told where the face, its family core and the framework sit.
 */
function faceWscript(face: string, target: Target): string {
  const rel = faceRelative(face);
  const core = familyCoreFor(ROOT, rel);
  const dirs: SandboxDirs = {
    engine: ENGINE_REL,
    face: rel,
    familyCore: core ? path.relative(ROOT, core).split(path.sep).join('/') : '',
    family: familyNameFor(ROOT, rel) || '',
    watchface: target.watchface,
  };
 
  return fillWscript(fs.readFileSync(WSCRIPT_TEMPLATE, 'utf8'), dirs);
}
 
/**
 * The first target name two faces share, or null when every name is unique.
 *
 * A sandbox is named after its target, so two faces with the same name build into one folder and
 * the second quietly replaces the first's .pbw.
 *
 * @param targetsByFace Each face's target names, keyed by face.
 * @return A message naming the target and both faces, or null.
 */
export function findTargetClash(targetsByFace: Record<string, string[]>): string | null {
  const owner = new Map<string, string>();
 
  for (const [face, names] of Object.entries(targetsByFace)) {
    for (const name of names) {
      const other = owner.get(name);
 
      if (other === face) {
        return `target "${name}" is declared twice by ${face}, so one would build over the other in targets/${name}`;
      }
 
      if (other) {
        return `target "${name}" is declared by both ${other} and ${face}, so one would build over the other in targets/${name}`;
      }
 
      owner.set(name, face);
    }
  }
 
  return null;
}
 
/**
 * A face's appinfo, read from its pebble.appinfo.json.
 *
 * @param face The face to read.
 * @return The parsed appinfo.
 */
export function readAppinfo(face: string): Appinfo {
  return JSON.parse(fs.readFileSync(appinfoPath(face), 'utf8'));
}
 
/** What the phone build needs from a face's appinfo, read in one go. */
export interface FaceBuildInfo {
  /** The name of every target the face declares, in its order. */
  targets: string[];
  /** The face's defines, from readDefines. */
  defines: Defines;
  /** The face's providers list, from readProviders. */
  providers: ProviderLists;
  /** The appinfo's displayName, which the settings page heading shows, or null for one with none. */
  appName?: string | null;
}
 
/**
 * The target names, the defines, and the providers list out of a face's appinfo.
 *
 * None of it reads the message keys, since the phone build reads this before the build knows the keys
 * the face's features bring. checkAppinfo checks the keys.
 *
 * A plugin app has no key groups, defines, or providers to read, so it has its one target and nothing
 * else. resolveTargets checks it, and refuses an appinfo that is neither a plugin app nor a face.
 *
 * @param appinfo The face's appinfo.
 * @param face The face the appinfo belongs to, which a refusal names.
 * @return The target names, in the order the face declares them, the defines, the checked providers,
 *   and the display name.
 */
export function faceBuildInfo(appinfo: Appinfo, face: string): FaceBuildInfo {
  // resolveTargets tells a plugin app from a face and refuses an appinfo that is neither, so it runs first
  const targets = resolveTargets(appinfo, face).map((target) => target.name);
 
  if (isPluginApp(appinfo)) {
    return { targets, defines: {}, providers: {} };
  }
 
  const defines = readDefines(appinfo, face);
 
  return {
    targets,
    defines,
    providers: readProviders(appinfo, face, defines),
    appName: displayNameOf(appinfo),
  };
}
 
/**
 * The name the settings page heading shows, the appinfo's displayName with the space round it trimmed.
 *
 * @param appinfo The face's appinfo.
 * @return The name, or null when the appinfo has none, or only space.
 */
export function displayNameOf(appinfo: { displayName?: unknown }): string | null {
  const name = typeof appinfo.displayName === 'string' ? appinfo.displayName.trim() : '';
 
  return name || null;
}
 
/**
 * Reads a face's appinfo once for its target names, its defines, and its providers list, for a phone
 * build run on its own. paf build reads them through prepareFace.
 *
 * The phone build compiles into the sandboxes before they are written, so a target name another face
 * already uses stops it here, before either face's sandbox is touched.
 *
 * @param face The face to read.
 * @return The target names, in the order the face declares them, the defines, and the checked providers.
 */
export function readFaceBuildInfo(face: string): FaceBuildInfo {
  const info = faceBuildInfo(readAppinfo(face), face);
 
  refuseTargetClash(face, info.targets);
  return info;
}
 
/**
 * The name of every target a face declares, which is also the name of each one's sandbox.
 *
 * @param face The face to read.
 * @return The target names, in the order the face declares them.
 */
export function faceTargetNames(face: string): string[] {
  return resolveTargets(readAppinfo(face), face).map((target) => target.name);
}
 
/** Every face's target names, kept once read, since a build of every face checks each against them. */
let targetNames: Record<string, string[]> | null = null;
 
/** Every face's target names, leaving out a face whose appinfo does not read, since its own build reports that. */
function allTargetNames(): Record<string, string[]> {
  if (targetNames) {
    return targetNames;
  }
 
  const byFace: Record<string, string[]> = {};
 
  for (const name of listFaceNames()) {
    try {
      byFace[name] = faceTargetNames(name);
    } catch {
      // unreadable, so it has no targets to clash with
    }
  }
 
  targetNames = byFace;
  return byFace;
}
 
/** Stops on a target name another face uses, or one the face declares twice. */
function refuseTargetClash(face: string, targets: string[]): void {
  const clash = findTargetClash({ ...allTargetNames(), [face]: targets });
 
  if (clash) {
    throw new ToolError(clash);
  }
}
 
/** What the build reads of a face before the phone build, read once and handed to every later step. */
export interface PreparedFace {
  /** The face's appinfo, as its file holds it. */
  appinfo: Appinfo;
  /** Every target the face declares, in its order. */
  targets: Target[];
  /** The appinfo checks that need no features, from prepareAppinfo. */
  info: AppinfoPrep;
  /** The face's own keys with its defines' keys added, which the start read stands in before it knows the features. */
  baseKeys: unknown;
  /** Whether the face is a plugin app, which has no phone build, start read, keys, or filled-in fields. */
  pluginApp: boolean;
}
 
/**
 * Reads a face's appinfo once and checks everything that does not need the features it passes.
 *
 * It stops on a target name another face uses, on anything prepareAppinfo stops on, and on the fields
 * fillAppinfo refuses whatever the features are, such as multi JS turned off. The build runs it before
 * the phone build, so any of these refusals leaves nothing written.
 *
 * @param face The face to read.
 * @param dev Whether the build was asked for with --dev, which turns dev mode on.
 * @param unit The unit's folder, whose paf.dev.config.json holds the dev switches.
 * @return The appinfo, the targets, the checked defines and providers, and the keys before the features.
 */
export function prepareFace(face: string, dev: boolean = false, unit: string = ROOT): PreparedFace {
  const appinfo = readAppinfo(face);
  const targets = resolveTargets(appinfo, face);
 
  refuseTargetClash(face, targets.map((target) => target.name));
 
  const info = prepareAppinfo(appinfo, face, unit, dev);
  const pluginApp = isPluginApp(appinfo);
  // a plugin app has no keys and none of the fields fillAppinfo fills, so it goes on as it is written
  const baseKeys = pluginApp ? undefined : fillAppinfo(appinfo, face, NO_START, info.defines, info.providers).messageKeys;
 
  return { appinfo, targets, info, baseKeys, pluginApp };
}
 
/** What writeSandboxes wrote, and what it read and checked in the appinfo on the way. */
export interface WrittenSandboxes {
  /** Each sandbox's absolute path, in the order the face declares its targets. */
  paths: string[];
  /** The appinfo as checkAppinfo read it. */
  checked: CheckedAppinfo;
  /** The message keys the manifest carries, the face's own and the ones fillAppinfo added. */
  messageKeys: unknown;
  /** Whether the face is a plugin app, which builds with no phone build, settings page, or header. */
  pluginApp: boolean;
  /** Each sandbox whose message keys differ from the ones its last passing build recorded, which has to build clean. */
  rekeyed: string[];
}
 
/** The file in each sandbox that holds the message keys its last passing pebble build was made with. */
export const BUILT_KEYS = 'built-keys.json';
 
/**
 * Whether a sandbox's last passing build was made with other message keys than these.
 *
 * The SDK reads the keys only when a sandbox's build starts clean, so a changed list fails the next
 * build on an undeclared MESSAGE_KEY_. paf starts a build clean when the appinfo's keys change, but the
 * features a face passes and its defines change the list too. The build takes the record away before
 * a sandbox's pebble clean or pebble build runs and writes it again only once pebble build passes, so
 * a record only ever sits beside a build made with its keys. A build that stops partway leaves none,
 * and the next build cleans, whatever keys it comes back with. A sandbox with no record, or one that
 * does not read, counts as changed, which costs one clean build.
 *
 * @param sandbox The sandbox's folder.
 * @param messageKeys The message keys the manifest carries now.
 * @return True when the sandbox has to build clean.
 */
export function keysChanged(sandbox: string, messageKeys: unknown): boolean {
  try {
    return fs.readFileSync(path.join(sandbox, BUILT_KEYS), 'utf8') !== JSON.stringify(messageKeys ?? null);
  } catch {
    return true;
  }
}
 
/**
 * Takes a sandbox's record away before its pebble clean or pebble build runs, since from then on its
 * build/ may no longer match the keys the record names.
 *
 * @param sandbox The sandbox's folder.
 */
export function forgetBuiltKeys(sandbox: string): void {
  fs.rmSync(path.join(sandbox, BUILT_KEYS), { force: true });
}
 
/**
 * Records the message keys a sandbox's pebble build passed with, which keysChanged reads next time.
 *
 * @param sandbox The sandbox's folder.
 * @param messageKeys The message keys the manifest carries.
 */
export function recordBuiltKeys(sandbox: string, messageKeys: unknown): void {
  writeIfChanged(path.join(sandbox, BUILT_KEYS), JSON.stringify(messageKeys ?? null));
}
 
/**
 * Writes every target sandbox a face declares.
 *
 * prepareFace has already stopped a target name another face uses, since the two would build into
 * one sandbox and the second would quietly replace the first's .pbw. Anything checkAppinfo refuses
 * stops it here before anything is written, and its warnings print once for the face rather than once
 * per target.
 *
 * The appinfo is filled in from what the build read of the face's phone code first, so every check
 * and every manifest sees the keys the features and the defines add.
 *
 * The build checks the face's settings page against the same checked appinfo, so it comes back with
 * the paths rather than being read and checked again.
 *
 * @param face The face to write sandboxes for.
 * @param prepared What prepareFace read and checked.
 * @param start What the build read from the face's compiled phone code.
 * @return Each sandbox's absolute path, in the order the face declares its targets, with the checked appinfo, its message keys, and the sandboxes whose keys changed.
 */
export function writeSandboxes(face: string, prepared: PreparedFace, start: FaceStartFacts): WrittenSandboxes {
  const pluginApp = isPluginApp(prepared.appinfo);
  // a plugin app's appinfo goes in as it is written, with nothing filled in
  const config = pluginApp ? prepared.appinfo : fillAppinfo(prepared.appinfo, face, start, prepared.info.defines, prepared.info.providers);
  // prepared.info already holds the dev switches the build was asked for, so dev is not read again here
  const checked = checkAppinfo(config, face, ROOT, false, prepared.info);
 
  for (const warning of checked.warnings) {
    console.warn(`warning: ${warning}`);
  }
 
  const rootPkg: RootPkg = JSON.parse(fs.readFileSync(ROOT_PKG, 'utf8'));
  // a plugin app has no message keys, so it never needs a clean for them
  const rekeyed = pluginApp ? [] : prepared.targets.map((target) => path.join(ROOT, 'targets', target.name)).filter((sandbox) => keysChanged(sandbox, config.messageKeys));
 
  // the header is the face's, so it is checked and written once for all its targets
  const devHeader = checked.devSwitches ? renderDevHeader(checked.devSwitches, { face, keys: [...keyNames(config.messageKeys)], defines: checked.defines, thumbnails: thumbnailFiles(face) }) : null;
 
  return { paths: prepared.targets.map((target) => writeTarget(face, config, rootPkg, target, checked, devHeader)), checked, messageKeys: config.messageKeys, pluginApp, rekeyed };
}
 
/**
 * What the command line asks for, as the lines it prints.
 *
 * With --targets it is a face's target names, so a script outside the framework's TypeScript uses
 * the same lookup every tool does. The sandboxes are written only by paf build, since their keys need
 * the features the face's compiled phone code passes, so a bare face is refused rather than given a
 * manifest missing those keys.
 */
function run(args: string[]): string[] {
  if (args[0] !== '--targets' || !args[1]) {
    throw new ToolError('usage: build-manifests.ts --targets <face>. The sandboxes need the keys the face\'s features bring, which only the compiled phone code says, so write them with paf build <face>');
  }
 
  return faceTargetNames(args[1]);
}
 
function main() {
  try {
    for (const line of run(process.argv.slice(2))) {
      console.log(line);
    }
  } catch (error) {
    reportFailure(error);
  }
}
 
Iif (isMainScript(import.meta)) {
  main();
}