elrh-cosca 0.2.0 → 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,11 +7,13 @@ 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:**
14
+ ### List of file-manipulation functions
13
15
 
14
- ### `createFileFromTemplate`
16
+ #### `createFileFromTemplate`
15
17
 
16
18
  ```ts
17
19
  async function createFileFromTemplate(
@@ -27,15 +29,7 @@ Path to `targetFile` is relative to `process.cwd()` which allows consumers to ru
27
29
 
28
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.
29
31
 
30
- ### `promptUser`
31
-
32
- ```ts
33
- export async function promptUser(question: string): Promise<boolean>
34
- ```
35
-
36
- Prints out a `question` to the console and waits for the input. Returns `true` when `y` is pressed and `false` otherwise.
37
-
38
- ### `updateConfigFile`
32
+ #### `updateConfigFile`
39
33
 
40
34
  ```ts
41
35
  async function updateConfigFile(
@@ -45,7 +39,7 @@ async function updateConfigFile(
45
39
 
46
40
  Prints out a `question` to the console and waits for the input. Returns `true` when `y` is pressed and `false` otherwise.
47
41
 
48
- ### `updateConfigFile`
42
+ #### `updateConfigFile`
49
43
 
50
44
  ```ts
51
45
  async function updateConfigFile(
@@ -67,7 +61,7 @@ Please [report](https://github.com/AloisSeckar/elrh-cosca/issues) any logical fl
67
61
 
68
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.
69
63
 
70
- ### `updateJsonFile`
64
+ #### `updateJsonFile`
71
65
 
72
66
  ```ts
73
67
  async function updateJsonFile(
@@ -83,10 +77,10 @@ Currently it only allows adding new values under top-level keys. If the `jsonKey
83
77
 
84
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.
85
79
 
86
- ### `updateTextFile`
80
+ #### `updateTextFile`
87
81
 
88
82
  ```ts
89
- export async function updateTextFile(
83
+ async function updateTextFile(
90
84
  pathToFile: string, rowsToAdd: string[], force: boolean = false
91
85
  ): Promise<void>
92
86
  ```
@@ -95,6 +89,34 @@ Takes a path to a plain text file and injects `rowsToAdd` at the end of the file
95
89
 
96
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.
97
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`).
119
+
98
120
  ## Tech stack
99
121
 
100
122
  - Developed with [TypeScript](https://www.typescriptlang.org/) in mind
@@ -102,6 +124,8 @@ By default the function asks for confirmation before attempting to alter the `ta
102
124
  - Build with [Vite](https://vitejs.dev/)
103
125
  - Tested with [Vitest](https://vitest.dev/)
104
126
 
127
+ See [Changelog](https://github.com/AloisSeckar/elrh-cosca/blob/main/CHANGELOG.md) for project history and development.
128
+
105
129
  ## Report bugs & contact
106
130
 
107
131
  Use GitHub issues to report bugs / propose enhancements / give feedback:
@@ -16298,20 +16298,26 @@ async function Dl(a, l, i = !1) {
16298
16298
  const o = At(process.cwd(), a), u = await Pl(o);
16299
16299
  let h = u.exports?.default;
16300
16300
  if (!h) {
16301
- const P = Object.keys(u.exports);
16302
- P.length === 1 && (h = u.exports[P[0]]);
16301
+ const S = Object.keys(u.exports);
16302
+ S.length === 1 && (h = u.exports[S[0]]);
16303
16303
  }
16304
16304
  if (!h && typeof u.exports == "object" && u.exports !== null)
16305
16305
  throw new Error(`It is currently not possible to handle CommonJS module.exports syntax of ${a}`);
16306
- if (console.warn("f", a), console.warn("e", h), !h)
16306
+ if (!h)
16307
16307
  throw new Error(`No suitable config export found in ${a}`);
16308
16308
  const d = h.$type === "function-call" ? h.$args?.[0] : h;
16309
16309
  if (!d || typeof d != "object")
16310
16310
  throw new Error(`Could not access config object in ${a}`);
16311
- console.warn("o", d), console.warn("n", l), sn(d, l);
16312
- const { code: g } = tn(u);
16313
- Rs(o, g, "utf8");
16314
- }
16311
+ const g = JSON.stringify(d);
16312
+ sn(d, l);
16313
+ const P = JSON.stringify(d);
16314
+ if (g !== P) {
16315
+ const { code: S } = tn(u);
16316
+ Rs(o, S, "utf8"), console.log(`'${a}' file updated.`);
16317
+ } else
16318
+ console.log(`'${a}' file already up to date.`);
16319
+ } else
16320
+ console.log(`Updating '${a}' skipped.`);
16315
16321
  }
16316
16322
  async function Ol(a, l, i, r = !1) {
16317
16323
  if (r || await wt(
@@ -16360,7 +16366,9 @@ async function Ml(a, l, i = !1) {
16360
16366
  }
16361
16367
  export {
16362
16368
  wl as createFileFromTemplate,
16369
+ Ca as parseQualifiedPath,
16363
16370
  wt as promptUser,
16371
+ Ia as resolvePackagePath,
16364
16372
  Dl as updateConfigFile,
16365
16373
  Ol as updateJsonFile,
16366
16374
  Ml as updateTextFile
@@ -1,6 +1,8 @@
1
- import { createFileFromTemplate } from './lib/create-file-from-template';
2
- import { promptUser } from './lib/prompt-user';
3
- import { updateConfigFile } from './lib/update-config-file';
4
- import { updateJsonFile } from './lib/update-json-file';
5
- import { updateTextFile } from './lib/update-text-file';
6
- export { createFileFromTemplate, promptUser, updateConfigFile, updateJsonFile, updateTextFile, };
1
+ import { createFileFromTemplate } from './functions/create-file-from-template';
2
+ import { updateConfigFile } from './functions/update-config-file';
3
+ import { updateJsonFile } from './functions/update-json-file';
4
+ import { updateTextFile } from './functions/update-text-file';
5
+ import { promptUser } from './terminal/prompt-user';
6
+ import { parseQualifiedPath } from './utils/parse-qualified-path';
7
+ import { resolvePackagePath } from './utils/resolve-package-path';
8
+ export { createFileFromTemplate, updateConfigFile, updateJsonFile, updateTextFile, promptUser, parseQualifiedPath, resolvePackagePath, };
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Expects path to file in `"package:relative/path/to/file"` format and splits it into `{ pkg, file }`.
3
+ * The package name can be scoped (e.g. `@scope/package`). Throws error on invalid input.
4
+ */
5
+ export declare function parseQualifiedPath(path: string): {
6
+ pkg: string;
7
+ file: string;
8
+ };
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Resolve a package's installed root directory *from the target app*.
3
+ * Package name can be scoped (e.g. `@scope/package`).
4
+ * Works with npm/yarn/pnpm, hoisting or not.
5
+ */
6
+ export declare function resolvePackagePath(packageName: string): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "elrh-cosca",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"
@@ -12,11 +12,9 @@
12
12
  ".": {
13
13
  "types": "./dist/types/index.d.ts",
14
14
  "import": "./dist/elrh-cosca.mjs",
15
- "require": "./dist/elrh-cosca.cjs",
16
15
  "default": "./dist/elrh-cosca.mjs"
17
16
  }
18
17
  },
19
- "main": "./dist/elrh-cosca.cjs",
20
18
  "module": "./dist/elrh-cosca.mjs",
21
19
  "types": "./dist/types/index.d.ts",
22
20
  "devDependencies": {
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env node
1
+ #!/usr/bin/env node
2
2
 
3
3
  // manual test script for COSCA functions
4
4
 
@@ -7,26 +7,22 @@ import {
7
7
  } from '../dist/elrh-cosca.mjs'
8
8
 
9
9
  async function main() {
10
- /*
11
10
  console.log('Test promptUser')
12
11
  const input = await promptUser('Is it today?')
13
12
  console.log('User input:', input)
14
13
 
15
14
  console.log('\nTest createFileFromTemplate')
16
- await createFileFromTemplate('vite:README.md', 'test.file1')
17
- await createFileFromTemplate('elrh-cosca:README.md', 'test.file2')
18
- */
15
+ await createFileFromTemplate('vitest:README.md', 'test/snapshots/test.file1')
16
+ await createFileFromTemplate('elrh-cosca:test/fixtures/text-file.txt', 'test/snapshots/test.file2')
19
17
 
20
18
  console.log('\nTest updateConfigFile')
21
- await updateConfigFile('test/cosca-test-config.ts', { compatibilityDate: '2025-08-26', extends: ['nuxt-iignis'], cosca: { exists: true, data: ['some', 'other'], raw: 'war' } })
19
+ await updateConfigFile('test/fixtures/config-file-default.ts', { compatibilityDate: '2025-08-26', extends: ['nuxt-iignis'], cosca: { exists: true, data: ['some', 'other'], raw: 'war' } })
22
20
 
23
- /*
24
21
  console.log('\nTest updateJsonFile')
25
- await updateJsonFile('package.json', 'scripts', { 'cosca' : 'cosca' })
22
+ await updateJsonFile('test/fixtures/json-file.json', 'scripts', { 'cosca' : 'cosca' })
26
23
 
27
24
  console.log('\nTest updateTextFile')
28
- await updateTextFile('.gitignore', ['# COSCA'])
29
- */
25
+ await updateTextFile('test/fixtures/text-file.txt', ['# COSCA'])
30
26
  }
31
27
 
32
28
  main().catch((err) => {
@@ -1,14 +0,0 @@
1
- /**
2
- * Expects path to file template in `"package:relative/path/to/file"` format and splits it into `{ pkg, file }`.
3
- * Throws error on invalid input.
4
- */
5
- export declare function parseTemplatePath(input: string): {
6
- pkg: string;
7
- file: string;
8
- };
9
- /**
10
- * Resolve a package's installed root directory *from the target app*.
11
- * Package name can be scoped.
12
- * Works with npm/yarn/pnpm, hoisting or not.
13
- */
14
- export declare function resolvePackagePath(packageName: string): string;
@@ -1,12 +0,0 @@
1
- /**
2
- * Normalized __filename that works in both ESM (.mjs) and CJS (.cjs).
3
- * In CJS, __filename is available as a global variable.
4
- * In MJS, import.meta.url is available instead.
5
- */
6
- export declare const module_file: string;
7
- /**
8
- * Normalized __dirname that works in both ESM (.mjs) and CJS (.cjs).
9
- * In CJS, __filename is available as a global variable.
10
- * In MJS, import.meta.url is available instead.
11
- */
12
- export declare const module_dir: string;
File without changes