From fce013da835db1aa52a62d0faad8ff5eae777024 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kh=E1=BA=A3i?= Date: Sun, 14 Apr 2024 17:52:15 +0700 Subject: [PATCH] refactor(pnpm): return type annotations (#7925) --- pnpm/src/checkForUpdates.ts | 2 +- pnpm/src/cmd/bin.ts | 6 +++--- pnpm/src/cmd/help.ts | 4 ++-- pnpm/src/cmd/index.ts | 6 +++--- pnpm/src/cmd/installTest.ts | 4 ++-- pnpm/src/cmd/recursive.ts | 8 ++++---- pnpm/src/cmd/root.ts | 6 +++--- pnpm/src/errorHandler.ts | 4 ++-- pnpm/src/formatError.ts | 2 +- pnpm/src/main.ts | 4 ++-- pnpm/src/parseCliArgs.ts | 4 ++-- pnpm/src/pnpm.ts | 4 ++-- pnpm/src/reporter/index.ts | 2 +- pnpm/src/reporter/silentReporter.ts | 2 +- pnpm/src/runNpm.ts | 3 ++- pnpm/src/shorthands.ts | 4 +--- 16 files changed, 32 insertions(+), 33 deletions(-) diff --git a/pnpm/src/checkForUpdates.ts b/pnpm/src/checkForUpdates.ts index 3e888da4d1..3a95b3e87a 100644 --- a/pnpm/src/checkForUpdates.ts +++ b/pnpm/src/checkForUpdates.ts @@ -13,7 +13,7 @@ interface State { const UPDATE_CHECK_FREQUENCY = 24 * 60 * 60 * 1000 // 1 day -export async function checkForUpdates (config: Config) { +export async function checkForUpdates (config: Config): Promise { const stateFile = path.join(config.stateDir, 'pnpm-state.json') let state: State | undefined try { diff --git a/pnpm/src/cmd/bin.ts b/pnpm/src/cmd/bin.ts index 6ec1e508d1..78afaaadc6 100644 --- a/pnpm/src/cmd/bin.ts +++ b/pnpm/src/cmd/bin.ts @@ -5,7 +5,7 @@ import renderHelp from 'render-help' export const rcOptionsTypes = cliOptionsTypes -export function cliOptionsTypes () { +export function cliOptionsTypes (): Record { return pick([ 'global', ], allTypes) @@ -13,7 +13,7 @@ export function cliOptionsTypes () { export const commandNames = ['bin'] -export function help () { +export function help (): string { return renderHelp({ description: 'Print the directory where pnpm will install executables.', descriptionLists: [ @@ -38,6 +38,6 @@ export async function handler ( opts: { bin: string } -) { +): Promise { return opts.bin } diff --git a/pnpm/src/cmd/help.ts b/pnpm/src/cmd/help.ts index b76242ba4d..ff06c35ffe 100644 --- a/pnpm/src/cmd/help.ts +++ b/pnpm/src/cmd/help.ts @@ -1,7 +1,7 @@ import { packageManager } from '@pnpm/cli-meta' import renderHelp from 'render-help' -export function createHelp (helpByCommandName: Record string>) { +export function createHelp (helpByCommandName: Record string>): (opts: unknown, params: string[]) => string { return function (opts: unknown, params: string[]) { let helpText!: string if (params.length === 0) { @@ -17,7 +17,7 @@ ${process['pkg'] != null ? ` (compiled to binary; bundled Node.js ${process.vers } } -function getHelpText () { +function getHelpText (): string { return renderHelp({ descriptionLists: [ { diff --git a/pnpm/src/cmd/index.ts b/pnpm/src/cmd/index.ts index c2d5f894f8..579682719f 100644 --- a/pnpm/src/cmd/index.ts +++ b/pnpm/src/cmd/index.ts @@ -202,17 +202,17 @@ handlerByCommandName['completion-server'] = createCompletionServer({ parseCliArgs, }) -function initialCompletion () { +function initialCompletion (): Array<{ name: string }> { return Object.keys(handlerByCommandName).map((name) => ({ name })) } export const pnpmCmds = handlerByCommandName -export function getCliOptionsTypes (commandName: string) { +export function getCliOptionsTypes (commandName: string): Record { return cliOptionsTypesByCommandName[commandName]?.() || {} } -export function getCommandFullName (commandName: string) { +export function getCommandFullName (commandName: string): string | null { return aliasToFullName.get(commandName) ?? (handlerByCommandName[commandName] ? commandName : null) } diff --git a/pnpm/src/cmd/installTest.ts b/pnpm/src/cmd/installTest.ts index 06471d558c..fc60fe9d78 100644 --- a/pnpm/src/cmd/installTest.ts +++ b/pnpm/src/cmd/installTest.ts @@ -10,7 +10,7 @@ export const rcOptionsTypes = install.rcOptionsTypes export const commandNames = ['install-test', 'it'] -export function help () { +export function help (): string { return renderHelp({ aliases: ['it'], description: 'Runs a `pnpm install` followed immediately by a `pnpm test`. It takes exactly the same arguments as `pnpm install`.', @@ -19,7 +19,7 @@ export function help () { }) } -export async function handler (opts: PnpmOptions, params: string[]) { +export async function handler (opts: PnpmOptions, params: string[]): Promise { await install.handler(opts) await test.handler(opts as any, params) // eslint-disable-line } diff --git a/pnpm/src/cmd/recursive.ts b/pnpm/src/cmd/recursive.ts index 55f6d710eb..9b11a734db 100644 --- a/pnpm/src/cmd/recursive.ts +++ b/pnpm/src/cmd/recursive.ts @@ -3,12 +3,12 @@ import { FILTERING } from '@pnpm/common-cli-options-help' import { WANTED_LOCKFILE } from '@pnpm/constants' import renderHelp from 'render-help' -export const rcOptionsTypes = () => ({}) -export const cliOptionsTypes = () => ({}) +export const rcOptionsTypes = (): Record => ({}) +export const cliOptionsTypes = (): Record => ({}) export const commandNames = ['recursive', 'multi', 'm'] -export function help () { +export function help (): string { return renderHelp({ description: 'Concurrently performs some actions in all subdirectories with a `package.json` (excluding node_modules). \ A `pnpm-workspace.yaml` file may be used to control what directories are searched for packages.', @@ -119,7 +119,7 @@ A shared lockfile also means that all dependencies of all projects will be in a }) } -export function handler () { +export function handler (): void { console.log(help()) process.exit(1) } diff --git a/pnpm/src/cmd/root.ts b/pnpm/src/cmd/root.ts index 1a405400d5..468617012a 100644 --- a/pnpm/src/cmd/root.ts +++ b/pnpm/src/cmd/root.ts @@ -6,7 +6,7 @@ import renderHelp from 'render-help' export const rcOptionsTypes = cliOptionsTypes -export function cliOptionsTypes () { +export function cliOptionsTypes (): Record { return pick([ 'global', ], allTypes) @@ -14,7 +14,7 @@ export function cliOptionsTypes () { export const commandNames = ['root'] -export function help () { +export function help (): string { return renderHelp({ description: 'Print the effective `node_modules` directory.', descriptionLists: [ @@ -39,6 +39,6 @@ export async function handler ( opts: { dir: string } -) { +): Promise { return `${path.join(opts.dir, 'node_modules')}\n` } diff --git a/pnpm/src/errorHandler.ts b/pnpm/src/errorHandler.ts index a96d888675..9b63ffb6ee 100644 --- a/pnpm/src/errorHandler.ts +++ b/pnpm/src/errorHandler.ts @@ -7,7 +7,7 @@ const getDescendentProcesses = promisify((pid: number, callback: (error: Error | pidTree(pid, { root: false }, callback) }) -export async function errorHandler (error: Error & { code?: string }) { +export async function errorHandler (error: Error & { code?: string }): Promise { if (error.name != null && error.name !== 'pnpm' && !error.name.startsWith('pnpm:')) { try { error.name = 'pnpm' @@ -43,7 +43,7 @@ export async function errorHandler (error: Error & { code?: string }) { }, 0) } -async function killProcesses (status: number) { +async function killProcesses (status: number): Promise { try { const descendentProcesses = await getDescendentProcesses(process.pid) for (const pid of descendentProcesses) { diff --git a/pnpm/src/formatError.ts b/pnpm/src/formatError.ts index b092e77681..bb230e6e7d 100644 --- a/pnpm/src/formatError.ts +++ b/pnpm/src/formatError.ts @@ -1,6 +1,6 @@ import chalk from 'chalk' -export function formatUnknownOptionsError (unknownOptions: Map) { +export function formatUnknownOptionsError (unknownOptions: Map): string { let output = chalk.bgRed.black('\u2009ERROR\u2009') const unknownOptionsArray = Array.from(unknownOptions.keys()) if (unknownOptionsArray.length > 1) { diff --git a/pnpm/src/main.ts b/pnpm/src/main.ts index 13c710bea3..cc38d97411 100644 --- a/pnpm/src/main.ts +++ b/pnpm/src/main.ts @@ -40,7 +40,7 @@ const DEPRECATED_OPTIONS = new Set([ // A workaround for the https://github.com/vercel/pkg/issues/897 issue. delete process.env.PKG_EXECPATH -export async function main (inputArgv: string[]) { +export async function main (inputArgv: string[]): Promise { let parsedCliArgs!: ParsedCliArgs try { parsedCliArgs = await parseCliArgs(inputArgv) @@ -317,7 +317,7 @@ export async function main (inputArgv: string[]) { } } -function printError (message: string, hint?: string) { +function printError (message: string, hint?: string): void { const ERROR = chalk.bgRed.black('\u2009ERROR\u2009') console.log(`${message.startsWith(ERROR) ? '' : ERROR + ' '}${chalk.red(message)}`) if (hint) { diff --git a/pnpm/src/parseCliArgs.ts b/pnpm/src/parseCliArgs.ts index 232ac743ec..bcb58d4e69 100644 --- a/pnpm/src/parseCliArgs.ts +++ b/pnpm/src/parseCliArgs.ts @@ -1,4 +1,4 @@ -import { parseCliArgs as parseCliArgsLib } from '@pnpm/parse-cli-args' +import { type ParsedCliArgs, parseCliArgs as parseCliArgsLib } from '@pnpm/parse-cli-args' import { getCliOptionsTypes, getCommandFullName, @@ -14,7 +14,7 @@ const RENAMED_OPTIONS = { store: 'store-dir', } -export async function parseCliArgs (inputArgv: string[]) { +export async function parseCliArgs (inputArgv: string[]): Promise { return parseCliArgsLib({ fallbackCommand: 'run', escapeArgs: ['create', 'dlx', 'exec'], diff --git a/pnpm/src/pnpm.ts b/pnpm/src/pnpm.ts index 0cbccb66d7..a54b46e506 100644 --- a/pnpm/src/pnpm.ts +++ b/pnpm/src/pnpm.ts @@ -55,7 +55,7 @@ const argv = process.argv.slice(2) } })() -async function runPnpm () { +async function runPnpm (): Promise { const { errorHandler } = await import('./errorHandler') try { const { main } = await import('./main') @@ -65,7 +65,7 @@ async function runPnpm () { } } -async function passThruToNpm () { +async function passThruToNpm (): Promise { const { runNpm } = await import('./runNpm') const { status } = await runNpm(argv) process.exit(status!) diff --git a/pnpm/src/reporter/index.ts b/pnpm/src/reporter/index.ts index 726d75c40a..ee140b0469 100644 --- a/pnpm/src/reporter/index.ts +++ b/pnpm/src/reporter/index.ts @@ -11,7 +11,7 @@ export function initReporter ( cmd: string | null config: Config } -) { +): void { switch (reporterType) { case 'default': initDefaultReporter({ diff --git a/pnpm/src/reporter/silentReporter.ts b/pnpm/src/reporter/silentReporter.ts index 939d62bca6..08050b521c 100644 --- a/pnpm/src/reporter/silentReporter.ts +++ b/pnpm/src/reporter/silentReporter.ts @@ -4,7 +4,7 @@ export function silentReporter ( streamParser: { on: (event: 'data', handler: (obj: LogBase) => void) => void } -) { +): void { streamParser.on('data', (obj: LogBase) => { if (obj.level !== 'error') return diff --git a/pnpm/src/runNpm.ts b/pnpm/src/runNpm.ts index 985f3af37c..cd9b7c29c0 100644 --- a/pnpm/src/runNpm.ts +++ b/pnpm/src/runNpm.ts @@ -1,9 +1,10 @@ +import { type SpawnSyncReturns } from 'child_process' import { packageManager } from '@pnpm/cli-meta' import { getConfig, types as allTypes } from '@pnpm/config' import { runNpm as _runNpm } from '@pnpm/run-npm' import pick from 'ramda/src/pick' -export async function runNpm (args: string[]) { +export async function runNpm (args: string[]): Promise> { const { config } = await getConfig({ cliOptions: {}, packageManager, diff --git a/pnpm/src/shorthands.ts b/pnpm/src/shorthands.ts index 8b4edfa61f..67d396787b 100644 --- a/pnpm/src/shorthands.ts +++ b/pnpm/src/shorthands.ts @@ -1,5 +1,4 @@ -// eslint-disable -export const shorthands = { +export const shorthands: Record = { s: '--reporter=silent', d: '--loglevel=info', dd: '--loglevel=verbose', @@ -33,4 +32,3 @@ export const shorthands = { i: '--interactive', F: '--filter', } -// eslint-enable