elrh-cosca 0.1.5 → 0.2.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 CHANGED
@@ -7,20 +7,125 @@ The **"COSCA"** abbreviation stands for **CO**de **SCA**ffolding which points ou
7
7
 
8
8
  ## How to use
9
9
 
10
+ **NOTE:** The library is **ESM only** and it is advised to use with at least **Node 18**.
11
+
10
12
  `npm install elrh-cosca` to include into your project.
11
13
 
12
- List of available functions:
13
- - `createFileFromTemplate`
14
- - `promptUser`
15
- - `updateJsonFile`
16
- - `updateTextFile`
14
+ ### List of file-manipulation functions
15
+
16
+ #### `createFileFromTemplate`
17
+
18
+ ```ts
19
+ async function createFileFromTemplate(
20
+ templateFile: string, targetFile: string, force: boolean = false
21
+ ): Promise<void>
22
+ ```
23
+
24
+ Takes a path to a template file from your project and will create a fresh copy in target project when invoked.
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.
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.
31
+
32
+ #### `updateConfigFile`
33
+
34
+ ```ts
35
+ async function updateConfigFile(
36
+ pathToFile: string, newConfig: Record<string | number | symbol, any>, force: boolean = false
37
+ ): Promise<void>
38
+ ```
39
+
40
+ Prints out a `question` to the console and waits for the input. Returns `true` when `y` is pressed and `false` otherwise.
41
+
42
+ #### `updateConfigFile`
43
+
44
+ ```ts
45
+ async function updateConfigFile(
46
+ pathToFile: string, newConfig: Record<string | number | symbol, any>, force: boolean = false
47
+ ): Promise<void>
48
+ ```
49
+
50
+ Takes a path to a configuration file and updates it with the provided `newConfig` object.
51
+
52
+ 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.
53
+
54
+ The merger is performed using [unjs/magicast](https://github.com/unjs/magicast). It should:
55
+ - preserve comments
56
+ - work recursively to allow deep-merge
57
+ - extend existing object with new keys from `newConfig`
58
+ - overwrite keys with same name with values from `newConfig`
59
+ - create a unique-union in case of arrays
60
+ Please [report](https://github.com/AloisSeckar/elrh-cosca/issues) any logical flaws and issues of the process.
61
+
62
+ 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.
63
+
64
+ #### `updateJsonFile`
65
+
66
+ ```ts
67
+ async function updateJsonFile(
68
+ pathToFile: string, jsonKey: string, newValues: Record<string | number | symbol, any>, force: boolean = false
69
+ ): Promise<void>
70
+ ```
71
+
72
+ Takes a path to a JSON file and injects `newValues` under `jsonKey` key.
73
+
74
+ Path to `targetFile` is relative to `process.cwd()`. The file must be a valid JSON file. It is parsed using plain `JSON.parse`.
75
+
76
+ 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.
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.
79
+
80
+ #### `updateTextFile`
81
+
82
+ ```ts
83
+ async function updateTextFile(
84
+ pathToFile: string, rowsToAdd: string[], force: boolean = false
85
+ ): Promise<void>
86
+ ```
87
+
88
+ 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.
89
+
90
+ 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.
91
+
92
+ ### List of terminal helpers
93
+
94
+ #### `promptUser`
95
+
96
+ ```ts
97
+ async function promptUser(question: string): Promise<boolean>
98
+ ```
99
+
100
+ Prints out a `question` to the console and waits for the input. Returns `true` when `y` is pressed and `false` otherwise.
101
+
102
+ ### List of other utils
103
+
104
+ #### `parseQualifiedPath`
105
+
106
+ ```ts
107
+ function parseQualifiedPath(path: string): { pkg: string; file: string }
108
+ ```
109
+
110
+ 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`).
111
+
112
+ #### `resolvePackagePath`
113
+
114
+ ```ts
115
+ function resolvePackagePath(pkg: string): string
116
+ ```
117
+
118
+ 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`).
17
119
 
18
120
  ## Tech stack
19
121
 
20
- - Developed with [TypeScript](https://www.typescriptlang.org/)
122
+ - Developed with [TypeScript](https://www.typescriptlang.org/) in mind
123
+ - Using [magicast](https://github.com/unjs/magicast) for parsing files
21
124
  - Build with [Vite](https://vitejs.dev/)
22
125
  - Tested with [Vitest](https://vitest.dev/)
23
126
 
127
+ See [Changelog](https://github.com/AloisSeckar/elrh-cosca/blob/main/CHANGELOG.md) for project history and development.
128
+
24
129
  ## Report bugs & contact
25
130
 
26
131
  Use GitHub issues to report bugs / propose enhancements / give feedback: