elrh-cosca 0.3.5 → 0.3.6

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.
Files changed (60) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +281 -265
  3. package/dist/elrh-cosca.mjs +14304 -15376
  4. package/dist/types/index.d.ts +1 -1
  5. package/dist/types/src/functions/remove-from-text-file.d.ts +11 -0
  6. package/dist/types/{main.d.ts → src/main.d.ts} +2 -1
  7. package/dist/types/test/checks-get-package-manager.test.d.ts +1 -0
  8. package/dist/types/test/checks-has-json-key.test.d.ts +1 -0
  9. package/dist/types/test/checks-has-text.test.d.ts +1 -0
  10. package/dist/types/test/checks-path-exists.test.d.ts +1 -0
  11. package/dist/types/test/cosca-test-setup.d.ts +1 -0
  12. package/dist/types/test/cosca-test-utils.d.ts +45 -0
  13. package/dist/types/test/fixtures/config-file-default.d.ts +10 -0
  14. package/dist/types/test/fixtures/config-file-named.d.ts +9 -0
  15. package/dist/types/test/functions-create-file-from-template.test.d.ts +1 -0
  16. package/dist/types/test/functions-create-file-from-web-template.test.d.ts +1 -0
  17. package/dist/types/test/functions-delete-path.test.d.ts +1 -0
  18. package/dist/types/test/functions-remove-from-json-file.test.d.ts +1 -0
  19. package/dist/types/test/functions-remove-from-text-file.test.d.ts +1 -0
  20. package/dist/types/test/functions-update-config-file.test.d.ts +1 -0
  21. package/dist/types/test/functions-update-json-file.test.d.ts +1 -0
  22. package/dist/types/test/functions-update-text-file.test.d.ts +1 -0
  23. package/dist/types/test/private-check-path.test.d.ts +1 -0
  24. package/dist/types/test/snapshots/updated-config-file-default-1.d.ts +13 -0
  25. package/dist/types/test/snapshots/updated-config-file-default-2.d.ts +14 -0
  26. package/dist/types/test/snapshots/updated-config-file-default-3.d.ts +16 -0
  27. package/dist/types/test/snapshots/updated-config-file-default-4.d.ts +18 -0
  28. package/dist/types/test/snapshots/updated-config-file-named-1.d.ts +12 -0
  29. package/dist/types/test/snapshots/updated-config-file-named-2.d.ts +13 -0
  30. package/dist/types/test/snapshots/updated-config-file-named-3.d.ts +15 -0
  31. package/dist/types/test/snapshots/updated-config-file-named-4.d.ts +17 -0
  32. package/dist/types/test/terminal-prompt-user.test.d.ts +1 -0
  33. package/dist/types/test/terninal-show-error.test.d.ts +1 -0
  34. package/dist/types/test/terninal-show-message.test.d.ts +1 -0
  35. package/dist/types/test/utils-get-env-value.test.d.ts +1 -0
  36. package/dist/types/test/utils-parse-qualified-path.test.d.ts +1 -0
  37. package/dist/types/test/utils-resolve-package-path.test.d.ts +1 -0
  38. package/package.json +6 -6
  39. package/test/cosca-test.js +55 -55
  40. /package/dist/types/{_private → src/_private}/check-path.d.ts +0 -0
  41. /package/dist/types/{_private → src/_private}/deep-merge-object.d.ts +0 -0
  42. /package/dist/types/{_private → src/_private}/fetch-file.d.ts +0 -0
  43. /package/dist/types/{checks → src/checks}/get-package-manager.d.ts +0 -0
  44. /package/dist/types/{checks → src/checks}/has-json-key.d.ts +0 -0
  45. /package/dist/types/{checks → src/checks}/has-text.d.ts +0 -0
  46. /package/dist/types/{checks → src/checks}/path-exists.d.ts +0 -0
  47. /package/dist/types/{functions → src/functions}/create-file-from-template.d.ts +0 -0
  48. /package/dist/types/{functions → src/functions}/create-file-from-web-template.d.ts +0 -0
  49. /package/dist/types/{functions → src/functions}/delete-path.d.ts +0 -0
  50. /package/dist/types/{functions → src/functions}/remove-from-json-file.d.ts +0 -0
  51. /package/dist/types/{functions → src/functions}/update-config-file.d.ts +0 -0
  52. /package/dist/types/{functions → src/functions}/update-json-file.d.ts +0 -0
  53. /package/dist/types/{functions → src/functions}/update-text-file.d.ts +0 -0
  54. /package/dist/types/{terminal → src/terminal}/prompt-user.d.ts +0 -0
  55. /package/dist/types/{terminal → src/terminal}/show-error.d.ts +0 -0
  56. /package/dist/types/{terminal → src/terminal}/show-message.d.ts +0 -0
  57. /package/dist/types/{types → src/types}/json.d.ts +0 -0
  58. /package/dist/types/{utils → src/utils}/get-env-value.d.ts +0 -0
  59. /package/dist/types/{utils → src/utils}/parse-qualified-path.d.ts +0 -0
  60. /package/dist/types/{utils → src/utils}/resolve-package-path.d.ts +0 -0
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 Alois Sečkár
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.
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Alois Sečkár
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 CHANGED
@@ -1,265 +1,281 @@
1
- # COSCA
2
- Library of file-writing functions that help building CLI scripts for making changes in target projects - like adding default configuration files or new sections in `package.json`.
3
-
4
- The first experimental "customers" are my [Nuxt Spec](https://github.com/AloisSeckar/nuxt-spec) and [Nuxt Ignis](https://github.com/AloisSeckar/nuxt-ignis) projects.
5
-
6
- The **"COSCA"** abbreviation stands for **CO**de **SCA**ffolding which points out the library's purpose of providing methods for altering existing and adding new files from scratch using Node-based filesystem APIs.
7
-
8
- ## How to use
9
-
10
- **NOTE:** The library is **ESM only** and it is advised to use with at least **Node 18**.
11
-
12
- `npm install elrh-cosca` to include into your project.
13
-
14
- ### List of file-manipulation functions
15
-
16
- #### `createFileFromTemplate`
17
-
18
- ```ts
19
- async function createFileFromTemplate(
20
- templateFile: string, targetFile: string, force: boolean = false, prompt: string = ''
21
- ): Promise<void>
22
- ```
23
-
24
- Gets a file definition from given `templateFile` and will create a fresh copy in target project.
25
-
26
- Path to `templateFile` must be prefixed with the package name to allow proper resolution, e.g. `your-package:path/to/template`. The package name can be scoped.
27
-
28
- Path to `targetFile` is relative to `process.cwd()` which allows consumers to run `npx your-script` in their project roots during development. Several checks are in place to prevent accidental and malicious paths being passed in. Path traversal outside of CWD or providing absolute paths is disallowed. If the target directory does not exist, it will be automatically created.
29
-
30
- By default the function asks for confirmation before attempting to create the file and if the file with the same name as `targetFile` is detected. Setting the last optional parameter `force` to `true` will suppress manual confirmation prompts. Passing a custom `prompt` allows tailoring your own question to the user.
31
-
32
- #### `createFileFromWebTemplate`
33
-
34
- ```ts
35
- async function createFileFromWebTemplate(
36
- url: string, targetFile: string, force: boolean = false, prompt: string = ''
37
- ): Promise<void>
38
- ```
39
-
40
- Gets a file definition from given `url` and will create a fresh copy in target project.
41
-
42
- Contents of `url` must be accessible via `node:https.get` function and will be fetched as raw text data.
43
-
44
- Path to `targetFile` is relative to `process.cwd()` which allows consumers to run `npx your-script` in their project roots during development. Several checks are in place to prevent accidental and malicious paths being passed in. Path traversal outside of CWD or providing absolute paths is disallowed. If the target directory does not exist, it will be automatically created.
45
-
46
- By default the function asks for confirmation before attempting to create the file and if the file with the same name as `targetFile` is detected. Setting the last optional parameter `force` to `true` will suppress manual confirmation prompts. Passing a custom `prompt` allows tailoring your own question to the user.
47
-
48
- #### `updateConfigFile`
49
-
50
- ```ts
51
- async function updateConfigFile(
52
- targetFile: string, newConfig: Record<string | number | symbol, any>,
53
- force: boolean = false, prompt: string = ''
54
- ): Promise<void>
55
- ```
56
-
57
- Takes a path to a configuration file and updates it with the provided `newConfig` object.
58
-
59
- Path to `targetFile` is relative to `process.cwd()`. Several checks are in place to prevent accidental and malicious paths being passed in. Path traversal outside of CWD or providing absolute paths is disallowed. The file currently must use ESM format with either `default` or named export of **exactly one** configuration object or function call with a configuration object as its argument.
60
-
61
- The merger is performed using [unjs/magicast](https://github.com/unjs/magicast). It should:
62
- - preserve comments
63
- - work recursively to allow deep-merge
64
- - extend existing object with new keys from `newConfig`
65
- - overwrite keys with same name with values from `newConfig`
66
- - create a unique-union in case of arrays
67
- Please [report](https://github.com/AloisSeckar/elrh-cosca/issues) any logical flaws and issues of the process.
68
-
69
- **Warning**: The function will fail, if the extracted object is proxied (e.g. when created using `defu`). In such case, the error would be:
70
-
71
- ```
72
- TypeError: 'set' on proxy: trap returned falsish for property '<YOUR_PROPERTY>'
73
- ```
74
-
75
- If possible, you need to alter your logic, e.g. by creating a new object via the spread operator.
76
-
77
-
78
- By default the function asks for confirmation before attempting to alter the `targetFile`. Setting the last optional parameter `force` to `true` will suppress manual confirmation prompts. Passing a custom `prompt` allows tailoring your own question to the user.
79
-
80
- #### `updateJsonFile`
81
-
82
- ```ts
83
- async function updateJsonFile(
84
- targetFile: string, jsonKey: string, patch: JsonValue,
85
- force: boolean = false, prompt: string = ''
86
- ): Promise<void>
87
- ```
88
-
89
- Takes a path to a JSON file and injects `patch` under `jsonKey` key. A `patch` is of `JsonValue` - a custom type defined as follows:
90
-
91
- ```ts
92
- type JsonPrimitive = string | number | boolean | null
93
- type JsonObject = { [key: string]: JsonValue }
94
- type JsonArray = JsonValue[]
95
- type JsonValue = JsonPrimitive | JsonObject | JsonArray
96
- ```
97
-
98
- Path to `targetFile` is relative to `process.cwd()`. Several checks are in place to prevent accidental and malicious paths being passed in. Path traversal outside of CWD or providing absolute paths is disallowed. The file must be a valid JSON file. It is parsed using plain `JSON.parse`.
99
-
100
- Currently it only allows adding new values under top-level keys. If the `jsonKey` exists, new values are merged into existing ones. Otherwise, new key is added. The function tracks if any real change was made and notifies the user if not.
101
-
102
- By default the function asks for confirmation before attempting to alter the `targetFile`. Setting the last optional parameter `force` to `true` will suppress manual confirmation prompts. Passing a custom `prompt` allows tailoring your own question to the user.
103
-
104
- #### `updateTextFile`
105
-
106
- ```ts
107
- async function updateTextFile(
108
- targetFile: string, rowsToAdd: string[], force: boolean = false, prompt: string = ''
109
- ): Promise<void>
110
- ```
111
-
112
- Takes a path to a plain text file and injects `rowsToAdd` at the end of the file, **providing they are not already present in the file**. The function tracks if any real change was made and notifies the user if not.
113
-
114
- Path to `targetFile` is relative to `process.cwd()`. Several checks are in place to prevent accidental and malicious paths being passed in. Path traversal outside of CWD or providing absolute paths is disallowed.
115
-
116
- By default the function asks for confirmation before attempting to alter the `targetFile`. Setting the last optional parameter `force` to `true` will suppress manual confirmation prompts. Passing a custom `prompt` allows tailoring your own question to the user.
117
-
118
- #### `removeFromJsonFile`
119
-
120
- ```ts
121
- async function removeFromJsonFile(
122
- targetFile: string, jsonKey: string, force: boolean = false, prompt: string = ''
123
- ): Promise<void>
124
- ```
125
-
126
- Takes a path to a JSON file and removes the specified `jsonKey`.
127
-
128
- Path to `targetFile` is relative to `process.cwd()`. Several checks are in place to prevent accidental and malicious paths being passed in. Path traversal outside of CWD or providing absolute paths is disallowed. The file must be a valid JSON file. It is parsed using plain `JSON.parse`.
129
-
130
- Given `jsonKey` might point to a nested key using dot notation, e.g. `a.b.c`. If the key is not present, the function does nothing.
131
-
132
- By default the function asks for confirmation before attempting to alter the `targetFile`. Setting the last optional parameter `force` to `true` will suppress manual confirmation prompts. Passing a custom `prompt` allows tailoring your own question to the user.
133
-
134
- #### `deletePath`
135
-
136
- ```ts
137
- async function deletePath(
138
- targetPath: string, force: boolean = false, prompt: string = ''
139
- ): Promise<void>
140
- ```
141
-
142
- Deletes given `targetPath` from FS. Path is resolved relatively to `process.cwd()`. Several checks are in place to prevent accidental and malicious paths being passed in. Path traversal outside of CWD or providing absolute paths is disallowed.
143
-
144
- If the `targetPath` does not exist, the function does nothing.
145
-
146
- By default the function asks for confirmation before attempting to delete the `targetPath`. Setting the last optional parameter `force` to `true` will suppress manual confirmation prompts. Passing a custom `prompt` allows tailoring your own question to the user.
147
-
148
- ### List of content checkers
149
-
150
- #### `pathExists`
151
-
152
- ```ts
153
- function pathExists(
154
- targetPath: string
155
- ): boolean
156
- ```
157
-
158
- Checks if the specified `targetPath` exists on FS. Path is resolved relatively to `process.cwd()`. Several checks are in place to prevent accidental and malicious paths being passed in. Path traversal outside of CWD or providing absolute paths is disallowed.
159
-
160
- If the path exists , the function returns true, false otherwise.
161
-
162
- #### `hasJsonKey`
163
-
164
- ```ts
165
- function hasJsonKey(
166
- targetFile: string, jsonKey: string
167
- ): boolean
168
- ```
169
-
170
- Checks whether given `jsonKey` exists in JSON file located at `targetFile`. Path is resolved relatively to `process.cwd()`. Several checks are in place to prevent accidental and malicious paths being passed in. Path traversal outside of CWD or providing absolute paths is disallowed.
171
-
172
- Given `jsonKey` might point to a nested key using dot notation, e.g. `a.b.c`. If the key is present, the function returns true, false otherwise.
173
-
174
- #### `hasText`
175
-
176
- ```ts
177
- function hasText(
178
- targetFile: string, pattern: string | RegExp, exact: boolean = false
179
- ): boolean
180
- ```
181
-
182
- Checks whether given `pattern` exists in text file located at `targetFile`. Path is resolved relatively to `process.cwd()`. Several checks are in place to prevent accidental and malicious paths being passed in. Path traversal outside of CWD or providing absolute paths is disallowed.
183
-
184
- The `pattern` might be a plain string or a regular expression. If it is present, the function returns true, false otherwise. By default partial matches are allowed for string patterns. If you set optional `exact` parameter to true, the string must completely match at least one line in the file. However, surrounding whitespaces are trimmed in both cases.
185
-
186
- #### `getPackageManager`
187
-
188
- ```ts
189
- function getPackageManager(): 'npm' | 'yarn' | 'pnpm' | 'deno' | 'bun'
190
- ```
191
-
192
- Tries to detect the package manager used in the current environment by checking for specific global variables and user agent strings. Fallbacks to `npm` if common checks fail to detect otherwise.
193
-
194
- ### List of terminal helpers
195
-
196
- #### `promptUser`
197
-
198
- ```ts
199
- async function promptUser(
200
- question: string,
201
- options?: { input?: NodeJS.ReadableStream; output?: NodeJS.WritableStream }
202
- ): Promise<boolean>
203
- ```
204
-
205
- Prints out a `question` to the console and waits for the input. Returns `true` when `y` is pressed and `false` otherwise.
206
-
207
- By default it uses `process.stdin` and `process.stdout` streams. To use custom NodeJS streams, `options` object with `input` and `output` properties can be optionally passed.
208
-
209
- #### `showMessage`
210
-
211
- ```ts
212
- async function showMessage(message: string, newlines: number = 1): Promise<void>
213
- ```
214
-
215
- Prints out a `message` to `process.stdout` and adds the specified number of newlines after it (default is 1).
216
-
217
- #### `showError`
218
-
219
- ```ts
220
- async function showError(message: string, newlines: number = 1): Promise<void>
221
- ```
222
-
223
- Prints out a `message` to `process.stderr` and adds the specified number of newlines after it (default is 1).
224
-
225
- ### List of other utils
226
-
227
- #### `getEnvValue`
228
-
229
- ```ts
230
- export function getEnvValue(
231
- key: string, envFilePath: string = resolve(process.cwd(), '.env')
232
- ): string | undefined
233
- ```
234
-
235
- Reads a `.env` file and returns the value of the specified key or `undefined` if key not found. By default it reads from `.env` in the current working directory (usually the root of the project). You can specify a custom path to `.env` file as the second `envFilePath` parameter.
236
-
237
- #### `parseQualifiedPath`
238
-
239
- ```ts
240
- function parseQualifiedPath(path: string): { pkg: string; file: string }
241
- ```
242
-
243
- Expects path to file in `"package:relative/path/to/file"` format and splits it into `{ pkg, file }`. The package name can be scoped (e.g. `@scope/package`).
244
-
245
- #### `resolvePackagePath`
246
-
247
- ```ts
248
- function resolvePackagePath(pkg: string): string
249
- ```
250
-
251
- Resolve a package's installed root directory *from the target app* - which can be either from within itself during development or from corresponding package dir inside *node_modules*. The package name can be scoped (e.g. `@scope/package`).
252
-
253
- ## Tech stack
254
-
255
- - Developed with [TypeScript](https://www.typescriptlang.org/) in mind
256
- - Using [magicast](https://github.com/unjs/magicast) for parsing files
257
- - Build with [Vite](https://vitejs.dev/)
258
- - Tested with [Vitest](https://vitest.dev/)
259
-
260
- See [Changelog](https://github.com/AloisSeckar/elrh-cosca/blob/main/CHANGELOG.md) for project history and development.
261
-
262
- ## Report bugs & contact
263
-
264
- Use GitHub issues to report bugs / propose enhancements / give feedback:
265
- https://github.com/AloisSeckar/elrh-cosca/issues
1
+ # COSCA
2
+
3
+ Library of file-writing functions that help building CLI scripts for making changes in target projects - like adding default configuration files or new sections in `package.json`.
4
+
5
+ The first experimental "customers" are my [Nuxt Spec](https://github.com/AloisSeckar/nuxt-spec) and [Nuxt Ignis](https://github.com/AloisSeckar/nuxt-ignis) projects.
6
+
7
+ The **"COSCA"** abbreviation stands for **CO**de **SCA**ffolding which points out the library's purpose of providing methods for altering existing and adding new files from scratch using Node-based filesystem APIs.
8
+
9
+ ## How to use
10
+
11
+ **NOTE:** The library is **ESM only** and it is advised to use with at least **Node 18**.
12
+
13
+ `npm install elrh-cosca` to include into your project.
14
+
15
+ ### List of file-manipulation functions
16
+
17
+ #### `createFileFromTemplate`
18
+
19
+ ```ts
20
+ async function createFileFromTemplate(
21
+ templateFile: string, targetFile: string, force: boolean = false, prompt: string = ''
22
+ ): Promise<void>
23
+ ```
24
+
25
+ Gets a file definition from given `templateFile` and will create a fresh copy in target project.
26
+
27
+ Path to `templateFile` must be prefixed with the package name to allow proper resolution, e.g. `your-package:path/to/template`. The package name can be scoped.
28
+
29
+ Path to `targetFile` is relative to `process.cwd()` which allows consumers to run `npx your-script` in their project roots during development. Several checks are in place to prevent accidental and malicious paths being passed in. Path traversal outside of CWD or providing absolute paths is disallowed. If the target directory does not exist, it will be automatically created.
30
+
31
+ By default the function asks for confirmation before attempting to create the file and if the file with the same name as `targetFile` is detected. Setting the last optional parameter `force` to `true` will suppress manual confirmation prompts. Passing a custom `prompt` allows tailoring your own question to the user.
32
+
33
+ #### `createFileFromWebTemplate`
34
+
35
+ ```ts
36
+ async function createFileFromWebTemplate(
37
+ url: string, targetFile: string, force: boolean = false, prompt: string = ''
38
+ ): Promise<void>
39
+ ```
40
+
41
+ Gets a file definition from given `url` and will create a fresh copy in target project.
42
+
43
+ Contents of `url` must be accessible via `node:https.get` function and will be fetched as raw text data.
44
+
45
+ Path to `targetFile` is relative to `process.cwd()` which allows consumers to run `npx your-script` in their project roots during development. Several checks are in place to prevent accidental and malicious paths being passed in. Path traversal outside of CWD or providing absolute paths is disallowed. If the target directory does not exist, it will be automatically created.
46
+
47
+ By default the function asks for confirmation before attempting to create the file and if the file with the same name as `targetFile` is detected. Setting the last optional parameter `force` to `true` will suppress manual confirmation prompts. Passing a custom `prompt` allows tailoring your own question to the user.
48
+
49
+ #### `updateConfigFile`
50
+
51
+ ```ts
52
+ async function updateConfigFile(
53
+ targetFile: string, newConfig: Record<string | number | symbol, any>,
54
+ force: boolean = false, prompt: string = ''
55
+ ): Promise<void>
56
+ ```
57
+
58
+ Takes a path to a configuration file and updates it with the provided `newConfig` object.
59
+
60
+ Path to `targetFile` is relative to `process.cwd()`. Several checks are in place to prevent accidental and malicious paths being passed in. Path traversal outside of CWD or providing absolute paths is disallowed. The file currently must use ESM format with either `default` or named export of **exactly one** configuration object or function call with a configuration object as its argument.
61
+
62
+ The merger is performed using [unjs/magicast](https://github.com/unjs/magicast). It should:
63
+
64
+ - preserve comments
65
+ - work recursively to allow deep-merge
66
+ - extend existing object with new keys from `newConfig`
67
+ - overwrite keys with same name with values from `newConfig`
68
+ - create a unique-union in case of arrays
69
+ Please [report](https://github.com/AloisSeckar/elrh-cosca/issues) any logical flaws and issues of the process.
70
+
71
+ **Warning**: The function will fail, if the extracted object is proxied (e.g. when created using `defu`). In such case, the error would be:
72
+
73
+ ```text
74
+ TypeError: 'set' on proxy: trap returned falsish for property '<YOUR_PROPERTY>'
75
+ ```
76
+
77
+ If possible, you need to alter your logic, e.g. by creating a new object via the spread operator.
78
+
79
+ By default the function asks for confirmation before attempting to alter the `targetFile`. Setting the last optional parameter `force` to `true` will suppress manual confirmation prompts. Passing a custom `prompt` allows tailoring your own question to the user.
80
+
81
+ #### `updateJsonFile`
82
+
83
+ ```ts
84
+ async function updateJsonFile(
85
+ targetFile: string, jsonKey: string, patch: JsonValue,
86
+ force: boolean = false, prompt: string = ''
87
+ ): Promise<void>
88
+ ```
89
+
90
+ Takes a path to a JSON file and injects `patch` under `jsonKey` key. A `patch` is of `JsonValue` - a custom type defined as follows:
91
+
92
+ ```ts
93
+ type JsonPrimitive = string | number | boolean | null
94
+ type JsonObject = { [key: string]: JsonValue }
95
+ type JsonArray = JsonValue[]
96
+ type JsonValue = JsonPrimitive | JsonObject | JsonArray
97
+ ```
98
+
99
+ Path to `targetFile` is relative to `process.cwd()`. Several checks are in place to prevent accidental and malicious paths being passed in. Path traversal outside of CWD or providing absolute paths is disallowed. The file must be a valid JSON file. It is parsed using plain `JSON.parse`.
100
+
101
+ Currently it only allows adding new values under top-level keys. If the `jsonKey` exists, new values are merged into existing ones. Otherwise, new key is added. The function tracks if any real change was made and notifies the user if not.
102
+
103
+ By default the function asks for confirmation before attempting to alter the `targetFile`. Setting the last optional parameter `force` to `true` will suppress manual confirmation prompts. Passing a custom `prompt` allows tailoring your own question to the user.
104
+
105
+ #### `updateTextFile`
106
+
107
+ ```ts
108
+ async function updateTextFile(
109
+ targetFile: string, rowsToAdd: string[], force: boolean = false, prompt: string = ''
110
+ ): Promise<void>
111
+ ```
112
+
113
+ Takes a path to a plain text file and injects `rowsToAdd` at the end of the file, **providing they are not already present in the file**. The function tracks if any real change was made and notifies the user if not.
114
+
115
+ Path to `targetFile` is relative to `process.cwd()`. Several checks are in place to prevent accidental and malicious paths being passed in. Path traversal outside of CWD or providing absolute paths is disallowed.
116
+
117
+ By default the function asks for confirmation before attempting to alter the `targetFile`. Setting the last optional parameter `force` to `true` will suppress manual confirmation prompts. Passing a custom `prompt` allows tailoring your own question to the user.
118
+
119
+ #### `removeFromJsonFile`
120
+
121
+ ```ts
122
+ async function removeFromJsonFile(
123
+ targetFile: string, jsonKey: string, force: boolean = false, prompt: string = ''
124
+ ): Promise<void>
125
+ ```
126
+
127
+ Takes a path to a JSON file and removes the specified `jsonKey`.
128
+
129
+ Path to `targetFile` is relative to `process.cwd()`. Several checks are in place to prevent accidental and malicious paths being passed in. Path traversal outside of CWD or providing absolute paths is disallowed. The file must be a valid JSON file. It is parsed using plain `JSON.parse`.
130
+
131
+ Given `jsonKey` might point to a nested key using dot notation, e.g. `a.b.c`. If the key is not present, the function does nothing.
132
+
133
+ By default the function asks for confirmation before attempting to alter the `targetFile`. Setting the last optional parameter `force` to `true` will suppress manual confirmation prompts. Passing a custom `prompt` allows tailoring your own question to the user.
134
+
135
+ #### `removeFromTextFile`
136
+
137
+ ```ts
138
+ async function removeFromTextFile(
139
+ targetFile: string, searchText: string, force: boolean = false, prompt: string = ''
140
+ ): Promise<void>
141
+ ```
142
+
143
+ Takes a path to a plain text file and removes all lines that include the given `searchText`. The matching is done using `String.includes()`, so partial matches within a line will cause that line to be removed. The function tracks if any real change was made and notifies the user if not.
144
+
145
+ Path to `targetFile` is relative to `process.cwd()`. Several checks are in place to prevent accidental and malicious paths being passed in. Path traversal outside of CWD or providing absolute paths is disallowed.
146
+
147
+ By default the function asks for confirmation before attempting to alter the `targetFile`. Setting the last optional parameter `force` to `true` will suppress manual confirmation prompts. Passing a custom `prompt` allows tailoring your own question to the user.
148
+
149
+ #### `deletePath`
150
+
151
+ ```ts
152
+ async function deletePath(
153
+ targetPath: string, force: boolean = false, prompt: string = ''
154
+ ): Promise<void>
155
+ ```
156
+
157
+ Deletes given `targetPath` from FS. Path is resolved relatively to `process.cwd()`. Several checks are in place to prevent accidental and malicious paths being passed in. Path traversal outside of CWD or providing absolute paths is disallowed.
158
+
159
+ If the `targetPath` does not exist, the function does nothing.
160
+
161
+ By default the function asks for confirmation before attempting to delete the `targetPath`. Setting the last optional parameter `force` to `true` will suppress manual confirmation prompts. Passing a custom `prompt` allows tailoring your own question to the user.
162
+
163
+ ### List of content checkers
164
+
165
+ #### `pathExists`
166
+
167
+ ```ts
168
+ function pathExists(
169
+ targetPath: string
170
+ ): boolean
171
+ ```
172
+
173
+ Checks if the specified `targetPath` exists on FS. Path is resolved relatively to `process.cwd()`. Several checks are in place to prevent accidental and malicious paths being passed in. Path traversal outside of CWD or providing absolute paths is disallowed.
174
+
175
+ If the path exists , the function returns true, false otherwise.
176
+
177
+ #### `hasJsonKey`
178
+
179
+ ```ts
180
+ function hasJsonKey(
181
+ targetFile: string, jsonKey: string
182
+ ): boolean
183
+ ```
184
+
185
+ Checks whether given `jsonKey` exists in JSON file located at `targetFile`. Path is resolved relatively to `process.cwd()`. Several checks are in place to prevent accidental and malicious paths being passed in. Path traversal outside of CWD or providing absolute paths is disallowed.
186
+
187
+ Given `jsonKey` might point to a nested key using dot notation, e.g. `a.b.c`. If the key is present, the function returns true, false otherwise.
188
+
189
+ #### `hasText`
190
+
191
+ ```ts
192
+ function hasText(
193
+ targetFile: string, pattern: string | RegExp, exact: boolean = false
194
+ ): boolean
195
+ ```
196
+
197
+ Checks whether given `pattern` exists in text file located at `targetFile`. Path is resolved relatively to `process.cwd()`. Several checks are in place to prevent accidental and malicious paths being passed in. Path traversal outside of CWD or providing absolute paths is disallowed.
198
+
199
+ The `pattern` might be a plain string or a regular expression. If it is present, the function returns true, false otherwise. By default partial matches are allowed for string patterns. If you set optional `exact` parameter to true, the string must completely match at least one line in the file. However, surrounding whitespaces are trimmed in both cases.
200
+
201
+ #### `getPackageManager`
202
+
203
+ ```ts
204
+ function getPackageManager(): 'npm' | 'yarn' | 'pnpm' | 'deno' | 'bun'
205
+ ```
206
+
207
+ Tries to detect the package manager used in the current environment by checking for specific global variables and user agent strings. Fallbacks to `npm` if common checks fail to detect otherwise.
208
+
209
+ ### List of terminal helpers
210
+
211
+ #### `promptUser`
212
+
213
+ ```ts
214
+ async function promptUser(
215
+ question: string,
216
+ options?: { input?: NodeJS.ReadableStream; output?: NodeJS.WritableStream }
217
+ ): Promise<boolean>
218
+ ```
219
+
220
+ Prints out a `question` to the console and waits for the input. Returns `true` when `y` is pressed and `false` otherwise.
221
+
222
+ By default it uses `process.stdin` and `process.stdout` streams. To use custom NodeJS streams, `options` object with `input` and `output` properties can be optionally passed.
223
+
224
+ #### `showMessage`
225
+
226
+ ```ts
227
+ async function showMessage(message: string, newlines: number = 1): Promise<void>
228
+ ```
229
+
230
+ Prints out a `message` to `process.stdout` and adds the specified number of newlines after it (default is 1).
231
+
232
+ #### `showError`
233
+
234
+ ```ts
235
+ async function showError(message: string, newlines: number = 1): Promise<void>
236
+ ```
237
+
238
+ Prints out a `message` to `process.stderr` and adds the specified number of newlines after it (default is 1).
239
+
240
+ ### List of other utils
241
+
242
+ #### `getEnvValue`
243
+
244
+ ```ts
245
+ export function getEnvValue(
246
+ key: string, envFilePath: string = resolve(process.cwd(), '.env')
247
+ ): string | undefined
248
+ ```
249
+
250
+ Reads a `.env` file and returns the value of the specified key or `undefined` if key not found. By default it reads from `.env` in the current working directory (usually the root of the project). You can specify a custom path to `.env` file as the second `envFilePath` parameter.
251
+
252
+ #### `parseQualifiedPath`
253
+
254
+ ```ts
255
+ function parseQualifiedPath(path: string): { pkg: string; file: string }
256
+ ```
257
+
258
+ Expects path to file in `"package:relative/path/to/file"` format and splits it into `{ pkg, file }`. The package name can be scoped (e.g. `@scope/package`).
259
+
260
+ #### `resolvePackagePath`
261
+
262
+ ```ts
263
+ function resolvePackagePath(pkg: string): string
264
+ ```
265
+
266
+ Resolve a package's installed root directory *from the target app* - which can be either from within itself during development or from corresponding package dir inside *node_modules*. The package name can be scoped (e.g. `@scope/package`).
267
+
268
+ ## Tech stack
269
+
270
+ - Developed with [TypeScript](https://www.typescriptlang.org/) in mind
271
+ - Using [magicast](https://github.com/unjs/magicast) for parsing files
272
+ - Build with [Vite](https://vitejs.dev/)
273
+ - Tested with [Vitest](https://vitest.dev/)
274
+
275
+ See [Changelog](https://github.com/AloisSeckar/elrh-cosca/blob/main/CHANGELOG.md) for project history and development.
276
+
277
+ ## Report bugs & contact
278
+
279
+ Use GitHub issues to report bugs / propose enhancements / give feedback:
280
+
281
+ [https://github.com/AloisSeckar/elrh-cosca/issues](https://github.com/AloisSeckar/elrh-cosca/issues)