refactor(pnpm): return type annotations (#7925)

This commit is contained in:
Khải authored and GitHub committed 2024-04-14 12:52:15 +02:00
1 parent d21464e790
commit fce013da83
16 files changed
+32 -33

No files matched your search

+1 -1
View File
@@ -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<void> {
const stateFile = path.join(config.stateDir, 'pnpm-state.json')
let state: State | undefined
try {
+3 -3
View File
@@ -5,7 +5,7 @@ import renderHelp from 'render-help'
export const rcOptionsTypes = cliOptionsTypes
export function cliOptionsTypes () {
export function cliOptionsTypes (): Record<string, unknown> {
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<string> {
return opts.bin
}
+2 -2
View File
@@ -1,7 +1,7 @@
import { packageManager } from '@pnpm/cli-meta'
import renderHelp from 'render-help'
export function createHelp (helpByCommandName: Record<string, () => string>) {
export function createHelp (helpByCommandName: Record<string, () => 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: [
{
+3 -3
View File
@@ -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<string, unknown> {
return cliOptionsTypesByCommandName[commandName]?.() || {}
}
export function getCommandFullName (commandName: string) {
export function getCommandFullName (commandName: string): string | null {
return aliasToFullName.get(commandName) ??
(handlerByCommandName[commandName] ? commandName : null)
}
+2 -2
View File
@@ -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<void> {
await install.handler(opts)
await test.handler(opts as any, params) // eslint-disable-line
}
+4 -4
View File
@@ -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<string, unknown> => ({})
export const cliOptionsTypes = (): Record<string, unknown> => ({})
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)
}
+3 -3
View File
@@ -6,7 +6,7 @@ import renderHelp from 'render-help'
export const rcOptionsTypes = cliOptionsTypes
export function cliOptionsTypes () {
export function cliOptionsTypes (): Record<string, unknown> {
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<string> {
return `${path.join(opts.dir, 'node_modules')}\n`
}
+2 -2
View File
@@ -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<void> {
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<void> {
try {
const descendentProcesses = await getDescendentProcesses(process.pid)
for (const pid of descendentProcesses) {
+1 -1
View File
@@ -1,6 +1,6 @@
import chalk from 'chalk'
export function formatUnknownOptionsError (unknownOptions: Map<string, string[]>) {
export function formatUnknownOptionsError (unknownOptions: Map<string, string[]>): string {
let output = chalk.bgRed.black('\u2009ERROR\u2009')
const unknownOptionsArray = Array.from(unknownOptions.keys())
if (unknownOptionsArray.length > 1) {
+2 -2
View File
@@ -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<void> {
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) {
+2 -2
View File
@@ -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<ParsedCliArgs> {
return parseCliArgsLib({
fallbackCommand: 'run',
escapeArgs: ['create', 'dlx', 'exec'],
+2 -2
View File
@@ -55,7 +55,7 @@ const argv = process.argv.slice(2)
}
})()
async function runPnpm () {
async function runPnpm (): Promise<void> {
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<void> {
const { runNpm } = await import('./runNpm')
const { status } = await runNpm(argv)
process.exit(status!)
+1 -1
View File
@@ -11,7 +11,7 @@ export function initReporter (
cmd: string | null
config: Config
}
) {
): void {
switch (reporterType) {
case 'default':
initDefaultReporter({
+1 -1
View File
@@ -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
+2 -1
View File
@@ -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<SpawnSyncReturns<Buffer>> {
const { config } = await getConfig({
cliOptions: {},
packageManager,
+1 -3
View File
@@ -1,5 +1,4 @@
// eslint-disable
export const shorthands = {
export const shorthands: Record<string, string> = {
s: '--reporter=silent',
d: '--loglevel=info',
dd: '--loglevel=verbose',
@@ -33,4 +32,3 @@ export const shorthands = {
i: '--interactive',
F: '--filter',
}
// eslint-enable