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