refactor(fetching): return type annotations (#7903)

This commit is contained in:
Khải
2024-04-12 05:32:48 +07:00
committed by GitHub
parent 9afef40eba
commit c17263c62a
5 changed files with 25 additions and 16 deletions

View File

@@ -17,7 +17,7 @@ export interface CreateDirectoryFetcherOptions {
export function createDirectoryFetcher (
opts?: CreateDirectoryFetcherOptions
) {
): { directory: DirectoryFetcher } {
const readFileStat: ReadFileStat = opts?.resolveSymlinks === true ? realFileStat : fileStat
const fetchFromDir = opts?.includeOnlyPackageFiles ? fetchPackageFilesFromDir : fetchAllFilesFromDir.bind(null, readFileStat)
@@ -33,10 +33,18 @@ export function createDirectoryFetcher (
type FetchFromDirOpts = Omit<DirectoryFetcherOptions, 'lockfileDir'>
interface FetchResult {
local: true
filesIndex: Record<string, string>
packageImportMethod: 'hardlink'
manifest: DependencyManifest
requiresBuild: boolean
}
export async function fetchFromDir (
dir: string,
opts: FetchFromDirOpts & CreateDirectoryFetcherOptions
) {
): Promise<FetchResult> {
if (opts.includeOnlyPackageFiles) {
return fetchPackageFilesFromDir(dir)
}
@@ -47,7 +55,7 @@ export async function fetchFromDir (
async function fetchAllFilesFromDir (
readFileStat: ReadFileStat,
dir: string
) {
): Promise<FetchResult> {
const filesIndex = await _fetchAllFilesFromDir(readFileStat, dir)
// In a regular pnpm workspace it will probably never happen that a dependency has no package.json file.
// Safe read was added to support the Bit workspace in which the components have no package.json files.
@@ -55,9 +63,9 @@ async function fetchAllFilesFromDir (
const manifest = await safeReadProjectManifestOnly(dir) as DependencyManifest ?? undefined
const requiresBuild = pkgRequiresBuild(manifest, filesIndex)
return {
local: true as const,
local: true,
filesIndex,
packageImportMethod: 'hardlink' as const,
packageImportMethod: 'hardlink',
manifest,
requiresBuild,
}
@@ -124,7 +132,7 @@ async function fileStat (filePath: string): Promise<{ filePath: string, stat: St
}
}
async function fetchPackageFilesFromDir (dir: string) {
async function fetchPackageFilesFromDir (dir: string): Promise<FetchResult> {
const files = await packlist(dir)
const filesIndex: Record<string, string> = Object.fromEntries(files.map((file) => [file, path.join(dir, file)]))
// In a regular pnpm workspace it will probably never happen that a dependency has no package.json file.
@@ -133,9 +141,9 @@ async function fetchPackageFilesFromDir (dir: string) {
const manifest = await safeReadProjectManifestOnly(dir) as DependencyManifest ?? undefined
const requiresBuild = pkgRequiresBuild(manifest, filesIndex)
return {
local: true as const,
local: true,
filesIndex,
packageImportMethod: 'hardlink' as const,
packageImportMethod: 'hardlink',
manifest,
requiresBuild,
}

View File

@@ -17,7 +17,7 @@ export interface CreateGitFetcherOptions {
ignoreScripts?: boolean
}
export function createGitFetcher (createOpts: CreateGitFetcherOptions) {
export function createGitFetcher (createOpts: CreateGitFetcherOptions): { git: GitFetcher } {
const allowedHosts = new Set(createOpts?.gitShallowHosts ?? [])
const ignoreScripts = createOpts.ignoreScripts ?? false
const preparePkg = preparePackage.bind(null, {
@@ -85,7 +85,7 @@ function prefixGitArgs (): string[] {
return process.platform === 'win32' ? ['-c', 'core.longpaths=true'] : []
}
function execGit (args: string[], opts?: object) {
async function execGit (args: string[], opts?: object): Promise<void> {
const fullArgs = prefixGitArgs().concat(args || [])
return execa('git', fullArgs, opts)
await execa('git', fullArgs, opts)
}

View File

@@ -1,7 +1,7 @@
import type { Resolution } from '@pnpm/resolver-base'
import type { Fetchers } from '@pnpm/fetcher-base'
import type { Fetchers, FetchFunction, DirectoryFetcher, GitFetcher } from '@pnpm/fetcher-base'
export function pickFetcher (fetcherByHostingType: Partial<Fetchers>, resolution: Resolution) {
export function pickFetcher (fetcherByHostingType: Partial<Fetchers>, resolution: Resolution): FetchFunction | DirectoryFetcher | GitFetcher {
let fetcherType = resolution.type
if (resolution.type == null) {
@@ -23,7 +23,7 @@ export function pickFetcher (fetcherByHostingType: Partial<Fetchers>, resolution
return fetch
}
export function isGitHostedPkgUrl (url: string) {
export function isGitHostedPkgUrl (url: string): boolean {
return (
url.startsWith('https://codeload.github.com/') ||
url.startsWith('https://bitbucket.org/') ||

View File

@@ -2,6 +2,7 @@ import { PnpmError } from '@pnpm/error'
import {
type FetchFunction,
type FetchOptions,
type FetchResult,
} from '@pnpm/fetcher-base'
import type { Cafs } from '@pnpm/cafs-types'
import {
@@ -70,7 +71,7 @@ async function fetchFromTarball (
tarball: string
},
opts: FetchOptions
) {
): Promise<FetchResult> {
if (ctx.offline) {
throw new PnpmError('NO_OFFLINE_TARBALL',
`A package is missing from the store but cannot download it in offline mode. The missing package may be downloaded from ${resolution.tarball}.`)

View File

@@ -30,7 +30,7 @@ export function createLocalTarballFetcher (): FetchFunction {
return fetch as FetchFunction
}
function resolvePath (where: string, spec: string) {
function resolvePath (where: string, spec: string): string {
if (isAbsolutePath.test(spec)) return spec
return path.resolve(where, spec)
}