elrh-cosca 0.1.2 → 0.1.4
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 +1 -1
- package/dist/elrh-cosca.cjs +3 -3
- package/dist/elrh-cosca.mjs +67 -44
- package/dist/types/utils/resolve-package.d.ts +13 -0
- package/package.json +4 -1
- package/test/cosca-test.js +26 -0
package/LICENSE
CHANGED
package/dist/elrh-cosca.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
2
|
-
`,"utf8"),console.log(`'${e}' file updated.`)):console.log(`'${e}' file already up to date.`)}else console.log(`Updating '${e}' skipped.`)}async function
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("node:path"),n=require("node:fs"),w=require("node:readline"),g=require("node:module"),y=e=>e&&e.__esModule?e:{default:e},h=y(w);async function p(e){const t=h.default.createInterface({input:process.stdin,output:process.stdout});return new Promise(o=>{t.question(e+" (y/N): ",r=>{t.close(),o(/^y(es)?$/i.test(r.trim()))})})}function m(e){const t=e.indexOf(":");if(t<=0||t===e.length-1)throw new Error(`Invalid input "${e}". Expected format is "package:relative/path/to/file".`);const o=e.slice(0,t).trim(),r=e.slice(t+1).replace(/^[/\\]+/,"");if(!o||o.endsWith("/")||o.includes(" "))throw new Error(`Invalid package name in templatePath: "${o}"`);return{pkg:o,file:r}}function $(e){try{const t=process.cwd(),o=a.join(t,"package.json"),r=n.readFileSync(o,"utf8");if(JSON.parse(r)?.name===e)return t}catch{}try{const o=g.createRequire(a.resolve(process.cwd(),"package.json")).resolve(`${e}/package.json`);return a.dirname(o)}catch{throw new Error(`Cannot find package "${e}" from ${process.cwd()}. Make sure it's installed or linked in this project.`)}}async function k(e,t,o=!1){if(o||await p(`This will create '${t}' file. Continue?`)){const{pkg:c,file:d}=m(e),l=$(c),s=a.resolve(l,d),i=a.resolve(process.cwd(),t);n.existsSync(s)||(console.error(`Template file not found at ${s}`),process.exit(1)),n.existsSync(i)&&(o||await p(`File '${t}' already exists. Overwrite?`)||(console.log("Aborted."),process.exit(0))),n.copyFileSync(s,i),console.log(`New file '${t}' successfully created.`)}else console.log(`Creation of '${t}' skipped.`)}async function x(e,t,o,r=!1){if(r||await p(`This will update '${e}' file. Continue?`)){const d=a.resolve(process.cwd(),e);if(!n.existsSync(d)){console.warn(`No '${e}' found in project root — skipping updates.`);return}const l=n.readFileSync(d,"utf8");let s;try{s=JSON.parse(l)}catch{console.error(`Could not parse '${e}' — skipping updates.`);return}s[t]=s[t]||{};let i=!1;for(const[u,f]of Object.entries(o))s[t][u]!==f&&(s[t][u]=f,i=!0);i?(n.writeFileSync(d,JSON.stringify(s,null,2)+`
|
|
2
|
+
`,"utf8"),console.log(`'${e}' file updated.`)):console.log(`'${e}' file already up to date.`)}else console.log(`Updating '${e}' skipped.`)}async function P(e,t,o=!1){if(o||await p(`This will update '${e}' file. Continue?`)){const c=a.resolve(process.cwd(),e);if(!n.existsSync(c)){console.warn(`No '${e}' found in project root — skipping updates.`);return}const l=n.readFileSync(c,"utf8").split(/\r?\n/);let s=!1;for(const i of t)l.includes(i)||(l.push(i),s=!0);s?(n.writeFileSync(c,l.join(`
|
|
3
3
|
`)+`
|
|
4
|
-
`,"utf8"),console.log(`'${e}' file updated.`)):console.log(`'${e}' file already up to date.`)}else console.log(`Updating '${e}' skipped.`)}exports.createFileFromTemplate
|
|
4
|
+
`,"utf8"),console.log(`'${e}' file updated.`)):console.log(`'${e}' file already up to date.`)}else console.log(`Updating '${e}' skipped.`)}exports.createFileFromTemplate=k;exports.promptUser=p;exports.updateJsonFile=x;exports.updateTextFile=P;
|
package/dist/elrh-cosca.mjs
CHANGED
|
@@ -1,79 +1,102 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
async function
|
|
6
|
-
const
|
|
1
|
+
import { join as m, resolve as l, dirname as $ } from "node:path";
|
|
2
|
+
import { readFileSync as u, existsSync as p, copyFileSync as h, writeFileSync as g } from "node:fs";
|
|
3
|
+
import k from "node:readline";
|
|
4
|
+
import { createRequire as y } from "node:module";
|
|
5
|
+
async function d(e) {
|
|
6
|
+
const t = k.createInterface({
|
|
7
7
|
input: process.stdin,
|
|
8
8
|
output: process.stdout
|
|
9
9
|
});
|
|
10
|
-
return new Promise((
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
return new Promise((o) => {
|
|
11
|
+
t.question(e + " (y/N): ", (n) => {
|
|
12
|
+
t.close(), o(/^y(es)?$/i.test(n.trim()));
|
|
13
13
|
});
|
|
14
14
|
});
|
|
15
15
|
}
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
function P(e) {
|
|
17
|
+
const t = e.indexOf(":");
|
|
18
|
+
if (t <= 0 || t === e.length - 1)
|
|
19
|
+
throw new Error(
|
|
20
|
+
`Invalid input "${e}". Expected format is "package:relative/path/to/file".`
|
|
21
|
+
);
|
|
22
|
+
const o = e.slice(0, t).trim(), n = e.slice(t + 1).replace(/^[/\\]+/, "");
|
|
23
|
+
if (!o || o.endsWith("/") || o.includes(" "))
|
|
24
|
+
throw new Error(`Invalid package name in templatePath: "${o}"`);
|
|
25
|
+
return { pkg: o, file: n };
|
|
26
|
+
}
|
|
27
|
+
function x(e) {
|
|
28
|
+
try {
|
|
29
|
+
const t = process.cwd(), o = m(t, "package.json"), n = u(o, "utf8");
|
|
30
|
+
if (JSON.parse(n)?.name === e)
|
|
31
|
+
return t;
|
|
32
|
+
} catch {
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
const o = y(l(process.cwd(), "package.json")).resolve(`${e}/package.json`);
|
|
36
|
+
return $(o);
|
|
37
|
+
} catch {
|
|
38
|
+
throw new Error(`Cannot find package "${e}" from ${process.cwd()}. Make sure it's installed or linked in this project.`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
async function S(e, t, o = !1) {
|
|
42
|
+
if (o || await d(
|
|
43
|
+
`This will create '${t}' file. Continue?`
|
|
21
44
|
)) {
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
`File '${
|
|
25
|
-
) || (console.log("Aborted."), process.exit(0))),
|
|
45
|
+
const { pkg: i, file: a } = P(e), c = x(i), s = l(c, a), r = l(process.cwd(), t);
|
|
46
|
+
p(s) || (console.error(`Template file not found at ${s}`), process.exit(1)), p(r) && (o || await d(
|
|
47
|
+
`File '${t}' already exists. Overwrite?`
|
|
48
|
+
) || (console.log("Aborted."), process.exit(0))), h(s, r), console.log(`New file '${t}' successfully created.`);
|
|
26
49
|
} else
|
|
27
|
-
console.log(`Creation of '${
|
|
50
|
+
console.log(`Creation of '${t}' skipped.`);
|
|
28
51
|
}
|
|
29
|
-
async function
|
|
30
|
-
if (
|
|
52
|
+
async function U(e, t, o, n = !1) {
|
|
53
|
+
if (n || await d(
|
|
31
54
|
`This will update '${e}' file. Continue?`
|
|
32
55
|
)) {
|
|
33
|
-
const
|
|
34
|
-
if (!
|
|
56
|
+
const a = l(process.cwd(), e);
|
|
57
|
+
if (!p(a)) {
|
|
35
58
|
console.warn(`No '${e}' found in project root — skipping updates.`);
|
|
36
59
|
return;
|
|
37
60
|
}
|
|
38
|
-
const
|
|
39
|
-
let
|
|
61
|
+
const c = u(a, "utf8");
|
|
62
|
+
let s;
|
|
40
63
|
try {
|
|
41
|
-
|
|
64
|
+
s = JSON.parse(c);
|
|
42
65
|
} catch {
|
|
43
66
|
console.error(`Could not parse '${e}' — skipping updates.`);
|
|
44
67
|
return;
|
|
45
68
|
}
|
|
46
|
-
t
|
|
47
|
-
let
|
|
48
|
-
for (const [
|
|
49
|
-
t
|
|
50
|
-
|
|
69
|
+
s[t] = s[t] || {};
|
|
70
|
+
let r = !1;
|
|
71
|
+
for (const [f, w] of Object.entries(o))
|
|
72
|
+
s[t][f] !== w && (s[t][f] = w, r = !0);
|
|
73
|
+
r ? (g(a, JSON.stringify(s, null, 2) + `
|
|
51
74
|
`, "utf8"), console.log(`'${e}' file updated.`)) : console.log(`'${e}' file already up to date.`);
|
|
52
75
|
} else
|
|
53
76
|
console.log(`Updating '${e}' skipped.`);
|
|
54
77
|
}
|
|
55
|
-
async function
|
|
56
|
-
if (
|
|
78
|
+
async function J(e, t, o = !1) {
|
|
79
|
+
if (o || await d(
|
|
57
80
|
`This will update '${e}' file. Continue?`
|
|
58
81
|
)) {
|
|
59
|
-
const
|
|
60
|
-
if (!
|
|
82
|
+
const i = l(process.cwd(), e);
|
|
83
|
+
if (!p(i)) {
|
|
61
84
|
console.warn(`No '${e}' found in project root — skipping updates.`);
|
|
62
85
|
return;
|
|
63
86
|
}
|
|
64
|
-
const
|
|
65
|
-
let
|
|
66
|
-
for (const
|
|
67
|
-
|
|
68
|
-
|
|
87
|
+
const c = u(i, "utf8").split(/\r?\n/);
|
|
88
|
+
let s = !1;
|
|
89
|
+
for (const r of t)
|
|
90
|
+
c.includes(r) || (c.push(r), s = !0);
|
|
91
|
+
s ? (g(i, c.join(`
|
|
69
92
|
`) + `
|
|
70
93
|
`, "utf8"), console.log(`'${e}' file updated.`)) : console.log(`'${e}' file already up to date.`);
|
|
71
94
|
} else
|
|
72
95
|
console.log(`Updating '${e}' skipped.`);
|
|
73
96
|
}
|
|
74
97
|
export {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
98
|
+
S as createFileFromTemplate,
|
|
99
|
+
d as promptUser,
|
|
100
|
+
U as updateJsonFile,
|
|
101
|
+
J as updateTextFile
|
|
79
102
|
};
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
+
* Works with npm/yarn/pnpm, hoisting or not.
|
|
12
|
+
*/
|
|
13
|
+
export declare function resolvePackagePath(packageName: string): string;
|
package/package.json
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// manual test script for COSCA functions
|
|
4
|
+
|
|
5
|
+
import { createFileFromTemplate, promptUser, updateJsonFile, updateTextFile } from '../dist/elrh-cosca.mjs'
|
|
6
|
+
|
|
7
|
+
async function main() {
|
|
8
|
+
console.log('Test promptUser')
|
|
9
|
+
const input = await promptUser('Is it today?')
|
|
10
|
+
console.log('User input:', input)
|
|
11
|
+
|
|
12
|
+
console.log('\nTest createFileFromTemplate')
|
|
13
|
+
await createFileFromTemplate('vite:README.md', 'test.file1')
|
|
14
|
+
await createFileFromTemplate('elrh-cosca:README.md', 'test.file2')
|
|
15
|
+
|
|
16
|
+
console.log('\nTest updateJsonFile')
|
|
17
|
+
await updateJsonFile('package.json', 'scripts', { 'cosca' : 'cosca' })
|
|
18
|
+
|
|
19
|
+
console.log('\nTest updateTextFile')
|
|
20
|
+
await updateTextFile('.gitignore', ['# COSCA'])
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
main().catch((err) => {
|
|
24
|
+
console.error('Error:', err)
|
|
25
|
+
process.exit(1)
|
|
26
|
+
})
|