Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions src/bin/vip-wp.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import gql from 'graphql-tag';
import readline from 'readline';
import SocketIO from 'socket.io-client';
import IOStream from 'socket.io-stream';
import { Writable } from 'stream';
import { Transform, Writable } from 'stream';

import { WPCliCommandOverSSH } from '../commands/wp-ssh';
import API, { API_HOST, disableGlobalGraphQLErrorHandling } from '../lib/api';
Expand Down Expand Up @@ -43,14 +43,32 @@ const cancelCommandChar = '\x03';
let currentJob = null;
let currentOffset = 0;
let commandRunning = false;
const isStdinTty = process.stdin.isTTY;

const normalizeNewlineStream = new Transform( {
transform( chunk, encoding, callback ) {
callback( null, chunk.toString().replace( /\r/g, '\n' ) );
},
} );

const pipeStreamsToProcess = ( { stdin, stdout: outStream } ) => {
process.stdin.pipe( stdin );
if ( isStdinTty ) {
process.stdin.pipe( normalizeNewlineStream ).pipe( stdin );
} else {
process.stdin.pipe( stdin );
}

outStream.pipe( process.stdout );
};

const unpipeStreamsFromProcess = ( { stdin, stdout: outStream } ) => {
process.stdin.unpipe( stdin );
if ( isStdinTty ) {
process.stdin.unpipe( normalizeNewlineStream );
normalizeNewlineStream.unpipe( stdin );
} else {
process.stdin.unpipe( stdin );
}

outStream.unpipe( process.stdout );
};

Expand Down Expand Up @@ -394,6 +412,7 @@ commandWrapper( {
terminal: true,
prompt: '',
historySize: 0,
crlfDelay: Infinity,
};

if ( isSubShell ) {
Expand Down Expand Up @@ -506,16 +525,6 @@ commandWrapper( {
} );
} );

// Fix to re-add the \n character that readline strips when terminal == true
process.stdin.on( 'data', data => {
// only run this in interactive mode for prompts from WP commands
if ( commandRunning && 0 === Buffer.compare( data, Buffer.from( '\r' ) ) ) {
if ( currentJob?.stdinStream ) {
currentJob.stdinStream.write( '\n' );
}
}
} );

subShellRl.on( 'SIGINT', async () => {
// if we have a 2nd SIGINT, exit immediately
if ( countSIGINT >= 1 ) {
Expand Down