express-fix-app-js 1.2.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/LICENSE +21 -0
- package/README.md +77 -0
- package/bin/cli.js +12 -0
- package/bin/core/getLatestVersion.js +13 -0
- package/bin/core/loadRunner.js +9 -0
- package/bin/v1/UpdateJs/checkLines.json +18 -0
- package/bin/v1/UpdateJs/common/AlterFile/buildUpdatedContent.js +19 -0
- package/bin/v1/UpdateJs/common/AlterFile/checkDuplicate.js +21 -0
- package/bin/v1/UpdateJs/common/AlterFile/findInsertIndex.js +30 -0
- package/bin/v1/UpdateJs/common/AlterFile/index.js +50 -0
- package/bin/v1/UpdateJs/common/readFile.js +8 -0
- package/bin/v1/UpdateJs/common/writeFile.js +10 -0
- package/bin/v1/UpdateJs/index.js +31 -0
- package/bin/v1/core/createFolder.js +34 -0
- package/bin/v1/core/parseInput.js +13 -0
- package/bin/v1/core/showUsage.js +50 -0
- package/bin/v1/start.js +18 -0
- package/bin/v2/UpdateJs/checkLines.json +19 -0
- package/bin/v2/UpdateJs/common/AlterFile/buildUpdatedContent.js +19 -0
- package/bin/v2/UpdateJs/common/AlterFile/checkDuplicate.js +21 -0
- package/bin/v2/UpdateJs/common/AlterFile/findInsertIndex.js +30 -0
- package/bin/v2/UpdateJs/common/AlterFile/index.js +44 -0
- package/bin/v2/UpdateJs/common/readFile.js +8 -0
- package/bin/v2/UpdateJs/common/writeFile.js +10 -0
- package/bin/v2/UpdateJs/index.js +30 -0
- package/bin/v2/core/createFolder.js +34 -0
- package/bin/v2/core/parseInput.js +13 -0
- package/bin/v2/core/showUsage.js +50 -0
- package/bin/v2/start.js +18 -0
- package/index.js +11 -0
- package/package.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 KeshavSoft
|
|
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 do so, subject to the
|
|
10
|
+
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,77 @@
|
|
|
1
|
+
# EndPoints Fix
|
|
2
|
+
|
|
3
|
+
Utility for automatically maintaining Express route files.
|
|
4
|
+
|
|
5
|
+
## Purpose
|
|
6
|
+
|
|
7
|
+
This module updates `end-points.js` files by:
|
|
8
|
+
|
|
9
|
+
* Adding new route handlers
|
|
10
|
+
* Preventing duplicate routes
|
|
11
|
+
* Preserving route order
|
|
12
|
+
* Maintaining consistent formatting
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## Generated Structure
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
import express from "express";
|
|
20
|
+
|
|
21
|
+
const tableName = "BillsTable";
|
|
22
|
+
|
|
23
|
+
const router = express.Router();
|
|
24
|
+
|
|
25
|
+
router.post("/Alter", express.json(), (req, res) =>
|
|
26
|
+
AlterFunc({ req, res, inTablePath: tablePath })
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
export { router };
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## Rules
|
|
35
|
+
|
|
36
|
+
### First Route
|
|
37
|
+
|
|
38
|
+
When the first route is inserted:
|
|
39
|
+
|
|
40
|
+
* Add one blank line after `const router = express.Router();`
|
|
41
|
+
* Add one blank line before `export { router };`
|
|
42
|
+
|
|
43
|
+
Example:
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
const router = express.Router();
|
|
47
|
+
|
|
48
|
+
router.post("/Alter", express.json(), handler);
|
|
49
|
+
|
|
50
|
+
export { router };
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Additional Routes
|
|
54
|
+
|
|
55
|
+
New routes are always appended after the last route.
|
|
56
|
+
|
|
57
|
+
Example:
|
|
58
|
+
|
|
59
|
+
```js
|
|
60
|
+
router.post("/Alter", express.json(), handler);
|
|
61
|
+
router.post("/Alter1", express.json(), handler);
|
|
62
|
+
router.post("/Alter2", express.json(), handler);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
No blank lines are inserted between routes.
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## Duplicate Protection
|
|
70
|
+
|
|
71
|
+
If a route already exists, no new route is added.
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
## Goal
|
|
76
|
+
|
|
77
|
+
Produce clean and predictable Express route files automatically.
|
package/bin/cli.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import getLatestVersion from "./core/getLatestVersion.js";
|
|
4
|
+
import loadRunner from "./core/loadRunner.js";
|
|
5
|
+
|
|
6
|
+
const run = async ({ }) => {
|
|
7
|
+
const version = getLatestVersion();
|
|
8
|
+
const runner = await loadRunner(version);
|
|
9
|
+
await runner({});
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
run({}).then();
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { fileURLToPath } from "url";
|
|
4
|
+
|
|
5
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
6
|
+
|
|
7
|
+
export default function getLatestVersion() {
|
|
8
|
+
const versions = fs.readdirSync(path.join(__dirname, ".."))
|
|
9
|
+
.filter(n => /^v\d+$/.test(n))
|
|
10
|
+
.sort((a, b) => parseInt(a.slice(1)) - parseInt(b.slice(1)));
|
|
11
|
+
|
|
12
|
+
return versions.at(-1);
|
|
13
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"importLines": {
|
|
3
|
+
"toInsertLine": "import funcFrom${endpoint} from './${endpoint}/controller.js';",
|
|
4
|
+
"duplicationCheck": "from './${endpoint}/controller.js'",
|
|
5
|
+
"insertAfter": [
|
|
6
|
+
"import funcFrom",
|
|
7
|
+
"import express"
|
|
8
|
+
]
|
|
9
|
+
},
|
|
10
|
+
"useLines": {
|
|
11
|
+
"toInsertLine": "router.post('/${endpoint}', express.json(), (req, res) => funcFrom${endpoint}({ req, res, inTablePath: tablePath }));",
|
|
12
|
+
"duplicationCheck": "router.use('/${endpoint}'",
|
|
13
|
+
"insertAfter": [
|
|
14
|
+
"router.",
|
|
15
|
+
"const router = "
|
|
16
|
+
]
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const startFunc = ({
|
|
2
|
+
content,
|
|
3
|
+
insertInfo,
|
|
4
|
+
toInsertLine,
|
|
5
|
+
insertAfter
|
|
6
|
+
}) => {
|
|
7
|
+
const before = content.slice(0, insertInfo.index);
|
|
8
|
+
|
|
9
|
+
const isFirstInsert =
|
|
10
|
+
insertInfo.matchedPattern === insertAfter[insertAfter.length - 1];
|
|
11
|
+
|
|
12
|
+
return before +
|
|
13
|
+
(isFirstInsert ? "\n" : "") +
|
|
14
|
+
toInsertLine +
|
|
15
|
+
"\n" +
|
|
16
|
+
content.slice(insertInfo.index);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export default startFunc;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const checkUseDuplicate = ({
|
|
2
|
+
inContent,
|
|
3
|
+
inFilePath,
|
|
4
|
+
inSearchText
|
|
5
|
+
}) => {
|
|
6
|
+
const lines = inContent.split("\n");
|
|
7
|
+
|
|
8
|
+
const lineIndex = lines.findIndex(line =>
|
|
9
|
+
line.includes(inSearchText)
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
return {
|
|
13
|
+
found: lineIndex !== -1,
|
|
14
|
+
filePath: inFilePath,
|
|
15
|
+
lineNumber: lineIndex !== -1
|
|
16
|
+
? lineIndex + 1
|
|
17
|
+
: null
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export default checkUseDuplicate;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const findInsertIndex = ({
|
|
2
|
+
inContent,
|
|
3
|
+
inPatterns = []
|
|
4
|
+
}) => {
|
|
5
|
+
const lines = inContent.split("\n");
|
|
6
|
+
|
|
7
|
+
let lineNumber = -1;
|
|
8
|
+
let matchedPattern = null;
|
|
9
|
+
|
|
10
|
+
lines.forEach((line, index) => {
|
|
11
|
+
const pattern = inPatterns.find(item => line.includes(item));
|
|
12
|
+
|
|
13
|
+
if (pattern) {
|
|
14
|
+
lineNumber = index;
|
|
15
|
+
matchedPattern = pattern;
|
|
16
|
+
};
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
index: lineNumber === -1
|
|
21
|
+
? -1
|
|
22
|
+
: lines
|
|
23
|
+
.slice(0, lineNumber + 1)
|
|
24
|
+
.join("\n")
|
|
25
|
+
.length + 1,
|
|
26
|
+
matchedPattern
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export default findInsertIndex;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import readFile from "../readFile.js";
|
|
2
|
+
import checkDuplicate from "./checkDuplicate.js";
|
|
3
|
+
import findInsertIndex from "./findInsertIndex.js";
|
|
4
|
+
import writeFile from "../writeFile.js";
|
|
5
|
+
|
|
6
|
+
import buildUpdatedContent from "./buildUpdatedContent.js";
|
|
7
|
+
|
|
8
|
+
const validateDuplicate = ({ content, jsFilePath, duplicationCheck }) => {
|
|
9
|
+
return checkDuplicate({
|
|
10
|
+
inContent: content,
|
|
11
|
+
inFilePath: jsFilePath,
|
|
12
|
+
inSearchText: duplicationCheck
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const locateInsertPoint = ({ content, insertAfter }) => {
|
|
17
|
+
return findInsertIndex({
|
|
18
|
+
inContent: content,
|
|
19
|
+
inPatterns: insertAfter
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const alterFile = ({
|
|
24
|
+
jsFilePath,
|
|
25
|
+
toInsertLine,
|
|
26
|
+
duplicationCheck,
|
|
27
|
+
insertAfter = [],
|
|
28
|
+
showLog = false
|
|
29
|
+
}) => {
|
|
30
|
+
const content = readFile(jsFilePath);
|
|
31
|
+
|
|
32
|
+
const duplicateInfo = validateDuplicate({ content, jsFilePath, duplicationCheck });
|
|
33
|
+
|
|
34
|
+
// const index = locateInsertPoint({ content, importInsertAfter });
|
|
35
|
+
const insertInfo = locateInsertPoint({
|
|
36
|
+
content,
|
|
37
|
+
insertAfter
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const updated = buildUpdatedContent({
|
|
41
|
+
content,
|
|
42
|
+
insertInfo,
|
|
43
|
+
toInsertLine,
|
|
44
|
+
insertAfter
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
writeFile(jsFilePath, updated);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export default alterFile;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// v2/AppJs/index.js
|
|
2
|
+
|
|
3
|
+
import checkLines from "./checkLines.json" with {type: "json"};
|
|
4
|
+
import alterFile from "./common/AlterFile/index.js";
|
|
5
|
+
|
|
6
|
+
const updateAppJs = ({ inJsFilePath, inCheckLines,
|
|
7
|
+
showLog = false }) => {
|
|
8
|
+
|
|
9
|
+
const localCheckLines = inCheckLines || checkLines;
|
|
10
|
+
// console.log("bbbbbbbbbbbb : ", localCheckLines);
|
|
11
|
+
|
|
12
|
+
alterFile({
|
|
13
|
+
jsFilePath: inJsFilePath,
|
|
14
|
+
toInsertLine: localCheckLines.importLines.toInsertLine,
|
|
15
|
+
duplicationCheck: localCheckLines.importLines.duplicationCheck,
|
|
16
|
+
insertAfter: localCheckLines.importLines.insertAfter,
|
|
17
|
+
showLog
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
alterFile({
|
|
21
|
+
jsFilePath: inJsFilePath,
|
|
22
|
+
toInsertLine: localCheckLines.useLines.toInsertLine,
|
|
23
|
+
duplicationCheck: localCheckLines.useLines.duplicationCheck,
|
|
24
|
+
insertAfter: localCheckLines.useLines.insertAfter,
|
|
25
|
+
showLog
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return false;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export default updateAppJs;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
|
|
3
|
+
export const createFolder = ({ source, destination, checkBeforeCreate = false, isAnnounce = true }) => {
|
|
4
|
+
if (checkBeforeCreate) {
|
|
5
|
+
return createFolderWithCheck({ source, destination, isAnnounce });
|
|
6
|
+
} else {
|
|
7
|
+
return createOnly({ source, destination });
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const createOnly = ({ source, destination }) => {
|
|
12
|
+
fs.mkdirSync(destination, { recursive: true });
|
|
13
|
+
|
|
14
|
+
fs.cpSync(source, destination, { recursive: true });
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
KTF: true
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const createFolderWithCheck = ({ source, destination, isAnnounce }) => {
|
|
22
|
+
if (fs.existsSync(destination)) {
|
|
23
|
+
if (isAnnounce) console.log("Folder already exists :", destination);
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
KTF: false,
|
|
27
|
+
KReason: "Folder already exists"
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
if (isAnnounce) console.log("Folder created :", destination);
|
|
32
|
+
|
|
33
|
+
return createOnly({ source, destination });
|
|
34
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export default function parseInput({ jsFilePath,
|
|
2
|
+
inCheckLines = {}, showLog }) {
|
|
3
|
+
|
|
4
|
+
const [...args] = process.argv.slice(2);
|
|
5
|
+
|
|
6
|
+
return {
|
|
7
|
+
showLog: args[1] === undefined
|
|
8
|
+
? showLog
|
|
9
|
+
: args[1] === "true",
|
|
10
|
+
inJsFilePath: jsFilePath || process.cwd(),
|
|
11
|
+
inCheckLines
|
|
12
|
+
};
|
|
13
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/*
|
|
2
|
+
KSchema CLI – Entry Flow
|
|
3
|
+
|
|
4
|
+
1. Read user input from terminal (parseInput)
|
|
5
|
+
2. If no command → show usage (first-time user safety)
|
|
6
|
+
3. If help flags → show usage (quick guidance)
|
|
7
|
+
4. Resolve command dynamically (no hardcoding logic)
|
|
8
|
+
5. If command not found → inform + guide back to usage
|
|
9
|
+
6. Execute command with parsed input
|
|
10
|
+
|
|
11
|
+
Goal:
|
|
12
|
+
- Zero confusion for user
|
|
13
|
+
- Single source of truth (showUsage)
|
|
14
|
+
- Easy to extend (just add commands, no core changes)
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export default function showUsage(version) {
|
|
18
|
+
const g = "\x1b[32m";
|
|
19
|
+
const y = "\x1b[33m";
|
|
20
|
+
const c = "\x1b[36m";
|
|
21
|
+
const gray = "\x1b[90m";
|
|
22
|
+
const r = "\x1b[0m";
|
|
23
|
+
|
|
24
|
+
console.log(`
|
|
25
|
+
${c}🚀 KSchema Api Generator v${version}${r}
|
|
26
|
+
|
|
27
|
+
${y}Usage:${r}
|
|
28
|
+
${g}npx @keshavsoft/kschema-api-gen${r} <command> [options]
|
|
29
|
+
|
|
30
|
+
${y}Commands:${r}
|
|
31
|
+
${g}StartEndPoint${r} Initialize a new folder and files
|
|
32
|
+
${g}AddSubRoute${r} Initialize a new folder and files
|
|
33
|
+
${g}AddTableName${r} Initialize a new folder and files for TableName
|
|
34
|
+
${g}ShowAll${r} Initialize a new folder and files for action
|
|
35
|
+
|
|
36
|
+
${g}CreateApi${r} Creates new end point and hooks to app.js
|
|
37
|
+
${g}InsertApi${r} Creates new InsertApi end point and hooks to app.js
|
|
38
|
+
|
|
39
|
+
${y}Examples:${r}
|
|
40
|
+
${gray}npx @keshavsoft/kschema-api-gen StartEndPoint${r}
|
|
41
|
+
${gray}npx @keshavsoft/kschema-api-gen AddSubRoute${r}
|
|
42
|
+
${gray}npx @keshavsoft/kschema-api-gen AddTableName${r}
|
|
43
|
+
${gray}npx @keshavsoft/kschema-api-gen ShowAll${r}
|
|
44
|
+
${gray}npx @keshavsoft/kschema-api-gen CreateApi Api/V1/journals/ShowAll${r}
|
|
45
|
+
${gray}npx @keshavsoft/kschema-api-gen InsertApi Api/V1/journals/Insert${r}
|
|
46
|
+
|
|
47
|
+
${y}Tip:${r}
|
|
48
|
+
${gray}npm i -g @keshavsoft/kschema-api-gen${r}
|
|
49
|
+
`);
|
|
50
|
+
}
|
package/bin/v1/start.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import parseInput from "./core/parseInput.js";
|
|
2
|
+
import showUsage from './core/showUsage.js';
|
|
3
|
+
|
|
4
|
+
import updateJs from "./UpdateJs/index.js";
|
|
5
|
+
|
|
6
|
+
import pkg from '../../package.json' with { type: 'json' };
|
|
7
|
+
|
|
8
|
+
const version = pkg.version;
|
|
9
|
+
|
|
10
|
+
const run = ({ jsFilePath, inCheckLines, showLog }) => {
|
|
11
|
+
const input = parseInput({ jsFilePath, inCheckLines, showLog });
|
|
12
|
+
|
|
13
|
+
if (input.cmd === "--help" || input.cmd === "-h" || input.cmd === "help") return showUsage(version);
|
|
14
|
+
|
|
15
|
+
updateJs(input);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export default run;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"importLines": {
|
|
3
|
+
"toInsertLine": "import { router as routerFrom<startEndPoint> } from './<startEndPoint>/routes.js';",
|
|
4
|
+
"duplicationCheck": "from './<startEndPoint>/routes.js';",
|
|
5
|
+
"insertAfter": [
|
|
6
|
+
"import routerFrom",
|
|
7
|
+
"import { router as routerFrom",
|
|
8
|
+
"import express"
|
|
9
|
+
]
|
|
10
|
+
},
|
|
11
|
+
"useLines": {
|
|
12
|
+
"toInsertLine": "app.use('/<startEndPoint>', routerFrom<startEndPoint>);",
|
|
13
|
+
"duplicationCheck": "app.use('/<startEndPoint>'",
|
|
14
|
+
"insertAfter": [
|
|
15
|
+
"app.",
|
|
16
|
+
"const app = "
|
|
17
|
+
]
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const startFunc = ({
|
|
2
|
+
content,
|
|
3
|
+
insertInfo,
|
|
4
|
+
toInsertLine,
|
|
5
|
+
insertAfter
|
|
6
|
+
}) => {
|
|
7
|
+
const before = content.slice(0, insertInfo.index);
|
|
8
|
+
|
|
9
|
+
const isFirstInsert =
|
|
10
|
+
insertInfo.matchedPattern === insertAfter[insertAfter.length - 1];
|
|
11
|
+
|
|
12
|
+
return before +
|
|
13
|
+
(isFirstInsert ? "\n" : "") +
|
|
14
|
+
toInsertLine +
|
|
15
|
+
"\n" +
|
|
16
|
+
content.slice(insertInfo.index);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export default startFunc;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const checkUseDuplicate = ({
|
|
2
|
+
inContent,
|
|
3
|
+
inFilePath,
|
|
4
|
+
inSearchText
|
|
5
|
+
}) => {
|
|
6
|
+
const lines = inContent.split("\n");
|
|
7
|
+
|
|
8
|
+
const lineIndex = lines.findIndex(line =>
|
|
9
|
+
line.includes(inSearchText)
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
return {
|
|
13
|
+
found: lineIndex !== -1,
|
|
14
|
+
filePath: inFilePath,
|
|
15
|
+
lineNumber: lineIndex !== -1
|
|
16
|
+
? lineIndex + 1
|
|
17
|
+
: null
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export default checkUseDuplicate;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const findInsertIndex = ({
|
|
2
|
+
inContent,
|
|
3
|
+
inPatterns = []
|
|
4
|
+
}) => {
|
|
5
|
+
const lines = inContent.split("\n");
|
|
6
|
+
|
|
7
|
+
let lineNumber = -1;
|
|
8
|
+
let matchedPattern = null;
|
|
9
|
+
|
|
10
|
+
lines.forEach((line, index) => {
|
|
11
|
+
const pattern = inPatterns.find(item => line.includes(item));
|
|
12
|
+
|
|
13
|
+
if (pattern) {
|
|
14
|
+
lineNumber = index;
|
|
15
|
+
matchedPattern = pattern;
|
|
16
|
+
};
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
index: lineNumber === -1
|
|
21
|
+
? -1
|
|
22
|
+
: lines
|
|
23
|
+
.slice(0, lineNumber + 1)
|
|
24
|
+
.join("\n")
|
|
25
|
+
.length + 1,
|
|
26
|
+
matchedPattern
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export default findInsertIndex;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import readFile from "../readFile.js";
|
|
2
|
+
import checkDuplicate from "./checkDuplicate.js";
|
|
3
|
+
import findInsertIndex from "./findInsertIndex.js";
|
|
4
|
+
import writeFile from "../writeFile.js";
|
|
5
|
+
|
|
6
|
+
import buildUpdatedContent from "./buildUpdatedContent.js";
|
|
7
|
+
|
|
8
|
+
const locateInsertPoint = ({ content, insertAfter }) => {
|
|
9
|
+
return findInsertIndex({
|
|
10
|
+
inContent: content,
|
|
11
|
+
inPatterns: insertAfter
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const alterFile = ({
|
|
16
|
+
jsFilePath,
|
|
17
|
+
toInsertLine,
|
|
18
|
+
duplicationCheck,
|
|
19
|
+
insertAfter = [],
|
|
20
|
+
showLog = false, inStartEndPoint
|
|
21
|
+
}) => {
|
|
22
|
+
const content = readFile(jsFilePath);
|
|
23
|
+
|
|
24
|
+
const duplicateInfo = checkDuplicate({
|
|
25
|
+
content, jsFilePath,
|
|
26
|
+
duplicationCheck, inStartEndPoint
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const insertInfo = locateInsertPoint({
|
|
30
|
+
content,
|
|
31
|
+
insertAfter
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const updated = buildUpdatedContent({
|
|
35
|
+
content,
|
|
36
|
+
insertInfo,
|
|
37
|
+
toInsertLine,
|
|
38
|
+
insertAfter
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
writeFile(jsFilePath, updated);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export default alterFile;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// v2/AppJs/index.js
|
|
2
|
+
import fixAnyJs from "express-fix-any-js";
|
|
3
|
+
import checkLines from "./checkLines.json" with {type: "json"};
|
|
4
|
+
import alterFile from "./common/AlterFile/index.js";
|
|
5
|
+
|
|
6
|
+
const alterLines = ({ inCheckLines, inStartEndPoint }) => {
|
|
7
|
+
inCheckLines.importLines.toInsertLine = inCheckLines.importLines.toInsertLine.replaceAll("<startEndPoint>", inStartEndPoint);
|
|
8
|
+
inCheckLines.importLines.duplicationCheck = inCheckLines.importLines.duplicationCheck.replaceAll("<startEndPoint>", inStartEndPoint).replaceAll("'", '"');
|
|
9
|
+
|
|
10
|
+
inCheckLines.useLines.toInsertLine = inCheckLines.useLines.toInsertLine.replaceAll("<startEndPoint>", inStartEndPoint);
|
|
11
|
+
inCheckLines.useLines.duplicationCheck = inCheckLines.useLines.duplicationCheck.replaceAll("<startEndPoint>", inStartEndPoint).replaceAll("'", '"');
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const updateAppJs = ({ inJsFilePath, inCheckLines, inStartEndPoint = "Api",
|
|
15
|
+
showLog = false }) => {
|
|
16
|
+
|
|
17
|
+
let localCheckLines = inCheckLines || checkLines;
|
|
18
|
+
|
|
19
|
+
alterLines({ inCheckLines: localCheckLines, inStartEndPoint });
|
|
20
|
+
|
|
21
|
+
fixAnyJs({
|
|
22
|
+
showLog,
|
|
23
|
+
jsFilePath: inJsFilePath,
|
|
24
|
+
inCheckLines: localCheckLines
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
return false;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export default updateAppJs;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
|
|
3
|
+
export const createFolder = ({ source, destination, checkBeforeCreate = false, isAnnounce = true }) => {
|
|
4
|
+
if (checkBeforeCreate) {
|
|
5
|
+
return createFolderWithCheck({ source, destination, isAnnounce });
|
|
6
|
+
} else {
|
|
7
|
+
return createOnly({ source, destination });
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const createOnly = ({ source, destination }) => {
|
|
12
|
+
fs.mkdirSync(destination, { recursive: true });
|
|
13
|
+
|
|
14
|
+
fs.cpSync(source, destination, { recursive: true });
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
KTF: true
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const createFolderWithCheck = ({ source, destination, isAnnounce }) => {
|
|
22
|
+
if (fs.existsSync(destination)) {
|
|
23
|
+
if (isAnnounce) console.log("Folder already exists :", destination);
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
KTF: false,
|
|
27
|
+
KReason: "Folder already exists"
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
if (isAnnounce) console.log("Folder created :", destination);
|
|
32
|
+
|
|
33
|
+
return createOnly({ source, destination });
|
|
34
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export default function parseInput({ jsFilePath,
|
|
2
|
+
inCheckLines = {}, showLog }) {
|
|
3
|
+
|
|
4
|
+
const [...args] = process.argv.slice(2);
|
|
5
|
+
|
|
6
|
+
return {
|
|
7
|
+
showLog: args[1] === undefined
|
|
8
|
+
? showLog
|
|
9
|
+
: args[1] === "true",
|
|
10
|
+
inJsFilePath: jsFilePath || process.cwd(),
|
|
11
|
+
inCheckLines
|
|
12
|
+
};
|
|
13
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/*
|
|
2
|
+
KSchema CLI – Entry Flow
|
|
3
|
+
|
|
4
|
+
1. Read user input from terminal (parseInput)
|
|
5
|
+
2. If no command → show usage (first-time user safety)
|
|
6
|
+
3. If help flags → show usage (quick guidance)
|
|
7
|
+
4. Resolve command dynamically (no hardcoding logic)
|
|
8
|
+
5. If command not found → inform + guide back to usage
|
|
9
|
+
6. Execute command with parsed input
|
|
10
|
+
|
|
11
|
+
Goal:
|
|
12
|
+
- Zero confusion for user
|
|
13
|
+
- Single source of truth (showUsage)
|
|
14
|
+
- Easy to extend (just add commands, no core changes)
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export default function showUsage(version) {
|
|
18
|
+
const g = "\x1b[32m";
|
|
19
|
+
const y = "\x1b[33m";
|
|
20
|
+
const c = "\x1b[36m";
|
|
21
|
+
const gray = "\x1b[90m";
|
|
22
|
+
const r = "\x1b[0m";
|
|
23
|
+
|
|
24
|
+
console.log(`
|
|
25
|
+
${c}🚀 KSchema Api Generator v${version}${r}
|
|
26
|
+
|
|
27
|
+
${y}Usage:${r}
|
|
28
|
+
${g}npx @keshavsoft/kschema-api-gen${r} <command> [options]
|
|
29
|
+
|
|
30
|
+
${y}Commands:${r}
|
|
31
|
+
${g}StartEndPoint${r} Initialize a new folder and files
|
|
32
|
+
${g}AddSubRoute${r} Initialize a new folder and files
|
|
33
|
+
${g}AddTableName${r} Initialize a new folder and files for TableName
|
|
34
|
+
${g}ShowAll${r} Initialize a new folder and files for action
|
|
35
|
+
|
|
36
|
+
${g}CreateApi${r} Creates new end point and hooks to app.js
|
|
37
|
+
${g}InsertApi${r} Creates new InsertApi end point and hooks to app.js
|
|
38
|
+
|
|
39
|
+
${y}Examples:${r}
|
|
40
|
+
${gray}npx @keshavsoft/kschema-api-gen StartEndPoint${r}
|
|
41
|
+
${gray}npx @keshavsoft/kschema-api-gen AddSubRoute${r}
|
|
42
|
+
${gray}npx @keshavsoft/kschema-api-gen AddTableName${r}
|
|
43
|
+
${gray}npx @keshavsoft/kschema-api-gen ShowAll${r}
|
|
44
|
+
${gray}npx @keshavsoft/kschema-api-gen CreateApi Api/V1/journals/ShowAll${r}
|
|
45
|
+
${gray}npx @keshavsoft/kschema-api-gen InsertApi Api/V1/journals/Insert${r}
|
|
46
|
+
|
|
47
|
+
${y}Tip:${r}
|
|
48
|
+
${gray}npm i -g @keshavsoft/kschema-api-gen${r}
|
|
49
|
+
`);
|
|
50
|
+
}
|
package/bin/v2/start.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import parseInput from "./core/parseInput.js";
|
|
2
|
+
import showUsage from './core/showUsage.js';
|
|
3
|
+
|
|
4
|
+
import updateJs from "./UpdateJs/index.js";
|
|
5
|
+
|
|
6
|
+
import pkg from '../../package.json' with { type: 'json' };
|
|
7
|
+
|
|
8
|
+
const version = pkg.version;
|
|
9
|
+
|
|
10
|
+
const run = ({ jsFilePath, inCheckLines, showLog }) => {
|
|
11
|
+
const input = parseInput({ jsFilePath, inCheckLines, showLog });
|
|
12
|
+
|
|
13
|
+
if (input.cmd === "--help" || input.cmd === "-h" || input.cmd === "help") return showUsage(version);
|
|
14
|
+
|
|
15
|
+
updateJs(input);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export default run;
|
package/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import getLatestVersion from "./bin/core/getLatestVersion.js";
|
|
2
|
+
|
|
3
|
+
const load = async ({ jsFilePath, inCheckLines, showLog }) => {
|
|
4
|
+
const v = getLatestVersion();
|
|
5
|
+
|
|
6
|
+
const module = await import(`./bin/${v}/start.js`);
|
|
7
|
+
|
|
8
|
+
await module.default({ jsFilePath, inCheckLines, showLog });
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export default load;
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "express-fix-app-js",
|
|
3
|
+
"version": "1.2.4",
|
|
4
|
+
"description": "CLI to build for appjs, the endpoints",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"cli",
|
|
7
|
+
"scaffold",
|
|
8
|
+
"templates",
|
|
9
|
+
"node",
|
|
10
|
+
"project-generator"
|
|
11
|
+
],
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"express-fix-any-js": "^1.3.3"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": "./index.js"
|
|
18
|
+
},
|
|
19
|
+
"bin": {
|
|
20
|
+
"express-fix-app-js": "./bin/cli.js"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"bin/",
|
|
24
|
+
"index.js",
|
|
25
|
+
"README.md",
|
|
26
|
+
"LICENSE"
|
|
27
|
+
],
|
|
28
|
+
"homepage": "https://cli.keshavsoft.com",
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "https://github.com/keshavsoft/express-fix-app-js"
|
|
32
|
+
},
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/keshavsoft/express-fix-app-js/issues"
|
|
35
|
+
}
|
|
36
|
+
}
|