refactor(releasing): return type annotations (#7882)

This commit is contained in:
Khải
2024-04-10 16:25:13 +07:00
committed by GitHub
parent c3cfea0e68
commit e6b8d01b65
5 changed files with 28 additions and 23 deletions

View File

@@ -14,17 +14,17 @@ import { logger } from '@pnpm/logger'
export const shorthands = install.shorthands
export function rcOptionsTypes () {
export function rcOptionsTypes (): Record<string, unknown> {
return install.rcOptionsTypes()
}
export function cliOptionsTypes () {
export function cliOptionsTypes (): Record<string, unknown> {
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<void> {
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<void> {
const { filesIndex } = await fetchFromDir(src, opts)
const importPkg = createIndexedPkgImporter('clone-or-copy')
importPkg(dest, { filesMap: filesIndex, force: true, resolvedFrom: 'local-dir' })

View File

@@ -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:')) {

View File

@@ -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<string[]>
export function rcOptionsTypes () {
export function rcOptionsTypes (): Record<string, unknown> {
return {
...cliOptionsTypes(),
...pick([
@@ -27,7 +27,7 @@ export function rcOptionsTypes () {
}
}
export function cliOptionsTypes () {
export function cliOptionsTypes (): Record<string, unknown> {
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<string> {
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<string | undefined> {
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<ProjectManifest> {
const { projectDir, embedReadme, modulesDir, manifest } = opts
const readmeFile = embedReadme ? await readReadmeFile(projectDir) : undefined
return createExportableManifest(projectDir, manifest, { readmeFile, modulesDir })

View File

@@ -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<string, unknown> {
return pick([
'access',
'git-checks',
@@ -34,7 +34,7 @@ export function rcOptionsTypes () {
], allTypes)
}
export function cliOptionsTypes () {
export function cliOptionsTypes (): Record<string, unknown> {
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<Config, 'allProjects' | 'gitChecks' | 'ignoreScripts' | 'publishBranch' | 'embedReadme'>,
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<PublishRecursiveOpts, 'workspaceDir'> & {
argv: {
@@ -137,7 +142,7 @@ export async function publish (
workspaceDir?: string
} & Pick<Config, 'allProjects' | 'gitChecks' | 'ignoreScripts' | 'publishBranch' | 'embedReadme' | 'packGzipLevel'>,
params: string[]
) {
): Promise<PublishResult> {
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<PublishRecursiveOpts, 'rawConfig' | 'argv'>) {
function getEnvWithTokens (opts: Pick<PublishRecursiveOpts, 'rawConfig' | 'argv'>): Record<string, string> {
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<void> {
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<void> {
for (const scriptName of scriptNames) {
if (!manifest.scripts?.[scriptName]) continue
await runLifecycleHook(scriptName, manifest, opts) // eslint-disable-line no-await-in-loop

View File

@@ -149,7 +149,7 @@ async function isAlreadyPublished (
},
pkgName: string,
pkgVersion: string
) {
): Promise<boolean> {
try {
await opts.resolve({ alias: pkgName, pref: pkgVersion }, {
lockfileDir: opts.lockfileDir,