refactor: add createResolver() to @pnpm/client

This commit is contained in:
Zoltan Kochan
2020-08-20 00:44:35 +03:00
parent a1cdae3dc7
commit 855f8b00a6
10 changed files with 46 additions and 56 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/client": minor
---
A new function created for just creating a resolver: `createResolver(opts: ClientOptions)`.

View File

@@ -1,4 +1,7 @@
import createResolve, { ResolverFactoryOptions } from '@pnpm/default-resolver' import createResolve, {
ResolveFunction,
ResolverFactoryOptions,
} from '@pnpm/default-resolver'
import { AgentOptions, createFetchFromRegistry } from '@pnpm/fetch' import { AgentOptions, createFetchFromRegistry } from '@pnpm/fetch'
import { FetchFromRegistry, GetCredentials, RetryTimeoutOptions } from '@pnpm/fetching-types' import { FetchFromRegistry, GetCredentials, RetryTimeoutOptions } from '@pnpm/fetching-types'
import fetchFromGit from '@pnpm/git-fetcher' import fetchFromGit from '@pnpm/git-fetcher'
@@ -6,11 +9,15 @@ import createTarballFetcher from '@pnpm/tarball-fetcher'
import getCredentialsByURI = require('credentials-by-uri') import getCredentialsByURI = require('credentials-by-uri')
import mem = require('mem') import mem = require('mem')
export default function (opts: { export { ResolveFunction }
export type ClientOptions = {
authConfig: Record<string, string>, authConfig: Record<string, string>,
retry?: RetryTimeoutOptions, retry?: RetryTimeoutOptions,
userAgent?: string, userAgent?: string,
} & ResolverFactoryOptions & AgentOptions) { } & ResolverFactoryOptions & AgentOptions
export default function (opts: ClientOptions) {
const fetchFromRegistry = createFetchFromRegistry(opts) const fetchFromRegistry = createFetchFromRegistry(opts)
const getCredentials = mem((registry: string) => getCredentialsByURI(opts.authConfig, registry)) const getCredentials = mem((registry: string) => getCredentialsByURI(opts.authConfig, registry))
return { return {
@@ -19,6 +26,12 @@ export default function (opts: {
} }
} }
export function createResolver (opts: ClientOptions) {
const fetchFromRegistry = createFetchFromRegistry(opts)
const getCredentials = mem((registry: string) => getCredentialsByURI(opts.authConfig, registry))
return createResolve(fetchFromRegistry, getCredentials, opts)
}
function createFetchers ( function createFetchers (
fetchFromRegistry: FetchFromRegistry, fetchFromRegistry: FetchFromRegistry,
getCredentials: GetCredentials, getCredentials: GetCredentials,

View File

@@ -35,10 +35,9 @@
"@pnpm/logger": ">=3.1.0" "@pnpm/logger": ">=3.1.0"
}, },
"dependencies": { "dependencies": {
"@pnpm/client": "workspace:^1.0.7",
"@pnpm/constants": "workspace:4.0.0", "@pnpm/constants": "workspace:4.0.0",
"@pnpm/default-resolver": "workspace:10.0.7",
"@pnpm/error": "workspace:1.3.0", "@pnpm/error": "workspace:1.3.0",
"@pnpm/fetch": "workspace:^2.1.3",
"@pnpm/lockfile-file": "workspace:3.0.12", "@pnpm/lockfile-file": "workspace:3.0.12",
"@pnpm/lockfile-utils": "workspace:2.0.16", "@pnpm/lockfile-utils": "workspace:2.0.16",
"@pnpm/manifest-utils": "workspace:1.0.3", "@pnpm/manifest-utils": "workspace:1.0.3",
@@ -47,9 +46,7 @@
"@pnpm/pick-registry-for-package": "workspace:1.0.3", "@pnpm/pick-registry-for-package": "workspace:1.0.3",
"@pnpm/store-path": "^4.0.2", "@pnpm/store-path": "^4.0.2",
"@pnpm/types": "workspace:6.2.0", "@pnpm/types": "workspace:6.2.0",
"credentials-by-uri": "^2.0.0",
"dependency-path": "workspace:5.0.3", "dependency-path": "workspace:5.0.3",
"mem": "^6.1.0",
"ramda": "^0.27.1", "ramda": "^0.27.1",
"semver": "^7.3.2" "semver": "^7.3.2"
}, },

View File

@@ -1,9 +1,10 @@
import createResolver, { ResolveFunction, ResolverFactoryOptions } from '@pnpm/default-resolver' import {
import { createFetchFromRegistry } from '@pnpm/fetch' ClientOptions,
createResolver,
ResolveFunction,
} from '@pnpm/client'
import pickRegistryForPackage from '@pnpm/pick-registry-for-package' import pickRegistryForPackage from '@pnpm/pick-registry-for-package'
import { DependencyManifest, Registries } from '@pnpm/types' import { DependencyManifest, Registries } from '@pnpm/types'
import getCredentialsByURI = require('credentials-by-uri')
import mem = require('mem')
type GetManifestOpts = { type GetManifestOpts = {
dir: string, dir: string,
@@ -12,14 +13,14 @@ type GetManifestOpts = {
registries: Registries, registries: Registries,
} }
export type ManifestGetterOptions = ResolverFactoryOptions & GetManifestOpts export type ManifestGetterOptions = Omit<ClientOptions, 'authConfig'>
& GetManifestOpts
& { rawConfig: Record<string, string> }
export function createManifestGetter ( export function createManifestGetter (
opts: ManifestGetterOptions opts: ManifestGetterOptions
): (packageName: string, pref: string) => Promise<DependencyManifest | null> { ): (packageName: string, pref: string) => Promise<DependencyManifest | null> {
const fetch = createFetchFromRegistry(opts) const resolve = createResolver({ ...opts, authConfig: opts.rawConfig })
const getCredentials = mem((registry: string) => getCredentialsByURI(opts.rawConfig, registry))
const resolve = createResolver(fetch, getCredentials, opts)
return getManifest.bind(null, resolve, opts) return getManifest.bind(null, resolve, opts)
} }

View File

@@ -1,4 +1,4 @@
import { ResolveFunction } from '@pnpm/default-resolver' import { ResolveFunction } from '@pnpm/client'
import test = require('tape') import test = require('tape')
import { getManifest } from '../lib/createManifestGetter' import { getManifest } from '../lib/createManifestGetter'

View File

@@ -9,18 +9,15 @@
"../../typings/**/*.d.ts" "../../typings/**/*.d.ts"
], ],
"references": [ "references": [
{
"path": "../client"
},
{ {
"path": "../constants" "path": "../constants"
}, },
{
"path": "../default-resolver"
},
{ {
"path": "../error" "path": "../error"
}, },
{
"path": "../fetch"
},
{ {
"path": "../lockfile-file" "path": "../lockfile-file"
}, },

View File

@@ -52,12 +52,11 @@
}, },
"dependencies": { "dependencies": {
"@pnpm/cli-utils": "workspace:0.4.22", "@pnpm/cli-utils": "workspace:0.4.22",
"@pnpm/client": "workspace:^1.0.7",
"@pnpm/config": "workspace:11.2.4", "@pnpm/config": "workspace:11.2.4",
"@pnpm/error": "workspace:1.3.0", "@pnpm/error": "workspace:1.3.0",
"@pnpm/exportable-manifest": "workspace:^1.0.1", "@pnpm/exportable-manifest": "workspace:^1.0.1",
"@pnpm/fetch": "workspace:^2.1.3",
"@pnpm/lifecycle": "workspace:9.2.2", "@pnpm/lifecycle": "workspace:9.2.2",
"@pnpm/npm-resolver": "workspace:9.1.0",
"@pnpm/pick-registry-for-package": "workspace:1.0.3", "@pnpm/pick-registry-for-package": "workspace:1.0.3",
"@pnpm/resolver-base": "workspace:7.0.3", "@pnpm/resolver-base": "workspace:7.0.3",
"@pnpm/run-npm": "workspace:2.0.3", "@pnpm/run-npm": "workspace:2.0.3",
@@ -66,10 +65,8 @@
"@pnpm/types": "workspace:6.2.0", "@pnpm/types": "workspace:6.2.0",
"@zkochan/rimraf": "^1.0.0", "@zkochan/rimraf": "^1.0.0",
"cp-file": "^9.0.0", "cp-file": "^9.0.0",
"credentials-by-uri": "^2.0.0",
"enquirer": "^2.3.6", "enquirer": "^2.3.6",
"fast-glob": "^3.2.4", "fast-glob": "^3.2.4",
"mem": "^6.1.0",
"mz": "^2.7.0", "mz": "^2.7.0",
"p-filter": "^2.1.0", "p-filter": "^2.1.0",
"ramda": "^0.27.1", "ramda": "^0.27.1",

View File

@@ -1,14 +1,10 @@
import { createResolver } from '@pnpm/client'
import { Config } from '@pnpm/config' import { Config } from '@pnpm/config'
import { createFetchFromRegistry } from '@pnpm/fetch'
import createResolver from '@pnpm/npm-resolver'
import pickRegistryForPackage from '@pnpm/pick-registry-for-package' import pickRegistryForPackage from '@pnpm/pick-registry-for-package'
import { ResolveFunction } from '@pnpm/resolver-base' import { ResolveFunction } from '@pnpm/resolver-base'
import runNpm from '@pnpm/run-npm'
import sortPackages from '@pnpm/sort-packages' import sortPackages from '@pnpm/sort-packages'
import storePath from '@pnpm/store-path' import storePath from '@pnpm/store-path'
import { Registries } from '@pnpm/types' import { Registries } from '@pnpm/types'
import getCredentialsByURI = require('credentials-by-uri')
import mem = require('mem')
import pFilter = require('p-filter') import pFilter = require('p-filter')
import { handler as publish } from './publish' import { handler as publish } from './publish'
@@ -54,9 +50,8 @@ export default async function (
) { ) {
const pkgs = Object.values(opts.selectedProjectsGraph).map((wsPkg) => wsPkg.package) const pkgs = Object.values(opts.selectedProjectsGraph).map((wsPkg) => wsPkg.package)
const storeDir = await storePath(opts.workspaceDir, opts.storeDir) const storeDir = await storePath(opts.workspaceDir, opts.storeDir)
const fetch = createFetchFromRegistry(opts) const resolve = createResolver(Object.assign(opts, {
const getCredentials = mem((registry: string) => getCredentialsByURI(opts.rawConfig, registry)) authConfig: opts.rawConfig,
const resolve = createResolver(fetch, getCredentials, Object.assign(opts, {
storeDir, storeDir,
})) as unknown as ResolveFunction })) as unknown as ResolveFunction
const pkgsToPublish = await pFilter(pkgs, async (pkg) => { const pkgsToPublish = await pFilter(pkgs, async (pkg) => {

View File

@@ -12,6 +12,9 @@
{ {
"path": "../cli-utils" "path": "../cli-utils"
}, },
{
"path": "../client"
},
{ {
"path": "../config" "path": "../config"
}, },
@@ -21,15 +24,9 @@
{ {
"path": "../exportable-manifest" "path": "../exportable-manifest"
}, },
{
"path": "../fetch"
},
{ {
"path": "../lifecycle" "path": "../lifecycle"
}, },
{
"path": "../npm-resolver"
},
{ {
"path": "../pick-registry-for-package" "path": "../pick-registry-for-package"
}, },

20
pnpm-lock.yaml generated
View File

@@ -1223,10 +1223,9 @@ importers:
version-selector-type: ^3.0.0 version-selector-type: ^3.0.0
packages/outdated: packages/outdated:
dependencies: dependencies:
'@pnpm/client': 'link:../client'
'@pnpm/constants': 'link:../constants' '@pnpm/constants': 'link:../constants'
'@pnpm/default-resolver': 'link:../default-resolver'
'@pnpm/error': 'link:../error' '@pnpm/error': 'link:../error'
'@pnpm/fetch': 'link:../fetch'
'@pnpm/lockfile-file': 'link:../lockfile-file' '@pnpm/lockfile-file': 'link:../lockfile-file'
'@pnpm/lockfile-utils': 'link:../lockfile-utils' '@pnpm/lockfile-utils': 'link:../lockfile-utils'
'@pnpm/manifest-utils': 'link:../manifest-utils' '@pnpm/manifest-utils': 'link:../manifest-utils'
@@ -1235,9 +1234,7 @@ importers:
'@pnpm/pick-registry-for-package': 'link:../pick-registry-for-package' '@pnpm/pick-registry-for-package': 'link:../pick-registry-for-package'
'@pnpm/store-path': 4.0.2 '@pnpm/store-path': 4.0.2
'@pnpm/types': 'link:../types' '@pnpm/types': 'link:../types'
credentials-by-uri: 2.0.0
dependency-path: 'link:../dependency-path' dependency-path: 'link:../dependency-path'
mem: 6.1.0
ramda: 0.27.1 ramda: 0.27.1
semver: 7.3.2 semver: 7.3.2
devDependencies: devDependencies:
@@ -1247,10 +1244,9 @@ importers:
'@types/semver': 7.3.1 '@types/semver': 7.3.1
npm-run-all: 4.1.5 npm-run-all: 4.1.5
specifiers: specifiers:
'@pnpm/client': 'workspace:^1.0.7'
'@pnpm/constants': 'workspace:4.0.0' '@pnpm/constants': 'workspace:4.0.0'
'@pnpm/default-resolver': 'workspace:10.0.7'
'@pnpm/error': 'workspace:1.3.0' '@pnpm/error': 'workspace:1.3.0'
'@pnpm/fetch': 'workspace:^2.1.3'
'@pnpm/lockfile-file': 'workspace:3.0.12' '@pnpm/lockfile-file': 'workspace:3.0.12'
'@pnpm/lockfile-utils': 'workspace:2.0.16' '@pnpm/lockfile-utils': 'workspace:2.0.16'
'@pnpm/logger': ^3.2.2 '@pnpm/logger': ^3.2.2
@@ -1263,9 +1259,7 @@ importers:
'@pnpm/types': 'workspace:6.2.0' '@pnpm/types': 'workspace:6.2.0'
'@types/ramda': ^0.27.14 '@types/ramda': ^0.27.14
'@types/semver': ^7.3.1 '@types/semver': ^7.3.1
credentials-by-uri: ^2.0.0
dependency-path: 'workspace:5.0.3' dependency-path: 'workspace:5.0.3'
mem: ^6.1.0
npm-run-all: ^4.1.5 npm-run-all: ^4.1.5
ramda: ^0.27.1 ramda: ^0.27.1
semver: ^7.3.2 semver: ^7.3.2
@@ -1790,12 +1784,11 @@ importers:
packages/plugin-commands-publishing: packages/plugin-commands-publishing:
dependencies: dependencies:
'@pnpm/cli-utils': 'link:../cli-utils' '@pnpm/cli-utils': 'link:../cli-utils'
'@pnpm/client': 'link:../client'
'@pnpm/config': 'link:../config' '@pnpm/config': 'link:../config'
'@pnpm/error': 'link:../error' '@pnpm/error': 'link:../error'
'@pnpm/exportable-manifest': 'link:../exportable-manifest' '@pnpm/exportable-manifest': 'link:../exportable-manifest'
'@pnpm/fetch': 'link:../fetch'
'@pnpm/lifecycle': 'link:../lifecycle' '@pnpm/lifecycle': 'link:../lifecycle'
'@pnpm/npm-resolver': 'link:../npm-resolver'
'@pnpm/pick-registry-for-package': 'link:../pick-registry-for-package' '@pnpm/pick-registry-for-package': 'link:../pick-registry-for-package'
'@pnpm/resolver-base': 'link:../resolver-base' '@pnpm/resolver-base': 'link:../resolver-base'
'@pnpm/run-npm': 'link:../run-npm' '@pnpm/run-npm': 'link:../run-npm'
@@ -1804,10 +1797,8 @@ importers:
'@pnpm/types': 'link:../types' '@pnpm/types': 'link:../types'
'@zkochan/rimraf': 1.0.0 '@zkochan/rimraf': 1.0.0
cp-file: 9.0.0 cp-file: 9.0.0
credentials-by-uri: 2.0.0
enquirer: 2.3.6 enquirer: 2.3.6
fast-glob: 3.2.4 fast-glob: 3.2.4
mem: 6.1.0
mz: 2.7.0 mz: 2.7.0
p-filter: 2.1.0 p-filter: 2.1.0
ramda: 0.27.1 ramda: 0.27.1
@@ -1832,13 +1823,12 @@ importers:
write-yaml-file: 4.1.0 write-yaml-file: 4.1.0
specifiers: specifiers:
'@pnpm/cli-utils': 'workspace:0.4.22' '@pnpm/cli-utils': 'workspace:0.4.22'
'@pnpm/client': 'workspace:^1.0.7'
'@pnpm/config': 'workspace:11.2.4' '@pnpm/config': 'workspace:11.2.4'
'@pnpm/error': 'workspace:1.3.0' '@pnpm/error': 'workspace:1.3.0'
'@pnpm/exportable-manifest': 'workspace:^1.0.1' '@pnpm/exportable-manifest': 'workspace:^1.0.1'
'@pnpm/fetch': 'workspace:^2.1.3'
'@pnpm/filter-workspace-packages': 'workspace:2.1.15' '@pnpm/filter-workspace-packages': 'workspace:2.1.15'
'@pnpm/lifecycle': 'workspace:9.2.2' '@pnpm/lifecycle': 'workspace:9.2.2'
'@pnpm/npm-resolver': 'workspace:9.1.0'
'@pnpm/pick-registry-for-package': 'workspace:1.0.3' '@pnpm/pick-registry-for-package': 'workspace:1.0.3'
'@pnpm/plugin-commands-publishing': 'link:' '@pnpm/plugin-commands-publishing': 'link:'
'@pnpm/prepare': 'workspace:0.0.9' '@pnpm/prepare': 'workspace:0.0.9'
@@ -1854,12 +1844,10 @@ importers:
'@types/sinon': ^9.0.4 '@types/sinon': ^9.0.4
'@zkochan/rimraf': ^1.0.0 '@zkochan/rimraf': ^1.0.0
cp-file: ^9.0.0 cp-file: ^9.0.0
credentials-by-uri: ^2.0.0
cross-spawn: ^7.0.3 cross-spawn: ^7.0.3
enquirer: ^2.3.6 enquirer: ^2.3.6
execa: ^4.0.3 execa: ^4.0.3
fast-glob: ^3.2.4 fast-glob: ^3.2.4
mem: ^6.1.0
mz: ^2.7.0 mz: ^2.7.0
p-filter: ^2.1.0 p-filter: ^2.1.0
path-exists: ^4.0.0 path-exists: ^4.0.0