Skip to content

Commit

Permalink
fix(wp): handling of CR caharacters
Browse files Browse the repository at this point in the history
  • Loading branch information
sjinks committed Jan 11, 2025
1 parent 0a6d195 commit c0dd0d4
Showing 1 changed file with 22 additions and 13 deletions.
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

0 comments on commit c0dd0d4

Please sign in to comment.