From e6b8d01b659342268dd7cdfbee6f78c2fe50ac8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kh=E1=BA=A3i?= Date: Wed, 10 Apr 2024 16:25:13 +0700 Subject: [PATCH] refactor(releasing): return type annotations (#7882) --- .../plugin-commands-deploy/src/deploy.ts | 10 ++++----- .../plugin-commands-deploy/src/deployHook.ts | 6 +++--- .../plugin-commands-publishing/src/pack.ts | 12 +++++------ .../plugin-commands-publishing/src/publish.ts | 21 ++++++++++++------- .../src/recursivePublish.ts | 2 +- 5 files changed, 28 insertions(+), 23 deletions(-) diff --git a/releasing/plugin-commands-deploy/src/deploy.ts b/releasing/plugin-commands-deploy/src/deploy.ts index bb55fdfe74..eff7d4af54 100644 --- a/releasing/plugin-commands-deploy/src/deploy.ts +++ b/releasing/plugin-commands-deploy/src/deploy.ts @@ -14,17 +14,17 @@ import { logger } from '@pnpm/logger' export const shorthands = install.shorthands -export function rcOptionsTypes () { +export function rcOptionsTypes (): Record { return install.rcOptionsTypes() } -export function cliOptionsTypes () { +export function cliOptionsTypes (): Record { return install.cliOptionsTypes() } export const commandNames = ['deploy'] -export function help () { +export function help (): string { return renderHelp({ description: 'Experimental! Deploy a package from a workspace', url: docsUrl('deploy'), @@ -57,7 +57,7 @@ export function help () { export async function handler ( opts: install.InstallCommandOptions, params: string[] -) { +): Promise { if (!opts.workspaceDir) { throw new PnpmError('CANNOT_DEPLOY', 'A deploy is only possible from inside a workspace') } @@ -117,7 +117,7 @@ export async function handler ( }) } -async function copyProject (src: string, dest: string, opts: { includeOnlyPackageFiles: boolean }) { +async function copyProject (src: string, dest: string, opts: { includeOnlyPackageFiles: boolean }): Promise { const { filesIndex } = await fetchFromDir(src, opts) const importPkg = createIndexedPkgImporter('clone-or-copy') importPkg(dest, { filesMap: filesIndex, force: true, resolvedFrom: 'local-dir' }) diff --git a/releasing/plugin-commands-deploy/src/deployHook.ts b/releasing/plugin-commands-deploy/src/deployHook.ts index b1dcb4591a..490311db1a 100644 --- a/releasing/plugin-commands-deploy/src/deployHook.ts +++ b/releasing/plugin-commands-deploy/src/deployHook.ts @@ -1,7 +1,7 @@ -import { DEPENDENCIES_FIELDS } from '@pnpm/types' +import { type ProjectManifest, DEPENDENCIES_FIELDS } from '@pnpm/types' -export function deployHook (pkg: any) { // eslint-disable-line - pkg.dependenciesMeta = pkg.dependenciesMeta || {} +export function deployHook (pkg: ProjectManifest): ProjectManifest { + pkg.dependenciesMeta = pkg.dependenciesMeta ?? {} for (const depField of DEPENDENCIES_FIELDS) { for (const [depName, depVersion] of Object.entries(pkg[depField] ?? {})) { if ((depVersion as string).startsWith('workspace:')) { diff --git a/releasing/plugin-commands-publishing/src/pack.ts b/releasing/plugin-commands-publishing/src/pack.ts index 6bad31368f..548c32eec7 100644 --- a/releasing/plugin-commands-publishing/src/pack.ts +++ b/releasing/plugin-commands-publishing/src/pack.ts @@ -18,7 +18,7 @@ import { runScriptsIfPresent } from './publish' const LICENSE_GLOB = 'LICEN{S,C}E{,.*}' // cspell:disable-line const findLicenses = fg.bind(fg, [LICENSE_GLOB]) as (opts: { cwd: string }) => Promise -export function rcOptionsTypes () { +export function rcOptionsTypes (): Record { return { ...cliOptionsTypes(), ...pick([ @@ -27,7 +27,7 @@ export function rcOptionsTypes () { } } -export function cliOptionsTypes () { +export function cliOptionsTypes (): Record { return { 'pack-destination': String, ...pick([ @@ -38,7 +38,7 @@ export function cliOptionsTypes () { export const commandNames = ['pack'] -export function help () { +export function help (): string { return renderHelp({ description: 'Create a tarball from a package', usages: ['pnpm pack'], @@ -66,7 +66,7 @@ export async function handler ( packDestination?: string workspaceDir?: string } -) { +): Promise { const { manifest: entryManifest, fileName: manifestFileName } = await readProjectManifest(opts.dir, opts) const _runScriptsIfPresent = runScriptsIfPresent.bind(null, { depPath: opts.dir, @@ -140,7 +140,7 @@ export async function handler ( return path.relative(opts.dir, path.join(dir, tarballName)) } -async function readReadmeFile (projectDir: string) { +async function readReadmeFile (projectDir: string): Promise { const files = await fs.promises.readdir(projectDir) const readmePath = files.find(name => /readme\.md$/i.test(name)) const readmeFile = readmePath ? await fs.promises.readFile(path.join(projectDir, readmePath), 'utf8') : undefined @@ -188,7 +188,7 @@ async function createPublishManifest (opts: { embedReadme?: boolean modulesDir: string manifest: ProjectManifest -}) { +}): Promise { const { projectDir, embedReadme, modulesDir, manifest } = opts const readmeFile = embedReadme ? await readReadmeFile(projectDir) : undefined return createExportableManifest(projectDir, manifest, { readmeFile, modulesDir }) diff --git a/releasing/plugin-commands-publishing/src/publish.ts b/releasing/plugin-commands-publishing/src/publish.ts index 34eeda3bca..1e77ea9022 100644 --- a/releasing/plugin-commands-publishing/src/publish.ts +++ b/releasing/plugin-commands-publishing/src/publish.ts @@ -18,7 +18,7 @@ import tempy from 'tempy' import * as pack from './pack' import { recursivePublish, type PublishRecursiveOpts } from './recursivePublish' -export function rcOptionsTypes () { +export function rcOptionsTypes (): Record { return pick([ 'access', 'git-checks', @@ -34,7 +34,7 @@ export function rcOptionsTypes () { ], allTypes) } -export function cliOptionsTypes () { +export function cliOptionsTypes (): Record { return { ...rcOptionsTypes(), 'dry-run': Boolean, @@ -47,7 +47,7 @@ export function cliOptionsTypes () { export const commandNames = ['publish'] -export function help () { +export function help (): string { return renderHelp({ description: 'Publishes a package to the npm registry.', descriptionLists: [ @@ -121,12 +121,17 @@ export async function handler ( workspaceDir?: string } & Pick, params: string[] -) { +): Promise<{ exitCode?: number } | undefined> { const result = await publish(opts, params) if (result?.manifest) return return result } +export interface PublishResult { + exitCode?: number + manifest?: ProjectManifest +} + export async function publish ( opts: Omit & { argv: { @@ -137,7 +142,7 @@ export async function publish ( workspaceDir?: string } & Pick, params: string[] -) { +): Promise { if (opts.gitChecks !== false && await isGitRepo()) { if (!(await isWorkingTreeClean())) { throw new PnpmError('GIT_UNCLEAN', 'Unclean working tree. Commit or stash changes first.', { @@ -258,7 +263,7 @@ Do you want to continue?`, * The npm CLI doesn't support token helpers, so we transform the token helper settings * to regular auth token settings that the npm CLI can understand. */ -function getEnvWithTokens (opts: Pick) { +function getEnvWithTokens (opts: Pick): Record { const tokenHelpers = Object.entries(opts.rawConfig).filter(([key]) => key.endsWith(':tokenHelper')) const tokenHelpersFromArgs = opts.argv.original .filter(arg => arg.includes(':tokenHelper=')) @@ -285,7 +290,7 @@ async function copyNpmrc ( workspaceDir?: string packDestination: string } -) { +): Promise { const localNpmrc = path.join(dir, '.npmrc') if (existsSync(localNpmrc)) { await fs.copyFile(localNpmrc, path.join(packDestination, '.npmrc')) @@ -302,7 +307,7 @@ export async function runScriptsIfPresent ( opts: RunLifecycleHookOptions, scriptNames: string[], manifest: ProjectManifest -) { +): Promise { for (const scriptName of scriptNames) { if (!manifest.scripts?.[scriptName]) continue await runLifecycleHook(scriptName, manifest, opts) // eslint-disable-line no-await-in-loop diff --git a/releasing/plugin-commands-publishing/src/recursivePublish.ts b/releasing/plugin-commands-publishing/src/recursivePublish.ts index 373c84e00f..67f9b47964 100644 --- a/releasing/plugin-commands-publishing/src/recursivePublish.ts +++ b/releasing/plugin-commands-publishing/src/recursivePublish.ts @@ -149,7 +149,7 @@ async function isAlreadyPublished ( }, pkgName: string, pkgVersion: string -) { +): Promise { try { await opts.resolve({ alias: pkgName, pref: pkgVersion }, { lockfileDir: opts.lockfileDir,