|
| 1 | +load("@//:common.bzl", "on_windows") |
| 2 | + |
| 3 | +# Builds a zip file of the compiled typscript-parser-wrapper and its dependencies. |
| 4 | +genrule( |
| 5 | + name = "typescript", |
| 6 | + srcs = [ |
| 7 | + "tsconfig.json", |
| 8 | + "package.json", |
| 9 | + "package-lock.json", |
| 10 | + ] + glob(["src/**"]), |
| 11 | + outs = ["javascript.zip"], |
| 12 | + cmd = "\n".join([ |
| 13 | + # the original working directory is not preserved anywhere, but needs to be accessible, as |
| 14 | + # all paths are relative to this |
| 15 | + # unfortunately, we need to change the working directory to run npm. |
| 16 | + "export BAZEL_ROOT=$$(pwd)", |
| 17 | + # we need a temp dir, and unfortunately, $TMPDIR is not set on Windows |
| 18 | + "export TEMP=$$(mktemp -d)", |
| 19 | + # Add node to the path so that npm run can find it - it's calling env node |
| 20 | + "export PATH=$$PATH:$$BAZEL_ROOT/$$(dirname $(execpath @nodejs//:node_bin))", |
| 21 | + "export NPM=$$BAZEL_ROOT/$(execpath @nodejs//:npm_bin)", |
| 22 | + # npm has a global cache which doesn't work on macos, where absolute paths aren't filtered out by the sandbox. |
| 23 | + # Therefore, set a temporary cache directory. |
| 24 | + "export NPM_CONFIG_USERCONFIG=$$TEMP/npmrc", |
| 25 | + "$$NPM config set cache $$TEMP/npm", |
| 26 | + "$$NPM config set update-notifier false", |
| 27 | + "rm -rf $(RULEDIR)/inputs", |
| 28 | + "cp -L -R $$(dirname $(execpath package.json)) $(RULEDIR)/inputs", |
| 29 | + "cd $(RULEDIR)/inputs", |
| 30 | + "$$NPM install", |
| 31 | + "$$NPM run build", |
| 32 | + "rm -rf node_modules", |
| 33 | + # Install again with only runtime deps |
| 34 | + "$$NPM install --prod", |
| 35 | + "mv node_modules build/", |
| 36 | + "mkdir -p javascript/tools/typescript-parser-wrapper", |
| 37 | + "mv build/* javascript/tools/typescript-parser-wrapper", |
| 38 | + "", |
| 39 | + ]) + on_windows( |
| 40 | + " && ".join([ |
| 41 | + "$$BAZEL_ROOT/$(execpath @bazel_tools//tools/zip:zipper) cC $$(cygpath -w $$BAZEL_ROOT/$@) $$(find javascript -name '*' -print)", |
| 42 | + "rm -rf $$TEMP", |
| 43 | + ]), |
| 44 | + " && ".join([ |
| 45 | + "$$BAZEL_ROOT/$(execpath @bazel_tools//tools/zip:zipper) cC $$BAZEL_ROOT/$@ $$(find javascript -name '*' -print)", |
| 46 | + "rm -rf $$TEMP", |
| 47 | + ]), |
| 48 | + ), |
| 49 | + tools = [ |
| 50 | + "@bazel_tools//tools/zip:zipper", |
| 51 | + "@nodejs//:node_bin", |
| 52 | + "@nodejs//:npm_bin", |
| 53 | + ], |
| 54 | + visibility = ["//visibility:public"], |
| 55 | +) |
0 commit comments