Skip to content

Commit

Permalink
getAllPackages: Minor refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Jun 23, 2023
1 parent e66f04f commit 5e46ec0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
17 changes: 10 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,21 @@ const npmInstall = async (

/** Runs the dependency upgrades. Loads the ncurc, finds the package file, and handles --deep. */
async function runUpgrades(options: Options, timeout?: NodeJS.Timeout): Promise<Index<string> | PackageFile | void> {
const [packageInfos, workspacePackages]: [PackageInfo[], string[]] = await getAllPackages(options)
const [selectedPackageInfos, workspacePackages]: [PackageInfo[], string[]] = await getAllPackages(options)

const packageFilepaths: string[] = packageInfos.map((packageInfo: PackageInfo) => packageInfo.filepath)
const packageFilepaths: string[] = selectedPackageInfos.map((packageInfo: PackageInfo) => packageInfo.filepath)

// enable deep mode if --deep, --workspace, --workspaces, or if multiple package files are found
const isWorkspace = options.workspaces || !!options.workspace?.length
options.deep = options.deep || isWorkspace || packageInfos.length > 1
options.deep = options.deep || isWorkspace || selectedPackageInfos.length > 1

let analysis: Index<PackageFile> | PackageFile | void
if (options.global) {
const analysis = await runGlobal(options)
clearTimeout(timeout)
return analysis
} else if (options.deep) {
analysis = await packageInfos.reduce(async (previousPromise, packageInfo: PackageInfo) => {
analysis = await selectedPackageInfos.reduce(async (previousPromise, packageInfo: PackageInfo) => {
const packages = await previousPromise
// copy object to prevent share .ncurc options between different packageFile, to prevent unpredictable behavior
const rcResult = await getNcuRc({ packageFile: packageInfo.filepath, color: options.color })
Expand All @@ -181,7 +181,7 @@ async function runUpgrades(options: Options, timeout?: NodeJS.Timeout): Promise<
...options,
...rcConfig,
packageFile: packageInfo.filepath,
workspacePackages,
workspacePackages: workspacePackages,
}
const { pkgData, pkgFile } = await findPackage(pkgOptions)
return {
Expand All @@ -200,8 +200,11 @@ async function runUpgrades(options: Options, timeout?: NodeJS.Timeout): Promise<
}
} else {
// mutate packageFile when glob pattern finds only single package
if (packageInfos.length === 1 && packageInfos[0].filepath !== (options.packageFile || 'package.json')) {
options.packageFile = packageInfos[0].filepath
if (
selectedPackageInfos.length === 1 &&
selectedPackageInfos[0].filepath !== (options.packageFile || 'package.json')
) {
options.packageFile = selectedPackageInfos[0].filepath
}
const { pkgData, pkgFile } = await findPackage(options)
analysis = await runLocal(options, pkgData, pkgFile)
Expand Down
11 changes: 4 additions & 7 deletions src/lib/getAllPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const readPnpmWorkspaces = async (pkgPath: string): Promise<PnpmWorkspaces | nul
}

/**
* Gets all workspace sub-package information
* Gets all workspace packages information.
*
* @param options the application options, used to determine which packages to return.
* @param defaultPackageFilename the default package filename
Expand Down Expand Up @@ -97,7 +97,7 @@ async function getWorkspacePackageInfos(

// add workspace packages
// --workspace
const filteredWorkspacePackageInfos: PackageInfo[] = allWorkspacePackageInfos.filter((packageInfo: PackageInfo) =>
const selectedWorkspacePackageInfos: PackageInfo[] = allWorkspacePackageInfos.filter((packageInfo: PackageInfo) =>
/* ignore coverage on optional-chaining */
/* c8 ignore next */
options.workspace?.some((workspace: string) =>
Expand All @@ -110,14 +110,11 @@ async function getWorkspacePackageInfos(
),
),
)

return [filteredWorkspacePackageInfos, allWorkspacePackageNames]
return [selectedWorkspacePackageInfos, allWorkspacePackageNames]
}

/**
* Gets all workspace filenames, or just the root workspace package file
*
* NOTE: this has been refactored out of index.ts
* Gets all local packages, including workspaces (depending on -w, -ws, and -root).
*
* @param options the application options, used to determine which packages to return.
* @returns PackageInfo[] an array of all package infos to be considered for updating
Expand Down

0 comments on commit 5e46ec0

Please sign in to comment.