/
yarn.config.cjs
134 lines (122 loc) · 4.95 KB
/
yarn.config.cjs
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// @ts-check
const independentWorkspaceIdents = new Set([
'monodeploy',
'@monodeploy/node',
'@monodeploy/plugin-github',
])
/**
* Enforces that all workspaces depend on each other using the workspace protocol.
*
* @param {import('@yarnpkg/types').Yarn.Constraints.Context} context
*/
function enforceWorkspaceDependenciesWhenPossible({ Yarn }) {
for (const dependency of Yarn.dependencies()) {
// Only enforce for workspaces
if (!Yarn.workspace({ ident: dependency.ident })) continue
if (!dependency.range.startsWith('workspace:')) {
dependency.update('workspace:*')
}
}
}
/**
* Enforces that @yarnpkg/* dependencies have consistent versions across
* peer and dev dependencies. Since devDependencies are easily updated using
* "yarn up" tooling and renovate, we will use the devDependency version as the
* source of truth.
*
* @param {import('@yarnpkg/types').Yarn.Constraints.Context} context
*/
function enforceYarnLibraryPeerAndDevConsistency({ Yarn }) {
for (const workspace of Yarn.workspaces()) {
for (const peerDependency of Yarn.dependencies({ workspace, type: 'peerDependencies' })) {
if (!peerDependency.ident.startsWith('@yarnpkg/')) continue
const devDependency = Yarn.dependency({
workspace,
type: 'devDependencies',
ident: peerDependency.ident,
})
if (devDependency) {
peerDependency.update(devDependency.range)
}
}
}
}
/**
* Monodeploy (CLI + Node + Plugins) should satisfy all dependency's peers.
*
* @param {import('@yarnpkg/types').Yarn.Constraints.Context} context
*/
function enforceMonodeploySatisfiesPeersDirectly({ Yarn }) {
for (const workspaceIdent of independentWorkspaceIdents) {
const workspace = Yarn.workspace({ ident: workspaceIdent })
if (!workspace) throw new Error(`Missing ${workspaceIdent}!`)
for (const dependency of Yarn.dependencies({ workspace, type: 'dependencies' })) {
if (!Yarn.workspace({ ident: dependency.ident })) continue
// Get all peers of the dependency and make sure we satisfy them
const peerDependencies = dependency.resolution?.peerDependencies
if (!peerDependencies) continue
for (const [peerName, peerRange] of peerDependencies.entries()) {
workspace.set(['dependencies', peerName], peerRange)
}
}
}
}
/**
* Enforces required manifest fields for workspaces.
*
* @param {import('@yarnpkg/types').Yarn.Constraints.Context} context
*/
function enforceRequiredWorkspaceFields({ Yarn }) {
for (const workspace of Yarn.workspaces()) {
if (workspace.manifest.private) continue
workspace.set(['repository', 'type'], 'git')
workspace.set(['repository', 'url'], 'https://github.com/tophat/monodeploy.git')
workspace.set(['repository', 'directory'], workspace.cwd)
workspace.set(['publishConfig', 'registry'], 'https://registry.npmjs.org/')
workspace.set(['publishConfig', 'access'], 'public')
}
}
/**
* Enforces Yarn libraries are declared as both peer and dependency.
* Further, @yarnpkg/* should not be listed as a direct dependency.
*
* Exclude independent workspaces (they get handled by a separate constraint).
*
* @param {import('@yarnpkg/types').Yarn.Constraints.Context} context
*/
function enforceYarnLibrariesPeerAndDev({ Yarn }) {
for (const workspace of Yarn.workspaces()) {
if (workspace.manifest.private) continue
if (workspace.ident && independentWorkspaceIdents.has(workspace.ident)) continue
for (const dependency of Yarn.dependencies({ workspace })) {
// Skip if not yarnpkg or if it's not peer/dep.
if (
!dependency.ident.startsWith('@yarnpkg/') ||
dependency.type === 'devDependencies'
) {
continue
}
// If it's a direct dependency, move it to peer and dev.
if (dependency.type === 'dependencies') {
workspace.set(['peerDependencies', dependency.ident], dependency.range)
dependency.delete()
} else if (dependency.type === 'peerDependencies') {
const devDep = Yarn.dependency({ workspace, ident: dependency.ident })
if (devDep) {
// If it's a peer, make sure it's also a dev.
// Use devDep as source of truth because 'yarn up' tooling modifies it directly
workspace.set(['devDependencies', dependency.ident], devDep.range)
}
}
}
}
}
module.exports = {
async constraints(ctx) {
enforceMonodeploySatisfiesPeersDirectly(ctx)
enforceWorkspaceDependenciesWhenPossible(ctx)
enforceYarnLibraryPeerAndDevConsistency(ctx)
enforceYarnLibrariesPeerAndDev(ctx)
enforceRequiredWorkspaceFields(ctx)
},
}