Skip to content

Commit

Permalink
refactor: resolve-dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Nov 20, 2021
1 parent 97da485 commit 9ba90c8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 27 deletions.
11 changes: 9 additions & 2 deletions packages/core/src/install/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,14 @@ const _installInContext: InstallFunction = async (projects, ctx, opts) => {
}), {})
}

let allowBuild: undefined | ((pkgName: string) => boolean)
if (opts.neverBuiltDependencies != null && opts.neverBuiltDependencies.length > 0) {
const neverBuiltDependencies = new Set(opts.neverBuiltDependencies)
allowBuild = (pkgName) => !neverBuiltDependencies.has(pkgName)
} else if (opts.onlyBuiltDependencies !== false && opts.onlyBuiltDependencies != null) {
const onlyBuiltDependencies = new Set(opts.onlyBuiltDependencies)
allowBuild = (pkgName) => onlyBuiltDependencies.has(pkgName)
}
let {
dependenciesGraph,
dependenciesByProjectId,
Expand All @@ -800,6 +808,7 @@ const _installInContext: InstallFunction = async (projects, ctx, opts) => {
} = await resolveDependencies(
projectsToResolve,
{
allowBuild,
currentLockfile: ctx.currentLockfile,
dryRun: opts.lockfileOnly,
engineStrict: opts.engineStrict,
Expand All @@ -808,8 +817,6 @@ const _installInContext: InstallFunction = async (projects, ctx, opts) => {
hooks: opts.hooks,
linkWorkspacePackagesDepth: opts.linkWorkspacePackagesDepth ?? (opts.saveWorkspaceProtocol ? 0 : -1),
lockfileDir: opts.lockfileDir,
neverBuiltDependencies: new Set(opts.neverBuiltDependencies),
onlyBuiltDependencies: opts.onlyBuiltDependencies ? new Set(opts.onlyBuiltDependencies) : false,
nodeVersion: opts.nodeVersion,
pnpmVersion: opts.packageManager.name === 'pnpm' ? opts.packageManager.version : '',
preferWorkspacePackages: opts.preferWorkspacePackages,
Expand Down
3 changes: 1 addition & 2 deletions packages/resolve-dependencies/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ export default async function (
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
if (
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
opts.neverBuiltDependencies?.has(pkg.name) ||
(opts.onlyBuiltDependencies !== false && !opts.onlyBuiltDependencies.has(pkg.name)) ||
(opts.allowBuild != null && !opts.allowBuild(pkg.name)) ||
(opts.wantedLockfile.packages?.[depPath] == null) ||
pkg.requiresBuild
) continue
Expand Down
25 changes: 6 additions & 19 deletions packages/resolve-dependencies/src/resolveDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export interface ChildrenByParentDepPath {
}

export interface ResolutionContext {
allowBuild?: (pkgName: string) => boolean
updatedSet: Set<string>
defaultTag: string
dryRun: boolean
Expand All @@ -128,8 +129,6 @@ export interface ResolutionContext {
currentLockfile: Lockfile
linkWorkspacePackagesDepth: number
lockfileDir: string
neverBuiltDependencies: Set<string>
onlyBuiltDependencies: false | Set<string>
storeController: StoreController
// the IDs of packages that are not installable
skipped: Set<string>
Expand Down Expand Up @@ -786,12 +785,11 @@ async function resolveDependency (
logFetchResult(pkgResponse, ctx.lockfileDir)

ctx.resolvedPackagesByDepPath[depPath] = getResolvedPackage({
allowBuild: ctx.allowBuild,
dependencyLockfile: currentPkg.dependencyLockfile,
depPath,
force: ctx.force,
hasBin,
neverBuiltDependencies: ctx.neverBuiltDependencies,
onlyBuiltDependencies: ctx.onlyBuiltDependencies,
pkg,
pkgResponse,
prepare,
Expand Down Expand Up @@ -864,12 +862,11 @@ function pkgIsLeaf (pkg: PackageManifest) {

function getResolvedPackage (
options: {
allowBuild?: (pkgName: string) => boolean
dependencyLockfile?: PackageSnapshot
depPath: string
force: boolean
hasBin: boolean
neverBuiltDependencies: Set<string>
onlyBuiltDependencies: false | Set<string>
pkg: PackageManifest
pkgResponse: PackageResponse
prepare: boolean
Expand All @@ -878,19 +875,9 @@ function getResolvedPackage (
) {
const peerDependencies = peerDependenciesWithoutOwn(options.pkg)

let requiresBuild: boolean | undefined
if (options.onlyBuiltDependencies === false || options.onlyBuiltDependencies.has(options.pkg.name)) {
// No onlyBuiltDependencies is specified or this package is explicitly allowed.
if (options.neverBuiltDependencies.has(options.pkg.name)) {
// This package is explicitly listed.
requiresBuild = false
} else {
// default resolution
requiresBuild = options.dependencyLockfile != null ? Boolean(options.dependencyLockfile.requiresBuild) : undefined
}
} else {
requiresBuild = false
}
const requiresBuild = (options.allowBuild == null || options.allowBuild(options.pkg.name))
? ((options.dependencyLockfile != null) ? Boolean(options.dependencyLockfile.requiresBuild) : undefined)
: false

return {
additionalInfo: {
Expand Down
6 changes: 2 additions & 4 deletions packages/resolve-dependencies/src/resolveDependencyTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export interface Importer<T> {
}

export interface ResolveDependenciesOptions {
allowBuild?: (pkgName: string) => boolean
currentLockfile: Lockfile
dryRun: boolean
engineStrict: boolean
Expand All @@ -53,8 +54,6 @@ export interface ResolveDependenciesOptions {
hooks: {
readPackage?: ReadPackageHook
}
neverBuiltDependencies?: Set<string>
onlyBuiltDependencies: false | Set<string>
nodeVersion: string
registries: Registries
pnpmVersion: string
Expand All @@ -77,6 +76,7 @@ export default async function<T> (

const wantedToBeSkippedPackageIds = new Set<string>()
const ctx = {
allowBuild: opts.allowBuild,
childrenByParentDepPath: {} as ChildrenByParentDepPath,
currentLockfile: opts.currentLockfile,
defaultTag: opts.tag,
Expand All @@ -87,8 +87,6 @@ export default async function<T> (
forceFullResolution: opts.forceFullResolution,
linkWorkspacePackagesDepth: opts.linkWorkspacePackagesDepth ?? -1,
lockfileDir: opts.lockfileDir,
neverBuiltDependencies: opts.neverBuiltDependencies ?? new Set(),
onlyBuiltDependencies: opts.onlyBuiltDependencies,
nodeVersion: opts.nodeVersion,
outdatedDependencies: {} as {[pkgId: string]: string},
pendingNodes: [] as PendingNode[],
Expand Down

0 comments on commit 9ba90c8

Please sign in to comment.