Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
cd3675280a | |||
7c14102324 | |||
cb41dbaf1c | |||
149eb800e7 | |||
91e84c8422 | |||
ff26cd0678 | |||
acdd729e06 | |||
522fbfc42c |
24
changelog.md
24
changelog.md
@ -1,5 +1,29 @@
|
||||
# Changelog
|
||||
|
||||
## 2025-02-20 - 3.2.3 - fix(core)
|
||||
Refactor Smartshell class for improved code clarity and performance
|
||||
|
||||
- Refactored `_exec` method to improve code clarity.
|
||||
- Introduced `IExecOptions` interface for better type handling.
|
||||
- Replaced promise defer with native promises in command execution methods.
|
||||
- Improved logging and error handling in child process execution.
|
||||
- Ensured robust process management with signals handling.
|
||||
|
||||
## 2024-12-13 - 3.2.2 - fix(core)
|
||||
Fix minor code style and formatting issues
|
||||
|
||||
|
||||
## 2024-12-13 - 3.2.1 - fix(dependencies)
|
||||
Update @types/node dependency version
|
||||
|
||||
- Updated @types/node dependency from version ^22.10.1 to ^22.10.2.
|
||||
|
||||
## 2024-12-09 - 3.2.0 - feat(SmartExecution)
|
||||
Add support for scheduling restarts to SmartExecution
|
||||
|
||||
- Introduced the ability to handle consecutive restarts efficiently in SmartExecution.
|
||||
- Ensures that multiple restart requests merge into a single additional restart request if one is already in progress.
|
||||
|
||||
## 2024-12-09 - 3.1.0 - feat(core)
|
||||
Refactor codebase and update dependencies.
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@push.rocks/smartshell",
|
||||
"private": false,
|
||||
"version": "3.1.0",
|
||||
"version": "3.2.3",
|
||||
"description": "A library for executing shell commands using promises.",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
@ -37,7 +37,7 @@
|
||||
"@git.zone/tsrun": "^1.3.3",
|
||||
"@git.zone/tstest": "^1.0.90",
|
||||
"@push.rocks/tapbundle": "^5.5.3",
|
||||
"@types/node": "^22.10.1"
|
||||
"@types/node": "^22.10.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"@push.rocks/smartdelay": "^3.0.1",
|
||||
|
70
pnpm-lock.yaml
generated
70
pnpm-lock.yaml
generated
@ -40,8 +40,8 @@ importers:
|
||||
specifier: ^5.5.3
|
||||
version: 5.5.3(@aws-sdk/client-sso-oidc@3.699.0(@aws-sdk/client-sts@3.699.0))(@aws-sdk/credential-providers@3.699.0(@aws-sdk/client-sso-oidc@3.699.0(@aws-sdk/client-sts@3.699.0)))(socks@2.8.3)
|
||||
'@types/node':
|
||||
specifier: ^22.10.1
|
||||
version: 22.10.1
|
||||
specifier: ^22.10.2
|
||||
version: 22.10.2
|
||||
|
||||
packages:
|
||||
|
||||
@ -1421,8 +1421,8 @@ packages:
|
||||
'@types/node-forge@1.3.11':
|
||||
resolution: {integrity: sha512-FQx220y22OKNTqaByeBGqHWYz4cl94tpcxeFdvBo3wjG6XPBuZ0BNgNZRV5J5TFmmcsJ4IzsLkmGRiQbnYsBEQ==}
|
||||
|
||||
'@types/node@22.10.1':
|
||||
resolution: {integrity: sha512-qKgsUwfHZV2WCWLAnVP1JqnpE6Im6h3Y0+fYgMTasNQ7V++CBX5OT1as0g0f+OyubbFqhf6XVNIsmN4IIhEgGQ==}
|
||||
'@types/node@22.10.2':
|
||||
resolution: {integrity: sha512-Xxr6BBRCAOQixvonOye19wnzyDiUtTeqldOOmj3CkeblonbccA12PFwlufvRdrpjXxqnmUaeiU5EOA+7s5diUQ==}
|
||||
|
||||
'@types/parse5@6.0.3':
|
||||
resolution: {integrity: sha512-SuT16Q1K51EAVPz1K29DJ/sXjhSQ0zjvsypYJ6tlwVsRV9jwW5Adq2ch8Dq8kDBCkYnELS7N7VNCSB5nC56t/g==}
|
||||
@ -5173,7 +5173,7 @@ snapshots:
|
||||
'@jest/schemas': 29.6.3
|
||||
'@types/istanbul-lib-coverage': 2.0.6
|
||||
'@types/istanbul-reports': 3.0.4
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
'@types/yargs': 17.0.33
|
||||
chalk: 4.1.2
|
||||
|
||||
@ -6515,14 +6515,14 @@ snapshots:
|
||||
|
||||
'@types/accepts@1.3.7':
|
||||
dependencies:
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
|
||||
'@types/babel__code-frame@7.0.6': {}
|
||||
|
||||
'@types/body-parser@1.19.5':
|
||||
dependencies:
|
||||
'@types/connect': 3.4.38
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
|
||||
'@types/buffer-json@2.0.3': {}
|
||||
|
||||
@ -6538,17 +6538,17 @@ snapshots:
|
||||
|
||||
'@types/clean-css@4.2.11':
|
||||
dependencies:
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
source-map: 0.6.1
|
||||
|
||||
'@types/co-body@6.1.3':
|
||||
dependencies:
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
'@types/qs': 6.9.17
|
||||
|
||||
'@types/connect@3.4.38':
|
||||
dependencies:
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
|
||||
'@types/content-disposition@0.5.8': {}
|
||||
|
||||
@ -6561,11 +6561,11 @@ snapshots:
|
||||
'@types/connect': 3.4.38
|
||||
'@types/express': 5.0.0
|
||||
'@types/keygrip': 1.0.6
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
|
||||
'@types/cors@2.8.17':
|
||||
dependencies:
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
|
||||
'@types/debounce@1.2.4': {}
|
||||
|
||||
@ -6579,14 +6579,14 @@ snapshots:
|
||||
|
||||
'@types/express-serve-static-core@4.19.5':
|
||||
dependencies:
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
'@types/qs': 6.9.16
|
||||
'@types/range-parser': 1.2.7
|
||||
'@types/send': 0.17.4
|
||||
|
||||
'@types/express-serve-static-core@5.0.2':
|
||||
dependencies:
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
'@types/qs': 6.9.17
|
||||
'@types/range-parser': 1.2.7
|
||||
'@types/send': 0.17.4
|
||||
@ -6611,30 +6611,30 @@ snapshots:
|
||||
|
||||
'@types/from2@2.3.5':
|
||||
dependencies:
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
|
||||
'@types/fs-extra@11.0.4':
|
||||
dependencies:
|
||||
'@types/jsonfile': 6.1.4
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
|
||||
'@types/fs-extra@9.0.13':
|
||||
dependencies:
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
|
||||
'@types/glob@7.2.0':
|
||||
dependencies:
|
||||
'@types/minimatch': 5.1.2
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
|
||||
'@types/glob@8.1.0':
|
||||
dependencies:
|
||||
'@types/minimatch': 5.1.2
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
|
||||
'@types/gunzip-maybe@1.4.2':
|
||||
dependencies:
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
|
||||
'@types/hast@3.0.4':
|
||||
dependencies:
|
||||
@ -6668,7 +6668,7 @@ snapshots:
|
||||
|
||||
'@types/jsonfile@6.1.4':
|
||||
dependencies:
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
|
||||
'@types/keygrip@1.0.6': {}
|
||||
|
||||
@ -6685,7 +6685,7 @@ snapshots:
|
||||
'@types/http-errors': 2.0.4
|
||||
'@types/keygrip': 1.0.6
|
||||
'@types/koa-compose': 3.2.8
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
|
||||
'@types/mdast@4.0.4':
|
||||
dependencies:
|
||||
@ -6703,9 +6703,9 @@ snapshots:
|
||||
|
||||
'@types/node-forge@1.3.11':
|
||||
dependencies:
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
|
||||
'@types/node@22.10.1':
|
||||
'@types/node@22.10.2':
|
||||
dependencies:
|
||||
undici-types: 6.20.0
|
||||
|
||||
@ -6725,19 +6725,19 @@ snapshots:
|
||||
|
||||
'@types/s3rver@3.7.4':
|
||||
dependencies:
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
|
||||
'@types/semver@7.5.8': {}
|
||||
|
||||
'@types/send@0.17.4':
|
||||
dependencies:
|
||||
'@types/mime': 1.3.5
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
|
||||
'@types/serve-static@1.15.7':
|
||||
dependencies:
|
||||
'@types/http-errors': 2.0.4
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
'@types/send': 0.17.4
|
||||
|
||||
'@types/sinon-chai@3.2.12':
|
||||
@ -6757,11 +6757,11 @@ snapshots:
|
||||
|
||||
'@types/tar-stream@2.2.3':
|
||||
dependencies:
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
|
||||
'@types/through2@2.0.41':
|
||||
dependencies:
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
|
||||
'@types/triple-beam@1.3.5': {}
|
||||
|
||||
@ -6785,7 +6785,7 @@ snapshots:
|
||||
|
||||
'@types/whatwg-url@8.2.2':
|
||||
dependencies:
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
'@types/webidl-conversions': 7.0.3
|
||||
|
||||
'@types/which@2.0.2': {}
|
||||
@ -6794,11 +6794,11 @@ snapshots:
|
||||
|
||||
'@types/ws@7.4.7':
|
||||
dependencies:
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
|
||||
'@types/ws@8.5.12':
|
||||
dependencies:
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
|
||||
'@types/yargs-parser@21.0.3': {}
|
||||
|
||||
@ -6808,7 +6808,7 @@ snapshots:
|
||||
|
||||
'@types/yauzl@2.10.3':
|
||||
dependencies:
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
optional: true
|
||||
|
||||
'@ungap/structured-clone@1.2.0': {}
|
||||
@ -7393,7 +7393,7 @@ snapshots:
|
||||
dependencies:
|
||||
'@types/cookie': 0.4.1
|
||||
'@types/cors': 2.8.17
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
accepts: 1.3.8
|
||||
base64id: 2.0.0
|
||||
cookie: 0.4.2
|
||||
@ -8106,7 +8106,7 @@ snapshots:
|
||||
jest-util@29.7.0:
|
||||
dependencies:
|
||||
'@jest/types': 29.6.3
|
||||
'@types/node': 22.10.1
|
||||
'@types/node': 22.10.2
|
||||
chalk: 4.1.2
|
||||
ci-info: 3.9.0
|
||||
graceful-fs: 4.2.11
|
||||
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartshell',
|
||||
version: '3.1.0',
|
||||
version: '3.2.3',
|
||||
description: 'A library for executing shell commands using promises.'
|
||||
}
|
||||
|
@ -1,24 +1,55 @@
|
||||
import * as plugins from './plugins.js'
|
||||
import * as plugins from './plugins.js';
|
||||
import { Smartshell, type IExecResultStreaming } from './classes.smartshell.js';
|
||||
|
||||
export interface IDeferred<T> {
|
||||
resolve: (value?: T | PromiseLike<T>) => void;
|
||||
reject: (reason?: any) => void;
|
||||
promise: Promise<T>;
|
||||
}
|
||||
|
||||
export class SmartExecution {
|
||||
public smartshell: Smartshell;
|
||||
public currentStreamingExecution: IExecResultStreaming;
|
||||
public commandString: string;
|
||||
|
||||
private isRestartInProgress = false;
|
||||
private isAnotherRestartRequested = false;
|
||||
|
||||
constructor(commandStringArg: string) {
|
||||
this.commandString = commandStringArg;
|
||||
}
|
||||
|
||||
public async restart() {
|
||||
if (!this.smartshell) {
|
||||
this.smartshell = new Smartshell({
|
||||
executor: 'bash',
|
||||
});
|
||||
/**
|
||||
* Schedules a restart. If a restart is currently in progress, any additional calls
|
||||
* to restart will merge into a single additional restart request, which will only execute
|
||||
* once the current restart completes.
|
||||
*/
|
||||
public async restart(): Promise<void> {
|
||||
if (this.isRestartInProgress) {
|
||||
// If there's already a restart in progress, just mark that another restart was requested
|
||||
this.isAnotherRestartRequested = true;
|
||||
return;
|
||||
}
|
||||
if (this.currentStreamingExecution) {
|
||||
await this.currentStreamingExecution.kill();
|
||||
|
||||
this.isRestartInProgress = true;
|
||||
try {
|
||||
if (!this.smartshell) {
|
||||
this.smartshell = new Smartshell({
|
||||
executor: 'bash',
|
||||
});
|
||||
}
|
||||
if (this.currentStreamingExecution) {
|
||||
await this.currentStreamingExecution.kill();
|
||||
}
|
||||
this.currentStreamingExecution = await this.smartshell.execStreaming(this.commandString);
|
||||
} finally {
|
||||
this.isRestartInProgress = false;
|
||||
}
|
||||
|
||||
// If another restart was requested while we were busy, we handle it now
|
||||
if (this.isAnotherRestartRequested) {
|
||||
this.isAnotherRestartRequested = false;
|
||||
await this.restart();
|
||||
}
|
||||
this.currentStreamingExecution = await this.smartshell.execStreaming(this.commandString);
|
||||
}
|
||||
}
|
@ -2,7 +2,6 @@ import * as plugins from './plugins.js';
|
||||
import { ShellEnv } from './classes.shellenv.js';
|
||||
import type { IShellEnvContructorOptions, TExecutor } from './classes.shellenv.js';
|
||||
import { ShellLog } from './classes.shelllog.js';
|
||||
|
||||
import * as cp from 'child_process';
|
||||
|
||||
// -- interfaces --
|
||||
@ -17,7 +16,15 @@ export interface IExecResultStreaming {
|
||||
kill: () => Promise<void>;
|
||||
terminate: () => Promise<void>;
|
||||
keyboardInterrupt: () => Promise<void>;
|
||||
customSignal: (signalArg: plugins.smartexit.TProcessSignal) => Promise<void>;
|
||||
customSignal: (signal: plugins.smartexit.TProcessSignal) => Promise<void>;
|
||||
}
|
||||
|
||||
interface IExecOptions {
|
||||
commandString: string;
|
||||
silent?: boolean;
|
||||
strict?: boolean;
|
||||
streaming?: boolean;
|
||||
interactive?: boolean;
|
||||
}
|
||||
|
||||
export class Smartshell {
|
||||
@ -29,61 +36,48 @@ export class Smartshell {
|
||||
}
|
||||
|
||||
/**
|
||||
* executes a given command async
|
||||
* Executes a given command asynchronously.
|
||||
*/
|
||||
private async _exec(options: {
|
||||
commandString: string;
|
||||
silent?: boolean;
|
||||
strict?: boolean;
|
||||
streaming?: boolean;
|
||||
interactive?: boolean;
|
||||
}): Promise<IExecResult | IExecResultStreaming | void> {
|
||||
private async _exec(options: IExecOptions): Promise<IExecResult | IExecResultStreaming | void> {
|
||||
if (options.interactive) {
|
||||
return await this._execInteractive(options);
|
||||
return await this._execInteractive({ commandString: options.commandString });
|
||||
}
|
||||
|
||||
return await this._execCommand(options);
|
||||
}
|
||||
|
||||
private async _execInteractive(options: {
|
||||
commandString: string;
|
||||
interactive?: boolean;
|
||||
}): Promise<void> {
|
||||
/**
|
||||
* Executes an interactive command.
|
||||
*/
|
||||
private async _execInteractive(options: Pick<IExecOptions, 'commandString'>): Promise<void> {
|
||||
// Skip interactive execution in CI environments.
|
||||
if (process.env.CI) {
|
||||
return;
|
||||
}
|
||||
|
||||
const done = plugins.smartpromise.defer();
|
||||
return new Promise<void>((resolve) => {
|
||||
const shell = cp.spawn(options.commandString, {
|
||||
stdio: 'inherit',
|
||||
shell: true,
|
||||
detached: true,
|
||||
});
|
||||
|
||||
const shell = cp.spawn(options.commandString, {
|
||||
stdio: 'inherit',
|
||||
shell: true,
|
||||
detached: true
|
||||
this.smartexit.addProcess(shell);
|
||||
|
||||
shell.on('close', (code) => {
|
||||
console.log(`Interactive shell terminated with code ${code}`);
|
||||
this.smartexit.removeProcess(shell);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
this.smartexit.addProcess(shell);
|
||||
|
||||
shell.on('close', (code) => {
|
||||
console.log(`interactive shell terminated with code ${code}`);
|
||||
this.smartexit.removeProcess(shell);
|
||||
done.resolve();
|
||||
});
|
||||
|
||||
await done.promise;
|
||||
}
|
||||
|
||||
private async _execCommand(options: {
|
||||
commandString: string;
|
||||
silent?: boolean;
|
||||
strict?: boolean;
|
||||
streaming?: boolean;
|
||||
}): Promise<IExecResult | IExecResultStreaming> {
|
||||
const done = plugins.smartpromise.defer<IExecResult | IExecResultStreaming>();
|
||||
const childProcessEnded = plugins.smartpromise.defer<IExecResult>();
|
||||
|
||||
/**
|
||||
* Executes a command and returns either a non-streaming result or a streaming interface.
|
||||
*/
|
||||
private async _execCommand(options: IExecOptions): Promise<IExecResult | IExecResultStreaming> {
|
||||
const commandToExecute = this.shellEnv.createEnvExecString(options.commandString);
|
||||
|
||||
const shellLogInstance = new ShellLog();
|
||||
|
||||
const execChildProcess = cp.spawn(commandToExecute, [], {
|
||||
shell: true,
|
||||
cwd: process.cwd(),
|
||||
@ -93,6 +87,7 @@ export class Smartshell {
|
||||
|
||||
this.smartexit.addProcess(execChildProcess);
|
||||
|
||||
// Capture stdout and stderr output.
|
||||
execChildProcess.stdout.on('data', (data) => {
|
||||
if (!options.silent) {
|
||||
shellLogInstance.writeToConsole(data);
|
||||
@ -107,47 +102,55 @@ export class Smartshell {
|
||||
shellLogInstance.addToBuffer(data);
|
||||
});
|
||||
|
||||
execChildProcess.on('exit', (code, signal) => {
|
||||
this.smartexit.removeProcess(execChildProcess);
|
||||
if (options.strict && code === 1) {
|
||||
done.reject();
|
||||
}
|
||||
// Wrap child process termination into a Promise.
|
||||
const childProcessEnded: Promise<IExecResult> = new Promise((resolve, reject) => {
|
||||
execChildProcess.on('exit', (code, signal) => {
|
||||
this.smartexit.removeProcess(execChildProcess);
|
||||
|
||||
const execResult = {
|
||||
exitCode: code,
|
||||
stdout: shellLogInstance.logStore.toString(),
|
||||
};
|
||||
const execResult: IExecResult = {
|
||||
exitCode: typeof code === 'number' ? code : (signal ? 1 : 0),
|
||||
stdout: shellLogInstance.logStore.toString(),
|
||||
};
|
||||
|
||||
if (!options.streaming) {
|
||||
done.resolve(execResult);
|
||||
}
|
||||
childProcessEnded.resolve(execResult);
|
||||
if (options.strict && code !== 0) {
|
||||
reject(new Error(`Command "${options.commandString}" exited with code ${code}`));
|
||||
} else {
|
||||
resolve(execResult);
|
||||
}
|
||||
});
|
||||
|
||||
execChildProcess.on('error', (error) => {
|
||||
this.smartexit.removeProcess(execChildProcess);
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
|
||||
// If streaming mode is enabled, return a streaming interface immediately.
|
||||
if (options.streaming) {
|
||||
done.resolve({
|
||||
return {
|
||||
childProcess: execChildProcess,
|
||||
finalPromise: childProcessEnded.promise,
|
||||
finalPromise: childProcessEnded,
|
||||
kill: async () => {
|
||||
console.log(`running tree kill with SIGKILL on process ${execChildProcess.pid}`);
|
||||
console.log(`Running tree kill with SIGKILL on process ${execChildProcess.pid}`);
|
||||
await plugins.smartexit.SmartExit.killTreeByPid(execChildProcess.pid, 'SIGKILL');
|
||||
},
|
||||
terminate: async () => {
|
||||
console.log(`running tree kill with SIGTERM on process ${execChildProcess.pid}`);
|
||||
console.log(`Running tree kill with SIGTERM on process ${execChildProcess.pid}`);
|
||||
await plugins.smartexit.SmartExit.killTreeByPid(execChildProcess.pid, 'SIGTERM');
|
||||
},
|
||||
keyboardInterrupt: async () => {
|
||||
console.log(`running tree kill with SIGINT on process ${execChildProcess.pid}`);
|
||||
console.log(`Running tree kill with SIGINT on process ${execChildProcess.pid}`);
|
||||
await plugins.smartexit.SmartExit.killTreeByPid(execChildProcess.pid, 'SIGINT');
|
||||
},
|
||||
customSignal: async (signalArg: plugins.smartexit.TProcessSignal) => {
|
||||
console.log(`running tree kill with custom signal ${signalArg} on process ${execChildProcess.pid}`);
|
||||
await plugins.smartexit.SmartExit.killTreeByPid(execChildProcess.pid, signalArg);
|
||||
customSignal: async (signal: plugins.smartexit.TProcessSignal) => {
|
||||
console.log(`Running tree kill with custom signal ${signal} on process ${execChildProcess.pid}`);
|
||||
await plugins.smartexit.SmartExit.killTreeByPid(execChildProcess.pid, signal);
|
||||
},
|
||||
});
|
||||
} as IExecResultStreaming;
|
||||
}
|
||||
|
||||
return await done.promise;
|
||||
// For non-streaming mode, wait for the process to complete.
|
||||
return await childProcessEnded;
|
||||
}
|
||||
|
||||
public async exec(commandString: string): Promise<IExecResult> {
|
||||
@ -166,41 +169,35 @@ export class Smartshell {
|
||||
return (await this._exec({ commandString, silent: true, strict: true })) as IExecResult;
|
||||
}
|
||||
|
||||
public async execStreaming(
|
||||
commandString: string,
|
||||
silent: boolean = false
|
||||
): Promise<IExecResultStreaming> {
|
||||
public async execStreaming(commandString: string, silent: boolean = false): Promise<IExecResultStreaming> {
|
||||
return (await this._exec({ commandString, silent, streaming: true })) as IExecResultStreaming;
|
||||
}
|
||||
|
||||
public async execStreamingSilent(commandString: string): Promise<IExecResultStreaming> {
|
||||
return (await this._exec({
|
||||
commandString,
|
||||
silent: true,
|
||||
streaming: true,
|
||||
})) as IExecResultStreaming;
|
||||
return (await this._exec({ commandString, silent: true, streaming: true })) as IExecResultStreaming;
|
||||
}
|
||||
|
||||
public async execInteractive(commandString: string) {
|
||||
public async execInteractive(commandString: string): Promise<void> {
|
||||
await this._exec({ commandString, interactive: true });
|
||||
}
|
||||
|
||||
public async execAndWaitForLine(
|
||||
commandString: string,
|
||||
regexArg: RegExp,
|
||||
silentArg: boolean = false
|
||||
) {
|
||||
let done = plugins.smartpromise.defer();
|
||||
let execStreamingResult = await this.execStreaming(commandString, silentArg);
|
||||
execStreamingResult.childProcess.stdout.on('data', (stdOutChunk: string) => {
|
||||
if (regexArg.test(stdOutChunk)) {
|
||||
done.resolve();
|
||||
}
|
||||
regex: RegExp,
|
||||
silent: boolean = false
|
||||
): Promise<void> {
|
||||
const execStreamingResult = await this.execStreaming(commandString, silent);
|
||||
return new Promise<void>((resolve) => {
|
||||
execStreamingResult.childProcess.stdout.on('data', (chunk: Buffer | string) => {
|
||||
const data = typeof chunk === 'string' ? chunk : chunk.toString();
|
||||
if (regex.test(data)) {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
return done.promise;
|
||||
}
|
||||
|
||||
public async execAndWaitForLineSilent(commandString: string, regexArg: RegExp) {
|
||||
return this.execAndWaitForLine(commandString, regexArg, true);
|
||||
public async execAndWaitForLineSilent(commandString: string, regex: RegExp): Promise<void> {
|
||||
return this.execAndWaitForLine(commandString, regex, true);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user