Skip to content

Commit

Permalink
Add execPath and resolvedExecPath
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Mar 30, 2024
1 parent 974339c commit 57c3101
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 2 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ bunx jsr add @cross/utils
- Generates a neatly formatted table representation of a 2D array and prints
it to the console.

- **execPath()**
- Get the pathto the current runtime executable.

- **resolvedExecPath()**
- Get the path to the current runtime executable, as indicated by the PATH
variable. Could be a version independent link such as `...nvm/current...`
instead of `...nvm/<version>...`.

**Classes**

- **Colors**
Expand Down Expand Up @@ -135,3 +143,29 @@ functionality:
];
table(myData);
```

- **execPath(): Promise<string>**
- Returns the actual path to the current runtime executable.
- **Examples:**
```javascript
const runtimeExecPath = await execPath();
// Log the runtime for debugging:
console.log(`Process started using ${runtimeExecPath}`);
// Spawn a child process with the same runtime:
const childProcess = spawn(runtimeExecPath, ["other-script.js"]);
```

- **resolvedExecPath(): Promise<string>**
- Returns the resolved path (through PATH) to the current runtime executable.
- **Examples:**
```javascript
const runtimeExecPath = await resolvedExecPath();
// Log the runtime for debugging:
console.log(`Process started using ${runtimeExecPath}`);
// Spawn a child process with the same runtime:
const childProcess = spawn(runtimeExecPath, ["other-script.js"]);
```
6 changes: 4 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
{
"name": "@cross/utils",
"version": "0.8.2",
"version": "0.9.0",
"exports": {
".": "./mod.ts",
"./ansi": "./utils/ansi.ts",
"./args": "./utils/args.ts",
"./exit": "./utils/exit.ts",
"./spawn": "./utils/spawn.ts",
"./cwd": "./utils/cwd.ts",
"./table": "./utils/table.ts"
"./table": "./utils/table.ts",
"./execpath": "./utils/execpath.ts"
},
"imports": {
"@cross/fs": "jsr:@cross/fs@^0.0.7",
"@cross/runtime": "jsr:@cross/runtime@^1.0.0",
"@cross/test": "jsr:@cross/test@^0.0.9",
"@std/assert": "jsr:@std/assert@^0.220.1"
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { args, ArgsParser } from "./utils/args.ts";
export { Colors, Cursor, stripAnsi } from "./utils/ansi.ts";
export { spawn } from "./utils/spawn.ts";
export { cwd } from "./utils/cwd.ts";
export { execPath, resolvedExecPath } from "./utils/execpath.ts";
75 changes: 75 additions & 0 deletions utils/execpath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { CurrentRuntime, Runtime } from "@cross/runtime";
import { which } from "@cross/fs/stat";

/**
* Cross-runtime compatible way to return the current executable path in a manner, regardless of Node, Deno or Bun.
*
* @returns {string} The current working directory path.
* @throws
* @example
* // import { execPath } from "@cross/utils";
*
* const currentExecPath = execPath();
* console.log("The path to the current runtime executable is :", currentExecPath);
*/
export function execPath(): Promise<string> {
if (CurrentRuntime === Runtime.Deno) {
//@ts-ignore cross-runtime
return Deno.execPath();
} else if (
CurrentRuntime === Runtime.Node || CurrentRuntime === Runtime.Bun
) {
//@ts-ignore cross-runtime
return process.execPath();
} else {
throw new Error(
`Cannot determine execPath using current runtime ('${CurrentRuntime}').`,
);
}
}

/**
* Cross-runtime compatible way to return the current executable path,
* prioritizing the executable's location within the user's PATH environment variable.
* This could result in a more generic path (e.g., `/home/.../nvm/current/node`) than the
* runtime's actual path (`/home/.../nvm/<version>/node`).
*
* @returns {string} The resolved executable path.
* @throws {Error} If the runtime executable cannot be found within the PATH.
* @example
* // import { resolvedExecPath } from "@cross/utils";
*
* const currentExecPath = await resolvedExecPath();
* console.log("The path to the current runtime executable is:", currentExecPath);
*/
export async function resolvedExecPath(): Promise<string> {
if (CurrentRuntime === Runtime.Deno) {
const foundDeno = await which("deno");
if (foundDeno !== null) {
return foundDeno;
} else {
//@ts-ignore cross-runtime
return Deno.execPath();
}
} else if (CurrentRuntime === Runtime.Node) {
const foundNode = await which("node");
if (foundNode !== null) {
return foundNode;
} else {
//@ts-ignore cross-runtime
return process.execPath();
}
} else if (CurrentRuntime === Runtime.Bun) {
const foundBun = await which("bun");
if (foundBun !== null) {
return foundBun;
} else {
//@ts-ignore cross-runtime
return process.execPath();
}
} else {
throw new Error(
`Cannot determine execPath using current runtime ('${CurrentRuntime}').`,
);
}
}

0 comments on commit 57c3101

Please sign in to comment.