fix(core): update

This commit is contained in:
Philipp Kunz 2023-06-22 14:16:16 +02:00
parent d84c7a16a4
commit 25c61d7d7d
3 changed files with 44 additions and 28 deletions

View File

@ -52,6 +52,10 @@ tap.test('should spawn an interactive cli', async () => {
await testSmartshell.execInteractive('echo "hi"'); await testSmartshell.execInteractive('echo "hi"');
}); });
tap.test('should spawn an interactive cli', async () => {
await testSmartshell.execInteractive('node');
});
tap.start({ tap.start({
throwOnError: true, throwOnError: true,
}); });

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@pushrocks/smartshell', name: '@pushrocks/smartshell',
version: '3.0.1', version: '3.0.2',
description: 'shell actions designed as promises' description: 'shell actions designed as promises'
} }

View File

@ -30,44 +30,45 @@ export class Smartshell {
* executes a given command async * executes a given command async
*/ */
private async _exec(options: { private async _exec(options: {
commandString: string, commandString: string;
silent?: boolean, silent?: boolean;
strict?: boolean, strict?: boolean;
streaming?: boolean, streaming?: boolean;
interactive?: boolean interactive?: boolean;
}): Promise<IExecResult | IExecResultStreaming | void> { }): Promise<IExecResult | IExecResultStreaming | void> {
if (options.interactive) { if (options.interactive) {
if (process.env.CI) { if (process.env.CI) {
return; return;
} }
const done = plugins.smartpromise.defer(); const done = plugins.smartpromise.defer();
const shell = cp.spawn('sh', [], { stdio: ['pipe', 'inherit', 'inherit'] });
this.smartexit.addProcess(shell); // Notice that stdio is set to 'inherit'
const shell = cp.spawn(options.commandString, {
shell.on('close', (code) => { stdio: 'inherit',
console.log(`interactive shell terminated with code ${code}`); shell: true,
this.smartexit.removeProcess(shell); detached: true
done.resolve();
}); });
let commandString = options.commandString; this.smartexit.addProcess(shell);
shell.stdin.write(commandString + '\n exit \n'); shell.on('close', (code) => {
console.log(`interactive shell terminated with code ${code}`);
this.smartexit.removeProcess(shell);
done.resolve();
});
await done.promise; await done.promise;
return; return;
} }
const done = plugins.smartpromise.defer<IExecResult | IExecResultStreaming>(); const done = plugins.smartpromise.defer<IExecResult | IExecResultStreaming>();
const childProcessEnded = plugins.smartpromise.defer<IExecResult>(); const childProcessEnded = plugins.smartpromise.defer<IExecResult>();
let commandToExecute = options.commandString; let commandToExecute = options.commandString;
commandToExecute = this.shellEnv.createEnvExecString(options.commandString); commandToExecute = this.shellEnv.createEnvExecString(options.commandString);
const spawnlogInstance = new ShellLog(); const spawnlogInstance = new ShellLog();
const execChildProcess = cp.spawn(commandToExecute, [], { const execChildProcess = cp.spawn(commandToExecute, [], {
shell: true, shell: true,
@ -143,19 +144,30 @@ 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(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; 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({ 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) {
await this._exec({ commandString, interactive: true }); await this._exec({ commandString, interactive: true });
} }
public async execAndWaitForLine(commandString: string, regexArg: RegExp, silentArg: boolean = false) { public async execAndWaitForLine(
commandString: string,
regexArg: RegExp,
silentArg: boolean = false
) {
let done = plugins.smartpromise.defer(); let done = plugins.smartpromise.defer();
let execStreamingResult = await this.execStreaming(commandString, silentArg); let execStreamingResult = await this.execStreaming(commandString, silentArg);
execStreamingResult.childProcess.stdout.on('data', (stdOutChunk: string) => { execStreamingResult.childProcess.stdout.on('data', (stdOutChunk: string) => {