ts

TypeScript <-> MoonBit bridge generator

typescript
javascript
moonbit
bridge
ffi
d.ts
moon add mizchi/ts@0.5.2
Download zip
Author
Version
0.5.2
License
Apache-2.0
Last updated
18 days ago
Downloads
882

Dependencies

README

#mizchi/ts

Status: Experimental

MoonBit と TypeScript の間で bridge package を生成する toolchain です。TypeScript の 型定義から MoonBit bridge を生成し、MoonBit package から JavaScript runtime と .d.ts を持つ npm package を生成します。生成物は再生成可能な output として扱います。

#Quick start

TypeScript dependencies を MoonBit から使う場合:

moon install mizchi/ts/cmd/ts2mbt ts2mbt generate

MoonBit package を npm package にする場合:

moon install mizchi/ts/cmd/mbt2ts cd my-moonbit-library mbt2ts --pkg cd npm && npm publish --access public

公開済み @mizchi/ts の type checker は global install なしで実行できます。

npx --package=@mizchi/ts mtsc --help

Hono を MoonBit から使う完全な手順は Quick start を参照してください。

#Requirements

  • MoonBit toolchainmoon$PATH にあること)
  • Node.js 24+
  • pnpm(verification scripts 用)
  • 任意: just

#Install

moon install mizchi/ts/cmd/ts2mbt moon install mizchi/ts/cmd/mbt2ts # 両方を一度に install moon install mizchi/ts/...

install 後は ~/.moon/bin/$PATH に追加します。source checkout から実行する 場合は moon run src/cmd/{ts2mbt,mbt2ts} -- ... を使います。

#Tools

Tool概要詳細
ts2mbtTypeScript declaration / npm package を MoonBit bridge に変換docs/ts2mbt.md
mbt2tsMoonBit package を TypeScript declaration / npm package に変換docs/mbt2ts.md
mtscTypeScript / TSX を型検査して JavaScript に変換docs/mtsc.md

tschecktsacc は開発用 command であり、公開 tool には含めません。

#Generated package contract

  • ts2mbt の output は consumer module の internal/generated/ に置く bridge package です。SCAFFOLD_DIAGNOSTICS.md で widen / omit した surface を確認します。
  • mbt2ts --pkg の output は npm/ です。moon.mod の version と metadata を使い、 package.jsonindex.js.d.ts、subpath export、必要なら npm bin を生成します。
  • どちらも output を手編集せず、入力と option から再生成してください。

詳細な type boundary、facade、runtime validation、package export、unsupported surface は 各 tool guide に記載しています。

#Diagnostics and examples

  • docs/ts2mbt.mdSCAFFOLD_DIAGNOSTICS.md、vendor と bridge。
  • docs/mbt2ts.mdAUTOLINK_DIAGNOSTICS.md、npm publish。
  • docs/mtsc.md — checker の CLI、ABI、既知ギャップ。
  • examples/just verify-examples で検証する runnable fixture。

#Development

moon fmt moon info just check just test just verify-scaffolds just verify-examples

checker の TypeScript conformance gate は次を使います。

just checker-conformance-oracle --max-fp 0 --max-legal-parsefail 1

軽量な conformance 集計には tsacc guide を参照してください。

優先度と既知の制約は checker priority を参照してください。

#License

Apache-2.0

#
CliArgScan

pub enum CliArgScan {
Args(Array[String])
HelpRequested
}

Outcome of a CLI subcommand argument scan.

  • Args(positionals): the parse succeeded; positionals are the subcommand-specific args (args[start:]).
  • HelpRequested: the next token after the subcommand was a help marker; the caller should print usage and return without erroring.

#
CLI_VERSION

let CLI_VERSION : String

mizchi/ts package version reported by --version / -V from each cmd binary. Sourced from moon.mod.json and bumped together with it at release time.

#
cli_bail

fn cli_bail(message : String) -> Unit

Bail out of the CLI: print Error: <message> then exit with status 1. Suitable for missing-argument, unknown-subcommand, and other usage errors that should not be silently ignored.

#
cli_clean_error

fn cli_clean_error(message : String) -> String

Sanitize a raw bridge / IO error message so the printed CLI text doesn't leak the MoonBit OSError("@fs.open(): ...") debug shape. Specifically: when the message contains OSError("..."), replace that fragment with the inner reason (after the first : delimiter) so users see a single-quoted file path + plain reason.

#
cli_exit

fn cli_exit(code : Int) -> Unit

Exit the CLI with the given status code. Use 0 for success and a non-zero value for any failure path.

#
cli_fail

fn cli_fail(binary_name : String) -> Unit

Print a one-line " command failed" banner and exit with status 1. Cmd dispatchers should call this from every error path so CI / scripts can detect failure.

#
cli_parse_subcommand_args

fn cli_parse_subcommand_args(args : Array[String], start : Int, min_args : Int, usage : String, help_hint? : String) -> CliArgScan

Parse the positional arguments for a subcommand. Returns HelpRequested when the user passed --help / -h / help. Calls cli_bail (which exits with status 1) when fewer than min_args positionals are present — the bail message includes a hint pointing at the per-subcommand --help so users know how to recover.

args is the full process argv slice (@env.args()), start is the index of the first positional after the subcommand verb, and usage is the bail message printed when the arity check fails. The optional help_hint (e.g. "ts2mbt decl --help") is appended to the bail message; pass "" to suppress it.

#
cli_print_version

fn cli_print_version(binary_name : String) -> Unit

Print the version banner for the given binary name and exit with status 0. Cmd dispatchers should intercept --version / -V and route here.

#
cli_subcommand_is_help

fn cli_subcommand_is_help(token : String) -> Bool

Treat the given subcommand token as a help request — --help, -h, or help. Cmd dispatchers use this to short-circuit per-subcommand help before treating the next positional as a path.

#
cli_token_is_version

fn cli_token_is_version(token : String) -> Bool

Treat the given top-level token as a version request — --version or -V. Cmd dispatchers use this to short-circuit the help/dispatch pipeline and just print the version banner.

#
compute_vendor_root

fn compute_vendor_root(module_root : String, moon_mod_source : String) -> String

Pure-function variant of resolve_default_vendor_root for testing. Given a moon module root and the raw moon.mod.json contents, returns the directory the vendor pipeline should write into.

When moon.mod.json does not declare a source field MoonBit treats the module root itself as the source directory, so the fallback here is "." rather than "src". Picking "src" for missing-source projects would emit the bridge under a phantom <root>/src/internal/generated/... path that the moon module can't see, and moon build would silently skip it.
async fn emit_js_link_config_from_mbti(file_path : String, output_path : String?) -> Bool

#
emit_moonbit_bridge

async fn emit_moonbit_bridge(file_path : String, module_spec : String, decl_output_path : String?, ffi_output_path : String?, bridge_output_path : String?) -> Bool

#
emit_moonbit_bridge_package

async fn emit_moonbit_bridge_package(file_path : String, module_spec : String, output_dir : String, bare_module_specifier? : String?, moonbitlang_async_integration? : Bool, runtime_validation? : Bool) -> Bool

#
emit_moonbit_decl

async fn emit_moonbit_decl(file_path : String, output_path : String?) -> Bool

#
emit_moonbit_js_ffi

async fn emit_moonbit_js_ffi(file_path : String, module_spec : String, ffi_output_path : String?, bridge_output_path : String?) -> Bool

#
emit_moonbit_scaffold_from_ts

async fn emit_moonbit_scaffold_from_ts(file_path : String, module_spec : String, output_dir : String, write_diagnostics? : Bool, bare_module_specifier? : String?) -> Bool

#
emit_typescript_decl

async fn emit_typescript_decl(file_path : String, output_path : String?) -> Bool

#
emit_typescript_decl_from_mbti

async fn emit_typescript_decl_from_mbti(file_path : String, output_path : String?) -> Bool

#
emit_typescript_facade_scaffold_from_mbti

async fn emit_typescript_facade_scaffold_from_mbti(file_path : String, output_dir : String, import_rewrite_path : String?) -> Bool

#
emit_typescript_package_from_mbti

async fn emit_typescript_package_from_mbti(file_path : String, output_dir : String, import_rewrite_path : String?) -> Bool

#
emit_typescript_scaffold_from_mbti

async fn emit_typescript_scaffold_from_mbti(file_path : String, output_dir : String, import_rewrite_path : String?) -> Bool

#
run_mbt_to_ts_pkg_cli

async fn run_mbt_to_ts_pkg_cli(args : Array[String], start : Int, print_help~ : () -> Unit) -> Bool

Public entry for mbt2ts --pkg. It discovers the enclosing MoonBit module from the caller's working directory and leaves all package policy to the project-level generator above.

#
run_mbt_to_ts_unified_cli

async fn run_mbt_to_ts_unified_cli(args : Array[String], start : Int, print_help~ : () -> Unit) -> Bool

Public entry: run the unified MoonBit -> TypeScript scaffold driver with the direction locked to mbt-to-ts. Used by src/cmd/mbt2ts.

#
run_ts_to_mbt_unified_cli

async fn run_ts_to_mbt_unified_cli(args : Array[String], start : Int, print_help~ : () -> Unit) -> Bool

Public entry: run the unified TS -> MoonBit scaffold driver with the direction locked to ts-to-mbt. Used by src/cmd/ts2mbt.

#
ts2mbt_generate_from_package_json

async fn ts2mbt_generate_from_package_json(package_json_path_override? : String?, vendor_root_override? : String?) -> Bool

Generate MoonBit bridges for every dependency listed in a package.json (dependencies + devDependencies) and write them under <source>/internal/generated/<safe>/. Emits a per-package summary at the end and returns true only when every package generated successfully.

package_json_path_override defaults to ./package.json.

#
ts2mbt_vendor_package

async fn ts2mbt_vendor_package(pkg_spec : String, module_spec_override? : String?, vendor_root_override? : String?, print_import_hint? : Bool) -> Bool