-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
fix(core): enforce fixed versions only for apps with createPackageJson #16398
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ export function createPackageJson( | |
} = {} | ||
): PackageJson { | ||
const projectNode = graph.nodes[projectName]; | ||
const isLibrary = projectNode.type === 'lib'; | ||
|
||
const { selfInputs, dependencyInputs } = options.target | ||
? getTargetInputs(readNxJson(), projectNode, options.target) | ||
|
@@ -91,6 +92,18 @@ export function createPackageJson( | |
} catch (e) {} | ||
} | ||
|
||
const getVersion = ( | ||
packageName, | ||
version, | ||
section: 'devDependencies' | 'dependencies' | ||
) => { | ||
return ( | ||
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. Maybe a nitpick, but IMO it's more clear like this: return isLibrary
? packageJson[section][packageName] ||
rootPackageJson[section]?.[packageName] ||
version
: packageJson[section][packageName] || version; Or this? if (isLibrary)
return packageJson[section][packageName] ||
rootPackageJson[section]?.[packageName] ||
version;
else
return packageJson[section][packageName] || version; Reasoning is that having |
||
packageJson[section][packageName] || | ||
(isLibrary && rootPackageJson[section]?.[packageName]) || | ||
version | ||
); | ||
}; | ||
|
||
const rootPackageJson = readJsonFile( | ||
`${options.root || workspaceRoot}/package.json` | ||
); | ||
|
@@ -103,44 +116,64 @@ export function createPackageJson( | |
// don't store dev dependencies for production | ||
if (!options.isProduction) { | ||
packageJson.devDependencies ??= {}; | ||
packageJson.devDependencies[packageName] = version; | ||
packageJson.devDependencies[packageName] = getVersion( | ||
packageName, | ||
version, | ||
'devDependencies' | ||
); | ||
} | ||
} else { | ||
if (!packageJson.peerDependencies?.[packageName]) { | ||
packageJson.dependencies ??= {}; | ||
packageJson.dependencies[packageName] = version; | ||
packageJson.dependencies[packageName] = getVersion( | ||
packageName, | ||
version, | ||
'dependencies' | ||
); | ||
} | ||
} | ||
}); | ||
Object.entries(npmDeps.peerDependencies).forEach(([packageName, version]) => { | ||
if (!packageJson.peerDependencies?.[packageName]) { | ||
if (rootPackageJson.dependencies?.[packageName]) { | ||
packageJson.dependencies ??= {}; | ||
packageJson.dependencies[packageName] = version; | ||
return; | ||
} | ||
if (!isLibrary) { | ||
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. I think it's fine for now, but if the app vs lib logic deviates more, we might consider separating them into two functions: |
||
Object.entries(npmDeps.peerDependencies).forEach( | ||
([packageName, version]) => { | ||
if (!packageJson.peerDependencies?.[packageName]) { | ||
if (rootPackageJson.dependencies?.[packageName]) { | ||
packageJson.dependencies ??= {}; | ||
packageJson.dependencies[packageName] = getVersion( | ||
packageName, | ||
version, | ||
'dependencies' | ||
); | ||
return; | ||
} | ||
|
||
const isOptionalPeer = | ||
npmDeps.peerDependenciesMeta[packageName]?.optional; | ||
if (!isOptionalPeer) { | ||
if ( | ||
!options.isProduction || | ||
rootPackageJson.dependencies?.[packageName] | ||
) { | ||
packageJson.peerDependencies ??= {}; | ||
packageJson.peerDependencies[packageName] = version; | ||
const isOptionalPeer = | ||
npmDeps.peerDependenciesMeta[packageName]?.optional; | ||
if (!isOptionalPeer) { | ||
if ( | ||
!options.isProduction || | ||
rootPackageJson.dependencies?.[packageName] | ||
) { | ||
packageJson.peerDependencies ??= {}; | ||
packageJson.peerDependencies[packageName] = getVersion( | ||
packageName, | ||
version, | ||
'dependencies' | ||
); | ||
} | ||
} else if (!options.isProduction) { | ||
// add peer optional dependencies if not in production | ||
packageJson.peerDependencies ??= {}; | ||
packageJson.peerDependencies[packageName] = version; | ||
packageJson.peerDependenciesMeta ??= {}; | ||
packageJson.peerDependenciesMeta[packageName] = { | ||
optional: true, | ||
}; | ||
} | ||
} | ||
} else if (!options.isProduction) { | ||
// add peer optional dependencies if not in production | ||
packageJson.peerDependencies ??= {}; | ||
packageJson.peerDependencies[packageName] = version; | ||
packageJson.peerDependenciesMeta ??= {}; | ||
packageJson.peerDependenciesMeta[packageName] = { | ||
optional: true, | ||
}; | ||
} | ||
} | ||
}); | ||
); | ||
} | ||
|
||
packageJson.devDependencies &&= sortObjectByKeys(packageJson.devDependencies); | ||
packageJson.dependencies &&= sortObjectByKeys(packageJson.dependencies); | ||
|
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.
Not a fan of having to mock these functions, but since it's used in existing tests let's go with it for now. Maybe something we can discuss later is testing strategies