elrh-cosca 0.1.0
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 +21 -0
- package/README.md +27 -0
- package/dist/elrh-cosca.cjs +4 -0
- package/dist/elrh-cosca.mjs +76 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/lib/create-file-from-template.d.ts +1 -0
- package/dist/types/lib/prompt-user.d.ts +1 -0
- package/dist/types/lib/update-json-file.d.ts +1 -0
- package/dist/types/lib/update-text-file.d.ts +1 -0
- package/dist/types/main.d.ts +5 -0
- package/dist/vite.svg +1 -0
- package/package.json +31 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Alois Sečkár
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# COSCA
|
|
2
|
+
Library of file-writting functions that help building CLI scripts for making changes in target projects - like adding default configuration files or new sections in `package.json`.
|
|
3
|
+
|
|
4
|
+
The first experimental "customers" are my [Nuxt Spec](https://github.com/AloisSeckar/nuxt-spec) and [Nuxt Ignis](https://github.com/AloisSeckar/nuxt-ignis) projects.
|
|
5
|
+
|
|
6
|
+
The **"COSCA"** abbreviation stands for **CO**de **SCA**ffolding which points out the library's purpose of providing methods for altering existing and adding new files from scratch using Node-based filesystem APIs.
|
|
7
|
+
|
|
8
|
+
## How to use
|
|
9
|
+
|
|
10
|
+
`npm install elrh-cosca` to include into your project.
|
|
11
|
+
|
|
12
|
+
List of available functions:
|
|
13
|
+
- `createFileFromTemplate`
|
|
14
|
+
- `promptUser`
|
|
15
|
+
- `updateJsonFile`
|
|
16
|
+
- `updateTextFile`
|
|
17
|
+
|
|
18
|
+
## Tech stack
|
|
19
|
+
|
|
20
|
+
- Developed with [TypeScript](https://www.typescriptlang.org/)
|
|
21
|
+
- Build with [Vite](https://vitejs.dev/)
|
|
22
|
+
- Tested with [Vitest](https://vitest.dev/)
|
|
23
|
+
|
|
24
|
+
## Report bugs & contact
|
|
25
|
+
|
|
26
|
+
Use GitHub issues to report bugs / propose enhancements / give feedback:
|
|
27
|
+
https://github.com/AloisSeckar/elrh-cosca/issues
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("path"),o=require("fs"),$=require("readline"),w=e=>e&&e.__esModule?e:{default:e},d=w(y),g=w($);async function u(e){const t=g.default.createInterface({input:process.stdin,output:process.stdout});return new Promise(n=>{t.question(e+" (y/N): ",c=>{t.close(),n(/^y(es)?$/i.test(c.trim()))})})}async function m(e,t,n=!1){if(n||await u(`This will create '${t}' file. Continue?`)){const l=d.default.resolve(process.cwd(),e),i=d.default.resolve(process.cwd(),t);o.existsSync(l)||(console.error(`Template file not found at ${l}`),process.exit(1)),o.existsSync(i)&&(n||await u(`File '${t}' already exists. Overwrite?`)||(console.log("Aborted."),process.exit(0))),o.copyFileSync(l,i),console.log(`New file '${t}' successfully created.`)}else console.log(`Creation of '${t}' skipped.`)}async function S(e,t,n,c=!1){if(c||await u(`This will update '${e}' file. Continue?`)){const i=d.default.resolve(process.cwd(),e);if(!o.existsSync(i)){console.warn(`No '${e}' found in project root — skipping updates.`);return}const r=o.readFileSync(i,"utf8");let s;try{s=JSON.parse(r)}catch{console.error(`Could not parse '${e}' — skipping updates.`);return}s[t]=s[t]||{};let a=!1;for(const[f,p]of Object.entries(n))s[t][f]!==p&&(s[t][f]=p,a=!0);a?(o.writeFileSync(i,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,n=!1){if(n||await u(`This will update '${e}' file. Continue?`)){const l=d.default.resolve(process.cwd(),e);if(!o.existsSync(l)){console.warn(`No '${e}' found in project root — skipping updates.`);return}const r=o.readFileSync(l,"utf8").split(/\r?\n/);let s=!1;for(const a of t)r.includes(a)||(r.push(a),s=!0);s?(o.writeFileSync(l,r.join(`
|
|
3
|
+
`)+`
|
|
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=S;exports.updateTextFile=x;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import a from "path";
|
|
2
|
+
import { existsSync as d, copyFileSync as m, readFileSync as w, writeFileSync as $ } from "fs";
|
|
3
|
+
import y from "readline";
|
|
4
|
+
async function u(e) {
|
|
5
|
+
const o = y.createInterface({
|
|
6
|
+
input: process.stdin,
|
|
7
|
+
output: process.stdout
|
|
8
|
+
});
|
|
9
|
+
return new Promise((t) => {
|
|
10
|
+
o.question(e + " (y/N): ", (r) => {
|
|
11
|
+
o.close(), t(/^y(es)?$/i.test(r.trim()));
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
async function h(e, o, t = !1) {
|
|
16
|
+
if (t || await u(
|
|
17
|
+
`This will create '${o}' file. Continue?`
|
|
18
|
+
)) {
|
|
19
|
+
const n = a.resolve(process.cwd(), e), i = a.resolve(process.cwd(), o);
|
|
20
|
+
d(n) || (console.error(`Template file not found at ${n}`), process.exit(1)), d(i) && (t || await u(
|
|
21
|
+
`File '${o}' already exists. Overwrite?`
|
|
22
|
+
) || (console.log("Aborted."), process.exit(0))), m(n, i), console.log(`New file '${o}' successfully created.`);
|
|
23
|
+
} else
|
|
24
|
+
console.log(`Creation of '${o}' skipped.`);
|
|
25
|
+
}
|
|
26
|
+
async function k(e, o, t, r = !1) {
|
|
27
|
+
if (r || await u(
|
|
28
|
+
`This will update '${e}' file. Continue?`
|
|
29
|
+
)) {
|
|
30
|
+
const i = a.resolve(process.cwd(), e);
|
|
31
|
+
if (!d(i)) {
|
|
32
|
+
console.warn(`No '${e}' found in project root — skipping updates.`);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const l = w(i, "utf8");
|
|
36
|
+
let s;
|
|
37
|
+
try {
|
|
38
|
+
s = JSON.parse(l);
|
|
39
|
+
} catch {
|
|
40
|
+
console.error(`Could not parse '${e}' — skipping updates.`);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
s[o] = s[o] || {};
|
|
44
|
+
let c = !1;
|
|
45
|
+
for (const [f, p] of Object.entries(t))
|
|
46
|
+
s[o][f] !== p && (s[o][f] = p, c = !0);
|
|
47
|
+
c ? ($(i, JSON.stringify(s, null, 2) + `
|
|
48
|
+
`, "utf8"), console.log(`'${e}' file updated.`)) : console.log(`'${e}' file already up to date.`);
|
|
49
|
+
} else
|
|
50
|
+
console.log(`Updating '${e}' skipped.`);
|
|
51
|
+
}
|
|
52
|
+
async function C(e, o, t = !1) {
|
|
53
|
+
if (t || await u(
|
|
54
|
+
`This will update '${e}' file. Continue?`
|
|
55
|
+
)) {
|
|
56
|
+
const n = a.resolve(process.cwd(), e);
|
|
57
|
+
if (!d(n)) {
|
|
58
|
+
console.warn(`No '${e}' found in project root — skipping updates.`);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const l = w(n, "utf8").split(/\r?\n/);
|
|
62
|
+
let s = !1;
|
|
63
|
+
for (const c of o)
|
|
64
|
+
l.includes(c) || (l.push(c), s = !0);
|
|
65
|
+
s ? ($(n, l.join(`
|
|
66
|
+
`) + `
|
|
67
|
+
`, "utf8"), console.log(`'${e}' file updated.`)) : console.log(`'${e}' file already up to date.`);
|
|
68
|
+
} else
|
|
69
|
+
console.log(`Updating '${e}' skipped.`);
|
|
70
|
+
}
|
|
71
|
+
export {
|
|
72
|
+
h as createFileFromTemplate,
|
|
73
|
+
u as promptUser,
|
|
74
|
+
k as updateJsonFile,
|
|
75
|
+
C as updateTextFile
|
|
76
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function createFileFromTemplate(templateFile: string, targetFile: string, force?: boolean): Promise<void>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function promptUser(question: string): Promise<boolean>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function updateJsonFile(pathToFile: string, jsonKey: string, newValues: Record<string, string>, force?: boolean): Promise<void>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function updateTextFile(pathToFile: string, rowsToAdd: string[], force?: boolean): Promise<void>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { createFileFromTemplate } from './lib/create-file-from-template';
|
|
2
|
+
import { promptUser } from './lib/prompt-user';
|
|
3
|
+
import { updateJsonFile } from './lib/update-json-file';
|
|
4
|
+
import { updateTextFile } from './lib/update-text-file';
|
|
5
|
+
export { createFileFromTemplate, promptUser, updateJsonFile, updateTextFile, };
|
package/dist/vite.svg
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "elrh-cosca",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist"
|
|
7
|
+
],
|
|
8
|
+
"main": "./dist/elrh-cosca.cjs",
|
|
9
|
+
"module": "./dist/elrh-cosca.mjs",
|
|
10
|
+
"types": "./dist/types/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/types/index.d.ts",
|
|
14
|
+
"import": "./dist/elrh-cosca.cjs",
|
|
15
|
+
"require": "./dist/elrh-cosca.mjs"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/node": "^24.3.0",
|
|
20
|
+
"typescript": "^5.9.2",
|
|
21
|
+
"vite": "^7.1.3",
|
|
22
|
+
"vite-plugin-dts": "^4.5.4",
|
|
23
|
+
"vitest": "^3.2.4"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"dev": "vite",
|
|
27
|
+
"build": "pnpm install && vitest run && tsc && vite build",
|
|
28
|
+
"preview": "vite preview",
|
|
29
|
+
"test": "vitest run"
|
|
30
|
+
}
|
|
31
|
+
}
|