|
| 1 | +/* eslint-env node */ |
| 2 | +// @ts-check |
| 3 | + |
| 4 | +/** |
| 5 | + * @typedef {import('@yarnpkg/types').Yarn.Constraints.Context} Context |
| 6 | + * @typedef {import('@yarnpkg/types').Yarn.Constraints.Workspace} Workspace |
| 7 | + */ |
| 8 | + |
| 9 | +/** @type {import('@yarnpkg/types')} */ |
| 10 | +const { defineConfig } = require(`@yarnpkg/types`) |
| 11 | + |
| 12 | +/** |
| 13 | + * This rule will enforce that a workspace MUST depend on the same version of a |
| 14 | + * dependency as the one used by the other workspaces. |
| 15 | + * |
| 16 | + * @param {Context} context |
| 17 | + */ |
| 18 | +function enforceConsistentDependenciesAcrossTheProject({ Yarn }) { |
| 19 | + for (const dependency of Yarn.dependencies()) { |
| 20 | + if (dependency.type === `peerDependencies`) { |
| 21 | + continue |
| 22 | + } |
| 23 | + |
| 24 | + for (const otherDependency of Yarn.dependencies({ |
| 25 | + ident: dependency.ident, |
| 26 | + })) { |
| 27 | + if (otherDependency.type === `peerDependencies`) { |
| 28 | + continue |
| 29 | + } |
| 30 | + |
| 31 | + if ( |
| 32 | + (dependency.type === `devDependencies` || |
| 33 | + otherDependency.type === `devDependencies`) && |
| 34 | + Yarn.workspace({ ident: otherDependency.ident }) |
| 35 | + ) { |
| 36 | + continue |
| 37 | + } |
| 38 | + |
| 39 | + dependency.update(otherDependency.range) |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * This rule will enforce that a workspace MUST depend on the same version of a |
| 46 | + * dependency as the one used by the other workspaces. |
| 47 | + * |
| 48 | + * @param {Context} context |
| 49 | + */ |
| 50 | +function enforceWorkspaceDependenciesWhenPossible({ Yarn }) { |
| 51 | + for (const dependency of Yarn.dependencies()) { |
| 52 | + if (!Yarn.workspace({ ident: dependency.ident })) { |
| 53 | + continue |
| 54 | + } |
| 55 | + |
| 56 | + dependency.update(`workspace:*`) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * This rule will enforce that a dependency doesn't appear in both `dependencies` |
| 62 | + * and `devDependencies`. |
| 63 | + * |
| 64 | + * @param {Context} context |
| 65 | + */ |
| 66 | +function enforceNotProdAndDevDependencies({ Yarn }) { |
| 67 | + for (const workspace of Yarn.workspaces()) { |
| 68 | + const dependencies = Yarn.dependencies({ workspace, type: 'dependencies' }) |
| 69 | + const devDependencies = Yarn.dependencies({ |
| 70 | + workspace, |
| 71 | + type: 'devDependencies', |
| 72 | + }) |
| 73 | + for (const dependency of dependencies) { |
| 74 | + if ( |
| 75 | + devDependencies.find( |
| 76 | + (devDependency) => devDependency.ident === dependency.ident, |
| 77 | + ) |
| 78 | + ) { |
| 79 | + dependency.error( |
| 80 | + `The dependency '${dependency.ident}' should not appear in both dependencies and devDependencies`, |
| 81 | + ) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * This rule will enforce that any package built with babel (identified by the |
| 89 | + * presence of a 'build:js' script in its `package.json`) must depend on the |
| 90 | + * '@babel/runtime-corejs3' and 'core-js' packages. |
| 91 | + * |
| 92 | + * @param {Context} context |
| 93 | + */ |
| 94 | +function enforceBabelDependencies({ Yarn }) { |
| 95 | + for (const workspace of Yarn.workspaces()) { |
| 96 | + const packageJson = workspace.manifest |
| 97 | + if (!packageJson.scripts?.[`build:js`]) { |
| 98 | + continue |
| 99 | + } |
| 100 | + |
| 101 | + const dependencies = Yarn.dependencies({ |
| 102 | + workspace, |
| 103 | + type: 'dependencies', |
| 104 | + }) |
| 105 | + const requiredDependencies = [`@babel/runtime-corejs3`, `core-js`] |
| 106 | + for (const dependency of requiredDependencies) { |
| 107 | + if (!dependencies.find((dep) => dep.ident === dependency)) { |
| 108 | + workspace.error( |
| 109 | + `The package '${workspace.cwd}' must depend on '${dependency}' to build with babel`, |
| 110 | + ) |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * This rule will enforce that the specified fields are present in the |
| 118 | + * `package.json` of all workspaces. |
| 119 | + * |
| 120 | + * @param {Context} context |
| 121 | + * @param {string[]} fields |
| 122 | + */ |
| 123 | +function enforceFieldsOnAllWorkspaces({ Yarn }, fields) { |
| 124 | + for (const workspace of Yarn.workspaces()) { |
| 125 | + // Skip the root workspace |
| 126 | + if (workspace.cwd === '.') { |
| 127 | + continue |
| 128 | + } |
| 129 | + |
| 130 | + for (const field of fields) { |
| 131 | + if (!workspace.manifest[field]) { |
| 132 | + workspace.error( |
| 133 | + `The field '${field}' is required in the package.json of '${workspace.cwd}'`, |
| 134 | + ) |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * This rule will enforce that the specified fields are present in the |
| 142 | + * `package.json` of all workspaces and that they have the expected value. |
| 143 | + * |
| 144 | + * @param {Context} context |
| 145 | + * @param {Record<string, ((workspace: Workspace) => any) | string>} fields |
| 146 | + */ |
| 147 | +function enforceFieldsWithValuesOnAllWorkspaces({ Yarn }, fields) { |
| 148 | + for (const workspace of Yarn.workspaces()) { |
| 149 | + // Skip the root workspace |
| 150 | + if (workspace.cwd === '.') { |
| 151 | + continue |
| 152 | + } |
| 153 | + |
| 154 | + for (const [field, value] of Object.entries(fields)) { |
| 155 | + workspace.set( |
| 156 | + field, |
| 157 | + typeof value === `function` ? value(workspace) : value, |
| 158 | + ) |
| 159 | + } |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +module.exports = defineConfig({ |
| 164 | + constraints: async (ctx) => { |
| 165 | + enforceConsistentDependenciesAcrossTheProject(ctx) |
| 166 | + enforceWorkspaceDependenciesWhenPossible(ctx) |
| 167 | + enforceNotProdAndDevDependencies(ctx) |
| 168 | + enforceBabelDependencies(ctx) |
| 169 | + enforceFieldsOnAllWorkspaces(ctx, [ |
| 170 | + 'name', |
| 171 | + 'version', |
| 172 | + // 'description', // TODO(jgmw): Add description to all packages and uncomment this line |
| 173 | + ]) |
| 174 | + enforceFieldsWithValuesOnAllWorkspaces(ctx, { |
| 175 | + license: 'MIT', |
| 176 | + ['repository.type']: 'git', |
| 177 | + ['repository.url']: 'git+https://github.com/redwoodjs/redwood.git', |
| 178 | + ['repository.directory']: (workspace) => workspace.cwd, |
| 179 | + }) |
| 180 | + }, |
| 181 | +}) |
0 commit comments