From 0c36911ae7cf8ffd07f3b66bde3e6c2b13546829 Mon Sep 17 00:00:00 2001 From: jhugman Date: Mon, 30 Sep 2024 21:05:59 +0100 Subject: [PATCH] Translates cli into javascript (#102) This PR translates the command line launcher in to JS. This means it can be launched from `package.json`. Adding to `package.json`: ```json "scripts": { "ubrn": "uniffi-bindgen-react-native" } ``` means now we can do: ```sh yarn ubrn --help ``` As a little extra: ```sh alias ubrn=$(yarn ubrn --path) ``` will make the command invokable from the shell. --- bin/cli | 27 +++++++++------------------ bin/cli.cjs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 4 ++-- 3 files changed, 62 insertions(+), 20 deletions(-) create mode 100644 bin/cli.cjs diff --git a/bin/cli b/bin/cli index b9059c1f..b2f5f390 100755 --- a/bin/cli +++ b/bin/cli @@ -1,19 +1,10 @@ -#!/usr/bin/env bash -# -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/ -# -set -e +#!/usr/bin/env node +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/ + */ -# Use Node to resolve the path to the module. The require.resolve -# function returns a thing like: -# -# PATH_TO/node_modules/uniffi-bindgen-react-native/typescript/src/index.ts -# -# So we drop the /typescript/src/index.ts. -resolved_path=$(node --print "require.resolve('uniffi-bindgen-react-native')") -root_dir=${resolved_path/\/typescript\/src\/index.ts*/} -manifest_path="${root_dir}/crates/ubrn_cli/Cargo.toml" - -cargo run --quiet --manifest-path "${manifest_path}" -- "$@" +// Launchable shell script. +// We just invoke the node cjs file. +import "./cli.cjs"; diff --git a/bin/cli.cjs b/bin/cli.cjs new file mode 100644 index 00000000..0119510c --- /dev/null +++ b/bin/cli.cjs @@ -0,0 +1,51 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/ + */ +const { execSync } = require("child_process"); +const path = require("path"); +const fs = require("fs"); + +function getRootDir() { + const currentDir = __dirname; + const repoMarker = "uniffi-bindgen-react-native"; + + // Check if the current directory is within the `uniffi-bindgen-react-native` repo + if (currentDir.includes(repoMarker)) { + let dir = currentDir; + while (dir !== path.parse(dir).root) { + if (fs.existsSync(path.join(dir, "Cargo.toml"))) { + return dir; + } + dir = path.dirname(dir); + } + } + + // Fallback to using require.resolve + const resolvedPath = require.resolve("uniffi-bindgen-react-native"); + return resolvedPath.replace(/\/typescript\/src\/index\.ts$/, ""); +} + +// Get the root directory +const rootDir = getRootDir(); + +const args = process.argv.slice(2); +if (args.length === 1 && args[0] === "--path") { + console.log(`${rootDir}/bin/cli`); + process.exit(0); +} + +// Construct the path to the Cargo.toml file +const manifestPath = path.join(rootDir, "crates/ubrn_cli/Cargo.toml"); + +// Run the cargo command +const command = `cargo run --quiet --manifest-path "${manifestPath}" -- ${args.join(" ")}`; +try { + execSync(command, { stdio: "inherit" }); +} catch (e) { + // cargo run errors are reported to stderr already. + // We do not want to show the JS error. + // All we have left is to exit with a non-zero exit code. + process.exit(1); +} diff --git a/package.json b/package.json index 1f98a8b2..13587e98 100644 --- a/package.json +++ b/package.json @@ -14,8 +14,8 @@ "type": "module", "main": "./typescript/src/index.ts", "bin": { - "ubrn": "./bin/cli", - "uniffi-bindgen-react-native": "./bin/cli" + "ubrn.cjs": "./bin/cli.cjs", + "uniffi-bindgen-react-native.cjs": "./bin/cli.cjs" }, "devDependencies": { "abortcontroller-polyfill": "^1.7.5",