Skip to content
This repository has been archived by the owner on Feb 11, 2025. It is now read-only.

Commit

Permalink
fix(eslint): move to optional peer dependencies
Browse files Browse the repository at this point in the history
Resolves #16
  • Loading branch information
mrmckeb committed Sep 21, 2022
1 parent 72bd355 commit 2f4ef76
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
extends: './eslint/node',
extends: ['./eslint/node'],
overrides: [
{
files: ['eslint/rules/**'],
Expand Down
4 changes: 4 additions & 0 deletions eslint/next.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const { JAVASCRIPT_FILES } = require('./constants');
const requirePackage = require('./utils/require-package');

requirePackage('next', 'next');
requirePackage('next', '@next/eslint-plugin-next');

This comment has been minimized.

Copy link
@gaspar09

gaspar09 Sep 27, 2022

This means that we need to install next and it's peer dependencies on the root of a monorepo.
Is there a more lightweight option that doesn't require next?


const babelOptions = {
presets: (() => {
Expand Down
3 changes: 3 additions & 0 deletions eslint/typescript.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const { TYPESCRIPT_FILES } = require('./constants');
const requirePackage = require('./utils/require-package');

requirePackage('typescript', 'typescript');

module.exports = {
overrides: [
Expand Down
36 changes: 36 additions & 0 deletions eslint/utils/require-package.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// @ts-check
const pkgJson = require('../../package.json');

/**
* @returns {string} The name of the package manager.
*/
function readPackageManager() {
const match = process.env.npm_config_user_agent?.match(/^(?<pm>\w+)\//);
return match?.groups ? match.groups?.pm : 'npm';
}

/**
* @param {string} configName
* @param {string} packageName
*/
module.exports = (configName, packageName) => {
try {
require.resolve(packageName);
} catch (e) {
const packageManager = readPackageManager();
const command = packageManager === 'yarn' ? 'add' : 'install';

/* eslint-disable no-console */
console.error(
`The \`${configName}\` config requires an optional peer dependency, which has not been installed.`,
);
console.error();
console.error('To install it, run:');
console.error(
`- ${packageManager} ${command} ${packageName}@${pkgJson.peerDependencies[packageName]}`,
);
/* eslint-enable no-console */

process.exit(1);
}
};
6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"prettier": "./prettier",
"dependencies": {
"@babel/core": "^7.19.1",
"@babel/eslint-parser": "^7.19.1",
"@rushstack/eslint-patch": "^1.2.0",
"@typescript-eslint/eslint-plugin": "^5.38.0",
Expand All @@ -46,7 +47,6 @@
"prettier-plugin-packagejson": "^2.2.18"
},
"devDependencies": {
"@babel/core": "^7.19.1",
"@commitlint/cli": "^17.1.2",
"@commitlint/config-conventional": "^17.1.0",
"@semantic-release/git": "^10.0.1",
Expand All @@ -57,17 +57,13 @@
"typescript": "^4.8.3"
},
"peerDependencies": {
"@babel/core": "^7.19.1",
"@next/eslint-plugin-next": "^12.3.0",
"eslint": "^8.12.0",
"next": "^12.3.0",
"prettier": "^2.6.1",
"typescript": "^4.8.3"
},
"peerDependenciesMeta": {
"@babel/core": {
"optional": true
},
"@next/eslint-plugin-next": {
"optional": true
},
Expand Down

0 comments on commit 2f4ef76

Please sign in to comment.