@node-cli/run 0.0.5

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Arno Versini
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # Node CLI run command
2
+
3
+ ![npm](https://img.shields.io/npm/v/@node-cli/run?label=version&logo=npm)
4
+
5
+ > @node-cli/run is a dead-simple script runner for nodejs command-line applications.
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ > cd your-project
11
+ > npm install --save-dev @node-cli/run
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ ```js
17
+ import { run } from "@node-cli/run";
18
+ const { stdout, stderr } = await run("npm config ls");
19
+ ```
20
+
21
+ ## API
22
+
23
+ **run(command, options) ⇒ `Promise <string>` | `Promise <object>`**
24
+
25
+ Runs a shell command asynchronously and returns both `stdout` and `stderr`. If the command fails to run (invalid command or the commands status is anything but 0), the call will throw an exception. The exception can be ignored if the `options.ignoreError` flag is true.
26
+
27
+ ### Arguments
28
+
29
+ | Argument | Type | Default |
30
+ | ------------------- | ------- | ------- |
31
+ | command | String | "" |
32
+ | options | Object | { } |
33
+ | options.ignoreError | Boolean | false |
34
+
35
+ ### Note
36
+
37
+ If `ignoreError` is used, the method will not throw but will instead return an object with the keys `exitCode` and `shortMessage`.
38
+
39
+ #### Examples
40
+
41
+ ```js
42
+ import { run } from "@node-cli/run";
43
+ const { stdout, stderr } = await run("npm config ls");
44
+ const { stdout, stderr } = await run(
45
+ "git add -A && git commit -a -m 'First commit'"
46
+ );
47
+ ```
48
+
49
+ ```js
50
+ import { run } from "@node-cli/run";
51
+ const { exitCode, shortMessage } = await runCommand("ls /not-a-folder", {
52
+ ignoreError: true,
53
+ });
54
+ // -> exitCode is 1 and shortMessage is "Command failed with exit code 1: ls /not-a-folder"
55
+ ```
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Runs a shell command asynchronously and
3
+ * returns both `stdout` and `stderr`.
4
+ * If the command fails to run (invalid command or the commands status is
5
+ * anything but 0), the call will throw an exception. The exception can be
6
+ * ignored if the `options.ignoreError` flag is true.
7
+ *
8
+ * @async
9
+ */
10
+ export declare const run: (command: string, options?: {
11
+ ignoreError?: boolean;
12
+ }) => Promise<any>;
package/dist/index.js ADDED
@@ -0,0 +1,41 @@
1
+ import { execaCommand } from "execa";
2
+ import kleur from "kleur";
3
+ /**
4
+ * Runs a shell command asynchronously and
5
+ * returns both `stdout` and `stderr`.
6
+ * If the command fails to run (invalid command or the commands status is
7
+ * anything but 0), the call will throw an exception. The exception can be
8
+ * ignored if the `options.ignoreError` flag is true.
9
+ *
10
+ * @async
11
+ */ export const run = async (command, options)=>{
12
+ const { ignoreError } = {
13
+ ignoreError: false,
14
+ ...options
15
+ };
16
+ try {
17
+ const { stdout , stderr } = await execaCommand(command, {
18
+ /**
19
+ * For some reason, a command with a " or ' in execa.command() will
20
+ * fail, but it works if shell is set to true... It would work if
21
+ * the execaCommand() API is not used:
22
+ * execa("ls", ["-l", "|", "wc"]);
23
+ * Same problems with &, &&, | and ||.
24
+ */ shell: command.includes('"') || command.includes("'") || command.includes("&&") || command.includes("&") || command.includes("||") || command.includes("|")
25
+ });
26
+ return {
27
+ stderr,
28
+ stdout
29
+ };
30
+ } catch (error) {
31
+ if (ignoreError) {
32
+ return {
33
+ exitCode: error.exitCode === undefined ? 1 : error.exitCode,
34
+ shortMessage: error.shortMessage,
35
+ stderr: error.exitCode === undefined ? 1 : error.exitCode
36
+ };
37
+ } else {
38
+ throw new Error(kleur.red(error));
39
+ }
40
+ }
41
+ };
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@node-cli/run",
3
+ "version": "0.0.5",
4
+ "license": "MIT",
5
+ "author": "Arno Versini",
6
+ "description": "A wrapper for child_process for nodejs CLI apps",
7
+ "type": "module",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": "./dist/index.js",
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "node": ">=16",
14
+ "dependencies": {
15
+ "execa": "7.1.1",
16
+ "kleur": "4.1.5"
17
+ },
18
+ "scripts": {
19
+ "build": "npm-run-all --serial clean build:types build:js",
20
+ "build:js": "swc --out-dir dist src",
21
+ "build:types": "tsc",
22
+ "clean": "rimraf dist types coverage",
23
+ "lint": "eslint \"src/*.ts\"",
24
+ "test": "jest",
25
+ "test:coverage": "npm run test -- --coverage",
26
+ "watch": "swc --watch --out-dir dist src"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public"
30
+ },
31
+ "gitHead": "6e7950590077d5934bf84c676f533696139f8eb9"
32
+ }