load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm")
load("@npm//@bazel/typescript:index.bzl", "ts_library")
load("//dev-infra:index.bzl", "ng_dev_rolled_up_generated_file")

ts_library(
    name = "cli",
    srcs = [
        "cli.ts",
    ],
    module_name = "@angular/dev-infra-private",
    deps = [
        "//dev-infra/caretaker",
        "//dev-infra/commit-message",
        "//dev-infra/format",
        "//dev-infra/misc",
        "//dev-infra/ngbot",
        "//dev-infra/pr",
        "//dev-infra/pullapprove",
        "//dev-infra/release",
        "//dev-infra/ts-circular-dependencies",
        "//dev-infra/utils",
        "@npm//@types/node",
        "@npm//@types/yargs",
        "@npm//yargs",
    ],
)

genrule(
    name = "package-json",
    srcs = [
        "tmpl-package.json",
        "//:package.json",
    ],
    outs = ["package.json"],
    cmd = """
        $(execpath //tools:inline-package-json-deps) $(execpath tmpl-package.json) \
            $(execpath //:package.json) $@
    """,
    tools = ["//tools:inline-package-json-deps"],
)

pkg_npm(
    name = "npm_package",
    srcs = [
        "index.bzl",
        "//dev-infra/bazel:files",
        "//dev-infra/benchmark:files",
    ],
    substitutions = {
        # angular/angular should not consume it's own packages, so we use
        # substitutions to replace these in the published version of dev-infra.
        "//dev-infra/": "@npm//@angular/dev-infra-private/",
        "//packages/benchpress": "@npm//@angular/benchpress",
        "//packages/bazel": "@npm//@angular/bazel",
        "//packages/zone.js/bundles:zone.umd.js": "@npm//zone.js",
        "//packages/core": "@npm//@angular/core",
        "//packages/platform-browser": "@npm//@angular/platform-browser",

        # This substitution is particularly verbose because we need to make sure
        # that only things available via Angular Bazel are imported from
        # tools/defaults.bzl.
        "load\\(\"//tools:defaults.bzl\", \"ng_module\"\\)": "load(\"@npm//@angular/bazel:index.bzl\", \"ng_module\")",
    },
    visibility = ["//visibility:public"],
    deps = [
        ":cli",
        ":package-json",
        "//dev-infra/benchmark/driver-utilities",
        "//dev-infra/commit-message",
        "//dev-infra/ts-circular-dependencies",
    ],
)

# Because the angular/angular repository relies on the local repository for running ng-dev commands,
# the rollup generated javascript files are committed into the repository to be used as node
# scripts. To ensure they stay up to date, they are created using a generated file test.
#
# Currently there are two generated files which are needed
#  ng-dev.js       -  The main script representing ng-dev
#  build-worker.js -  The worker script for the `ng-dev release build` command, allowing it to run
#                     in a forked process.
ng_dev_rolled_up_generated_file(
    name = "ng-dev",
    entry_point = ":cli.ts",
    rollup_args = [
        "--plugin",
        "rollup-plugin-hashbang",
    ],
    deps = [
        ":cli",
        # TODO(josephperrott): Determine if this plugin is the best method for ensuring the hashbang
        # in both local and published use case.
        "@npm//rollup-plugin-hashbang",
    ],
)

ng_dev_rolled_up_generated_file(
    name = "build-worker",
    entry_point = "//dev-infra/release/build:build-worker.ts",
    deps = [
        "//dev-infra/release/build",
    ],
)