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
|
# 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)
|
## 2024-12-09 - 3.1.0 - feat(core)
|
||||||
Refactor codebase and update dependencies.
|
Refactor codebase and update dependencies.
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@push.rocks/smartshell",
|
"name": "@push.rocks/smartshell",
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "3.1.0",
|
"version": "3.2.3",
|
||||||
"description": "A library for executing shell commands using promises.",
|
"description": "A library for executing shell commands using promises.",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist_ts/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
@ -37,7 +37,7 @@
|
|||||||
"@git.zone/tsrun": "^1.3.3",
|
"@git.zone/tsrun": "^1.3.3",
|
||||||
"@git.zone/tstest": "^1.0.90",
|
"@git.zone/tstest": "^1.0.90",
|
||||||
"@push.rocks/tapbundle": "^5.5.3",
|
"@push.rocks/tapbundle": "^5.5.3",
|
||||||
"@types/node": "^22.10.1"
|
"@types/node": "^22.10.2"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@push.rocks/smartdelay": "^3.0.1",
|
"@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
|
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)
|
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':
|
'@types/node':
|
||||||
specifier: ^22.10.1
|
specifier: ^22.10.2
|
||||||
version: 22.10.1
|
version: 22.10.2
|
||||||
|
|
||||||
packages:
|
packages:
|
||||||
|
|
||||||
@ -1421,8 +1421,8 @@ packages:
|
|||||||
'@types/node-forge@1.3.11':
|
'@types/node-forge@1.3.11':
|
||||||
resolution: {integrity: sha512-FQx220y22OKNTqaByeBGqHWYz4cl94tpcxeFdvBo3wjG6XPBuZ0BNgNZRV5J5TFmmcsJ4IzsLkmGRiQbnYsBEQ==}
|
resolution: {integrity: sha512-FQx220y22OKNTqaByeBGqHWYz4cl94tpcxeFdvBo3wjG6XPBuZ0BNgNZRV5J5TFmmcsJ4IzsLkmGRiQbnYsBEQ==}
|
||||||
|
|
||||||
'@types/node@22.10.1':
|
'@types/node@22.10.2':
|
||||||
resolution: {integrity: sha512-qKgsUwfHZV2WCWLAnVP1JqnpE6Im6h3Y0+fYgMTasNQ7V++CBX5OT1as0g0f+OyubbFqhf6XVNIsmN4IIhEgGQ==}
|
resolution: {integrity: sha512-Xxr6BBRCAOQixvonOye19wnzyDiUtTeqldOOmj3CkeblonbccA12PFwlufvRdrpjXxqnmUaeiU5EOA+7s5diUQ==}
|
||||||
|
|
||||||
'@types/parse5@6.0.3':
|
'@types/parse5@6.0.3':
|
||||||
resolution: {integrity: sha512-SuT16Q1K51EAVPz1K29DJ/sXjhSQ0zjvsypYJ6tlwVsRV9jwW5Adq2ch8Dq8kDBCkYnELS7N7VNCSB5nC56t/g==}
|
resolution: {integrity: sha512-SuT16Q1K51EAVPz1K29DJ/sXjhSQ0zjvsypYJ6tlwVsRV9jwW5Adq2ch8Dq8kDBCkYnELS7N7VNCSB5nC56t/g==}
|
||||||
@ -5173,7 +5173,7 @@ snapshots:
|
|||||||
'@jest/schemas': 29.6.3
|
'@jest/schemas': 29.6.3
|
||||||
'@types/istanbul-lib-coverage': 2.0.6
|
'@types/istanbul-lib-coverage': 2.0.6
|
||||||
'@types/istanbul-reports': 3.0.4
|
'@types/istanbul-reports': 3.0.4
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
'@types/yargs': 17.0.33
|
'@types/yargs': 17.0.33
|
||||||
chalk: 4.1.2
|
chalk: 4.1.2
|
||||||
|
|
||||||
@ -6515,14 +6515,14 @@ snapshots:
|
|||||||
|
|
||||||
'@types/accepts@1.3.7':
|
'@types/accepts@1.3.7':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
|
|
||||||
'@types/babel__code-frame@7.0.6': {}
|
'@types/babel__code-frame@7.0.6': {}
|
||||||
|
|
||||||
'@types/body-parser@1.19.5':
|
'@types/body-parser@1.19.5':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/connect': 3.4.38
|
'@types/connect': 3.4.38
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
|
|
||||||
'@types/buffer-json@2.0.3': {}
|
'@types/buffer-json@2.0.3': {}
|
||||||
|
|
||||||
@ -6538,17 +6538,17 @@ snapshots:
|
|||||||
|
|
||||||
'@types/clean-css@4.2.11':
|
'@types/clean-css@4.2.11':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
source-map: 0.6.1
|
source-map: 0.6.1
|
||||||
|
|
||||||
'@types/co-body@6.1.3':
|
'@types/co-body@6.1.3':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
'@types/qs': 6.9.17
|
'@types/qs': 6.9.17
|
||||||
|
|
||||||
'@types/connect@3.4.38':
|
'@types/connect@3.4.38':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
|
|
||||||
'@types/content-disposition@0.5.8': {}
|
'@types/content-disposition@0.5.8': {}
|
||||||
|
|
||||||
@ -6561,11 +6561,11 @@ snapshots:
|
|||||||
'@types/connect': 3.4.38
|
'@types/connect': 3.4.38
|
||||||
'@types/express': 5.0.0
|
'@types/express': 5.0.0
|
||||||
'@types/keygrip': 1.0.6
|
'@types/keygrip': 1.0.6
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
|
|
||||||
'@types/cors@2.8.17':
|
'@types/cors@2.8.17':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
|
|
||||||
'@types/debounce@1.2.4': {}
|
'@types/debounce@1.2.4': {}
|
||||||
|
|
||||||
@ -6579,14 +6579,14 @@ snapshots:
|
|||||||
|
|
||||||
'@types/express-serve-static-core@4.19.5':
|
'@types/express-serve-static-core@4.19.5':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
'@types/qs': 6.9.16
|
'@types/qs': 6.9.16
|
||||||
'@types/range-parser': 1.2.7
|
'@types/range-parser': 1.2.7
|
||||||
'@types/send': 0.17.4
|
'@types/send': 0.17.4
|
||||||
|
|
||||||
'@types/express-serve-static-core@5.0.2':
|
'@types/express-serve-static-core@5.0.2':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
'@types/qs': 6.9.17
|
'@types/qs': 6.9.17
|
||||||
'@types/range-parser': 1.2.7
|
'@types/range-parser': 1.2.7
|
||||||
'@types/send': 0.17.4
|
'@types/send': 0.17.4
|
||||||
@ -6611,30 +6611,30 @@ snapshots:
|
|||||||
|
|
||||||
'@types/from2@2.3.5':
|
'@types/from2@2.3.5':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
|
|
||||||
'@types/fs-extra@11.0.4':
|
'@types/fs-extra@11.0.4':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/jsonfile': 6.1.4
|
'@types/jsonfile': 6.1.4
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
|
|
||||||
'@types/fs-extra@9.0.13':
|
'@types/fs-extra@9.0.13':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
|
|
||||||
'@types/glob@7.2.0':
|
'@types/glob@7.2.0':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/minimatch': 5.1.2
|
'@types/minimatch': 5.1.2
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
|
|
||||||
'@types/glob@8.1.0':
|
'@types/glob@8.1.0':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/minimatch': 5.1.2
|
'@types/minimatch': 5.1.2
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
|
|
||||||
'@types/gunzip-maybe@1.4.2':
|
'@types/gunzip-maybe@1.4.2':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
|
|
||||||
'@types/hast@3.0.4':
|
'@types/hast@3.0.4':
|
||||||
dependencies:
|
dependencies:
|
||||||
@ -6668,7 +6668,7 @@ snapshots:
|
|||||||
|
|
||||||
'@types/jsonfile@6.1.4':
|
'@types/jsonfile@6.1.4':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
|
|
||||||
'@types/keygrip@1.0.6': {}
|
'@types/keygrip@1.0.6': {}
|
||||||
|
|
||||||
@ -6685,7 +6685,7 @@ snapshots:
|
|||||||
'@types/http-errors': 2.0.4
|
'@types/http-errors': 2.0.4
|
||||||
'@types/keygrip': 1.0.6
|
'@types/keygrip': 1.0.6
|
||||||
'@types/koa-compose': 3.2.8
|
'@types/koa-compose': 3.2.8
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
|
|
||||||
'@types/mdast@4.0.4':
|
'@types/mdast@4.0.4':
|
||||||
dependencies:
|
dependencies:
|
||||||
@ -6703,9 +6703,9 @@ snapshots:
|
|||||||
|
|
||||||
'@types/node-forge@1.3.11':
|
'@types/node-forge@1.3.11':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
|
|
||||||
'@types/node@22.10.1':
|
'@types/node@22.10.2':
|
||||||
dependencies:
|
dependencies:
|
||||||
undici-types: 6.20.0
|
undici-types: 6.20.0
|
||||||
|
|
||||||
@ -6725,19 +6725,19 @@ snapshots:
|
|||||||
|
|
||||||
'@types/s3rver@3.7.4':
|
'@types/s3rver@3.7.4':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
|
|
||||||
'@types/semver@7.5.8': {}
|
'@types/semver@7.5.8': {}
|
||||||
|
|
||||||
'@types/send@0.17.4':
|
'@types/send@0.17.4':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/mime': 1.3.5
|
'@types/mime': 1.3.5
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
|
|
||||||
'@types/serve-static@1.15.7':
|
'@types/serve-static@1.15.7':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/http-errors': 2.0.4
|
'@types/http-errors': 2.0.4
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
'@types/send': 0.17.4
|
'@types/send': 0.17.4
|
||||||
|
|
||||||
'@types/sinon-chai@3.2.12':
|
'@types/sinon-chai@3.2.12':
|
||||||
@ -6757,11 +6757,11 @@ snapshots:
|
|||||||
|
|
||||||
'@types/tar-stream@2.2.3':
|
'@types/tar-stream@2.2.3':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
|
|
||||||
'@types/through2@2.0.41':
|
'@types/through2@2.0.41':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
|
|
||||||
'@types/triple-beam@1.3.5': {}
|
'@types/triple-beam@1.3.5': {}
|
||||||
|
|
||||||
@ -6785,7 +6785,7 @@ snapshots:
|
|||||||
|
|
||||||
'@types/whatwg-url@8.2.2':
|
'@types/whatwg-url@8.2.2':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
'@types/webidl-conversions': 7.0.3
|
'@types/webidl-conversions': 7.0.3
|
||||||
|
|
||||||
'@types/which@2.0.2': {}
|
'@types/which@2.0.2': {}
|
||||||
@ -6794,11 +6794,11 @@ snapshots:
|
|||||||
|
|
||||||
'@types/ws@7.4.7':
|
'@types/ws@7.4.7':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
|
|
||||||
'@types/ws@8.5.12':
|
'@types/ws@8.5.12':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
|
|
||||||
'@types/yargs-parser@21.0.3': {}
|
'@types/yargs-parser@21.0.3': {}
|
||||||
|
|
||||||
@ -6808,7 +6808,7 @@ snapshots:
|
|||||||
|
|
||||||
'@types/yauzl@2.10.3':
|
'@types/yauzl@2.10.3':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
optional: true
|
optional: true
|
||||||
|
|
||||||
'@ungap/structured-clone@1.2.0': {}
|
'@ungap/structured-clone@1.2.0': {}
|
||||||
@ -7393,7 +7393,7 @@ snapshots:
|
|||||||
dependencies:
|
dependencies:
|
||||||
'@types/cookie': 0.4.1
|
'@types/cookie': 0.4.1
|
||||||
'@types/cors': 2.8.17
|
'@types/cors': 2.8.17
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
accepts: 1.3.8
|
accepts: 1.3.8
|
||||||
base64id: 2.0.0
|
base64id: 2.0.0
|
||||||
cookie: 0.4.2
|
cookie: 0.4.2
|
||||||
@ -8106,7 +8106,7 @@ snapshots:
|
|||||||
jest-util@29.7.0:
|
jest-util@29.7.0:
|
||||||
dependencies:
|
dependencies:
|
||||||
'@jest/types': 29.6.3
|
'@jest/types': 29.6.3
|
||||||
'@types/node': 22.10.1
|
'@types/node': 22.10.2
|
||||||
chalk: 4.1.2
|
chalk: 4.1.2
|
||||||
ci-info: 3.9.0
|
ci-info: 3.9.0
|
||||||
graceful-fs: 4.2.11
|
graceful-fs: 4.2.11
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartshell',
|
name: '@push.rocks/smartshell',
|
||||||
version: '3.1.0',
|
version: '3.2.3',
|
||||||
description: 'A library for executing shell commands using promises.'
|
description: 'A library for executing shell commands using promises.'
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,38 @@
|
|||||||
import * as plugins from './plugins.js'
|
import * as plugins from './plugins.js';
|
||||||
import { Smartshell, type IExecResultStreaming } from './classes.smartshell.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 {
|
export class SmartExecution {
|
||||||
public smartshell: Smartshell;
|
public smartshell: Smartshell;
|
||||||
public currentStreamingExecution: IExecResultStreaming;
|
public currentStreamingExecution: IExecResultStreaming;
|
||||||
public commandString: string;
|
public commandString: string;
|
||||||
|
|
||||||
|
private isRestartInProgress = false;
|
||||||
|
private isAnotherRestartRequested = false;
|
||||||
|
|
||||||
constructor(commandStringArg: string) {
|
constructor(commandStringArg: string) {
|
||||||
this.commandString = commandStringArg;
|
this.commandString = commandStringArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async restart() {
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.isRestartInProgress = true;
|
||||||
|
try {
|
||||||
if (!this.smartshell) {
|
if (!this.smartshell) {
|
||||||
this.smartshell = new Smartshell({
|
this.smartshell = new Smartshell({
|
||||||
executor: 'bash',
|
executor: 'bash',
|
||||||
@ -20,5 +42,14 @@ export class SmartExecution {
|
|||||||
await this.currentStreamingExecution.kill();
|
await this.currentStreamingExecution.kill();
|
||||||
}
|
}
|
||||||
this.currentStreamingExecution = await this.smartshell.execStreaming(this.commandString);
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,7 +2,6 @@ import * as plugins from './plugins.js';
|
|||||||
import { ShellEnv } from './classes.shellenv.js';
|
import { ShellEnv } from './classes.shellenv.js';
|
||||||
import type { IShellEnvContructorOptions, TExecutor } from './classes.shellenv.js';
|
import type { IShellEnvContructorOptions, TExecutor } from './classes.shellenv.js';
|
||||||
import { ShellLog } from './classes.shelllog.js';
|
import { ShellLog } from './classes.shelllog.js';
|
||||||
|
|
||||||
import * as cp from 'child_process';
|
import * as cp from 'child_process';
|
||||||
|
|
||||||
// -- interfaces --
|
// -- interfaces --
|
||||||
@ -17,7 +16,15 @@ export interface IExecResultStreaming {
|
|||||||
kill: () => Promise<void>;
|
kill: () => Promise<void>;
|
||||||
terminate: () => Promise<void>;
|
terminate: () => Promise<void>;
|
||||||
keyboardInterrupt: () => 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 {
|
export class Smartshell {
|
||||||
@ -29,61 +36,48 @@ export class Smartshell {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* executes a given command async
|
* Executes a given command asynchronously.
|
||||||
*/
|
*/
|
||||||
private async _exec(options: {
|
private async _exec(options: IExecOptions): Promise<IExecResult | IExecResultStreaming | void> {
|
||||||
commandString: string;
|
|
||||||
silent?: boolean;
|
|
||||||
strict?: boolean;
|
|
||||||
streaming?: boolean;
|
|
||||||
interactive?: boolean;
|
|
||||||
}): Promise<IExecResult | IExecResultStreaming | void> {
|
|
||||||
if (options.interactive) {
|
if (options.interactive) {
|
||||||
return await this._execInteractive(options);
|
return await this._execInteractive({ commandString: options.commandString });
|
||||||
}
|
}
|
||||||
|
|
||||||
return await this._execCommand(options);
|
return await this._execCommand(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _execInteractive(options: {
|
/**
|
||||||
commandString: string;
|
* Executes an interactive command.
|
||||||
interactive?: boolean;
|
*/
|
||||||
}): Promise<void> {
|
private async _execInteractive(options: Pick<IExecOptions, 'commandString'>): Promise<void> {
|
||||||
|
// Skip interactive execution in CI environments.
|
||||||
if (process.env.CI) {
|
if (process.env.CI) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const done = plugins.smartpromise.defer();
|
return new Promise<void>((resolve) => {
|
||||||
|
|
||||||
const shell = cp.spawn(options.commandString, {
|
const shell = cp.spawn(options.commandString, {
|
||||||
stdio: 'inherit',
|
stdio: 'inherit',
|
||||||
shell: true,
|
shell: true,
|
||||||
detached: true
|
detached: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.smartexit.addProcess(shell);
|
this.smartexit.addProcess(shell);
|
||||||
|
|
||||||
shell.on('close', (code) => {
|
shell.on('close', (code) => {
|
||||||
console.log(`interactive shell terminated with code ${code}`);
|
console.log(`Interactive shell terminated with code ${code}`);
|
||||||
this.smartexit.removeProcess(shell);
|
this.smartexit.removeProcess(shell);
|
||||||
done.resolve();
|
resolve();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
await done.promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _execCommand(options: {
|
/**
|
||||||
commandString: string;
|
* Executes a command and returns either a non-streaming result or a streaming interface.
|
||||||
silent?: boolean;
|
*/
|
||||||
strict?: boolean;
|
private async _execCommand(options: IExecOptions): Promise<IExecResult | IExecResultStreaming> {
|
||||||
streaming?: boolean;
|
|
||||||
}): Promise<IExecResult | IExecResultStreaming> {
|
|
||||||
const done = plugins.smartpromise.defer<IExecResult | IExecResultStreaming>();
|
|
||||||
const childProcessEnded = plugins.smartpromise.defer<IExecResult>();
|
|
||||||
|
|
||||||
const commandToExecute = this.shellEnv.createEnvExecString(options.commandString);
|
const commandToExecute = this.shellEnv.createEnvExecString(options.commandString);
|
||||||
|
|
||||||
const shellLogInstance = new ShellLog();
|
const shellLogInstance = new ShellLog();
|
||||||
|
|
||||||
const execChildProcess = cp.spawn(commandToExecute, [], {
|
const execChildProcess = cp.spawn(commandToExecute, [], {
|
||||||
shell: true,
|
shell: true,
|
||||||
cwd: process.cwd(),
|
cwd: process.cwd(),
|
||||||
@ -93,6 +87,7 @@ export class Smartshell {
|
|||||||
|
|
||||||
this.smartexit.addProcess(execChildProcess);
|
this.smartexit.addProcess(execChildProcess);
|
||||||
|
|
||||||
|
// Capture stdout and stderr output.
|
||||||
execChildProcess.stdout.on('data', (data) => {
|
execChildProcess.stdout.on('data', (data) => {
|
||||||
if (!options.silent) {
|
if (!options.silent) {
|
||||||
shellLogInstance.writeToConsole(data);
|
shellLogInstance.writeToConsole(data);
|
||||||
@ -107,47 +102,55 @@ export class Smartshell {
|
|||||||
shellLogInstance.addToBuffer(data);
|
shellLogInstance.addToBuffer(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Wrap child process termination into a Promise.
|
||||||
|
const childProcessEnded: Promise<IExecResult> = new Promise((resolve, reject) => {
|
||||||
execChildProcess.on('exit', (code, signal) => {
|
execChildProcess.on('exit', (code, signal) => {
|
||||||
this.smartexit.removeProcess(execChildProcess);
|
this.smartexit.removeProcess(execChildProcess);
|
||||||
if (options.strict && code === 1) {
|
|
||||||
done.reject();
|
|
||||||
}
|
|
||||||
|
|
||||||
const execResult = {
|
const execResult: IExecResult = {
|
||||||
exitCode: code,
|
exitCode: typeof code === 'number' ? code : (signal ? 1 : 0),
|
||||||
stdout: shellLogInstance.logStore.toString(),
|
stdout: shellLogInstance.logStore.toString(),
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!options.streaming) {
|
if (options.strict && code !== 0) {
|
||||||
done.resolve(execResult);
|
reject(new Error(`Command "${options.commandString}" exited with code ${code}`));
|
||||||
|
} else {
|
||||||
|
resolve(execResult);
|
||||||
}
|
}
|
||||||
childProcessEnded.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) {
|
if (options.streaming) {
|
||||||
done.resolve({
|
return {
|
||||||
childProcess: execChildProcess,
|
childProcess: execChildProcess,
|
||||||
finalPromise: childProcessEnded.promise,
|
finalPromise: childProcessEnded,
|
||||||
kill: async () => {
|
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');
|
await plugins.smartexit.SmartExit.killTreeByPid(execChildProcess.pid, 'SIGKILL');
|
||||||
},
|
},
|
||||||
terminate: async () => {
|
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');
|
await plugins.smartexit.SmartExit.killTreeByPid(execChildProcess.pid, 'SIGTERM');
|
||||||
},
|
},
|
||||||
keyboardInterrupt: async () => {
|
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');
|
await plugins.smartexit.SmartExit.killTreeByPid(execChildProcess.pid, 'SIGINT');
|
||||||
},
|
},
|
||||||
customSignal: async (signalArg: plugins.smartexit.TProcessSignal) => {
|
customSignal: async (signal: plugins.smartexit.TProcessSignal) => {
|
||||||
console.log(`running tree kill with custom signal ${signalArg} on process ${execChildProcess.pid}`);
|
console.log(`Running tree kill with custom signal ${signal} on process ${execChildProcess.pid}`);
|
||||||
await plugins.smartexit.SmartExit.killTreeByPid(execChildProcess.pid, signalArg);
|
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> {
|
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;
|
return (await this._exec({ commandString, silent: true, strict: true })) as IExecResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async execStreaming(
|
public async execStreaming(commandString: string, silent: boolean = false): Promise<IExecResultStreaming> {
|
||||||
commandString: string,
|
|
||||||
silent: boolean = false
|
|
||||||
): Promise<IExecResultStreaming> {
|
|
||||||
return (await this._exec({ commandString, silent, streaming: true })) as IExecResultStreaming;
|
return (await this._exec({ commandString, silent, streaming: true })) as IExecResultStreaming;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async execStreamingSilent(commandString: string): Promise<IExecResultStreaming> {
|
public async execStreamingSilent(commandString: string): Promise<IExecResultStreaming> {
|
||||||
return (await this._exec({
|
return (await this._exec({ commandString, silent: true, streaming: true })) as IExecResultStreaming;
|
||||||
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 });
|
await this._exec({ commandString, interactive: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
public async execAndWaitForLine(
|
public async execAndWaitForLine(
|
||||||
commandString: string,
|
commandString: string,
|
||||||
regexArg: RegExp,
|
regex: RegExp,
|
||||||
silentArg: boolean = false
|
silent: boolean = false
|
||||||
) {
|
): Promise<void> {
|
||||||
let done = plugins.smartpromise.defer();
|
const execStreamingResult = await this.execStreaming(commandString, silent);
|
||||||
let execStreamingResult = await this.execStreaming(commandString, silentArg);
|
return new Promise<void>((resolve) => {
|
||||||
execStreamingResult.childProcess.stdout.on('data', (stdOutChunk: string) => {
|
execStreamingResult.childProcess.stdout.on('data', (chunk: Buffer | string) => {
|
||||||
if (regexArg.test(stdOutChunk)) {
|
const data = typeof chunk === 'string' ? chunk : chunk.toString();
|
||||||
done.resolve();
|
if (regex.test(data)) {
|
||||||
|
resolve();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return done.promise;
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async execAndWaitForLineSilent(commandString: string, regexArg: RegExp) {
|
public async execAndWaitForLineSilent(commandString: string, regex: RegExp): Promise<void> {
|
||||||
return this.execAndWaitForLine(commandString, regexArg, true);
|
return this.execAndWaitForLine(commandString, regex, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user