# 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/.
import json import os import shutil
import mozpack.path as mozpath from mach.decorators import Command, CommandArgument, SubCommand
@Command("ts", category="misc", description="Run TypeScript and related commands.") def ts(ctx): """
TypeScript related commands to build/update typelibs and type-check js.
Example: # Use tsc to check types in extensions framework code:
$ ./mach ts check toolkit/components/extensions """
ctx._sub_mach(["help", "ts"]) return 1
@SubCommand("ts", "build", description="Build typelibs.")
@CommandArgument("lib", choices=targets, nargs="?") def build(ctx, lib): """Command to build the target typelibs."""
if lib isNone: for t in targets: if rv := build(ctx, t): return rv return 0
if lib == "xpcom": # When we hook this up to be part of the build, we'll have # an explicit list of files. For now, just get all of them.
dir = mozpath.join(ctx.topobjdir, "config/makefiles/xpidl") ifnot os.path.isdir(dir): return build_required(lib, dir)
files = [f for f in os.listdir(dir) if f.endswith(".d.json")] ifnot len(files): return build_required(lib, f"*.d.json files in {dir}")
if lib == "dom": # Same as above, get all *.webidl files for now.
dir = mozpath.join(ctx.topsrcdir, "dom")
files = [] for subdir in ["webidl", "chrome-webidl"]: for file in os.listdir(mozpath.join(dir, subdir)): if file.endswith(".webidl"):
files.append(subdir + "/" + file)
@SubCommand("ts", "check", description="Check types in a project using tsc.")
@CommandArgument("paths", nargs="+", help="Path to a (dir with) tsconfig.json.") def check(ctx, paths): for p in paths:
rv = node(ctx, "node_modules/typescript/bin/tsc", "--project", p) if rv: return rv return 0
@SubCommand("ts", "setup", description="Install TypeScript and other dependencies.") def setup(ctx): # Install locally under tools/ts/node_modules, to avoid any conflicts for now.
os.chdir(mozpath.dirname(__file__)) return ctx._sub_mach(["npm", "ci"])
for lib in targets:
file = f"lib.gecko.{lib}.d.ts"
path = mozpath.join(ctx.distdir, "@types", file) ifnot os.path.exists(path):
print(f"[ERROR] {path} not found. Did you run `mach ts build`?") return 1
# This command inherently goes in a confusing direction, we're copying: # from `<topobjdir>/dist/@types` files generated with `mach ts build`, # into `<topsrcdir>/tools/@types` typelib dir back in your source tree.
print(f"[INFO] Updating {typelib_dir}/{file}")
shutil.copy(path, typelib_dir)
print("[WARNING] Your source tree was updated, you should commit the changes.")
def maybe_setup(ctx): """Check if npm modules are installed, and run setup if needed."""
dir = mozpath.dirname(__file__)
package_json = json.load(open(mozpath.join(dir, "package.json")))
needs_setup = False
# TODO: Use proper version checking from tools/lint/eslint/setup_helper.py. for module in package_json.get("devDependencies", {}):
path = mozpath.join(dir, "node_modules", module, "package.json") ifnot os.path.isfile(path):
print(f"Missing node module {path}.")
needs_setup = True break
if needs_setup:
print("Running npm install.")
setup(ctx)
def build_required(lib, item):
print(f"Missing {item}.")
print(f"Building {lib} typelib requires a full Firefox build.") return 1
Messung V0.5
¤ Dauer der Verarbeitung: 0.15 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.