elrh-cosca 0.1.4 → 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.
@@ -1,4 +1,4 @@
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(`
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=k;exports.promptUser=p;exports.updateJsonFile=x;exports.updateTextFile=P;
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;
@@ -1,102 +1,107 @@
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({
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";
4
+ async function p(e) {
5
+ const o = y.createInterface({
7
6
  input: process.stdin,
8
7
  output: process.stdout
9
8
  });
10
- return new Promise((o) => {
11
- t.question(e + " (y/N): ", (n) => {
12
- t.close(), o(/^y(es)?$/i.test(n.trim()));
9
+ return new Promise((t) => {
10
+ o.question(e + " (y/N): ", (s) => {
11
+ o.close(), t(/^y(es)?$/i.test(s.trim()));
13
12
  });
14
13
  });
15
14
  }
16
15
  function P(e) {
17
- const t = e.indexOf(":");
18
- if (t <= 0 || t === e.length - 1)
16
+ const o = e.indexOf(":");
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 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 };
21
+ const t = e.slice(0, o).trim(), s = e.slice(o + 1).replace(/^[/\\]+/, "");
22
+ if (!t || t.endsWith("/") || t.includes(" "))
23
+ throw new Error(`Invalid package name in templatePath: "${t}"`);
24
+ return { pkg: t, file: s };
26
25
  }
27
26
  function x(e) {
27
+ const o = process.cwd();
28
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 {
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);
33
34
  }
34
35
  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.`);
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);
39
43
  }
44
+ throw new Error(`Cannot find package "${e}" from ${process.cwd()}. Make sure it's installed or linked in this project.`);
40
45
  }
41
- async function S(e, t, o = !1) {
42
- if (o || await d(
43
- `This will create '${t}' file. Continue?`
46
+ async function j(e, o, t = !1) {
47
+ if (t || await p(
48
+ `This will create '${o}' file. Continue?`
44
49
  )) {
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.`);
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(
52
+ `File '${o}' already exists. Overwrite?`
53
+ ) || (console.log("Aborted."), process.exit(0))), k(n, c), console.log(`New file '${o}' successfully created.`);
49
54
  } else
50
- console.log(`Creation of '${t}' skipped.`);
55
+ console.log(`Creation of '${o}' skipped.`);
51
56
  }
52
- async function U(e, t, o, n = !1) {
53
- if (n || await d(
57
+ async function S(e, o, t, s = !1) {
58
+ if (s || await p(
54
59
  `This will update '${e}' file. Continue?`
55
60
  )) {
56
- const a = l(process.cwd(), e);
57
- if (!p(a)) {
61
+ const l = a(process.cwd(), e);
62
+ if (!d(l)) {
58
63
  console.warn(`No '${e}' found in project root — skipping updates.`);
59
64
  return;
60
65
  }
61
- const c = u(a, "utf8");
62
- let s;
66
+ const i = w(l, "utf8");
67
+ let n;
63
68
  try {
64
- s = JSON.parse(c);
69
+ n = JSON.parse(i);
65
70
  } catch {
66
71
  console.error(`Could not parse '${e}' — skipping updates.`);
67
72
  return;
68
73
  }
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) + `
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) + `
74
79
  `, "utf8"), console.log(`'${e}' file updated.`)) : console.log(`'${e}' file already up to date.`);
75
80
  } else
76
81
  console.log(`Updating '${e}' skipped.`);
77
82
  }
78
- async function J(e, t, o = !1) {
79
- if (o || await d(
83
+ async function U(e, o, t = !1) {
84
+ if (t || await p(
80
85
  `This will update '${e}' file. Continue?`
81
86
  )) {
82
- const i = l(process.cwd(), e);
83
- if (!p(i)) {
87
+ const r = a(process.cwd(), e);
88
+ if (!d(r)) {
84
89
  console.warn(`No '${e}' found in project root — skipping updates.`);
85
90
  return;
86
91
  }
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(`
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(`
92
97
  `) + `
93
98
  `, "utf8"), console.log(`'${e}' file updated.`)) : console.log(`'${e}' file already up to date.`);
94
99
  } else
95
100
  console.log(`Updating '${e}' skipped.`);
96
101
  }
97
102
  export {
98
- S as createFileFromTemplate,
99
- d as promptUser,
100
- U as updateJsonFile,
101
- J as updateTextFile
103
+ j as createFileFromTemplate,
104
+ p as promptUser,
105
+ S as updateJsonFile,
106
+ U as updateTextFile
102
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
14
  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.1.4",
3
+ "version": "0.1.5",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"