summaryrefslogtreecommitdiffstats
path: root/tools/ts/build_services.js
blob: 397eff7074219d323ffd5d84a88bb5690a612047 (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

/**
 * Build: <objdir>/dist/@types/lib.gecko.services.d.ts,
 *
 * from:  <objdir>/xpcom/components/services.json,
 *        generated by a previous build step.
 */

const fs = require("fs");

const HEADER = `/**
 * NOTE: Do not modify this file by hand.
 * Content was generated from services.json.
 */
`;

const SERVICES = {
  cpmm: ["ContentProcessMessageManager"],
  mm: ["ChromeMessageBroadcaster"],
  ppmm: ["ParentProcessMessageManager"],
};

function main(lib_dts, services_json) {
  let interfaces = JSON.parse(fs.readFileSync(services_json, "utf8"));

  for (let [iface, svc] of Object.entries(interfaces)) {
    SERVICES[svc] = SERVICES[svc] ?? [];
    SERVICES[svc].push(iface);
  }

  let lines = [HEADER];
  lines.push("interface JSServices {");
  for (let [svc, ifaces] of Object.entries(SERVICES).sort()) {
    lines.push(`  ${svc}: ${ifaces.join(" & ")};`);
  }
  lines.push("}\n");

  let dts = lines.join("\n");
  console.log(`[INFO] ${lib_dts} (${dts.length.toLocaleString()} bytes)`);
  fs.writeFileSync(lib_dts, dts);
}

main(...process.argv.slice(2));