From c17263c62ac8928434fe8efed97d76048c1a1f62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kh=E1=BA=A3i?= Date: Fri, 12 Apr 2024 05:32:48 +0700 Subject: [PATCH] refactor(fetching): return type annotations (#7903) --- fetching/directory-fetcher/src/index.ts | 24 ++++++++++++------- fetching/git-fetcher/src/index.ts | 6 ++--- fetching/pick-fetcher/src/index.ts | 6 ++--- fetching/tarball-fetcher/src/index.ts | 3 ++- .../src/localTarballFetcher.ts | 2 +- 5 files changed, 25 insertions(+), 16 deletions(-) diff --git a/fetching/directory-fetcher/src/index.ts b/fetching/directory-fetcher/src/index.ts index d3a7305887..8ba8172da1 100644 --- a/fetching/directory-fetcher/src/index.ts +++ b/fetching/directory-fetcher/src/index.ts @@ -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 +interface FetchResult { + local: true + filesIndex: Record + packageImportMethod: 'hardlink' + manifest: DependencyManifest + requiresBuild: boolean +} + export async function fetchFromDir ( dir: string, opts: FetchFromDirOpts & CreateDirectoryFetcherOptions -) { +): Promise { if (opts.includeOnlyPackageFiles) { return fetchPackageFilesFromDir(dir) } @@ -47,7 +55,7 @@ export async function fetchFromDir ( async function fetchAllFilesFromDir ( readFileStat: ReadFileStat, dir: string -) { +): Promise { 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 { const files = await packlist(dir) const filesIndex: Record = 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, } diff --git a/fetching/git-fetcher/src/index.ts b/fetching/git-fetcher/src/index.ts index 96b4a88776..b5fbf0d72f 100644 --- a/fetching/git-fetcher/src/index.ts +++ b/fetching/git-fetcher/src/index.ts @@ -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 { const fullArgs = prefixGitArgs().concat(args || []) - return execa('git', fullArgs, opts) + await execa('git', fullArgs, opts) } diff --git a/fetching/pick-fetcher/src/index.ts b/fetching/pick-fetcher/src/index.ts index 417671f7b1..46592fe961 100644 --- a/fetching/pick-fetcher/src/index.ts +++ b/fetching/pick-fetcher/src/index.ts @@ -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, resolution: Resolution) { +export function pickFetcher (fetcherByHostingType: Partial, resolution: Resolution): FetchFunction | DirectoryFetcher | GitFetcher { let fetcherType = resolution.type if (resolution.type == null) { @@ -23,7 +23,7 @@ export function pickFetcher (fetcherByHostingType: Partial, 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/') || diff --git a/fetching/tarball-fetcher/src/index.ts b/fetching/tarball-fetcher/src/index.ts index 7e141b87bc..efc10346d4 100644 --- a/fetching/tarball-fetcher/src/index.ts +++ b/fetching/tarball-fetcher/src/index.ts @@ -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 { 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}.`) diff --git a/fetching/tarball-fetcher/src/localTarballFetcher.ts b/fetching/tarball-fetcher/src/localTarballFetcher.ts index 798ffd2c2f..b33a0aa245 100644 --- a/fetching/tarball-fetcher/src/localTarballFetcher.ts +++ b/fetching/tarball-fetcher/src/localTarballFetcher.ts @@ -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) }