-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(dep-check): introduce the new configuration schema #1911
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
488abdf
refactor(align-deps): introduce the new configuration schema
tido64 a5c9680
update update-readme script
tido64 92381c2
error handling
tido64 24dbb8b
address feedback
tido64 742f29f
split v2_profilesSatisfying
tido64 2d97a88
move preset functions to separate file
tido64 b60928d
move changes to align-deps
tido64 2c5ad0a
clean up v1 code and add tests
tido64 c776457
refactor cli
tido64 b3ec7ef
throw when semverCoerce fails
tido64 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,56 @@ | ||
import type { KitConfig } from "@rnx-kit/config"; | ||
import { getKitCapabilities, getKitConfig } from "@rnx-kit/config"; | ||
import { error, info, warn } from "@rnx-kit/console"; | ||
import { isPackageManifest, readPackage } from "@rnx-kit/tools-node/package"; | ||
import chalk from "chalk"; | ||
import { diffLinesUnified } from "jest-diff"; | ||
import path from "path"; | ||
import { getRequirements } from "./dependencies"; | ||
import { migrateConfig } from "./compatibility/config"; | ||
import { gatherRequirements, getRequirements } from "./dependencies"; | ||
import { findBadPackages } from "./findBadPackages"; | ||
import { modifyManifest } from "./helpers"; | ||
import { updatePackageManifest } from "./manifest"; | ||
import { getProfilesFor, resolveCustomProfiles } from "./profiles"; | ||
import type { CheckConfig, CheckOptions, Command } from "./types"; | ||
import { filterPreset, mergePresets } from "./preset"; | ||
import { resolveCustomProfiles } from "./profiles"; | ||
import type { | ||
AlignDepsConfig, | ||
CheckConfig, | ||
CheckOptions, | ||
Command, | ||
ErrorCode, | ||
Options, | ||
Preset, | ||
} from "./types"; | ||
|
||
export function containsValidPresets(config: KitConfig["alignDeps"]): boolean { | ||
const presets = config?.presets; | ||
return !presets || (Array.isArray(presets) && presets.length > 0); | ||
} | ||
|
||
export function containsValidRequirements( | ||
config: KitConfig["alignDeps"] | ||
): boolean { | ||
const requirements = config?.requirements; | ||
if (requirements) { | ||
if (Array.isArray(requirements)) { | ||
return requirements.length > 0; | ||
} else if (typeof requirements === "object") { | ||
return ( | ||
Array.isArray(requirements.production) && | ||
requirements.production.length > 0 | ||
); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
function ensurePreset(preset: Preset, requirements: string[]): void { | ||
if (Object.keys(preset).length === 0) { | ||
throw new Error( | ||
`No profiles could satisfy requirements: ${requirements.join(", ")}` | ||
); | ||
} | ||
} | ||
|
||
export function getCheckConfig( | ||
manifestPath: string, | ||
|
@@ -87,33 +128,159 @@ export function getCheckConfig( | |
}; | ||
} | ||
|
||
export function checkPackageManifest( | ||
manifestPath: string, | ||
options: CheckOptions | ||
): number { | ||
const result = options.config || getCheckConfig(manifestPath, options); | ||
if (typeof result === "number") { | ||
return result; | ||
export function getConfig( | ||
manifestPath: string | ||
): AlignDepsConfig | CheckConfig | ErrorCode { | ||
const manifest = readPackage(manifestPath); | ||
if (!isPackageManifest(manifest)) { | ||
return "invalid-manifest"; | ||
} | ||
|
||
const badPackages = findBadPackages(manifest); | ||
if (badPackages) { | ||
warn( | ||
`Known bad packages are found in '${manifest.name}':\n` + | ||
badPackages | ||
.map((pkg) => `\t${pkg.name}@${pkg.version}: ${pkg.reason}`) | ||
.join("\n") | ||
); | ||
} | ||
|
||
const projectRoot = path.dirname(manifestPath); | ||
const kitConfig = getKitConfig({ cwd: projectRoot }); | ||
if (!kitConfig) { | ||
return "not-configured"; | ||
} | ||
|
||
const { kitType = "library", alignDeps, ...config } = kitConfig; | ||
if (alignDeps) { | ||
const errors = []; | ||
if (!containsValidPresets(alignDeps)) { | ||
errors.push(`${manifestPath}: 'alignDeps.presets' cannot be empty`); | ||
} | ||
if (!containsValidRequirements(alignDeps)) { | ||
errors.push(`${manifestPath}: 'alignDeps.requirements' cannot be empty`); | ||
} | ||
if (errors.length > 0) { | ||
for (const e of errors) { | ||
error(e); | ||
} | ||
return "invalid-configuration"; | ||
} | ||
return { | ||
kitType, | ||
alignDeps: { | ||
presets: ["microsoft/react-native"], | ||
requirements: [], | ||
capabilities: [], | ||
...alignDeps, | ||
}, | ||
...config, | ||
manifest, | ||
}; | ||
} | ||
|
||
const { | ||
capabilities, | ||
customProfiles, | ||
reactNativeDevVersion, | ||
reactNativeVersion, | ||
} = getKitCapabilities(config); | ||
|
||
return { | ||
kitType, | ||
reactNativeVersion, | ||
reactNativeDevVersion, | ||
...(config.reactNativeDevVersion ? { reactNativeDevVersion } : undefined), | ||
capabilities, | ||
customProfilesPath, | ||
customProfiles, | ||
manifest, | ||
} = result; | ||
} as CheckConfig; | ||
} | ||
|
||
function resolve( | ||
{ kitType, alignDeps, manifest }: AlignDepsConfig, | ||
projectRoot: string, | ||
options: CheckOptions | ||
) { | ||
const { capabilities, presets, requirements } = alignDeps; | ||
|
||
const prodRequirements = Array.isArray(requirements) | ||
? requirements | ||
: requirements.production; | ||
const mergedPreset = mergePresets(presets, projectRoot); | ||
const initialProdPreset = filterPreset(prodRequirements, mergedPreset); | ||
ensurePreset(initialProdPreset, prodRequirements); | ||
|
||
const devPreset = (() => { | ||
if (kitType === "app") { | ||
// Preset for development is unused when the package is an app. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sidenote: we need to make sure this is clear in the docs - I can see people not be aware |
||
return {}; | ||
} else if (Array.isArray(requirements)) { | ||
return initialProdPreset; | ||
} else { | ||
const devRequirements = requirements.development; | ||
const devPreset = filterPreset(devRequirements, mergedPreset); | ||
ensurePreset(devPreset, devRequirements); | ||
return devPreset; | ||
} | ||
})(); | ||
|
||
if (kitType === "app") { | ||
const { preset: prodMergedPreset, capabilities: mergedCapabilities } = | ||
gatherRequirements( | ||
projectRoot, | ||
manifest, | ||
initialProdPreset, | ||
capabilities, | ||
options | ||
); | ||
return { | ||
devPreset, | ||
prodPreset: prodMergedPreset, | ||
capabilities: mergedCapabilities, | ||
}; | ||
} | ||
|
||
return { devPreset, prodPreset: initialProdPreset, capabilities }; | ||
} | ||
|
||
export function checkPackageManifest( | ||
manifestPath: string, | ||
options: CheckOptions, | ||
inputConfig = getConfig(manifestPath) | ||
): ErrorCode { | ||
if (typeof inputConfig === "string") { | ||
return inputConfig; | ||
} | ||
|
||
const config = migrateConfig(inputConfig); | ||
const { devPreset, prodPreset, capabilities } = resolve( | ||
config, | ||
path.dirname(manifestPath), | ||
options | ||
); | ||
if (capabilities.length === 0) { | ||
return options.uncheckedReturnCode || 0; | ||
return "success"; | ||
} | ||
|
||
const { kitType, manifest } = config; | ||
|
||
if (kitType === "app") { | ||
info( | ||
"Aligning dependencies according to the following profiles:", | ||
Object.keys(prodPreset).join(", ") | ||
); | ||
} else { | ||
info("Aligning dependencies according to the following profiles:"); | ||
info("\t- Development:", Object.keys(devPreset).join(", ")); | ||
info("\t- Production:", Object.keys(prodPreset).join(", ")); | ||
} | ||
|
||
const updatedManifest = updatePackageManifest( | ||
manifest, | ||
capabilities, | ||
getProfilesFor(reactNativeVersion, customProfilesPath), | ||
getProfilesFor(reactNativeDevVersion, customProfilesPath), | ||
Object.values(prodPreset), | ||
Object.values(devPreset), | ||
kitType | ||
); | ||
|
||
|
@@ -136,23 +303,13 @@ export function checkPackageManifest( | |
} | ||
); | ||
console.log(diff); | ||
|
||
error( | ||
"Changes are needed to satisfy all requirements. Re-run with `--write` to have dep-check apply them." | ||
); | ||
|
||
const url = chalk.bold("https://aka.ms/dep-check"); | ||
info(`Visit ${url} for more information about dep-check.`); | ||
|
||
return 1; | ||
return "unsatisfied"; | ||
} | ||
} | ||
|
||
return 0; | ||
return "success"; | ||
} | ||
|
||
export function makeCheckCommand(options: CheckOptions): Command { | ||
return (manifest: string) => { | ||
return checkPackageManifest(manifest, options); | ||
}; | ||
export function makeCheckCommand(options: Options): Command { | ||
return (manifest: string) => checkPackageManifest(manifest, options); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you do nested destructuring like this? More of a curiosity than a suggestion...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you can. It affects legibility so I don't do it as much.