* Enforces proper declaration of dev dependencies. * * This rule checks if certain tools (like Prettier) are used in any workspace * and ensures they're declared as dev dependencies in those workspaces. * * @param {Context} context - The Yarn constraint context.
({ Yarn })
| 215 | * @param {Context} context - The Yarn constraint context. |
| 216 | */ |
| 217 | function enforceDevDependenciesAreProperlyDeclared({ Yarn }) { |
| 218 | const dependencies = Object.fromEntries( |
| 219 | Object.entries(enforcedDevDependencies).map(([key, { ident }]) => [ |
| 220 | key, |
| 221 | Yarn.dependency({ ident }), |
| 222 | ]), |
| 223 | ); |
| 224 | |
| 225 | for (const workspace of Yarn.workspaces()) { |
| 226 | /** @type {Record<string, string> | undefined} */ |
| 227 | const scripts = workspace.manifest.scripts; |
| 228 | |
| 229 | if (!scripts) { |
| 230 | continue; |
| 231 | } |
| 232 | |
| 233 | const dependsOn = { |
| 234 | prettier: false, |
| 235 | waitOn: false, |
| 236 | rimraf: false, |
| 237 | eslint: false, |
| 238 | typescript: false, |
| 239 | crossEnv: false, |
| 240 | }; |
| 241 | |
| 242 | for (const script of Object.values(scripts)) { |
| 243 | for (const [key, { commands }] of Object.entries( |
| 244 | enforcedDevDependencies, |
| 245 | )) { |
| 246 | if (workspace.ident === "@local/eslint" && key === "eslint") { |
| 247 | continue; |
| 248 | } |
| 249 | |
| 250 | const scriptSplit = script.split(" "); |
| 251 | |
| 252 | if (commands.some((command) => scriptSplit.includes(command))) { |
| 253 | dependsOn[key] = true; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | for (const [key, value] of Object.entries(dependsOn)) { |
| 259 | if (!value) { |
| 260 | if (!dependencies[key]) { |
| 261 | // dependency does not exist, so doesn't need to be enforced, as it doesn't exist |
| 262 | continue; |
| 263 | } |
| 264 | |
| 265 | if ( |
| 266 | allowedUnscriptedDevDependencies[workspace.ident]?.has( |
| 267 | dependencies[key].ident, |
| 268 | ) |
| 269 | ) { |
| 270 | continue; |
| 271 | } |
| 272 | |
| 273 | workspace.unset(`devDependencies.${dependencies[key].ident}`); |
| 274 | continue; |