# BEGIN-INTERNAL
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm")
load("@npm_bazel_typescript//:index.bzl", "ts_library")
load("//tools:defaults.bzl", "jasmine_node_test")

ts_library(
    name = "lib",
    srcs = glob(["lib/*.ts"]),
    module_name = "ts-api-guardian",
    tsconfig = "//tools:tsconfig.json",
    visibility = ["//visibility:public"],
    deps = [
        "@npm//@types/diff",
        "@npm//@types/minimist",
        "@npm//@types/node",
        "@npm//chalk",
        "@npm//diff",
        "@npm//minimist",
        "@npm//typescript",
    ],
)

# Copy Angular's license to govern ts-api-guardian as well.
# We use a genrule to put it in this package, so it will be in the right root directory.
genrule(
    name = "license",
    srcs = ["//:LICENSE"],
    outs = ["LICENSE"],
    cmd = "cp $< $@",
)

pkg_npm(
    name = "ts-api-guardian",
    srcs = [
        "BUILD.bazel",
        "README.md",
        "bin/ts-api-guardian",
        "index.bzl",
        "package.json",
    ],
    substitutions = {
        "@angular//tools/ts-api-guardian:bin": "@npm_ts_api_guardian//:bin",
        "@angular//tools/ts-api-guardian:lib": "@npm//ts-api-guardian",
        "angular/tools/ts-api-guardian/": "npm_ts_api_guardian/",
    },
    deps = [
        ":lib",
        ":license",
    ],
)

#######################################3
# Tests for this package

ts_library(
    name = "test_lib",
    testonly = True,
    srcs = glob(
        ["test/*.ts"],
        exclude = ["test/bootstrap.ts"],
    ),
    tsconfig = "//tools:tsconfig-test",
    deps = [
        ":lib",
        "@npm//@types/chai",
        "@npm//@types/jasmine",
        "@npm//@types/node",
        "@npm//chai",
        "@npm//jasmine",
        "@npm//typescript",
    ],
)

ts_library(
    name = "bootstrap",
    testonly = True,
    srcs = ["test/bootstrap.ts"],
    tsconfig = "//tools:tsconfig-test",
    deps = ["@npm//@types/node"],
)

# Select the es5 .js output of the ts_library :boostrap target
# with `output_group = "es5_sources"` for use in the jasmine_node_test
# below. This exposes an internal detail of ts_library that is not ideal.
# TODO(gregmagolan): clean this up by using tsc() in this case rather than ts_library
filegroup(
    name = "bootstrap_es5",
    testonly = True,
    srcs = [":bootstrap"],
    output_group = "es5_sources",
)

jasmine_node_test(
    name = "tests",
    srcs = [
        ":test_lib",
    ],
    bootstrap = [":bootstrap_es5"],
    data = glob([
        "test/fixtures/*.ts",
        "test/fixtures/*.patch",
    ]) + [
        ":ts-api-guardian",
    ],
)
# END-INTERNAL

filegroup(
    name = "bin",
    srcs = glob(["lib/*.js"]) + ["bin/ts-api-guardian"],
    visibility = ["//visibility:public"],
)

# Exported to be referenced as entry_point of the nodejs_binary
exports_files(["bin/ts-api-guardian"])