Compare commits

...

3 Commits

Author SHA1 Message Date
ivuorinen
56362dad96 chore(release): publish
- @ivuorinen/config-checker@1.1.0
2023-10-25 19:20:33 +00:00
Ismo Vuorinen
9d372493e8 feat(config-checker): new package: config-checker 2023-10-25 22:19:45 +03:00
Ismo Vuorinen
83e3823b9e chore(repo): updated lerna workspace 2023-10-24 12:30:37 +03:00
12 changed files with 232 additions and 1 deletions

View File

@@ -14,5 +14,6 @@
}
},
"npmClient": "yarn",
"packages": ["packages/*"]
"packages": ["packages/*"],
"$schema": "node_modules/lerna/schemas/lerna-schema.json"
}

View File

@@ -0,0 +1,9 @@
# 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.0 (2023-10-25)
### Features
- **config-checker:** new package: config-checker ([9d37249](https://github.com/ivuorinen/base-configs/commit/9d372493e844694781877cd4853d87198590a0ad))

View File

@@ -0,0 +1,9 @@
MIT License
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 in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -0,0 +1,56 @@
# `@ivuorinen/config-checker` <!-- omit in toc -->
[![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 _`dependencies`_:
```sh
# npm
npm install @ivuorinen/config-checker --save
# Yarn
yarn add @ivuorinen/config-checker
```
## Usage
```js
const configChecker = require('@ivuorinen/config-checker')
// Check for existance of configuration files.
// Module name for Eslint would be 'eslint' for example.
const configFiles = configChecker('module-name')
if (configFiles.length > 0) {
console.log('Found configuration files', configFiles)
} else {
console.log('No configuration files found')
}
```
## 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.md) file for details.
[changelog-link]: ./CHANGELOG.md
[contributing-link]: https://github.com/ivuorinen/.github/blob/main/CONTRIBUTING.md
[issue-link]: https://github.com/ivuorinen/base-configs/issues
[license-badge]: https://img.shields.io/github/license/ivuorinen/base-configs?style=flat-square&labelColor=292a44&color=663399
[license-link]: ./LICENSE
[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/base-configs/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/base-configs

View File

@@ -0,0 +1,23 @@
'use strict'
const path = require('path')
const configChecker = require('..')
const assert = require('assert').strict
const configs = configChecker('test', path.join(__dirname, 'fixtures'))
/**
* Checks if an array contains a file.
*
* @param {string[]} configs - The array of configs to check.
* @param {string} file - The file to check for.
* @returns {boolean} - True if the array contains the file.
*/
function arrayContains(configs = [], file = '') {
return configs.some(config => config === file)
}
assert.ok(arrayContains(configs, '.testrc'))
assert.ok(arrayContains(configs, '.config/testrc.ts'))
console.info('configChecker tests passed')

View File

@@ -0,0 +1,19 @@
'use strict'
const configurationPaths = require('../lib/configuration-paths')
const configItems = configurationPaths('[module name]')
const longestLine = configItems.reduce((a, b) =>
a.length > b.length ? a : b
).length
const mdTable = configItems.map(file => {
const diff = longestLine - file.length
return `| ${file}${' '.repeat(diff)} |`
})
const header = 'Searched configuration files'
console.log('| ' + header + ' '.repeat(longestLine - header.length) + ' |')
console.log('| ' + '-'.repeat(longestLine) + ' |')
mdTable.forEach(line => console.log(line))

View File

@@ -0,0 +1,31 @@
'use strict'
const fs = require('fs')
const path = require('path')
const process = require('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

View File

@@ -0,0 +1,37 @@
'use strict'
const path = require('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.
*/
function 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

View File

@@ -0,0 +1,42 @@
{
"name": "@ivuorinen/config-checker",
"version": "1.1.0",
"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/base-configs/tree/main/packages/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/base-configs.git"
},
"scripts": {
"test": "node ./__tests__/config-checker.test.js",
"files": "node ./helpers/files.js"
},
"bugs": {
"url": "https://github.com/ivuorinen/base-configs/issues"
}
}

View File

@@ -0,0 +1,4 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1