11import process from 'process'
2+ import { Transform } from 'stream'
3+ import { stripVTControlCharacters } from 'util'
24
35import execa from 'execa'
4- // @ts -expect-error TS(7016) FIXME: Could not find a declaration file for module 'stri... Remove this comment to see the full error message
5- import stripAnsiCc from 'strip-ansi-control-characters'
66
77import { stopSpinner , type Spinner } from '../lib/spinner.js'
8+
89import { chalk , log , NETLIFYDEVERR , NETLIFYDEVWARN } from './command-helpers.js'
910import { processOnExit } from './dev.js'
1011
11- /**
12- * @type {(() => Promise<void>)[] } - array of functions to run before the process exits
13- */
14- // @ts -expect-error TS(7034) FIXME: Variable 'cleanupWork' implicitly has type 'any[]'... Remove this comment to see the full error message
15- const cleanupWork = [ ]
12+ const isErrnoException = ( value : unknown ) : value is NodeJS . ErrnoException =>
13+ value instanceof Error && Object . hasOwn ( value , 'code' )
14+
15+ const createStripAnsiControlCharsStream = ( ) : Transform =>
16+ new Transform ( {
17+ transform ( chunk , _encoding , callback ) {
18+ callback ( null , stripVTControlCharacters ( typeof chunk === 'string' ? chunk : ( chunk as unknown ) ?. toString ( ) ?? '' ) )
19+ } ,
20+ } )
21+
22+ const cleanupWork : ( ( ) => Promise < void > ) [ ] = [ ]
1623
1724let cleanupStarted = false
1825
19- /**
20- * @param {object } input
21- * @param {number= } input.exitCode The exit code to return when exiting the process after cleanup
22- */
2326const cleanupBeforeExit = async ( { exitCode } : { exitCode ?: number | undefined } = { } ) => {
2427 // If cleanup has started, then wherever started it will be responsible for exiting
2528 if ( ! cleanupStarted ) {
2629 cleanupStarted = true
2730 try {
28- // @ts -expect-error TS(7005) FIXME: Variable 'cleanupWork' implicitly has an 'any[]' t... Remove this comment to see the full error message
2931 await Promise . all ( cleanupWork . map ( ( cleanup ) => cleanup ( ) ) )
3032 } finally {
33+ // eslint-disable-next-line n/no-process-exit
3134 process . exit ( exitCode )
3235 }
3336 }
@@ -63,7 +66,7 @@ export const runCommand = (
6366 // In this case, we want to manually control when to clear and when to render a frame, so we turn this off.
6467 stopSpinner ( { error : false , spinner } )
6568 }
66- const pipeDataWithSpinner = ( writeStream : NodeJS . WriteStream , chunk : any ) => {
69+ const pipeDataWithSpinner = ( writeStream : NodeJS . WriteStream , chunk : string | Uint8Array ) => {
6770 if ( spinner ?. isSpinning ) {
6871 spinner . clear ( )
6972 }
@@ -72,15 +75,19 @@ export const runCommand = (
7275 } )
7376 }
7477
75- // @ts -expect-error TS(2531) FIXME: Object is possibly 'null'.
76- commandProcess . stdout . pipe ( stripAnsiCc . stream ( ) ) . on ( 'data' , pipeDataWithSpinner . bind ( null , process . stdout ) )
77- // @ts -expect-error TS(2531) FIXME: Object is possibly 'null'.
78- commandProcess . stderr . pipe ( stripAnsiCc . stream ( ) ) . on ( 'data' , pipeDataWithSpinner . bind ( null , process . stderr ) )
79- // @ts -expect-error TS(2345) FIXME: Argument of type 'Writable | null' is not assignab... Remove this comment to see the full error message
80- process . stdin . pipe ( commandProcess . stdin )
78+ commandProcess . stdout
79+ ?. pipe ( createStripAnsiControlCharsStream ( ) )
80+ . on ( 'data' , pipeDataWithSpinner . bind ( null , process . stdout ) )
81+ commandProcess . stderr
82+ ?. pipe ( createStripAnsiControlCharsStream ( ) )
83+ . on ( 'data' , pipeDataWithSpinner . bind ( null , process . stderr ) )
84+ if ( commandProcess . stdin != null ) {
85+ process . stdin . pipe ( commandProcess . stdin )
86+ }
8187
8288 // we can't try->await->catch since we don't want to block on the framework server which
8389 // is a long running process
90+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
8491 commandProcess . then ( async ( ) => {
8592 const result = await commandProcess
8693 const [ commandWithoutArgs ] = command . split ( ' ' )
@@ -92,9 +99,10 @@ export const runCommand = (
9299 )
93100 } else {
94101 const errorMessage = result . failed
95- ? // @ts -expect-error TS(2339) FIXME: Property 'shortMessage' does not exist on type 'Ex... Remove this comment to see the full error message
96- `${ NETLIFYDEVERR } ${ result . shortMessage } `
97- : `${ NETLIFYDEVWARN } "${ command } " exited with code ${ result . exitCode } `
102+ ? // @ts -expect-error FIXME(serhalp): We use `reject: false` which means the resolved value is either the resolved value
103+ // or the rejected value, but the types aren't smart enough to know this.
104+ `${ NETLIFYDEVERR } ${ result . shortMessage as string } `
105+ : `${ NETLIFYDEVWARN } "${ command } " exited with code ${ result . exitCode . toString ( ) } `
98106
99107 log ( `${ errorMessage } . Shutting down Netlify Dev server` )
100108 }
@@ -108,18 +116,10 @@ export const runCommand = (
108116 return commandProcess
109117}
110118
111- /**
112- *
113- * @param {object } config
114- * @param {string } config.command
115- * @param {* } config.error
116- * @returns
117- */
118- // @ts -expect-error TS(7031) FIXME: Binding element 'command' implicitly has an 'any' ... Remove this comment to see the full error message
119- const isNonExistingCommandError = ( { command, error : commandError } ) => {
119+ const isNonExistingCommandError = ( { command, error : commandError } : { command : string ; error : unknown } ) => {
120120 // `ENOENT` is only returned for non Windows systems
121121 // See https://github.com/sindresorhus/execa/pull/447
122- if ( commandError . code === 'ENOENT' ) {
122+ if ( isErrnoException ( commandError ) && commandError . code === 'ENOENT' ) {
123123 return true
124124 }
125125
@@ -130,6 +130,7 @@ const isNonExistingCommandError = ({ command, error: commandError }) => {
130130
131131 // this only works on English versions of Windows
132132 return (
133+ commandError instanceof Error &&
133134 typeof commandError . message === 'string' &&
134135 commandError . message . includes ( 'is not recognized as an internal or external command' )
135136 )
0 commit comments