@j-o-r/sh 1.1.0 → 1.1.2
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 +9 -6
- package/lib/SH.js +1 -1
- package/lib/Test.js +22 -14
- package/package.json +4 -2
- package/types/SH.d.ts +2 -1
- package/types/SHDispatch.d.ts +1 -1
- package/types/SHExecute.d.ts +1 -1
- package/types/Test.d.ts +7 -9
package/README.md
CHANGED
|
@@ -158,14 +158,17 @@ test.add('Test an Array in sync', () => {
|
|
|
158
158
|
test.add('Test is test in async', async () => {
|
|
159
159
|
assert.strictEqual(jsType(test), 'Test');
|
|
160
160
|
});
|
|
161
|
-
test.add('Test is test in a promise',
|
|
162
|
-
new Promise((resolve, _reject) => {
|
|
161
|
+
test.add('Test is test in async, returning a promise', async () => {
|
|
162
|
+
return new Promise((resolve, _reject) => {
|
|
163
163
|
assert.strictEqual(jsType(test), 'Test');
|
|
164
164
|
resolve();
|
|
165
|
-
})
|
|
166
|
-
)
|
|
167
|
-
|
|
168
|
-
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
const report = await test.run();
|
|
168
|
+
if (report.errors > 0) {
|
|
169
|
+
process.exit(1);
|
|
170
|
+
}
|
|
171
|
+
// await test.run([0,3]); // only run test 0 and 3
|
|
169
172
|
```
|
|
170
173
|
|
|
171
174
|
## License
|
package/lib/SH.js
CHANGED
|
@@ -23,9 +23,9 @@
|
|
|
23
23
|
// Changes Made:
|
|
24
24
|
// - The code has been or is being reformatted to comply with ES2020 standards.
|
|
25
25
|
// - Some methods were added and existing ones were modified or deleted to enhance usability.
|
|
26
|
-
// - Most methods were deleted,
|
|
27
26
|
// - The namespace has been changed from '$' to 'SH'.
|
|
28
27
|
// Modified by: jorrit.duin+sh[AT]gmail.com
|
|
28
|
+
/** @type {assert} */
|
|
29
29
|
import assert from 'node:assert';
|
|
30
30
|
import SHDispatch from './SHDispatch.js';
|
|
31
31
|
import Test from './Test.js'
|
package/lib/Test.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import assert from 'node:assert/strict';
|
|
2
2
|
import { jsType } from './SH.js'
|
|
3
|
-
const FNC = ['Function', 'AsyncFunction'
|
|
3
|
+
const FNC = ['Function', 'AsyncFunction'];
|
|
4
4
|
// Settle async calls in a SYNC function
|
|
5
|
-
const SETTLE_ASYNC =
|
|
5
|
+
const SETTLE_ASYNC = 50;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {(function(): Promise<any>)} AsyncFunction
|
|
9
|
+
*/
|
|
6
10
|
|
|
7
11
|
/**
|
|
8
12
|
* @typedef {Object} testDefinition
|
|
9
13
|
* @prop {string} description
|
|
10
|
-
* @prop {
|
|
14
|
+
* @prop {Function|AsyncFunction} callback - syc/ async function
|
|
11
15
|
*/
|
|
12
16
|
|
|
13
17
|
/**
|
|
@@ -25,6 +29,7 @@ const SETTLE_ASYNC = 100;
|
|
|
25
29
|
*/
|
|
26
30
|
/**
|
|
27
31
|
* Get the current time
|
|
32
|
+
* used for calculating a duration
|
|
28
33
|
* @returns {number}
|
|
29
34
|
*/
|
|
30
35
|
function getNow() {
|
|
@@ -32,7 +37,7 @@ function getNow() {
|
|
|
32
37
|
}
|
|
33
38
|
|
|
34
39
|
/**
|
|
35
|
-
* Get the duration
|
|
40
|
+
* Get the duration based on a previous gathered start time
|
|
36
41
|
* @param {number} start - start time
|
|
37
42
|
* @returns {number}
|
|
38
43
|
*/
|
|
@@ -76,17 +81,17 @@ class Test {
|
|
|
76
81
|
*/
|
|
77
82
|
#detectErrors(active = true) {
|
|
78
83
|
const errListener = (err) => {
|
|
79
|
-
this
|
|
84
|
+
this.#handleError(err)
|
|
80
85
|
}
|
|
81
86
|
if (active) {
|
|
82
87
|
if (!this.#catchErrors) {
|
|
83
88
|
process.on('uncaughtException', errListener);
|
|
84
|
-
this
|
|
89
|
+
this.#catchErrors = true;
|
|
85
90
|
}
|
|
86
91
|
} else {
|
|
87
92
|
if (this.#catchErrors) {
|
|
88
93
|
process.removeListener('uncaughtException', errListener);
|
|
89
|
-
this
|
|
94
|
+
this.#catchErrors = false;
|
|
90
95
|
}
|
|
91
96
|
}
|
|
92
97
|
|
|
@@ -95,7 +100,7 @@ class Test {
|
|
|
95
100
|
/**
|
|
96
101
|
*
|
|
97
102
|
* @param {string} description
|
|
98
|
-
* @param {Function|AsyncFunction
|
|
103
|
+
* @param {Function|AsyncFunction} callback - sync / async function
|
|
99
104
|
* @throws Error when conditions are not met
|
|
100
105
|
*/
|
|
101
106
|
add(description, callback) {
|
|
@@ -103,7 +108,7 @@ class Test {
|
|
|
103
108
|
throw new Error(`'description' should be a string`)
|
|
104
109
|
}
|
|
105
110
|
if (!FNC.includes(jsType(callback))) {
|
|
106
|
-
throw new Error(`'callback' should be a (async)
|
|
111
|
+
throw new Error(`'callback' should be a (async) Function`)
|
|
107
112
|
}
|
|
108
113
|
this.#tests.push({ description, callback });
|
|
109
114
|
}
|
|
@@ -126,6 +131,7 @@ class Test {
|
|
|
126
131
|
const type = jsType(cb.callback);
|
|
127
132
|
let error;
|
|
128
133
|
try {
|
|
134
|
+
if (!this.#quite) process.stdout.write(`${i}. ${cb.description}\n`);
|
|
129
135
|
if (type === 'Function') {
|
|
130
136
|
cb.callback();
|
|
131
137
|
// To settle async calls in sync functions
|
|
@@ -133,21 +139,21 @@ class Test {
|
|
|
133
139
|
await new Promise(resolve => setTimeout(resolve, this.#TO)); // This will pause the the current loop.
|
|
134
140
|
// add timeout for an honest execution time
|
|
135
141
|
start = start + this.#TO;
|
|
136
|
-
} else if (type === 'Promise') {
|
|
137
|
-
await Promise.all([cb.callback]);
|
|
138
142
|
} else {
|
|
139
143
|
await cb.callback();
|
|
140
144
|
}
|
|
141
145
|
} catch (e) {
|
|
146
|
+
process.stdout.write(`\x1b[31mERROR: ${i}. ${cb.description}\x1b[0m\n`);
|
|
142
147
|
if (!errors) {
|
|
143
148
|
errors = true
|
|
144
149
|
}
|
|
145
150
|
error = e;
|
|
146
151
|
}
|
|
147
152
|
const duration = getDuration(start);
|
|
148
|
-
if (!this.#quite) process.stdout.write(
|
|
153
|
+
if (!this.#quite && !error) process.stdout.write(`Finished in: ${duration} ms\n`);
|
|
154
|
+
|
|
149
155
|
if (error) {
|
|
150
|
-
this
|
|
156
|
+
this.#handleError(error);
|
|
151
157
|
}
|
|
152
158
|
this.#reports[i] = { description: cb.description, duration };
|
|
153
159
|
}
|
|
@@ -196,10 +202,12 @@ class Test {
|
|
|
196
202
|
this.#errors = [];
|
|
197
203
|
}
|
|
198
204
|
/**
|
|
205
|
+
* @private
|
|
199
206
|
* Handle an error outside the callstack
|
|
200
207
|
* for the current test
|
|
208
|
+
* @param {Error}
|
|
201
209
|
*/
|
|
202
|
-
|
|
210
|
+
#handleError(err) {
|
|
203
211
|
if (this.#currentTest > -1) {
|
|
204
212
|
// Register this error
|
|
205
213
|
// Always print out errors
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@j-o-r/sh",
|
|
3
3
|
"author": "Jorrit Duin <j-o-r@duin.work>",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "1.1.
|
|
5
|
+
"version": "1.1.2",
|
|
6
6
|
"description": "Execute shell commands on Linux-based systems from javascript",
|
|
7
7
|
"main": "lib/SH.js",
|
|
8
8
|
"types": "types/SH.d.ts",
|
|
@@ -21,7 +21,9 @@
|
|
|
21
21
|
"url": "https://codeberg.org/duin/sh"
|
|
22
22
|
},
|
|
23
23
|
"license": "Apache License, Version 2.0",
|
|
24
|
-
"dependencies": {
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@types/node": "^22.10.10"
|
|
26
|
+
},
|
|
25
27
|
"bugs": {
|
|
26
28
|
"url": "https://codeberg.org/duin/sh/issues"
|
|
27
29
|
},
|
package/types/SH.d.ts
CHANGED
|
@@ -87,9 +87,10 @@ export function parseArgs(args: string[]): object;
|
|
|
87
87
|
* Determine a javascript type
|
|
88
88
|
*
|
|
89
89
|
* @param {any} fn - Any let type
|
|
90
|
-
* @returns {string} The object /
|
|
90
|
+
* @returns {string} The "real" object / typeof name
|
|
91
91
|
*/
|
|
92
92
|
export function jsType(fn: any): string;
|
|
93
93
|
import Test from './Test.js';
|
|
94
|
+
import assert from 'node:assert';
|
|
94
95
|
import SHDispatch from './SHDispatch.js';
|
|
95
96
|
export { Test, assert };
|
package/types/SHDispatch.d.ts
CHANGED
package/types/SHExecute.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ declare class SHExecute {
|
|
|
9
9
|
* @param {string} [payload] - data to write
|
|
10
10
|
* @retuns {Promise<object>}
|
|
11
11
|
*/
|
|
12
|
-
runSync(payload?: string | undefined):
|
|
12
|
+
runSync(payload?: string | undefined): import("child_process").SpawnSyncReturns<Buffer> & import("child_process").SpawnSyncReturns<string> & import("child_process").SpawnSyncReturns<string | Buffer>;
|
|
13
13
|
/**
|
|
14
14
|
* @param {string} [payload] - data to write
|
|
15
15
|
* @retuns {Promise<string>}
|
package/types/Test.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
export default Test;
|
|
2
|
+
export type AsyncFunction = (() => Promise<any>);
|
|
2
3
|
export type testDefinition = {
|
|
3
4
|
description: string;
|
|
4
|
-
|
|
5
|
+
/**
|
|
6
|
+
* - syc/ async function
|
|
7
|
+
*/
|
|
8
|
+
callback: Function | AsyncFunction;
|
|
5
9
|
};
|
|
6
10
|
export type testReport = {
|
|
7
11
|
description: string;
|
|
@@ -37,14 +41,13 @@ declare class Test {
|
|
|
37
41
|
* @param {number} timeout - in MS, default 100
|
|
38
42
|
*/
|
|
39
43
|
syncTimeout(timeout: number): void;
|
|
40
|
-
catchErrors: boolean | undefined;
|
|
41
44
|
/**
|
|
42
45
|
*
|
|
43
46
|
* @param {string} description
|
|
44
|
-
* @param {Function|AsyncFunction
|
|
47
|
+
* @param {Function|AsyncFunction} callback - sync / async function
|
|
45
48
|
* @throws Error when conditions are not met
|
|
46
49
|
*/
|
|
47
|
-
add(description: string, callback: Function | AsyncFunction
|
|
50
|
+
add(description: string, callback: Function | AsyncFunction): void;
|
|
48
51
|
/**
|
|
49
52
|
* Execute tests
|
|
50
53
|
* @param {number[]} [execute] - limit the execution tests
|
|
@@ -55,10 +58,5 @@ declare class Test {
|
|
|
55
58
|
* Empty tests
|
|
56
59
|
*/
|
|
57
60
|
reset(): void;
|
|
58
|
-
/**
|
|
59
|
-
* Handle an error outside the callstack
|
|
60
|
-
* for the current test
|
|
61
|
-
*/
|
|
62
|
-
_handleError(err: any): void;
|
|
63
61
|
#private;
|
|
64
62
|
}
|