Skip to content

Commit 1049c9a

Browse files
committed
refactor: fix types and eslint errors in utils/shell.ts
1 parent fe8e4a0 commit 1049c9a

File tree

2 files changed

+23
-41
lines changed

2 files changed

+23
-41
lines changed

eslint_temporary_suppressions.js

-13
Original file line numberDiff line numberDiff line change
@@ -1071,19 +1071,6 @@ export default [
10711071
'@typescript-eslint/prefer-nullish-coalescing': 'off',
10721072
},
10731073
},
1074-
{
1075-
files: ['src/utils/shell.ts'],
1076-
rules: {
1077-
'@typescript-eslint/no-unsafe-return': 'off',
1078-
'@typescript-eslint/no-unsafe-call': 'off',
1079-
'n/no-process-exit': 'off',
1080-
'@typescript-eslint/no-explicit-any': 'off',
1081-
'@typescript-eslint/no-unsafe-argument': 'off',
1082-
'@typescript-eslint/no-unsafe-member-access': 'off',
1083-
'@typescript-eslint/no-floating-promises': 'off',
1084-
'@typescript-eslint/restrict-template-expressions': 'off',
1085-
},
1086-
},
10871074
{
10881075
files: ['src/utils/sign-redirect.ts'],
10891076
rules: {

src/utils/shell.ts

+23-28
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,25 @@ import { processOnExit } from './dev.js'
1212
const isErrnoException = (value: unknown): value is NodeJS.ErrnoException =>
1313
value instanceof Error && Object.hasOwn(value, 'code')
1414

15-
const createStripAnsiControlCharsStream = (): Transform => new Transform({
16-
transform(chunk, _encoding, callback) {
17-
const text = typeof chunk === 'string' ? chunk : chunk.toString()
18-
callback(null, stripVTControlCharacters(text))
19-
}
20-
})
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+
})
2121

2222
const cleanupWork: (() => Promise<void>)[] = []
2323

2424
let cleanupStarted = false
2525

26-
/**
27-
* @param {object} input
28-
* @param {number=} input.exitCode The exit code to return when exiting the process after cleanup
29-
*/
3026
const cleanupBeforeExit = async ({ exitCode }: { exitCode?: number | undefined } = {}) => {
3127
// If cleanup has started, then wherever started it will be responsible for exiting
3228
if (!cleanupStarted) {
3329
cleanupStarted = true
3430
try {
35-
// @ts-expect-error TS(7005) FIXME: Variable 'cleanupWork' implicitly has an 'any[]' t... Remove this comment to see the full error message
3631
await Promise.all(cleanupWork.map((cleanup) => cleanup()))
3732
} finally {
33+
// eslint-disable-next-line n/no-process-exit
3834
process.exit(exitCode)
3935
}
4036
}
@@ -70,7 +66,7 @@ export const runCommand = (
7066
// In this case, we want to manually control when to clear and when to render a frame, so we turn this off.
7167
stopSpinner({ error: false, spinner })
7268
}
73-
const pipeDataWithSpinner = (writeStream: NodeJS.WriteStream, chunk: any) => {
69+
const pipeDataWithSpinner = (writeStream: NodeJS.WriteStream, chunk: string | Uint8Array) => {
7470
if (spinner?.isSpinning) {
7571
spinner.clear()
7672
}
@@ -79,14 +75,19 @@ export const runCommand = (
7975
})
8076
}
8177

82-
commandProcess.stdout?.pipe(createStripAnsiControlCharsStream()).on('data', pipeDataWithSpinner.bind(null, process.stdout))
83-
commandProcess.stderr?.pipe(createStripAnsiControlCharsStream()).on('data', pipeDataWithSpinner.bind(null, process.stderr))
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))
8484
if (commandProcess.stdin != null) {
85-
process.stdin?.pipe(commandProcess.stdin)
85+
process.stdin.pipe(commandProcess.stdin)
8686
}
8787

8888
// we can't try->await->catch since we don't want to block on the framework server which
8989
// is a long running process
90+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
9091
commandProcess.then(async () => {
9192
const result = await commandProcess
9293
const [commandWithoutArgs] = command.split(' ')
@@ -98,9 +99,10 @@ export const runCommand = (
9899
)
99100
} else {
100101
const errorMessage = result.failed
101-
? // @ts-expect-error TS(2339) FIXME: Property 'shortMessage' does not exist on type 'Ex... Remove this comment to see the full error message
102-
`${NETLIFYDEVERR} ${result.shortMessage}`
103-
: `${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()}`
104106

105107
log(`${errorMessage}. Shutting down Netlify Dev server`)
106108
}
@@ -114,18 +116,10 @@ export const runCommand = (
114116
return commandProcess
115117
}
116118

117-
/**
118-
*
119-
* @param {object} config
120-
* @param {string} config.command
121-
* @param {*} config.error
122-
* @returns
123-
*/
124-
// @ts-expect-error TS(7031) FIXME: Binding element 'command' implicitly has an 'any' ... Remove this comment to see the full error message
125-
const isNonExistingCommandError = ({ command, error: commandError }) => {
119+
const isNonExistingCommandError = ({ command, error: commandError }: { command: string; error: unknown }) => {
126120
// `ENOENT` is only returned for non Windows systems
127121
// See https://github.com/sindresorhus/execa/pull/447
128-
if (commandError.code === 'ENOENT') {
122+
if (isErrnoException(commandError) && commandError.code === 'ENOENT') {
129123
return true
130124
}
131125

@@ -136,6 +130,7 @@ const isNonExistingCommandError = ({ command, error: commandError }) => {
136130

137131
// this only works on English versions of Windows
138132
return (
133+
commandError instanceof Error &&
139134
typeof commandError.message === 'string' &&
140135
commandError.message.includes('is not recognized as an internal or external command')
141136
)

0 commit comments

Comments
 (0)