Skip to content

Commit a59c66f

Browse files
authored
fix(deps): remove trivial dependencies
* types: add type declaration files for untyped maxstache deps * types: add type declaration for untyped express-logging * fix(deps): remove untyped dep `strip-ansi-control-characters` `stripVTControlCharacters` was added in 16.11.0 (we support >+18.14.0) * refactor: fix types and eslint errors in utils/shell.ts
1 parent 5fd9add commit a59c66f

File tree

9 files changed

+63
-65
lines changed

9 files changed

+63
-65
lines changed

eslint_temporary_suppressions.js

-13
Original file line numberDiff line numberDiff line change
@@ -1056,19 +1056,6 @@ export default [
10561056
'@typescript-eslint/prefer-nullish-coalescing': 'off',
10571057
},
10581058
},
1059-
{
1060-
files: ['src/utils/shell.ts'],
1061-
rules: {
1062-
'@typescript-eslint/no-unsafe-return': 'off',
1063-
'@typescript-eslint/no-unsafe-call': 'off',
1064-
'n/no-process-exit': 'off',
1065-
'@typescript-eslint/no-explicit-any': 'off',
1066-
'@typescript-eslint/no-unsafe-argument': 'off',
1067-
'@typescript-eslint/no-unsafe-member-access': 'off',
1068-
'@typescript-eslint/no-floating-promises': 'off',
1069-
'@typescript-eslint/restrict-template-expressions': 'off',
1070-
},
1071-
},
10721059
{
10731060
files: ['src/utils/sign-redirect.ts'],
10741061
rules: {

package-lock.json

-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@
143143
"readdirp": "4.1.2",
144144
"semver": "7.7.1",
145145
"source-map-support": "0.5.21",
146-
"strip-ansi-control-characters": "2.0.0",
147146
"tempy": "3.1.0",
148147
"terminal-link": "4.0.0",
149148
"toml": "3.0.0",

src/lib/functions/server.ts

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type { IncomingHttpHeaders } from 'http'
44
import path from 'path'
55

66
import express, { type Request, type RequestHandler } from 'express'
7-
// @ts-expect-error TS(7016) FIXME: Could not find a declaration file for module 'expr... Remove this comment to see the full error message
87
import expressLogging from 'express-logging'
98
import { jwtDecode } from 'jwt-decode'
109

src/utils/copy-template-dir/copy-template-dir.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,10 @@ import path from 'path'
2424
import { pipeline } from 'stream'
2525
import { promisify } from 'util'
2626

27-
// @ts-expect-error TS(7016) FIXME: Could not find a declaration file for module 'maxstache... Remove this comment to see the full error message
2827
import maxstache from 'maxstache'
29-
// @ts-expect-error TS(7016) FIXME: Could not find a declaration file for module 'maxstache-stream... Remove this comment to see the full error message
3028
import maxstacheStream from 'maxstache-stream'
3129
import { readdirp, EntryInfo, ReaddirpStream } from 'readdirp'
3230

33-
const noop = (): void => undefined
34-
3531
// Remove a leading underscore
3632
function removeUnderscore(filepath: string): string {
3733
const parts = filepath.split(path.sep)
@@ -56,9 +52,7 @@ async function writeFile(outDir: string, vars: Record<string, string>, file: Ent
5652
}
5753

5854
// High throughput template dir writes
59-
export async function copyTemplateDir(srcDir: string, outDir: string, vars: any): Promise<string[]> {
60-
if (!vars) vars = noop
61-
55+
export async function copyTemplateDir(srcDir: string, outDir: string, vars: Record<string, string>): Promise<string[]> {
6256
assert.strictEqual(typeof srcDir, 'string')
6357
assert.strictEqual(typeof outDir, 'string')
6458
assert.strictEqual(typeof vars, 'object')

src/utils/shell.ts

+33-32
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
11
import process from 'process'
2+
import { Transform } from 'stream'
3+
import { stripVTControlCharacters } from 'util'
24

35
import 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

77
import { stopSpinner, type Spinner } from '../lib/spinner.js'
8+
89
import { chalk, log, NETLIFYDEVERR, NETLIFYDEVWARN } from './command-helpers.js'
910
import { 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

1724
let 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-
*/
2326
const 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
)

types/express-logging/index.d.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
declare module 'express-logging' {
2+
import { RequestHandler } from 'express';
3+
4+
interface LoggerOptions {blacklist?: string[]}
5+
6+
interface Logger {
7+
info(...args: unknown[]): void;
8+
error(...args: unknown[]): void;
9+
warn(...args: unknown[]): void;
10+
debug?(...args: unknown[]): void;
11+
log(...args: unknown[]): void;
12+
}
13+
14+
function expressLogging(logger?: Logger, options?: LoggerOptions): RequestHandler;
15+
16+
export default expressLogging;
17+
}

types/maxstache-stream/index.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
declare module 'maxstache-stream' {
2+
import { Transform } from 'stream';
3+
4+
function maxstacheStream(vars: Record<string, string>): Transform;
5+
6+
export default maxstacheStream;
7+
}

types/maxstache/index.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare module 'maxstache' {
2+
function maxstache(str: string, ctx: Record<string, string>): string;
3+
4+
export default maxstache;
5+
}

0 commit comments

Comments
 (0)