elrh-cosca 0.2.7 → 0.3.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/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
- pathToFile: string, newConfig: Record<string | number | symbol, any>,
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
@@ -66,20 +66,36 @@ The merger is performed using [unjs/magicast](https://github.com/unjs/magicast).
66
66
  - create a unique-union in case of arrays
67
67
  Please [report](https://github.com/AloisSeckar/elrh-cosca/issues) any logical flaws and issues of the process.
68
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
+
69
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. Passsing a custom `prompt` allows tailoring your own question to the user.
70
79
 
71
80
  #### `updateJsonFile`
72
81
 
73
82
  ```ts
74
83
  async function updateJsonFile(
75
- pathToFile: string, jsonKey: string, newValues: Record<string | number | symbol, any>,
84
+ targetFile: string, jsonKey: string, patch: JsonValue,
76
85
  force: boolean = false, prompt: string = ''
77
86
  ): Promise<void>
78
87
  ```
79
88
 
80
- Takes a path to a JSON file and injects `newValues` under `jsonKey` key.
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
+ ```
81
97
 
82
- 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`.
83
99
 
84
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.
85
101
 
@@ -89,14 +105,46 @@ By default the function asks for confirmation before attempting to alter the `ta
89
105
 
90
106
  ```ts
91
107
  async function updateTextFile(
92
- pathToFile: string, rowsToAdd: string[], force: boolean = false, prompt: string = ''
108
+ targetFile: string, rowsToAdd: string[], force: boolean = false, prompt: string = ''
93
109
  ): Promise<void>
94
110
  ```
95
111
 
96
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.
97
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
+
98
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.
99
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
+
100
148
  ### List of terminal helpers
101
149
 
102
150
  #### `promptUser`