@ivuorinen/config-checker 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Ismo Vuorinen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,139 @@
1
+ # `@ivuorinen/config-checker` <!-- omit in toc -->
2
+
3
+ [![npm package][npm-badge]][npm-link] [![license MIT][license-badge]][license-link] [![ivuorinen's Code Style][style-badge]][style-link]
4
+
5
+ Check for existence of common configuration files in usually used locations.
6
+
7
+ ## Installation
8
+
9
+ Install `this package` as a _`dependency`_:
10
+
11
+ ```sh
12
+ # npm
13
+ npm install @ivuorinen/config-checker --save
14
+
15
+ # Yarn
16
+ yarn add @ivuorinen/config-checker
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ This package is intended to be used in lifecycle events of other packages.
22
+
23
+ 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.
24
+
25
+ ### Real world example
26
+
27
+ For commitlint-config you can find the usage in the [commitlint-config postinstall.cjs][commitlint-postinstall-link] file.
28
+
29
+ Here's a snippet from the file:
30
+
31
+ ```js
32
+ const process = require("process");
33
+ const checkConfig = require("@ivuorinen/config-checker");
34
+ const foundConfig = checkConfig("commitlint");
35
+
36
+ if (foundConfig.length > 0) {
37
+ console.log("commitlint-config: Found existing commitlint config file, skipping creation.");
38
+ console.log(
39
+ "commitlint-config: If you want to create a new config file, please remove the existing one.",
40
+ );
41
+ console.log(`commitlint-config: Found config files at: ${foundConfig.join(", ")}`);
42
+ process.exit(0);
43
+ }
44
+ ```
45
+
46
+ ### Locations scanned
47
+
48
+ | Searched configuration files |
49
+ | ----------------------------------- |
50
+ | `[module name]` |
51
+ | `[module name]`rc |
52
+ | `[module name]`rc.json |
53
+ | `[module name]`rc.yaml |
54
+ | `[module name]`rc.yml |
55
+ | `[module name]`rc.js |
56
+ | `[module name]`rc.ts |
57
+ | `[module name]`rc.mjs |
58
+ | `[module name]`rc.cjs |
59
+ | `[module name]`.jsonc |
60
+ | `[module name]`.yaml |
61
+ | `[module name]`.json |
62
+ | `[module name]`.config.js |
63
+ | `[module name]`.config.ts |
64
+ | `[module name]`.config.mjs |
65
+ | `[module name]`.config.cjs |
66
+ | .`[module name]` |
67
+ | .`[module name]`rc |
68
+ | .`[module name]`rc.json |
69
+ | .`[module name]`rc.yaml |
70
+ | .`[module name]`rc.yml |
71
+ | .`[module name]`rc.js |
72
+ | .`[module name]`rc.ts |
73
+ | .`[module name]`rc.mjs |
74
+ | .`[module name]`rc.cjs |
75
+ | .`[module name]`.jsonc |
76
+ | .`[module name]`.yaml |
77
+ | .`[module name]`.json |
78
+ | .`[module name]`.config.js |
79
+ | .`[module name]`.config.ts |
80
+ | .`[module name]`.config.mjs |
81
+ | .`[module name]`.config.cjs |
82
+ | .config/`[module name]` |
83
+ | .config/`[module name]`rc |
84
+ | .config/`[module name]`rc.json |
85
+ | .config/`[module name]`rc.yaml |
86
+ | .config/`[module name]`rc.yml |
87
+ | .config/`[module name]`rc.js |
88
+ | .config/`[module name]`rc.ts |
89
+ | .config/`[module name]`rc.mjs |
90
+ | .config/`[module name]`rc.cjs |
91
+ | .config/`[module name]`.jsonc |
92
+ | .config/`[module name]`.yaml |
93
+ | .config/`[module name]`.json |
94
+ | .config/`[module name]`.config.js |
95
+ | .config/`[module name]`.config.ts |
96
+ | .config/`[module name]`.config.mjs |
97
+ | .config/`[module name]`.config.cjs |
98
+ | .config/.`[module name]` |
99
+ | .config/.`[module name]`rc |
100
+ | .config/.`[module name]`rc.json |
101
+ | .config/.`[module name]`rc.yaml |
102
+ | .config/.`[module name]`rc.yml |
103
+ | .config/.`[module name]`rc.js |
104
+ | .config/.`[module name]`rc.ts |
105
+ | .config/.`[module name]`rc.mjs |
106
+ | .config/.`[module name]`rc.cjs |
107
+ | .config/.`[module name]`.jsonc |
108
+ | .config/.`[module name]`.yaml |
109
+ | .config/.`[module name]`.json |
110
+ | .config/.`[module name]`.config.js |
111
+ | .config/.`[module name]`.config.ts |
112
+ | .config/.`[module name]`.config.mjs |
113
+ | .config/.`[module name]`.config.cjs |
114
+
115
+ This list has been generated using `npm run generate-table`.
116
+
117
+ ## Contributing
118
+
119
+ 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].
120
+
121
+ ## Changelog
122
+
123
+ See [CHANGELOG][changelog-link] for a human-readable history of changes.
124
+
125
+ ## License
126
+
127
+ This project is licensed under the MIT License - see the [LICENSE.md][license-link] file for details.
128
+
129
+ [changelog-link]: ./CHANGELOG.md
130
+ [contributing-link]: https://github.com/ivuorinen/.github/blob/main/CONTRIBUTING.md
131
+ [issue-link]: https://github.com/ivuorinen/config-checker/issues
132
+ [license-badge]: https://img.shields.io/github/license/ivuorinen/config-checker?style=flat-square&labelColor=292a44&color=663399
133
+ [license-link]: ./LICENSE.md
134
+ [npm-badge]: https://img.shields.io/npm/v/@ivuorinen/config-checker?style=flat-square&labelColor=292a44&color=663399
135
+ [npm-link]: https://www.npmjs.com/package/@ivuorinen/config-checker
136
+ [pull-request-link]: https://github.com/ivuorinen/config-checker/pulls
137
+ [style-badge]: https://img.shields.io/badge/code_style-ivuorinen%E2%80%99s-663399.svg?labelColor=292a44&style=flat-square
138
+ [style-link]: https://github.com/ivuorinen/config-checker
139
+ [commitlint-postinstall-link]: https://github.com/ivuorinen/base-configs-commitlint/scripts/postinstall.js
@@ -0,0 +1,32 @@
1
+ /* eslint no-console: "off", n/no-process-exit: "off" -- CLI app that gives users feedback */
2
+
3
+ const fs = require('node:fs')
4
+ const path = require('node:path')
5
+ // noinspection NpmUsedModulesInstalled
6
+ const process = require('node:process')
7
+ const configurationPaths = require('./configuration-paths')
8
+
9
+ /**
10
+ * Checks for the existence of a configuration file.
11
+ * @param {string} moduleName The name of the module to check for.
12
+ * @param {string} pathPrefix The prefix to add to the path.
13
+ * @returns {string[]} - The paths to the configuration files.
14
+ */
15
+ const configChecker = (moduleName, pathPrefix = '') => {
16
+ let searchPath = process.env.INIT_CWD
17
+ if (pathPrefix.length > 0) {
18
+ searchPath = pathPrefix
19
+ }
20
+
21
+ const allFiles = configurationPaths(moduleName)
22
+
23
+ if (process.env.DEBUG) {
24
+ const filesWithPath = allFiles.map(file => path.join(searchPath, file))
25
+ console.log(filesWithPath)
26
+ }
27
+
28
+ // Look for config files in defined search path, and return found.
29
+ return allFiles.filter(file => fs.existsSync(path.join(searchPath, file)))
30
+ }
31
+
32
+ module.exports = configChecker
@@ -0,0 +1,35 @@
1
+ const path = require('node:path')
2
+
3
+ /**
4
+ * Returns an array of configuration paths.
5
+ * @param {string} moduleName The name of the module to check for.
6
+ * @returns {string[]} - The paths to the configuration files.
7
+ */
8
+ const configurationPaths = moduleName => {
9
+ const filesPlain = [
10
+ moduleName,
11
+ `${moduleName}rc`,
12
+ `${moduleName}rc.json`,
13
+ `${moduleName}rc.yaml`,
14
+ `${moduleName}rc.yml`,
15
+ `${moduleName}rc.js`,
16
+ `${moduleName}rc.ts`,
17
+ `${moduleName}rc.mjs`,
18
+ `${moduleName}rc.cjs`,
19
+ `${moduleName}.jsonc`,
20
+ `${moduleName}.yaml`,
21
+ `${moduleName}.json`,
22
+ `${moduleName}.config.js`,
23
+ `${moduleName}.config.ts`,
24
+ `${moduleName}.config.mjs`,
25
+ `${moduleName}.config.cjs`
26
+ ]
27
+
28
+ const filesDot = filesPlain.map(file => `.${file}`)
29
+ const bothFiles = filesPlain.concat(filesDot)
30
+ const filesInConfig = bothFiles.map(file => path.join('.config', file))
31
+
32
+ return bothFiles.concat(filesInConfig)
33
+ }
34
+
35
+ module.exports = configurationPaths
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@ivuorinen/config-checker",
3
+ "version": "1.0.0",
4
+ "description": "Checks the commonly used configuration locations for configuration files",
5
+ "keywords": [
6
+ "check-config",
7
+ "config",
8
+ "ivuorinen",
9
+ "config-checker",
10
+ "cosmicconfig",
11
+ "rcfile",
12
+ "rcfiles"
13
+ ],
14
+ "author": {
15
+ "name": "Ismo Vuorinen",
16
+ "url": "https://github.com/ivuorinen"
17
+ },
18
+ "homepage": "https://github.com/ivuorinen/config-checker#readme",
19
+ "license": "MIT",
20
+ "main": "lib/config-checker.js",
21
+ "directories": {
22
+ "lib": "lib",
23
+ "test": "__tests__"
24
+ },
25
+ "files": [
26
+ "lib"
27
+ ],
28
+ "publishConfig": {
29
+ "access": "public"
30
+ },
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/ivuorinen/config-checker.git"
34
+ },
35
+ "scripts": {
36
+ "test": "node ./__tests__/config-checker.test.js",
37
+ "generate-table": "node ./helpers/generate-md-table.js",
38
+ "eslint:fix": "eslint '**/*.{js,tsx,ts,mjs,cjs}' --fix",
39
+ "eslint:report": "eslint '**/*.{js,tsx,ts,mjs,cjs}'",
40
+ "markdownlint:fix": "markdownlint '**/*.md' --fix",
41
+ "markdownlint:report": "markdownlint '**/*.md'",
42
+ "prettier:fix": "prettier '**/*.{js,tsx,ts,json,mjs,cjs}' --write",
43
+ "prettier:report": "prettier '**/*.{js,tsx,ts,json,mjs,cjs}' --check"
44
+ },
45
+ "bugs": {
46
+ "url": "https://github.com/ivuorinen/config-checker/issues"
47
+ },
48
+ "devDependencies": {
49
+ "@ivuorinen/eslint-config": "^0.5.13",
50
+ "@ivuorinen/markdownlint-config": "^0.3.16",
51
+ "@ivuorinen/prettier-config": "^0.3.12",
52
+ "@ivuorinen/semantic-release-config": "^0.3.18"
53
+ }
54
+ }