topkat-utils 1.2.51 → 1.2.53

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/backend.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./dist/backend')
package/backend.ts ADDED
@@ -0,0 +1,78 @@
1
+
2
+
3
+ import { C } from './src/logger-utils'
4
+ import { exec } from 'child_process'
5
+
6
+
7
+ /**
8
+ * @param {String} command cli command to execute
9
+ * @param {Obect} config
10
+ * * logOutputStream
11
+ * * stringOrRegexpToSearchForConsideringDone => when the output contain this string or matches this regexp, process is considered done. Else it will wait for exit
12
+ * * timeout => timeout before killing process in seconds
13
+ * * execOptions => see nodeJs exec command
14
+ * * keyCodeToSend => { a: 1000, b: 2000 } keyCode: afterNMs; Eg: key a will be triggered after N milliseconds
15
+ * * errorHandle (log: log the error as an error) (throw: throw the error)
16
+ * * streamConsoleOutput: output => { } callback for streaming output
17
+ */
18
+ export async function execWaitForOutput(
19
+ command: string,
20
+ config = {} as Partial<{
21
+ logOutputStream: boolean
22
+ stringOrRegexpToSearchForConsideringDone: string
23
+ nbSecondsBeforeKillingProcess: number
24
+ keyCodeToSend: { [keyCode: string]: number },
25
+ errorHandle: 'log' | 'error'
26
+ streamConsoleOutput: (outputStr: string) => any,
27
+ execOptions: {cwd?: string, [seeNodeJsDocOnExec: string]: any}
28
+ }>
29
+ ): Promise<string | undefined> {
30
+
31
+ let outputStream = ''
32
+ let execOptions
33
+
34
+ const { nbSecondsBeforeKillingProcess = 20, streamConsoleOutput = () => true, errorHandle = 'throw', logOutputStream = true, stringOrRegexpToSearchForConsideringDone, keyCodeToSend = {} } = config
35
+
36
+ try {
37
+ return await new Promise((res, reject) => {
38
+ const to = setTimeout(() => {
39
+ C.error(`Exec timeout for ${command}`)
40
+ reject(`Exec timeout for ${command}`)
41
+ }, nbSecondsBeforeKillingProcess * 1000)
42
+
43
+ const process2 = exec(command, execOptions)
44
+ const resolve = () => {
45
+ process2?.kill('SIGINT')
46
+ clearTimeout(to)
47
+ res(outputStream)
48
+ }
49
+ const stdCallback = data => {
50
+ if (logOutputStream) C.log(data)
51
+ streamConsoleOutput(data)
52
+ outputStream += data
53
+ if (stringOrRegexpToSearchForConsideringDone) {
54
+ const regexp = typeof stringOrRegexpToSearchForConsideringDone === 'string' ? new RegExp(stringOrRegexpToSearchForConsideringDone) : stringOrRegexpToSearchForConsideringDone
55
+ if (regexp.test(data)) resolve()
56
+ }
57
+ }
58
+ const exitCallback = exitCode => {
59
+ if (exitCode === 0) resolve()
60
+ else if (stringOrRegexpToSearchForConsideringDone || typeof exitCode === 'number' && exitCode !== 0) reject(exitCode)
61
+ else resolve()
62
+ }
63
+ process2.stderr?.on('data', stdCallback)
64
+ process2.stdout?.on('data', stdCallback)
65
+ process2.on('exit', exitCallback)
66
+ process2.on('close', exitCallback)
67
+ for (const keyCode in keyCodeToSend) {
68
+ setTimeout(() => {
69
+ process2.stdin?.write(keyCode)
70
+ if (logOutputStream) C.log('Sending charcode to stdin: ' + keyCode)
71
+ }, keyCodeToSend[keyCode])
72
+ }
73
+ })
74
+ } catch (error) {
75
+ if (errorHandle === 'log') C.error(`Something went wrong using this command: ${command}\nPlease check this log:\n${outputStream}`)
76
+ else throw `Something went wrong using this command: ${command}\nPlease check this log:\n${outputStream}`
77
+ }
78
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * @param {String} command cli command to execute
3
+ * @param {Obect} config
4
+ * * logOutputStream
5
+ * * stringOrRegexpToSearchForConsideringDone => when the output contain this string or matches this regexp, process is considered done. Else it will wait for exit
6
+ * * timeout => timeout before killing process in seconds
7
+ * * execOptions => see nodeJs exec command
8
+ * * keyCodeToSend => { a: 1000, b: 2000 } keyCode: afterNMs; Eg: key a will be triggered after N milliseconds
9
+ * * errorHandle (log: log the error as an error) (throw: throw the error)
10
+ * * streamConsoleOutput: output => { } callback for streaming output
11
+ */
12
+ export declare function execWaitForOutput(command: string, config?: Partial<{
13
+ logOutputStream: boolean;
14
+ stringOrRegexpToSearchForConsideringDone: string;
15
+ nbSecondsBeforeKillingProcess: number;
16
+ keyCodeToSend: {
17
+ [keyCode: string]: number;
18
+ };
19
+ errorHandle: 'log' | 'error';
20
+ streamConsoleOutput: (outputStr: string) => any;
21
+ execOptions: {
22
+ [seeNodeJsDocOnExec: string]: any;
23
+ cwd?: string | undefined;
24
+ };
25
+ }>): Promise<string | undefined>;
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.execWaitForOutput = void 0;
4
+ const logger_utils_1 = require("./src/logger-utils");
5
+ const child_process_1 = require("child_process");
6
+ /**
7
+ * @param {String} command cli command to execute
8
+ * @param {Obect} config
9
+ * * logOutputStream
10
+ * * stringOrRegexpToSearchForConsideringDone => when the output contain this string or matches this regexp, process is considered done. Else it will wait for exit
11
+ * * timeout => timeout before killing process in seconds
12
+ * * execOptions => see nodeJs exec command
13
+ * * keyCodeToSend => { a: 1000, b: 2000 } keyCode: afterNMs; Eg: key a will be triggered after N milliseconds
14
+ * * errorHandle (log: log the error as an error) (throw: throw the error)
15
+ * * streamConsoleOutput: output => { } callback for streaming output
16
+ */
17
+ async function execWaitForOutput(command, config = {}) {
18
+ let outputStream = '';
19
+ let execOptions;
20
+ const { nbSecondsBeforeKillingProcess = 20, streamConsoleOutput = () => true, errorHandle = 'throw', logOutputStream = true, stringOrRegexpToSearchForConsideringDone, keyCodeToSend = {} } = config;
21
+ try {
22
+ return await new Promise((res, reject) => {
23
+ var _a, _b;
24
+ const to = setTimeout(() => {
25
+ logger_utils_1.C.error(`Exec timeout for ${command}`);
26
+ reject(`Exec timeout for ${command}`);
27
+ }, nbSecondsBeforeKillingProcess * 1000);
28
+ const process2 = (0, child_process_1.exec)(command, execOptions);
29
+ const resolve = () => {
30
+ process2 === null || process2 === void 0 ? void 0 : process2.kill('SIGINT');
31
+ clearTimeout(to);
32
+ res(outputStream);
33
+ };
34
+ const stdCallback = data => {
35
+ if (logOutputStream)
36
+ logger_utils_1.C.log(data);
37
+ streamConsoleOutput(data);
38
+ outputStream += data;
39
+ if (stringOrRegexpToSearchForConsideringDone) {
40
+ const regexp = typeof stringOrRegexpToSearchForConsideringDone === 'string' ? new RegExp(stringOrRegexpToSearchForConsideringDone) : stringOrRegexpToSearchForConsideringDone;
41
+ if (regexp.test(data))
42
+ resolve();
43
+ }
44
+ };
45
+ const exitCallback = exitCode => {
46
+ if (exitCode === 0)
47
+ resolve();
48
+ else if (stringOrRegexpToSearchForConsideringDone || typeof exitCode === 'number' && exitCode !== 0)
49
+ reject(exitCode);
50
+ else
51
+ resolve();
52
+ };
53
+ (_a = process2.stderr) === null || _a === void 0 ? void 0 : _a.on('data', stdCallback);
54
+ (_b = process2.stdout) === null || _b === void 0 ? void 0 : _b.on('data', stdCallback);
55
+ process2.on('exit', exitCallback);
56
+ process2.on('close', exitCallback);
57
+ for (const keyCode in keyCodeToSend) {
58
+ setTimeout(() => {
59
+ var _a;
60
+ (_a = process2.stdin) === null || _a === void 0 ? void 0 : _a.write(keyCode);
61
+ if (logOutputStream)
62
+ logger_utils_1.C.log('Sending charcode to stdin: ' + keyCode);
63
+ }, keyCodeToSend[keyCode]);
64
+ }
65
+ });
66
+ }
67
+ catch (error) {
68
+ if (errorHandle === 'log')
69
+ logger_utils_1.C.error(`Something went wrong using this command: ${command}\nPlease check this log:\n${outputStream}`);
70
+ else
71
+ throw `Something went wrong using this command: ${command}\nPlease check this log:\n${outputStream}`;
72
+ }
73
+ }
74
+ exports.execWaitForOutput = execWaitForOutput;
75
+ //# sourceMappingURL=backend.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"backend.js","sourceRoot":"","sources":["../backend.ts"],"names":[],"mappings":";;;AAEA,qDAAsC;AACtC,iDAAoC;AAGpC;;;;;;;;;;GAUG;AACI,KAAK,UAAU,iBAAiB,CACnC,OAAe,EACf,SAAS,EAQP;IAGF,IAAI,YAAY,GAAG,EAAE,CAAA;IACrB,IAAI,WAA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": "dist",
package/tsconfig.json CHANGED
@@ -18,6 +18,7 @@
18
18
  },
19
19
  "include": [
20
20
  "./**/*.ts",
21
- "index.ts"
21
+ "index.ts",
22
+ "backend.ts"
22
23
  ],
23
24
  }