Compare commits
No commits in common. "master" and "v3.0.6" have entirely different histories.
31
changelog.md
31
changelog.md
@ -1,36 +1,5 @@
|
||||
# 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.
|
||||
|
||||
- Refactored core classes with improved structure and modularization.
|
||||
- Updated tsbuild, tsrun, tapbundle, and @types/node dependencies to newer versions.
|
||||
- Improved build script in package.json to use tsbuild with tsfolders.
|
||||
|
||||
## 2024-09-17 - 3.0.6 - fix(core)
|
||||
Fix interactive shell execution and update dependencies
|
||||
|
||||
|
14
package.json
14
package.json
@ -1,14 +1,14 @@
|
||||
{
|
||||
"name": "@push.rocks/smartshell",
|
||||
"private": false,
|
||||
"version": "3.2.3",
|
||||
"version": "3.0.6",
|
||||
"description": "A library for executing shell commands using promises.",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"test": "(tstest test/)",
|
||||
"build": "(tsbuild tsfolders --web)",
|
||||
"build": "(tsbuild --web)",
|
||||
"buildDocs": "tsdoc"
|
||||
},
|
||||
"repository": {
|
||||
@ -33,11 +33,11 @@
|
||||
},
|
||||
"homepage": "https://code.foss.global/push.rocks/smartshell",
|
||||
"devDependencies": {
|
||||
"@git.zone/tsbuild": "^2.2.0",
|
||||
"@git.zone/tsrun": "^1.3.3",
|
||||
"@git.zone/tsbuild": "^2.1.84",
|
||||
"@git.zone/tsrun": "^1.2.49",
|
||||
"@git.zone/tstest": "^1.0.90",
|
||||
"@push.rocks/tapbundle": "^5.5.3",
|
||||
"@types/node": "^22.10.2"
|
||||
"@push.rocks/tapbundle": "^5.0.24",
|
||||
"@types/node": "^22.5.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@push.rocks/smartdelay": "^3.0.1",
|
||||
@ -45,7 +45,7 @@
|
||||
"@push.rocks/smartpromise": "^4.0.4",
|
||||
"@types/which": "^3.0.4",
|
||||
"tree-kill": "^1.2.2",
|
||||
"which": "^5.0.0"
|
||||
"which": "^4.0.0"
|
||||
},
|
||||
"files": [
|
||||
"ts/**/*",
|
||||
|
3721
pnpm-lock.yaml
generated
3721
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartshell',
|
||||
version: '3.2.3',
|
||||
version: '3.0.6',
|
||||
description: 'A library for executing shell commands using promises.'
|
||||
}
|
||||
|
@ -1,55 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,203 +0,0 @@
|
||||
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 --
|
||||
export interface IExecResult {
|
||||
exitCode: number;
|
||||
stdout: string;
|
||||
}
|
||||
|
||||
export interface IExecResultStreaming {
|
||||
childProcess: cp.ChildProcess;
|
||||
finalPromise: Promise<IExecResult>;
|
||||
kill: () => Promise<void>;
|
||||
terminate: () => Promise<void>;
|
||||
keyboardInterrupt: () => Promise<void>;
|
||||
customSignal: (signal: plugins.smartexit.TProcessSignal) => Promise<void>;
|
||||
}
|
||||
|
||||
interface IExecOptions {
|
||||
commandString: string;
|
||||
silent?: boolean;
|
||||
strict?: boolean;
|
||||
streaming?: boolean;
|
||||
interactive?: boolean;
|
||||
}
|
||||
|
||||
export class Smartshell {
|
||||
public shellEnv: ShellEnv;
|
||||
public smartexit = new plugins.smartexit.SmartExit();
|
||||
|
||||
constructor(optionsArg: IShellEnvContructorOptions) {
|
||||
this.shellEnv = new ShellEnv(optionsArg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a given command asynchronously.
|
||||
*/
|
||||
private async _exec(options: IExecOptions): Promise<IExecResult | IExecResultStreaming | void> {
|
||||
if (options.interactive) {
|
||||
return await this._execInteractive({ commandString: options.commandString });
|
||||
}
|
||||
return await this._execCommand(options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes an interactive command.
|
||||
*/
|
||||
private async _execInteractive(options: Pick<IExecOptions, 'commandString'>): Promise<void> {
|
||||
// Skip interactive execution in CI environments.
|
||||
if (process.env.CI) {
|
||||
return;
|
||||
}
|
||||
|
||||
return new Promise<void>((resolve) => {
|
||||
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();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(),
|
||||
env: process.env,
|
||||
detached: false,
|
||||
});
|
||||
|
||||
this.smartexit.addProcess(execChildProcess);
|
||||
|
||||
// Capture stdout and stderr output.
|
||||
execChildProcess.stdout.on('data', (data) => {
|
||||
if (!options.silent) {
|
||||
shellLogInstance.writeToConsole(data);
|
||||
}
|
||||
shellLogInstance.addToBuffer(data);
|
||||
});
|
||||
|
||||
execChildProcess.stderr.on('data', (data) => {
|
||||
if (!options.silent) {
|
||||
shellLogInstance.writeToConsole(data);
|
||||
}
|
||||
shellLogInstance.addToBuffer(data);
|
||||
});
|
||||
|
||||
// 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: IExecResult = {
|
||||
exitCode: typeof code === 'number' ? code : (signal ? 1 : 0),
|
||||
stdout: shellLogInstance.logStore.toString(),
|
||||
};
|
||||
|
||||
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) {
|
||||
return {
|
||||
childProcess: execChildProcess,
|
||||
finalPromise: childProcessEnded,
|
||||
kill: async () => {
|
||||
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}`);
|
||||
await plugins.smartexit.SmartExit.killTreeByPid(execChildProcess.pid, 'SIGTERM');
|
||||
},
|
||||
keyboardInterrupt: async () => {
|
||||
console.log(`Running tree kill with SIGINT on process ${execChildProcess.pid}`);
|
||||
await plugins.smartexit.SmartExit.killTreeByPid(execChildProcess.pid, 'SIGINT');
|
||||
},
|
||||
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;
|
||||
}
|
||||
|
||||
// For non-streaming mode, wait for the process to complete.
|
||||
return await childProcessEnded;
|
||||
}
|
||||
|
||||
public async exec(commandString: string): Promise<IExecResult> {
|
||||
return (await this._exec({ commandString })) as IExecResult;
|
||||
}
|
||||
|
||||
public async execSilent(commandString: string): Promise<IExecResult> {
|
||||
return (await this._exec({ commandString, silent: true })) as IExecResult;
|
||||
}
|
||||
|
||||
public async execStrict(commandString: string): Promise<IExecResult> {
|
||||
return (await this._exec({ commandString, strict: true })) as IExecResult;
|
||||
}
|
||||
|
||||
public async execStrictSilent(commandString: string): Promise<IExecResult> {
|
||||
return (await this._exec({ commandString, silent: true, strict: true })) as IExecResult;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public async execInteractive(commandString: string): Promise<void> {
|
||||
await this._exec({ commandString, interactive: true });
|
||||
}
|
||||
|
||||
public async execAndWaitForLine(
|
||||
commandString: string,
|
||||
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();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public async execAndWaitForLineSilent(commandString: string, regex: RegExp): Promise<void> {
|
||||
return this.execAndWaitForLine(commandString, regex, true);
|
||||
}
|
||||
}
|
@ -1,3 +1,2 @@
|
||||
export * from './classes.smartshell.js';
|
||||
export * from './classes.smartexecution.js';
|
||||
export { which } from './plugins.js';
|
||||
export * from './smartshell.classes.smartshell.js';
|
||||
export { which } from './smartshell.plugins.js';
|
||||
|
@ -1,4 +1,4 @@
|
||||
import * as plugins from './plugins.js';
|
||||
import * as plugins from './smartshell.plugins.js';
|
||||
|
||||
/**
|
||||
* a log handler for spawned logs
|
206
ts/smartshell.classes.smartshell.ts
Normal file
206
ts/smartshell.classes.smartshell.ts
Normal file
@ -0,0 +1,206 @@
|
||||
import * as plugins from './smartshell.plugins.js';
|
||||
import { ShellEnv } from './smartshell.classes.shellenv.js';
|
||||
import type { IShellEnvContructorOptions, TExecutor } from './smartshell.classes.shellenv.js';
|
||||
import { ShellLog } from './smartshell.classes.shelllog.js';
|
||||
|
||||
import * as cp from 'child_process';
|
||||
|
||||
// -- interfaces --
|
||||
export interface IExecResult {
|
||||
exitCode: number;
|
||||
stdout: string;
|
||||
}
|
||||
|
||||
export interface IExecResultStreaming {
|
||||
childProcess: cp.ChildProcess;
|
||||
finalPromise: Promise<IExecResult>;
|
||||
kill: () => Promise<void>;
|
||||
terminate: () => Promise<void>;
|
||||
keyboardInterrupt: () => Promise<void>;
|
||||
customSignal: (signalArg: plugins.smartexit.TProcessSignal) => Promise<void>;
|
||||
}
|
||||
|
||||
export class Smartshell {
|
||||
public shellEnv: ShellEnv;
|
||||
public smartexit = new plugins.smartexit.SmartExit();
|
||||
|
||||
constructor(optionsArg: IShellEnvContructorOptions) {
|
||||
this.shellEnv = new ShellEnv(optionsArg);
|
||||
}
|
||||
|
||||
/**
|
||||
* executes a given command async
|
||||
*/
|
||||
private async _exec(options: {
|
||||
commandString: string;
|
||||
silent?: boolean;
|
||||
strict?: boolean;
|
||||
streaming?: boolean;
|
||||
interactive?: boolean;
|
||||
}): Promise<IExecResult | IExecResultStreaming | void> {
|
||||
if (options.interactive) {
|
||||
return await this._execInteractive(options);
|
||||
}
|
||||
|
||||
return await this._execCommand(options);
|
||||
}
|
||||
|
||||
private async _execInteractive(options: {
|
||||
commandString: string;
|
||||
interactive?: boolean;
|
||||
}): Promise<void> {
|
||||
if (process.env.CI) {
|
||||
return;
|
||||
}
|
||||
|
||||
const done = plugins.smartpromise.defer();
|
||||
|
||||
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);
|
||||
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>();
|
||||
|
||||
const commandToExecute = this.shellEnv.createEnvExecString(options.commandString);
|
||||
|
||||
const shellLogInstance = new ShellLog();
|
||||
const execChildProcess = cp.spawn(commandToExecute, [], {
|
||||
shell: true,
|
||||
cwd: process.cwd(),
|
||||
env: process.env,
|
||||
detached: false,
|
||||
});
|
||||
|
||||
this.smartexit.addProcess(execChildProcess);
|
||||
|
||||
execChildProcess.stdout.on('data', (data) => {
|
||||
if (!options.silent) {
|
||||
shellLogInstance.writeToConsole(data);
|
||||
}
|
||||
shellLogInstance.addToBuffer(data);
|
||||
});
|
||||
|
||||
execChildProcess.stderr.on('data', (data) => {
|
||||
if (!options.silent) {
|
||||
shellLogInstance.writeToConsole(data);
|
||||
}
|
||||
shellLogInstance.addToBuffer(data);
|
||||
});
|
||||
|
||||
execChildProcess.on('exit', (code, signal) => {
|
||||
this.smartexit.removeProcess(execChildProcess);
|
||||
if (options.strict && code === 1) {
|
||||
done.reject();
|
||||
}
|
||||
|
||||
const execResult = {
|
||||
exitCode: code,
|
||||
stdout: shellLogInstance.logStore.toString(),
|
||||
};
|
||||
|
||||
if (!options.streaming) {
|
||||
done.resolve(execResult);
|
||||
}
|
||||
childProcessEnded.resolve(execResult);
|
||||
});
|
||||
|
||||
if (options.streaming) {
|
||||
done.resolve({
|
||||
childProcess: execChildProcess,
|
||||
finalPromise: childProcessEnded.promise,
|
||||
kill: async () => {
|
||||
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}`);
|
||||
await plugins.smartexit.SmartExit.killTreeByPid(execChildProcess.pid, 'SIGTERM');
|
||||
},
|
||||
keyboardInterrupt: async () => {
|
||||
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);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return await done.promise;
|
||||
}
|
||||
|
||||
public async exec(commandString: string): Promise<IExecResult> {
|
||||
return (await this._exec({ commandString })) as IExecResult;
|
||||
}
|
||||
|
||||
public async execSilent(commandString: string): Promise<IExecResult> {
|
||||
return (await this._exec({ commandString, silent: true })) as IExecResult;
|
||||
}
|
||||
|
||||
public async execStrict(commandString: string): Promise<IExecResult> {
|
||||
return (await this._exec({ commandString, strict: true })) as IExecResult;
|
||||
}
|
||||
|
||||
public async execStrictSilent(commandString: string): Promise<IExecResult> {
|
||||
return (await this._exec({ commandString, silent: true, strict: true })) as IExecResult;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public async execInteractive(commandString: string) {
|
||||
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();
|
||||
}
|
||||
});
|
||||
return done.promise;
|
||||
}
|
||||
|
||||
public async execAndWaitForLineSilent(commandString: string, regexArg: RegExp) {
|
||||
return this.execAndWaitForLine(commandString, regexArg, true);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user