elrh-cosca 0.3.4 → 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.
- package/LICENSE +21 -21
- package/README.md +281 -265
- package/dist/elrh-cosca.mjs +14305 -16595
- package/dist/types/index.d.ts +1 -1
- package/dist/types/src/checks/has-text.d.ts +10 -0
- package/dist/types/src/functions/remove-from-text-file.d.ts +11 -0
- package/dist/types/{main.d.ts → src/main.d.ts} +2 -1
- package/dist/types/test/checks-get-package-manager.test.d.ts +1 -0
- package/dist/types/test/checks-has-json-key.test.d.ts +1 -0
- package/dist/types/test/checks-has-text.test.d.ts +1 -0
- package/dist/types/test/checks-path-exists.test.d.ts +1 -0
- package/dist/types/test/cosca-test-setup.d.ts +1 -0
- package/dist/types/test/cosca-test-utils.d.ts +45 -0
- package/dist/types/test/fixtures/config-file-default.d.ts +10 -0
- package/dist/types/test/fixtures/config-file-named.d.ts +9 -0
- package/dist/types/test/functions-create-file-from-template.test.d.ts +1 -0
- package/dist/types/test/functions-create-file-from-web-template.test.d.ts +1 -0
- package/dist/types/test/functions-delete-path.test.d.ts +1 -0
- package/dist/types/test/functions-remove-from-json-file.test.d.ts +1 -0
- package/dist/types/test/functions-remove-from-text-file.test.d.ts +1 -0
- package/dist/types/test/functions-update-config-file.test.d.ts +1 -0
- package/dist/types/test/functions-update-json-file.test.d.ts +1 -0
- package/dist/types/test/functions-update-text-file.test.d.ts +1 -0
- package/dist/types/test/private-check-path.test.d.ts +1 -0
- package/dist/types/test/snapshots/updated-config-file-default-1.d.ts +13 -0
- package/dist/types/test/snapshots/updated-config-file-default-2.d.ts +14 -0
- package/dist/types/test/snapshots/updated-config-file-default-3.d.ts +16 -0
- package/dist/types/test/snapshots/updated-config-file-default-4.d.ts +18 -0
- package/dist/types/test/snapshots/updated-config-file-named-1.d.ts +12 -0
- package/dist/types/test/snapshots/updated-config-file-named-2.d.ts +13 -0
- package/dist/types/test/snapshots/updated-config-file-named-3.d.ts +15 -0
- package/dist/types/test/snapshots/updated-config-file-named-4.d.ts +17 -0
- package/dist/types/test/terminal-prompt-user.test.d.ts +1 -0
- package/dist/types/test/terninal-show-error.test.d.ts +1 -0
- package/dist/types/test/terninal-show-message.test.d.ts +1 -0
- package/dist/types/test/utils-get-env-value.test.d.ts +1 -0
- package/dist/types/test/utils-parse-qualified-path.test.d.ts +1 -0
- package/dist/types/test/utils-resolve-package-path.test.d.ts +1 -0
- package/package.json +6 -6
- package/test/cosca-test.js +55 -55
- package/dist/types/checks/has-text.d.ts +0 -9
- /package/dist/types/{_private → src/_private}/check-path.d.ts +0 -0
- /package/dist/types/{_private → src/_private}/deep-merge-object.d.ts +0 -0
- /package/dist/types/{_private → src/_private}/fetch-file.d.ts +0 -0
- /package/dist/types/{checks → src/checks}/get-package-manager.d.ts +0 -0
- /package/dist/types/{checks → src/checks}/has-json-key.d.ts +0 -0
- /package/dist/types/{checks → src/checks}/path-exists.d.ts +0 -0
- /package/dist/types/{functions → src/functions}/create-file-from-template.d.ts +0 -0
- /package/dist/types/{functions → src/functions}/create-file-from-web-template.d.ts +0 -0
- /package/dist/types/{functions → src/functions}/delete-path.d.ts +0 -0
- /package/dist/types/{functions → src/functions}/remove-from-json-file.d.ts +0 -0
- /package/dist/types/{functions → src/functions}/update-config-file.d.ts +0 -0
- /package/dist/types/{functions → src/functions}/update-json-file.d.ts +0 -0
- /package/dist/types/{functions → src/functions}/update-text-file.d.ts +0 -0
- /package/dist/types/{terminal → src/terminal}/prompt-user.d.ts +0 -0
- /package/dist/types/{terminal → src/terminal}/show-error.d.ts +0 -0
- /package/dist/types/{terminal → src/terminal}/show-message.d.ts +0 -0
- /package/dist/types/{types → src/types}/json.d.ts +0 -0
- /package/dist/types/{utils → src/utils}/get-env-value.d.ts +0 -0
- /package/dist/types/{utils → src/utils}/parse-qualified-path.d.ts +0 -0
- /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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
-
|
|
65
|
-
-
|
|
66
|
-
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
type
|
|
94
|
-
type
|
|
95
|
-
type
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
```
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
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)
|