Skip to content

Commit

Permalink
Allocate pty (#556)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrmarti committed Jul 26, 2023
1 parent a74814e commit b23fe83
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
18 changes: 10 additions & 8 deletions src/spec-common/commonUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export interface ExecFunction {

export interface PtyExec {
onData: Event<string>;
write(data: string): void;
write?(data: string): void;
resize(cols: number, rows: number): void;
exit: Promise<{ code: number | undefined; signal: number | undefined }>;
terminate(): Promise<void>;
Expand Down Expand Up @@ -198,14 +198,16 @@ export async function runCommand(options: {
return new Promise<{ cmdOutput: string }>((resolve, reject) => {
let cmdOutput = '';

if (stdin) {
p.write(stdin);
const subs: Disposable[] = [];
if (p.write) {
if (stdin) {
p.write(stdin);
}
if (onDidInput) {
subs.push(onDidInput(data => p.write!(data)));
}
}

const subs = [
onDidInput && onDidInput(data => p.write(data)),
];

p.onData(chunk => {
cmdOutput += chunk;
if (print === 'continuous') {
Expand Down Expand Up @@ -403,7 +405,7 @@ export function plainExecAsPtyExec(plain: ExecFunction, allowInheritTTY: boolean
}
return {
onData: onDataEmitter.event,
write: p.stdin ? p.stdin.write.bind(p.stdin) : () => {},
write: p.stdin ? p.stdin.write.bind(p.stdin) : undefined,
resize: () => {},
exit: p.exit.then(({ code, signal }) => ({
code: typeof code === 'number' ? code : undefined,
Expand Down
8 changes: 4 additions & 4 deletions src/spec-common/injectHeadless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,10 +578,10 @@ export async function runRemoteCommand(params: { output: Log; onDidInput?: Event
}
}
});
if (params.onDidInput) {
params.onDidInput(data => p.write(data));
} else if (params.stdin) {
const listener = (data: Buffer): void => p.write(data.toString());
if (p.write && params.onDidInput) {
params.onDidInput(data => p.write!(data));
} else if (p.write && params.stdin) {
const listener = (data: Buffer): void => p.write!(data.toString());
const stdin = params.stdin;
if (stdin.isTTY) {
stdin.setRawMode(true);
Expand Down
6 changes: 3 additions & 3 deletions src/spec-shutdown/dockerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,10 @@ export async function dockerComposePtyCLI(params: DockerCLIParameters | PartialP
});
}

export function dockerExecFunction(params: DockerCLIParameters | PartialExecParameters | DockerResolverParameters, containerName: string, user: string | undefined): ExecFunction {
export function dockerExecFunction(params: DockerCLIParameters | PartialExecParameters | DockerResolverParameters, containerName: string, user: string | undefined, pty = false): ExecFunction {
return async function (execParams: ExecParameters): Promise<Exec> {
const { exec, cmd, args, env } = toExecParameters(params);
const { argsPrefix, args: execArgs } = toDockerExecArgs(containerName, user, execParams, false);
const { argsPrefix, args: execArgs } = toDockerExecArgs(containerName, user, execParams, pty);
return exec({
cmd,
args: (args || []).concat(execArgs),
Expand All @@ -328,7 +328,7 @@ export function dockerExecFunction(params: DockerCLIParameters | PartialExecPara
export async function dockerPtyExecFunction(params: PartialPtyExecParameters | DockerResolverParameters, containerName: string, user: string | undefined, loadNativeModule: <T>(moduleName: string) => Promise<T | undefined>, allowInheritTTY: boolean): Promise<PtyExecFunction> {
const pty = await loadNativeModule<typeof ptyType>('node-pty');
if (!pty) {
const plain = dockerExecFunction(params, containerName, user);
const plain = dockerExecFunction(params, containerName, user, true);
return plainExecAsPtyExec(plain, allowInheritTTY);
}

Expand Down

0 comments on commit b23fe83

Please sign in to comment.