@alphahydrae/exec 0.2.2 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -11,7 +11,7 @@ console.log(`
11
11
  This will never print because the executing Node.js
12
12
  program is replaced by the executed command, keeping
13
13
  the same process ID and file descriptors.
14
- `)
14
+ `);
15
15
  ```
16
16
 
17
17
  If you're familiar with Bash's `exec` function, this is the same for Node.js.
@@ -20,6 +20,15 @@ This package was developed to be used in Node.js scripts that are frontends to
20
20
  execute other commands. For example, a script that would build and execute a
21
21
  complex SSH or Ansible command.
22
22
 
23
+ <!-- START doctoc generated TOC please keep comment here to allow auto update -->
24
+ <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
25
+
26
+ - [Installation](#installation)
27
+ - [Support matrix](#support-matrix)
28
+ - [Credits](#credits)
29
+
30
+ <!-- END doctoc generated TOC please keep comment here to allow auto update -->
31
+
23
32
  ## Installation
24
33
 
25
34
  ```bash
@@ -40,3 +49,17 @@ npm i @alphahydrae/exec
40
49
  > The `exec` family of functions is part of the
41
50
  > [POSIX](https://en.wikipedia.org/wiki/POSIX) operating system API, so it will
42
51
  > not work on Windows.
52
+
53
+ ## Credits
54
+
55
+ This package is a re-implementation of
56
+ https://github.com/jprichardson/node-kexec in Rust, also inspired by the
57
+ following conversations:
58
+
59
+ - [A way to call execl, execle, execlp, execv, execvP or execvp from Node.js](https://stackoverflow.com/a/77774287/249893)
60
+ - [execve in node](https://groups.google.com/g/nodejs/c/4vtWG1KCQC4)
61
+
62
+ Also a big thank you to the following Rust projects for making it easy:
63
+
64
+ - [NAPI-RS](https://napi.rs) (build pre-compiled Node.js addons in Rust)
65
+ - [nix](https://docs.rs/nix) (Rust-friendly bindings to the various *nix system functions)
package/exec.d.ts CHANGED
@@ -1 +1,18 @@
1
- export declare function execvp(file: string, args: Array<string>): void;
1
+ /**
2
+ * Performs the execvp system call with the given file and arguments, replacing
3
+ * the current process image with the new one.
4
+ *
5
+ * execvp('ls', ['-l', '.']);
6
+ *
7
+ * This function does not return if successful, as the current process is
8
+ * replaced by the new one. An error may be thrown, with an error message
9
+ * containing the error code returned by execvp.
10
+ *
11
+ * @param {string} file The file to execute. If not a path, the PATH environment
12
+ * variable is searched.
13
+ * @param {string[]} args The arguments to pass to the new process.
14
+ * `process.argv0` is automatically prepended to the
15
+ * arguments.
16
+ * @throws {Error} If the execvp system call fails.
17
+ */
18
+ export declare function execvp(file: string, args: string[]): void;
package/exec.js CHANGED
@@ -1,8 +1,26 @@
1
1
  const native = require('./index.js');
2
2
 
3
+ /**
4
+ * Performs the execvp system call with the given file and arguments, replacing
5
+ * the current process image with the new one.
6
+ *
7
+ * This function does not return if successful, as the current process is
8
+ * replaced by the new one. An error may be thrown, with an error message
9
+ * containing the error code returned by execvp.
10
+ *
11
+ * @example
12
+ * import { execvp } from '@alphahydrae/exec';
13
+ * execvp('ls', ['-l', '.']);
14
+ *
15
+ * @param {string} file The file to execute. If not a path, the PATH environment
16
+ * variable is searched.
17
+ * @param {string[]} args The arguments to pass to the new process. The `file`
18
+ * argument is automatically prepended to the arguments.
19
+ * @throws {Error} If the execvp system call fails.
20
+ */
3
21
  module.exports.execvp = function (file, args) {
4
22
  native.doNotCloseOnExit(process.stdin.fd);
5
23
  native.doNotCloseOnExit(process.stdout.fd);
6
24
  native.doNotCloseOnExit(process.stderr.fd);
7
- native.execvp(file, [process.argv0, ...args]);
25
+ native.execvp(file, [file, ...args]);
8
26
  };
package/index.d.ts CHANGED
@@ -3,5 +3,41 @@
3
3
 
4
4
  /* auto-generated by NAPI-RS */
5
5
 
6
+ /**
7
+ * Performs the execvp system call with the given file and arguments, replacing
8
+ * the current process image with the new one.
9
+ *
10
+ * This function does not return if successful, as the current process is
11
+ * replaced by the new one. An error may be thrown, with an error message
12
+ * containing the error code returned by execvp.
13
+ *
14
+ * Note that the close-on-exec flag should be cleared for the process's file
15
+ * descriptors. Otherwise, they will be closed automatically when the new
16
+ * process is executed, which will likely make it fail. Use the
17
+ * `doNotCloseOnExit` function to clear the flag for a file descriptor.
18
+ *
19
+ * @example
20
+ * import { execvp } from '@alphahydrae/exec';
21
+ * execvp('ls', ['ls', '-l', '.']);
22
+ *
23
+ * @param {string} file The file to execute. If not a path, the PATH environment
24
+ * variable is searched.
25
+ * @param {string[]} args The arguments to pass to the new process. Note that
26
+ * the first argument should be the name of the file
27
+ * being executed.
28
+ * @throws {Error} If the execvp system call fails.
29
+ */
6
30
  export declare function execvp(file: string, args: Array<string>): void
31
+ /**
32
+ * Clears the close-on-exec flag for the given file descriptor, preventing it
33
+ * from being closed automatically when a new process is executed with the exec
34
+ * family of functions.
35
+ *
36
+ * @example
37
+ * doNotCloseOnExit(process.stdout.fd);
38
+ *
39
+ * @param {number} file The file descriptor to close (e.g. `process.stdout.fd`).
40
+ * @returns {undefined}
41
+ * @throws {Error} If the fcntl system call fails.
42
+ */
7
43
  export declare function doNotCloseOnExit(fd: number): void
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alphahydrae/exec",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "The execvp function for Node.js",
5
5
  "main": "exec.js",
6
6
  "repository": {
@@ -39,6 +39,7 @@
39
39
  "build:debug": "napi build --platform",
40
40
  "check": "npm run lint && npm run format",
41
41
  "check:write": "npm run lint && npm run format:write",
42
+ "doctoc": "doctoc --github --notitle README.md && prettier --write README.md",
42
43
  "format": "npm run format:cargo && npm run format:prettier",
43
44
  "format:cargo": "cargo fmt --check",
44
45
  "format:cargo:write": "cargo fmt",
@@ -53,7 +54,7 @@
53
54
  "devDependencies": {
54
55
  "@napi-rs/cli": "^2.18.4",
55
56
  "@swc-node/register": "^1.10.6",
56
- "@swc/core": "^1.6.13",
57
+ "@swc/core": "^1.10.12",
57
58
  "chalk": "^5.3.0",
58
59
  "doctoc": "^2.2.1",
59
60
  "jest": "^29.7.0",
@@ -65,11 +66,11 @@
65
66
  "node": "^18 || ^20 || ^22"
66
67
  },
67
68
  "optionalDependencies": {
68
- "@alphahydrae/exec-darwin-x64": "0.2.2",
69
- "@alphahydrae/exec-linux-x64-gnu": "0.2.2",
70
- "@alphahydrae/exec-linux-x64-musl": "0.2.2",
71
- "@alphahydrae/exec-darwin-arm64": "0.2.2",
72
- "@alphahydrae/exec-linux-arm64-gnu": "0.2.2",
73
- "@alphahydrae/exec-linux-arm64-musl": "0.2.2"
69
+ "@alphahydrae/exec-darwin-x64": "0.2.4",
70
+ "@alphahydrae/exec-linux-x64-gnu": "0.2.4",
71
+ "@alphahydrae/exec-linux-x64-musl": "0.2.4",
72
+ "@alphahydrae/exec-darwin-arm64": "0.2.4",
73
+ "@alphahydrae/exec-linux-arm64-gnu": "0.2.4",
74
+ "@alphahydrae/exec-linux-arm64-musl": "0.2.4"
74
75
  }
75
76
  }