Skip to content

Commit

Permalink
Enhance config discovery with cosmiconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
MericKarabulut committed Aug 26, 2023
1 parent 255ac93 commit dc363be
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 18 deletions.
82 changes: 82 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"check-node-version": "^4.1.0",
"clean-webpack-plugin": "^3.0.0",
"copy-webpack-plugin": "^10.2.0",
"cosmiconfig": "^8.2.0",
"cross-spawn": "^5.1.0",
"css-loader": "^6.2.0",
"cssnano": "^6.0.1",
Expand Down
8 changes: 2 additions & 6 deletions packages/scripts/scripts/lint-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
hasFileArgInCLI,
hasPackageProp,
hasProjectFile,
searchConfig,
} = require( '../utils' );

const args = getArgsFromCLI();
Expand All @@ -24,12 +25,7 @@ const defaultFilesArgs = hasFileArgInCLI() ? [] : [ '.' ];
const hasLintConfig =
hasArgInCLI( '-c' ) ||
hasArgInCLI( '--config' ) ||
hasProjectFile( '.eslintrc.js' ) ||
hasProjectFile( '.eslintrc.json' ) ||
hasProjectFile( '.eslintrc.yaml' ) ||
hasProjectFile( '.eslintrc.yml' ) ||
hasProjectFile( 'eslintrc.config.js' ) ||
hasProjectFile( '.eslintrc' ) ||
searchConfig( 'eslint' ) ||
hasPackageProp( 'eslintConfig' );

// When a configuration is not provided by the project, use from the default
Expand Down
8 changes: 2 additions & 6 deletions packages/scripts/scripts/lint-pkg-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
hasFileArgInCLI,
hasProjectFile,
hasPackageProp,
searchConfig,
} = require( '../utils' );

const args = getArgsFromCLI();
Expand All @@ -24,12 +25,7 @@ const defaultFilesArgs = hasFileArgInCLI() ? [] : [ '.' ];
const hasLintConfig =
hasArgInCLI( '-c' ) ||
hasArgInCLI( '--configFile' ) ||
hasProjectFile( '.npmpackagejsonlintrc.js' ) ||
hasProjectFile( '.npmpackagejsonlintrc.json' ) ||
hasProjectFile( '.npmpackagejsonlintrc.yaml' ) ||
hasProjectFile( '.npmpackagejsonlintrc.yml' ) ||
hasProjectFile( 'npmpackagejsonlint.config.js' ) ||
hasProjectFile( '.npmpackagejsonlintrc' ) ||
searchConfig( 'npmpackagejsonlint' ) ||
hasPackageProp( 'npmpackagejsonlint' ) ||
// npm-package-json-lint v3.x used a different prop name.
hasPackageProp( 'npmPackageJsonLintConfig' );
Expand Down
8 changes: 2 additions & 6 deletions packages/scripts/scripts/lint-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
hasFileArgInCLI,
hasProjectFile,
hasPackageProp,
searchConfig,
} = require( '../utils' );

const args = getArgsFromCLI();
Expand All @@ -23,12 +24,7 @@ const defaultFilesArgs = hasFileArgInCLI() ? [] : [ '**/*.{css,pcss,scss}' ];
// See: https://stylelint.io/user-guide/configuration
const hasLintConfig =
hasArgInCLI( '--config' ) ||
hasProjectFile( '.stylelintrc.js' ) ||
hasProjectFile( '.stylelintrc.json' ) ||
hasProjectFile( '.stylelintrc.yaml' ) ||
hasProjectFile( '.stylelintrc.yml' ) ||
hasProjectFile( 'stylelint.config.js' ) ||
hasProjectFile( '.stylelintrc' ) ||
searchConfig( 'stylelint' ) ||
hasPackageProp( 'stylelint' );

const defaultConfigArgs = ! hasLintConfig
Expand Down
12 changes: 12 additions & 0 deletions packages/scripts/utils/cosmiconf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* External dependencies
*/
// eslint-disable-next-line import/no-extraneous-dependencies
const { cosmiconfigSync } = require( 'cosmiconfig' );

const searchConfig = ( confName ) => {
const explorer = cosmiconfigSync( confName );
return explorer.search() !== null;
};

module.exports = { searchConfig };
2 changes: 2 additions & 0 deletions packages/scripts/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const {
} = require( './config' );
const { fromProjectRoot, fromConfigRoot, hasProjectFile } = require( './file' );
const { getPackageProp, hasPackageProp } = require( './package' );
const { searchConfig } = require( './cosmiconf' );

module.exports = {
fromProjectRoot,
Expand All @@ -48,4 +49,5 @@ module.exports = {
hasPrettierConfig,
hasProjectFile,
spawnScript,
searchConfig,
};

0 comments on commit dc363be

Please sign in to comment.