Skip to content

Commit

Permalink
Translates cli into javascript (#102)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jhugman authored Sep 30, 2024
1 parent 20a8699 commit 0c36911
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 20 deletions.
27 changes: 9 additions & 18 deletions bin/cli
Original file line number Diff line number Diff line change
@@ -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";
51 changes: 51 additions & 0 deletions bin/cli.cjs
Original file line number Diff line number Diff line change
@@ -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);
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 0c36911

Please sign in to comment.