"""Rule for running JS/TS scripts with Bun.""" def _bun_binary_impl(ctx): toolchain = ctx.toolchains["//bun:toolchain_type"] bun_bin = toolchain.bun.bun_bin entry_point = ctx.file.entry_point launcher = ctx.actions.declare_file(ctx.label.name) ctx.actions.write( output = launcher, is_executable = True, content = """#!/usr/bin/env bash set -euo pipefail exec \"{}\" run \"{}\" \"$@\" """.format(bun_bin.path, entry_point.path), ) transitive_files = [] if ctx.attr.node_modules: transitive_files.append(ctx.attr.node_modules[DefaultInfo].files) runfiles = ctx.runfiles( files = [bun_bin, entry_point] + ctx.files.data, transitive_files = depset(transitive = transitive_files), ) return [ DefaultInfo( executable = launcher, runfiles = runfiles, ), ] bun_binary = rule( implementation = _bun_binary_impl, attrs = { "entry_point": attr.label( mandatory = True, allow_single_file = [".js", ".ts", ".jsx", ".tsx", ".mjs", ".cjs"], ), "node_modules": attr.label(), "data": attr.label_list(allow_files = True), }, executable = True, toolchains = ["//bun:toolchain_type"], )