elrh-cosca 0.1.3 → 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/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=k;exports.promptUser=
|
|
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,92 +1,102 @@
|
|
|
1
|
-
import { resolve as
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import { createRequire as
|
|
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
|
-
function
|
|
17
|
-
const
|
|
18
|
-
if (
|
|
16
|
+
function P(e) {
|
|
17
|
+
const t = e.indexOf(":");
|
|
18
|
+
if (t <= 0 || t === e.length - 1)
|
|
19
19
|
throw new Error(
|
|
20
20
|
`Invalid input "${e}". Expected format is "package:relative/path/to/file".`
|
|
21
21
|
);
|
|
22
|
-
const
|
|
23
|
-
if (!
|
|
24
|
-
throw new Error(`Invalid package name in templatePath: "${
|
|
25
|
-
return { pkg:
|
|
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
26
|
}
|
|
27
27
|
function x(e) {
|
|
28
|
-
|
|
29
|
-
|
|
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
|
+
}
|
|
30
40
|
}
|
|
31
|
-
async function
|
|
32
|
-
if (
|
|
33
|
-
`This will create '${
|
|
41
|
+
async function S(e, t, o = !1) {
|
|
42
|
+
if (o || await d(
|
|
43
|
+
`This will create '${t}' file. Continue?`
|
|
34
44
|
)) {
|
|
35
|
-
const { pkg:
|
|
36
|
-
|
|
37
|
-
`File '${
|
|
38
|
-
) || (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.`);
|
|
39
49
|
} else
|
|
40
|
-
console.log(`Creation of '${
|
|
50
|
+
console.log(`Creation of '${t}' skipped.`);
|
|
41
51
|
}
|
|
42
|
-
async function
|
|
43
|
-
if (
|
|
52
|
+
async function U(e, t, o, n = !1) {
|
|
53
|
+
if (n || await d(
|
|
44
54
|
`This will update '${e}' file. Continue?`
|
|
45
55
|
)) {
|
|
46
|
-
const
|
|
47
|
-
if (!
|
|
56
|
+
const a = l(process.cwd(), e);
|
|
57
|
+
if (!p(a)) {
|
|
48
58
|
console.warn(`No '${e}' found in project root — skipping updates.`);
|
|
49
59
|
return;
|
|
50
60
|
}
|
|
51
|
-
const
|
|
61
|
+
const c = u(a, "utf8");
|
|
52
62
|
let s;
|
|
53
63
|
try {
|
|
54
|
-
s = JSON.parse(
|
|
64
|
+
s = JSON.parse(c);
|
|
55
65
|
} catch {
|
|
56
66
|
console.error(`Could not parse '${e}' — skipping updates.`);
|
|
57
67
|
return;
|
|
58
68
|
}
|
|
59
|
-
s[
|
|
60
|
-
let
|
|
61
|
-
for (const [f,
|
|
62
|
-
s[
|
|
63
|
-
|
|
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) + `
|
|
64
74
|
`, "utf8"), console.log(`'${e}' file updated.`)) : console.log(`'${e}' file already up to date.`);
|
|
65
75
|
} else
|
|
66
76
|
console.log(`Updating '${e}' skipped.`);
|
|
67
77
|
}
|
|
68
|
-
async function
|
|
69
|
-
if (
|
|
78
|
+
async function J(e, t, o = !1) {
|
|
79
|
+
if (o || await d(
|
|
70
80
|
`This will update '${e}' file. Continue?`
|
|
71
81
|
)) {
|
|
72
|
-
const
|
|
73
|
-
if (!
|
|
82
|
+
const i = l(process.cwd(), e);
|
|
83
|
+
if (!p(i)) {
|
|
74
84
|
console.warn(`No '${e}' found in project root — skipping updates.`);
|
|
75
85
|
return;
|
|
76
86
|
}
|
|
77
|
-
const
|
|
87
|
+
const c = u(i, "utf8").split(/\r?\n/);
|
|
78
88
|
let s = !1;
|
|
79
|
-
for (const
|
|
80
|
-
|
|
81
|
-
s ? (
|
|
89
|
+
for (const r of t)
|
|
90
|
+
c.includes(r) || (c.push(r), s = !0);
|
|
91
|
+
s ? (g(i, c.join(`
|
|
82
92
|
`) + `
|
|
83
93
|
`, "utf8"), console.log(`'${e}' file updated.`)) : console.log(`'${e}' file already up to date.`);
|
|
84
94
|
} else
|
|
85
95
|
console.log(`Updating '${e}' skipped.`);
|
|
86
96
|
}
|
|
87
97
|
export {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
98
|
+
S as createFileFromTemplate,
|
|
99
|
+
d as promptUser,
|
|
100
|
+
U as updateJsonFile,
|
|
101
|
+
J as updateTextFile
|
|
92
102
|
};
|
|
@@ -10,4 +10,4 @@ export declare function parseTemplatePath(input: string): {
|
|
|
10
10
|
* Resolve a package's installed root directory *from the target app*.
|
|
11
11
|
* Works with npm/yarn/pnpm, hoisting or not.
|
|
12
12
|
*/
|
|
13
|
-
export declare function resolvePackagePath(
|
|
13
|
+
export declare function resolvePackagePath(packageName: string): string;
|
package/package.json
CHANGED
package/test/cosca-test.js
CHANGED
|
@@ -10,7 +10,8 @@ async function main() {
|
|
|
10
10
|
console.log('User input:', input)
|
|
11
11
|
|
|
12
12
|
console.log('\nTest createFileFromTemplate')
|
|
13
|
-
await createFileFromTemplate('vite:README.md', 'test.
|
|
13
|
+
await createFileFromTemplate('vite:README.md', 'test.file1')
|
|
14
|
+
await createFileFromTemplate('elrh-cosca:README.md', 'test.file2')
|
|
14
15
|
|
|
15
16
|
console.log('\nTest updateJsonFile')
|
|
16
17
|
await updateJsonFile('package.json', 'scripts', { 'cosca' : 'cosca' })
|