elrh-cosca 0.2.8 → 0.3.1
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/README.md +65 -7
- package/dist/elrh-cosca.mjs +2398 -2268
- package/dist/types/_private/check-path.d.ts +4 -0
- package/dist/types/checks/has-json-key.d.ts +9 -0
- package/dist/types/checks/has-text.d.ts +9 -0
- package/dist/types/functions/create-file-from-template.d.ts +1 -1
- package/dist/types/functions/create-file-from-web-template.d.ts +1 -1
- package/dist/types/functions/delete-path.d.ts +10 -0
- package/dist/types/functions/remove-from-json-file.d.ts +11 -0
- package/dist/types/functions/update-config-file.d.ts +3 -3
- package/dist/types/functions/update-json-file.d.ts +4 -10
- package/dist/types/functions/update-text-file.d.ts +3 -3
- package/dist/types/main.d.ts +5 -1
- package/dist/types/types/json.d.ts +6 -0
- package/package.json +1 -1
- package/test/cosca-test.js +7 -1
package/README.md
CHANGED
|
@@ -25,7 +25,7 @@ Gets a file definition from given `templateFile` and will create a fresh copy in
|
|
|
25
25
|
|
|
26
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
27
|
|
|
28
|
-
Path to `targetFile` is relative to `process.cwd()` which allows consumers to run `npx your-script` in their project roots during development.
|
|
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
29
|
|
|
30
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. Passsing a custom `prompt` allows tailoring your own question to the user.
|
|
31
31
|
|
|
@@ -41,7 +41,7 @@ Gets a file definition from given `url` and will create a fresh copy in target p
|
|
|
41
41
|
|
|
42
42
|
Contents of `url` must be accessible via `node:https.get` function and will be fetched as raw text data.
|
|
43
43
|
|
|
44
|
-
Path to `targetFile` is relative to `process.cwd()` which allows consumers to run `npx your-script` in their project roots during development.
|
|
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
45
|
|
|
46
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. Passsing a custom `prompt` allows tailoring your own question to the user.
|
|
47
47
|
|
|
@@ -49,14 +49,14 @@ By default the function asks for confirmation before attempting to create the fi
|
|
|
49
49
|
|
|
50
50
|
```ts
|
|
51
51
|
async function updateConfigFile(
|
|
52
|
-
|
|
52
|
+
targetFile: string, newConfig: Record<string | number | symbol, any>,
|
|
53
53
|
force: boolean = false, prompt: string = ''
|
|
54
54
|
): Promise<void>
|
|
55
55
|
```
|
|
56
56
|
|
|
57
57
|
Takes a path to a configuration file and updates it with the provided `newConfig` object.
|
|
58
58
|
|
|
59
|
-
Path to `targetFile` is relative to `process.cwd()`. 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.
|
|
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
60
|
|
|
61
61
|
The merger is performed using [unjs/magicast](https://github.com/unjs/magicast). It should:
|
|
62
62
|
- preserve comments
|
|
@@ -81,7 +81,7 @@ By default the function asks for confirmation before attempting to alter the `ta
|
|
|
81
81
|
|
|
82
82
|
```ts
|
|
83
83
|
async function updateJsonFile(
|
|
84
|
-
|
|
84
|
+
targetFile: string, jsonKey: string, patch: JsonValue,
|
|
85
85
|
force: boolean = false, prompt: string = ''
|
|
86
86
|
): Promise<void>
|
|
87
87
|
```
|
|
@@ -95,7 +95,7 @@ type JsonArray = JsonValue[]
|
|
|
95
95
|
type JsonValue = JsonPrimitive | JsonObject | JsonArray
|
|
96
96
|
```
|
|
97
97
|
|
|
98
|
-
Path to `targetFile` is relative to `process.cwd()`. The file must be a valid JSON file. It is parsed using plain `JSON.parse`.
|
|
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
99
|
|
|
100
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
101
|
|
|
@@ -105,14 +105,72 @@ By default the function asks for confirmation before attempting to alter the `ta
|
|
|
105
105
|
|
|
106
106
|
```ts
|
|
107
107
|
async function updateTextFile(
|
|
108
|
-
|
|
108
|
+
targetFile: string, rowsToAdd: string[], force: boolean = false, prompt: string = ''
|
|
109
109
|
): Promise<void>
|
|
110
110
|
```
|
|
111
111
|
|
|
112
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
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. Passsing 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
|
+
|
|
114
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. Passsing a custom `prompt` allows tailoring your own question to the user.
|
|
115
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. Passsing a custom `prompt` allows tailoring your own question to the user.
|
|
147
|
+
|
|
148
|
+
### List of content checkers
|
|
149
|
+
|
|
150
|
+
#### `hasJsonKey`
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
function hasJsonKey(
|
|
154
|
+
targetFile: string, jsonKey: string
|
|
155
|
+
): boolean
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
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.
|
|
159
|
+
|
|
160
|
+
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.
|
|
161
|
+
|
|
162
|
+
#### `hasText`
|
|
163
|
+
|
|
164
|
+
```ts
|
|
165
|
+
function hasText(
|
|
166
|
+
targetFile: string, row: string
|
|
167
|
+
): boolean
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
Checks whether given `row` 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.
|
|
171
|
+
|
|
172
|
+
If the `row` is present, the function returns true, false otherwise. Row must be matched completely, but surrounding whitespaces are ignored.
|
|
173
|
+
|
|
116
174
|
### List of terminal helpers
|
|
117
175
|
|
|
118
176
|
#### `promptUser`
|