From 2c000e8e7eeedce4e976f1e9783384074cbc987c Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Mon, 23 Jun 2025 02:32:13 +0300 Subject: [PATCH] feat: initial import from base-configs --- CHANGELOG.md | 86 +++++++++++++++++ LICENSE.md | 2 +- README.md | 139 +++++++++++++++++++++++++++ __tests__/config-checker.test.js | 20 ++++ __tests__/fixtures/.config/testrc.ts | 0 __tests__/fixtures/.testrc | 0 helpers/generate-md-table.js | 19 ++++ lib/config-checker.js | 32 ++++++ lib/configuration-paths.js | 35 +++++++ package.json | 42 ++++++++ yarn.lock | 4 + 11 files changed, 378 insertions(+), 1 deletion(-) create mode 100644 CHANGELOG.md create mode 100644 README.md create mode 100644 __tests__/config-checker.test.js create mode 100644 __tests__/fixtures/.config/testrc.ts create mode 100644 __tests__/fixtures/.testrc create mode 100644 helpers/generate-md-table.js create mode 100644 lib/config-checker.js create mode 100644 lib/configuration-paths.js create mode 100644 package.json create mode 100644 yarn.lock diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..704edc3 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,86 @@ +# Change Log + +All notable changes to this project will be documented in this file. +See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. + +## 1.1.12 (2025-02-23) + +**Note:** Version bump only for package @ivuorinen/config-checker + + + + + +## 1.1.11 (2025-02-23) + + +### Bug Fixes + +* **deps:** update globals to 16.0.0 ([#342](https://github.com/ivuorinen/base-configs/issues/342)) ([04b333b](https://github.com/ivuorinen/base-configs/commit/04b333b445b6beb4344d2f9102bb5d3dc72ae26a)) + + + + + +## 1.1.10 (2025-01-17) + + +### Bug Fixes + +* **deps:** update eslint-config-prettier to 10.0.1 ([#324](https://github.com/ivuorinen/base-configs/issues/324)) ([8a0d38e](https://github.com/ivuorinen/base-configs/commit/8a0d38e173df40b771d42b694a24145559200506)) + + + + + +## 1.1.9 (2024-11-25) + +**Note:** Version bump only for package @ivuorinen/config-checker + +## 1.1.8 (2024-11-16) + +### Bug Fixes + +- upgrade browserslist from 4.24.0 to 4.24.2 ([#302](https://github.com/ivuorinen/base-configs/issues/302)) ([cbb1754](https://github.com/ivuorinen/base-configs/commit/cbb17540f3cd7fc81f0032e557568c65ed0a9744)) + +## 1.1.7 (2024-10-20) + +### Bug Fixes + +- upgrade browserslist from 4.23.3 to 4.24.0 ([#297](https://github.com/ivuorinen/base-configs/issues/297)) ([a99c55a](https://github.com/ivuorinen/base-configs/commit/a99c55aab760142b5d77ad80ce5d44b25dde17d7)) + +## 1.1.6 (2024-10-19) + +### Bug Fixes + +- upgrade markdownlint-cli from 0.41.0 to 0.42.0 ([#298](https://github.com/ivuorinen/base-configs/issues/298)) ([f57190d](https://github.com/ivuorinen/base-configs/commit/f57190d55c27101f66583cc0000733b2707c1e5f)) + +## [1.1.5](https://github.com/ivuorinen/base-configs/compare/@ivuorinen/config-checker@1.1.4...@ivuorinen/config-checker@1.1.5) (2024-08-21) + +**Note:** Version bump only for package @ivuorinen/config-checker + +## 1.1.4 (2024-07-02) + +### Bug Fixes + +- **workflows:** commit lint fix ([3cc07cf](https://github.com/ivuorinen/base-configs/commit/3cc07cf3ffd8743860a07bb85aa4d275bb63094e)) + +## [1.1.3](https://github.com/ivuorinen/base-configs/compare/@ivuorinen/config-checker@1.1.2...@ivuorinen/config-checker@1.1.3) (2024-05-05) + +### Bug Fixes + +- add an initial value to "reduce()" call ([#238](https://github.com/ivuorinen/base-configs/issues/238)) ([52372db](https://github.com/ivuorinen/base-configs/commit/52372dbe371bae56b44d79e2150b61200b071a1f)) + +## [1.1.2](https://github.com/ivuorinen/base-configs/compare/@ivuorinen/config-checker@1.1.1...@ivuorinen/config-checker@1.1.2) (2023-10-31) + +**Note:** Version bump only for package @ivuorinen/config-checker + +## [1.1.1](https://github.com/ivuorinen/base-configs/compare/@ivuorinen/config-checker@1.1.0...@ivuorinen/config-checker@1.1.1) (2023-10-30) + +**Note:** Version bump only for package @ivuorinen/config-checker + +# 1.1.0 (2023-10-25) + +### Features + +- **config-checker:** new package: config-checker ([9d37249](https://github.com/ivuorinen/base-configs/commit/9d372493e844694781877cd4853d87198590a0ad)) diff --git a/LICENSE.md b/LICENSE.md index 8d1507b..1272de8 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2025 Ismo Vuorinen +Copyright (c) 2023 Ismo Vuorinen Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md new file mode 100644 index 0000000..7133534 --- /dev/null +++ b/README.md @@ -0,0 +1,139 @@ +# `@ivuorinen/config-checker` + +[![npm package][npm-badge]][npm-link] [![license MIT][license-badge]][license-link] [![ivuorinen's Code Style][style-badge]][style-link] + +Check for existence of common configuration files in usually used locations. + +## Installation + +Install `this package` as a _`dependency`_: + +```sh +# npm +npm install @ivuorinen/config-checker --save + +# Yarn +yarn add @ivuorinen/config-checker +``` + +## Usage + +This package is intended to be used in lifecycle events of other packages. + +Configuration packages located in this repository use this tool to check for existence of configuration files, and if they exist, they will not create new ones. + +### Real world example + +For commitlint-config you can find the usage in the [commitlint-config postinstall.cjs][commitlint-postinstall-link] file. + +Here's a snippet from the file: + +```js +const process = require("process"); +const checkConfig = require("@ivuorinen/config-checker"); +const foundConfig = checkConfig("commitlint"); + +if (foundConfig.length > 0) { + console.log("commitlint-config: Found existing commitlint config file, skipping creation."); + console.log( + "commitlint-config: If you want to create a new config file, please remove the existing one.", + ); + console.log(`commitlint-config: Found config files at: ${foundConfig.join(", ")}`); + process.exit(0); +} +``` + +### Locations scanned + +| Searched configuration files | +| ----------------------------------- | +| `[module name]` | +| `[module name]`rc | +| `[module name]`rc.json | +| `[module name]`rc.yaml | +| `[module name]`rc.yml | +| `[module name]`rc.js | +| `[module name]`rc.ts | +| `[module name]`rc.mjs | +| `[module name]`rc.cjs | +| `[module name]`.jsonc | +| `[module name]`.yaml | +| `[module name]`.json | +| `[module name]`.config.js | +| `[module name]`.config.ts | +| `[module name]`.config.mjs | +| `[module name]`.config.cjs | +| .`[module name]` | +| .`[module name]`rc | +| .`[module name]`rc.json | +| .`[module name]`rc.yaml | +| .`[module name]`rc.yml | +| .`[module name]`rc.js | +| .`[module name]`rc.ts | +| .`[module name]`rc.mjs | +| .`[module name]`rc.cjs | +| .`[module name]`.jsonc | +| .`[module name]`.yaml | +| .`[module name]`.json | +| .`[module name]`.config.js | +| .`[module name]`.config.ts | +| .`[module name]`.config.mjs | +| .`[module name]`.config.cjs | +| .config/`[module name]` | +| .config/`[module name]`rc | +| .config/`[module name]`rc.json | +| .config/`[module name]`rc.yaml | +| .config/`[module name]`rc.yml | +| .config/`[module name]`rc.js | +| .config/`[module name]`rc.ts | +| .config/`[module name]`rc.mjs | +| .config/`[module name]`rc.cjs | +| .config/`[module name]`.jsonc | +| .config/`[module name]`.yaml | +| .config/`[module name]`.json | +| .config/`[module name]`.config.js | +| .config/`[module name]`.config.ts | +| .config/`[module name]`.config.mjs | +| .config/`[module name]`.config.cjs | +| .config/.`[module name]` | +| .config/.`[module name]`rc | +| .config/.`[module name]`rc.json | +| .config/.`[module name]`rc.yaml | +| .config/.`[module name]`rc.yml | +| .config/.`[module name]`rc.js | +| .config/.`[module name]`rc.ts | +| .config/.`[module name]`rc.mjs | +| .config/.`[module name]`rc.cjs | +| .config/.`[module name]`.jsonc | +| .config/.`[module name]`.yaml | +| .config/.`[module name]`.json | +| .config/.`[module name]`.config.js | +| .config/.`[module name]`.config.ts | +| .config/.`[module name]`.config.mjs | +| .config/.`[module name]`.config.cjs | + +This list has been generated using `npm run generate-table`. + +## Contributing + +If you are interested in helping contribute, please take a look at our [contribution guidelines][contributing-link] and open an [issue][issue-link] or [pull request][pull-request-link]. + +## Changelog + +See [CHANGELOG][changelog-link] for a human-readable history of changes. + +## License + +This project is licensed under the MIT License - see the [LICENSE.md][license-link] file for details. + +[changelog-link]: ./CHANGELOG.md +[contributing-link]: https://github.com/ivuorinen/.github/blob/main/CONTRIBUTING.md +[issue-link]: https://github.com/ivuorinen/config-checker/issues +[license-badge]: https://img.shields.io/github/license/ivuorinen/config-checker?style=flat-square&labelColor=292a44&color=663399 +[license-link]: ./LICENSE.md +[npm-badge]: https://img.shields.io/npm/v/@ivuorinen/config-checker?style=flat-square&labelColor=292a44&color=663399 +[npm-link]: https://www.npmjs.com/package/@ivuorinen/config-checker +[pull-request-link]: https://github.com/ivuorinen/config-checker/pulls +[style-badge]: https://img.shields.io/badge/code_style-ivuorinen%E2%80%99s-663399.svg?labelColor=292a44&style=flat-square +[style-link]: https://github.com/ivuorinen/config-checker +[commitlint-postinstall-link]: https://github.com/ivuorinen/base-configs-commitlint/scripts/postinstall.js diff --git a/__tests__/config-checker.test.js b/__tests__/config-checker.test.js new file mode 100644 index 0000000..7e67816 --- /dev/null +++ b/__tests__/config-checker.test.js @@ -0,0 +1,20 @@ +/* eslint no-console: "off" -- CLI app that gives users feedback */ + +const path = require('node:path') +const configChecker = require('..') +const assert = require('node:assert').strict + +const configs = configChecker('test', path.join(__dirname, 'fixtures')) + +/** + * Checks if an array contains a file. + * @param {string[]} c The array of configs to check. + * @param {string} file The file to check for. + * @returns {boolean} - True if the array contains the file. + */ +const arrayContains = (c = [], file = '') => c.includes(file) + +assert.ok(arrayContains(configs, '.testrc')) +assert.ok(arrayContains(configs, '.config/testrc.ts')) + +console.info('configChecker tests passed') diff --git a/__tests__/fixtures/.config/testrc.ts b/__tests__/fixtures/.config/testrc.ts new file mode 100644 index 0000000..e69de29 diff --git a/__tests__/fixtures/.testrc b/__tests__/fixtures/.testrc new file mode 100644 index 0000000..e69de29 diff --git a/helpers/generate-md-table.js b/helpers/generate-md-table.js new file mode 100644 index 0000000..ff6e7d1 --- /dev/null +++ b/helpers/generate-md-table.js @@ -0,0 +1,19 @@ +/* eslint no-console: "off" -- This is a cli app that generates cli output */ + +const configurationPaths = require('../lib/configuration-paths') + +const configItems = configurationPaths('`[module name]`') +const longestLine = configItems.reduce((a, b) => (a.length > b.length ? a : b), 0).length + +const mdTable = configItems.map(file => { + const diff = longestLine - file.length + return `| ${file}${' '.repeat(diff)} |` +}) + +console.log('# Configuration files\n') + +const header = 'Searched configuration files' +console.log(`| ${header}${' '.repeat(longestLine - header.length)} |`) +console.log(`| ${'-'.repeat(longestLine)} |`) + +mdTable.forEach(line => console.log(line)) diff --git a/lib/config-checker.js b/lib/config-checker.js new file mode 100644 index 0000000..1fed785 --- /dev/null +++ b/lib/config-checker.js @@ -0,0 +1,32 @@ +/* eslint no-console: "off", n/no-process-exit: "off" -- CLI app that gives users feedback */ + +const fs = require('node:fs') +const path = require('node:path') +// noinspection NpmUsedModulesInstalled +const process = require('node:process') +const configurationPaths = require('./configuration-paths') + +/** + * Checks for the existence of a configuration file. + * @param {string} moduleName The name of the module to check for. + * @param {string} pathPrefix The prefix to add to the path. + * @returns {string[]} - The paths to the configuration files. + */ +const configChecker = (moduleName, pathPrefix = '') => { + let searchPath = process.env.INIT_CWD + if (pathPrefix.length > 0) { + searchPath = pathPrefix + } + + const allFiles = configurationPaths(moduleName) + + if (process.env.DEBUG) { + const filesWithPath = allFiles.map(file => path.join(searchPath, file)) + console.log(filesWithPath) + } + + // Look for config files in defined search path, and return found. + return allFiles.filter(file => fs.existsSync(path.join(searchPath, file))) +} + +module.exports = configChecker diff --git a/lib/configuration-paths.js b/lib/configuration-paths.js new file mode 100644 index 0000000..178c028 --- /dev/null +++ b/lib/configuration-paths.js @@ -0,0 +1,35 @@ +const path = require('node:path') + +/** + * Returns an array of configuration paths. + * @param {string} moduleName The name of the module to check for. + * @returns {string[]} - The paths to the configuration files. + */ +const configurationPaths = moduleName => { + const filesPlain = [ + moduleName, + `${moduleName}rc`, + `${moduleName}rc.json`, + `${moduleName}rc.yaml`, + `${moduleName}rc.yml`, + `${moduleName}rc.js`, + `${moduleName}rc.ts`, + `${moduleName}rc.mjs`, + `${moduleName}rc.cjs`, + `${moduleName}.jsonc`, + `${moduleName}.yaml`, + `${moduleName}.json`, + `${moduleName}.config.js`, + `${moduleName}.config.ts`, + `${moduleName}.config.mjs`, + `${moduleName}.config.cjs` + ] + + const filesDot = filesPlain.map(file => `.${file}`) + const bothFiles = filesPlain.concat(filesDot) + const filesInConfig = bothFiles.map(file => path.join('.config', file)) + + return bothFiles.concat(filesInConfig) +} + +module.exports = configurationPaths diff --git a/package.json b/package.json new file mode 100644 index 0000000..b0e8242 --- /dev/null +++ b/package.json @@ -0,0 +1,42 @@ +{ + "name": "@ivuorinen/config-checker", + "version": "1.1.12", + "description": "Checks the commonly used configuration locations for configuration files", + "keywords": [ + "check-config", + "config", + "ivuorinen", + "config-checker", + "cosmicconfig", + "rcfile", + "rcfiles" + ], + "author": { + "name": "Ismo Vuorinen", + "url": "https://github.com/ivuorinen" + }, + "homepage": "https://github.com/ivuorinen/config-checker#readme", + "license": "MIT", + "main": "lib/config-checker.js", + "directories": { + "lib": "lib", + "test": "__tests__" + }, + "files": [ + "lib" + ], + "publishConfig": { + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/ivuorinen/config-checker.git" + }, + "scripts": { + "test": "node ./__tests__/config-checker.test.js", + "generate-table": "node ./helpers/generate-md-table.js" + }, + "bugs": { + "url": "https://github.com/ivuorinen/config-checker/issues" + } +} diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..fb57ccd --- /dev/null +++ b/yarn.lock @@ -0,0 +1,4 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + +