mirror of
https://github.com/ivuorinen/config-checker.git
synced 2026-01-26 11:24:02 +00:00
feat: initial import from base-configs
This commit is contained in:
32
lib/config-checker.js
Normal file
32
lib/config-checker.js
Normal file
@@ -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
|
||||
35
lib/configuration-paths.js
Normal file
35
lib/configuration-paths.js
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user