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.
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("node:path"),n=require("node:fs"),w=require("node:readline"),g=require("node:module"),m=e=>e&&e.__esModule?e:{default:e},y=m(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): ",i=>{t.close(),o(/^y(es)?$/i.test(i.trim()))})})}function $(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(),i=e.slice(t+1).replace(/^[/\\]+/,"");if(!o||o.endsWith("/")||o.includes(" "))throw new Error(`Invalid package name in templatePath: "${o}"`);return{pkg:o,file:i}}function h(e){const o=g.createRequire(d.resolve(process.cwd(),"package.json")).resolve(`${e}/package.json`);return d.dirname(o)}async function k(e,t,o=!1){if(o||await u(`This will create '${t}' file. Continue?`)){const{pkg:c,file:a}=$(e),l=h(c),s=d.resolve(l,a),r=d.resolve(process.cwd(),t);n.existsSync(s)||(console.error(`Template file not found at ${s}`),process.exit(1)),n.existsSync(r)&&(o||await u(`File '${t}' already exists. Overwrite?`)||(console.log("Aborted."),process.exit(0))),n.copyFileSync(s,r),console.log(`New file '${t}' successfully created.`)}else console.log(`Creation of '${t}' skipped.`)}async function v(e,t,o,i=!1){if(i||await u(`This will update '${e}' file. Continue?`)){const a=d.resolve(process.cwd(),e);if(!n.existsSync(a)){console.warn(`No '${e}' found in project root — skipping updates.`);return}const l=n.readFileSync(a,"utf8");let s;try{s=JSON.parse(l)}catch{console.error(`Could not parse '${e}' — skipping updates.`);return}s[t]=s[t]||{};let r=!1;for(const[p,f]of Object.entries(o))s[t][p]!==f&&(s[t][p]=f,r=!0);r?(n.writeFileSync(a,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 x(e,t,o=!1){if(o||await u(`This will update '${e}' file. Continue?`)){const c=d.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 r of t)l.includes(r)||(l.push(r),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=u;exports.updateJsonFile=v;exports.updateTextFile=x;
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,46 +1,61 @@
1
- import { resolve as a, dirname as g } from "node:path";
2
- import { existsSync as d, copyFileSync as $, readFileSync as w, writeFileSync as m } from "node:fs";
3
- import h from "node:readline";
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 = h.createInterface({
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): ", (r) => {
12
- o.close(), t(/^y(es)?$/i.test(r.trim()));
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 y(e) {
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(), r = e.slice(o + 1).replace(/^[/\\]+/, "");
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: r };
24
+ return { pkg: t, file: s };
26
25
  }
27
26
  function x(e) {
28
- const t = k(a(process.cwd(), "package.json")).resolve(`${e}/package.json`);
29
- return g(t);
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 U(e, o, t = !1) {
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: c, file: l } = y(e), i = x(c), s = a(i, l), n = a(process.cwd(), o);
36
- d(s) || (console.error(`Template file not found at ${s}`), process.exit(1)), d(n) && (t || await p(
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))), $(s, n), console.log(`New file '${o}' successfully created.`);
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 j(e, o, t, r = !1) {
43
- if (r || await p(
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 s;
67
+ let n;
53
68
  try {
54
- s = JSON.parse(i);
69
+ n = JSON.parse(i);
55
70
  } catch {
56
71
  console.error(`Could not parse '${e}' — skipping updates.`);
57
72
  return;
58
73
  }
59
- s[o] = s[o] || {};
60
- let n = !1;
61
- for (const [f, u] of Object.entries(t))
62
- s[o][f] !== u && (s[o][f] = u, n = !0);
63
- n ? (m(l, 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) + `
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 N(e, o, t = !1) {
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 c = a(process.cwd(), e);
73
- if (!d(c)) {
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(c, "utf8").split(/\r?\n/);
78
- let s = !1;
79
- for (const n of o)
80
- i.includes(n) || (i.push(n), s = !0);
81
- s ? (m(c, i.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(`
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
- U as createFileFromTemplate,
103
+ j as createFileFromTemplate,
89
104
  p as promptUser,
90
- j as updateJsonFile,
91
- N as updateTextFile
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(pkg: string): string;
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.3",
3
+ "version": "0.1.5",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"
@@ -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.file')
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' })