-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathyarn.config.cjs
More file actions
53 lines (49 loc) · 2.04 KB
/
yarn.config.cjs
File metadata and controls
53 lines (49 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/** @type {import('@yarnpkg/types')} */
const { defineConfig } = require('@yarnpkg/types');
// Optional dependencies are still considered of type "dependencies"
// This function finds if a dependency is optional by checking the workspace manifest optionalDependencies.
const isOptionalDependency = (dependency) => {
return (
Object.keys(dependency.workspace.manifest.optionalDependencies ?? {}).find(
(optionalDependency) => optionalDependency === dependency.ident,
) !== undefined
);
};
/**
* This rule will enforce that a workspace MUST depend on the same version of
* a dependency as the one used by the other workspaces.
*
* @param {import('@yarnpkg/types').Yarn.Constraints.Context} context
*/
const enforceConsistentDependenciesAcrossTheProject = ({ Yarn }) => {
for (const dependency of Yarn.dependencies()) {
if (dependency.type === `peerDependencies`) continue;
// There's a bug in yarn constraint dependency.update where the update function expects
// the dependency to be part of dependencies instead of optionalDependencies.
if (isOptionalDependency(dependency)) continue;
for (const otherDependency of Yarn.dependencies({ ident: dependency.ident })) {
if (otherDependency.type === `peerDependencies`) continue;
dependency.update(otherDependency.range);
}
}
};
/**
* This rule will enforce that a workspace MUST depend on the local "workspace:^"
* version of a dependency if it exists.
*
* @param {import('@yarnpkg/types').Yarn.Constraints.Context} context
*/
const enforceWorkspaceDependenciesWhereAvailable = ({ Yarn }) => {
for (const workspace of Yarn.workspaces()) {
for (const workspacePackageDependency of Yarn.dependencies({ ident: workspace.ident })) {
if (workspacePackageDependency.type === 'peerDependencies') continue;
workspacePackageDependency.update('workspace:^');
}
}
};
module.exports = defineConfig({
async constraints(context) {
enforceConsistentDependenciesAcrossTheProject(context);
enforceWorkspaceDependenciesWhereAvailable(context);
},
});