Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(wp): handling of CR characters #2189

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
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 API, { API_HOST, disableGlobalGraphQLErrorHandling } from '../lib/api';
import commandWrapper, { getEnvIdentifier } from '../lib/cli/command';
Expand Down Expand Up @@ -41,14 +41,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 @@ -386,6 +404,7 @@ commandWrapper( {
terminal: true,
prompt: '',
historySize: 0,
crlfDelay: Infinity,
};

if ( isSubShell ) {
Expand Down Expand Up @@ -498,16 +517,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
Loading