kschema-api-gen-appjs 1.3.5 → 1.4.1

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.
Files changed (62) hide show
  1. package/bin/v4/StartEndPoint/Helpers/steps/UpdateAppJs/common/readFile.js +8 -0
  2. package/bin/v4/StartEndPoint/Helpers/steps/UpdateAppJs/common/writeFile.js +10 -0
  3. package/bin/v4/StartEndPoint/Helpers/steps/UpdateAppJs/index.js +19 -0
  4. package/bin/v4/StartEndPoint/Helpers/steps/UpdateAppJs/updateImports/buildImport.js +9 -0
  5. package/bin/v4/StartEndPoint/Helpers/steps/UpdateAppJs/updateImports/checkDuplicate.js +8 -0
  6. package/bin/v4/StartEndPoint/Helpers/steps/UpdateAppJs/updateImports/findInsertIndex.js +10 -0
  7. package/bin/v4/StartEndPoint/Helpers/steps/UpdateAppJs/updateImports/index.js +41 -0
  8. package/bin/v4/StartEndPoint/Helpers/steps/UpdateAppJs/updateUse/buildUseLine.js +9 -0
  9. package/bin/v4/StartEndPoint/Helpers/steps/UpdateAppJs/updateUse/checkDuplicate.js +8 -0
  10. package/bin/v4/StartEndPoint/Helpers/steps/UpdateAppJs/updateUse/findInsertIndex.js +10 -0
  11. package/bin/v4/StartEndPoint/Helpers/steps/UpdateAppJs/updateUse/index.js +42 -0
  12. package/bin/v4/StartEndPoint/Helpers/steps/announce.js +9 -0
  13. package/bin/v4/StartEndPoint/Helpers/steps/decideTemplate.js +3 -0
  14. package/bin/v4/StartEndPoint/Helpers/steps/locateDestination.js +7 -0
  15. package/bin/v4/StartEndPoint/Helpers/steps/locateSource.js +13 -0
  16. package/bin/v4/StartEndPoint/Helpers/steps/resolveFolderName.js +20 -0
  17. package/bin/v4/StartEndPoint/Helpers/template/routes.js +5 -0
  18. package/bin/v4/StartEndPoint/index.js +49 -0
  19. package/bin/v4/commands/AddSubRoute/steps/UpdateRoutesJs/common/readFile.js +8 -0
  20. package/bin/v4/commands/AddSubRoute/steps/UpdateRoutesJs/common/writeFile.js +10 -0
  21. package/bin/v4/commands/AddSubRoute/steps/UpdateRoutesJs/updateImports/buildImport.js +9 -0
  22. package/bin/v4/commands/AddSubRoute/steps/UpdateRoutesJs/updateImports/checkDuplicate.js +8 -0
  23. package/bin/v4/commands/AddSubRoute/steps/UpdateRoutesJs/updateImports/findInsertIndex.js +10 -0
  24. package/bin/v4/commands/AddSubRoute/steps/UpdateRoutesJs/updateImports/index.js +41 -0
  25. package/bin/v4/commands/AddSubRoute/steps/UpdateRoutesJs/updateUse/buildUseLine.js +11 -0
  26. package/bin/v4/commands/AddSubRoute/steps/UpdateRoutesJs/updateUse/checkDuplicate.js +8 -0
  27. package/bin/v4/commands/AddSubRoute/steps/UpdateRoutesJs/updateUse/findInsertIndex.js +10 -0
  28. package/bin/v4/commands/AddSubRoute/steps/UpdateRoutesJs/updateUse/index.js +42 -0
  29. package/bin/v4/commands/AddSubRoute/steps/announce.js +9 -0
  30. package/bin/v4/commands/AddSubRoute/steps/createFolder.js +7 -0
  31. package/bin/v4/commands/AddSubRoute/steps/decideTemplate.js +3 -0
  32. package/bin/v4/commands/AddSubRoute/steps/locateDestination.js +5 -0
  33. package/bin/v4/commands/AddSubRoute/steps/locateSource.js +13 -0
  34. package/bin/v4/commands/AddSubRoute/steps/resolveFolderName.js +20 -0
  35. package/bin/v4/commands/AddSubRoute/steps/updateAppJs.js +7 -0
  36. package/bin/v4/commands/AddSubRoute/template/routes.js +5 -0
  37. package/bin/v4/commands/StartEndPoint/steps/UpdateAppJs/common/readFile.js +8 -0
  38. package/bin/v4/commands/StartEndPoint/steps/UpdateAppJs/common/writeFile.js +10 -0
  39. package/bin/v4/commands/StartEndPoint/steps/UpdateAppJs/index.js +11 -0
  40. package/bin/v4/commands/StartEndPoint/steps/UpdateAppJs/updateImports/buildImport.js +9 -0
  41. package/bin/v4/commands/StartEndPoint/steps/UpdateAppJs/updateImports/checkDuplicate.js +8 -0
  42. package/bin/v4/commands/StartEndPoint/steps/UpdateAppJs/updateImports/findInsertIndex.js +10 -0
  43. package/bin/v4/commands/StartEndPoint/steps/UpdateAppJs/updateImports/index.js +41 -0
  44. package/bin/v4/commands/StartEndPoint/steps/UpdateAppJs/updateUse/buildUseLine.js +9 -0
  45. package/bin/v4/commands/StartEndPoint/steps/UpdateAppJs/updateUse/checkDuplicate.js +8 -0
  46. package/bin/v4/commands/StartEndPoint/steps/UpdateAppJs/updateUse/findInsertIndex.js +10 -0
  47. package/bin/v4/commands/StartEndPoint/steps/UpdateAppJs/updateUse/index.js +42 -0
  48. package/bin/v4/commands/StartEndPoint/steps/announce.js +9 -0
  49. package/bin/v4/commands/StartEndPoint/steps/decideTemplate.js +3 -0
  50. package/bin/v4/commands/StartEndPoint/steps/locateDestination.js +7 -0
  51. package/bin/v4/commands/StartEndPoint/steps/locateSource.js +13 -0
  52. package/bin/v4/commands/StartEndPoint/steps/resolveFolderName.js +20 -0
  53. package/bin/v4/commands/StartEndPoint/template/routes.js +5 -0
  54. package/bin/v4/commands/addSubRoute.js +42 -0
  55. package/bin/v4/commands/startEndPoint.js +44 -0
  56. package/bin/v4/core/createFolder.js +34 -0
  57. package/bin/v4/core/parseInput.js +16 -0
  58. package/bin/v4/core/resolveCommand.js +14 -0
  59. package/bin/v4/core/showUsage.js +50 -0
  60. package/bin/v4/start.js +19 -0
  61. package/index.js +6 -5
  62. package/package.json +1 -1
@@ -0,0 +1,8 @@
1
+ import fs from "fs";
2
+
3
+ const readFile = (inAppJsPath) => {
4
+ const localPath = inAppJsPath;
5
+ return fs.readFileSync(localPath, "utf-8");
6
+ };
7
+
8
+ export default readFile;
@@ -0,0 +1,10 @@
1
+ import fs from "fs";
2
+
3
+ const writeFile = (inAppJsPath, inContent) => {
4
+ const localPath = inAppJsPath;
5
+ const localContent = inContent;
6
+
7
+ fs.writeFileSync(localPath, localContent);
8
+ };
9
+
10
+ export default writeFile;
@@ -0,0 +1,19 @@
1
+ import fs from "fs";
2
+
3
+ import updateImports from "./updateImports/index.js";
4
+ import updateAppUse from "./updateUse/index.js";
5
+
6
+ const updateAppJs = ({ appJsPath, endpoint, showLog }) => {
7
+ if (!fs.existsSync(appJsPath)) {
8
+ if (showLog) console.log("app.js file not found ?");
9
+
10
+ return false;
11
+ };
12
+
13
+ updateImports({ appJsPath, endpoint });
14
+ updateAppUse({ appJsPath, endpoint });
15
+
16
+ return false;
17
+ };
18
+
19
+ export default updateAppJs;
@@ -0,0 +1,9 @@
1
+ const buildImport = (inEndpoint) => {
2
+ const localEndpoint = inEndpoint;
3
+
4
+ const safeName = localEndpoint.replace(/[^a-zA-Z0-9]/g, "_");
5
+
6
+ return `import { router as routerFrom${safeName} } from "./${localEndpoint}/routes.js";`;
7
+ };
8
+
9
+ export default buildImport;
@@ -0,0 +1,8 @@
1
+ const checkUseDuplicate = (inContent, inEndpoint) => {
2
+ const localContent = inContent;
3
+ const localEndpoint = inEndpoint;
4
+
5
+ return localContent.includes(`app.use("/${localEndpoint}"`);
6
+ };
7
+
8
+ export default checkUseDuplicate;
@@ -0,0 +1,10 @@
1
+ const findInsertIndex = (inContent) => {
2
+ const localContent = inContent;
3
+
4
+ const matches = [...localContent.matchAll(/^\s*import.*$/gm)];
5
+ const last = matches.at(-1);
6
+
7
+ return last ? last.index + last[0].length : 0;
8
+ };
9
+
10
+ export default findInsertIndex;
@@ -0,0 +1,41 @@
1
+ import readFile from "../common/readFile.js";
2
+ import buildImport from "./buildImport.js";
3
+ import checkDuplicate from "./checkDuplicate.js";
4
+ import findInsertIndex from "./findInsertIndex.js";
5
+ import writeFile from "../common/writeFile.js";
6
+
7
+ const updateImports = ({ appJsPath, endpoint, showLog }) => {
8
+ const summary = {
9
+ import: { added: false, line: null },
10
+ };
11
+
12
+ const content = readFile(appJsPath);
13
+ const importLine = buildImport(endpoint);
14
+
15
+ if (checkDuplicate(content, endpoint)) {
16
+ summary.import.skipped = true;
17
+
18
+ if (showLog) console.log(summary);
19
+
20
+ return summary;
21
+ };
22
+
23
+ const index = findInsertIndex(content);
24
+
25
+ const before = content.slice(0, index);
26
+ const lineNumber = before.split("\n").length + 1;
27
+
28
+ const updated =
29
+ before + "\n" + importLine + content.slice(index);
30
+
31
+ writeFile(appJsPath, updated);
32
+
33
+ summary.import.added = true;
34
+ summary.import.line = lineNumber;
35
+
36
+ if (showLog) console.log(summary);
37
+
38
+ return summary;
39
+ };
40
+
41
+ export default updateImports;
@@ -0,0 +1,9 @@
1
+ const buildUseLine = (inEndpoint) => {
2
+ const localEndpoint = inEndpoint;
3
+
4
+ const safeName = localEndpoint.replace(/[^a-zA-Z0-9]/g, "_");
5
+
6
+ return `app.use("/${localEndpoint}", routerFrom${safeName});`;
7
+ };
8
+
9
+ export default buildUseLine;
@@ -0,0 +1,8 @@
1
+ const checkDuplicate = (inContent, inEndpoint) => {
2
+ const localContent = inContent;
3
+ const localEndpoint = inEndpoint;
4
+
5
+ return localContent.includes(`app.use("/${localEndpoint}"`);
6
+ };
7
+
8
+ export default checkDuplicate;
@@ -0,0 +1,10 @@
1
+ const findUseInsertIndex = (inContent) => {
2
+ const localContent = inContent;
3
+
4
+ const matches = [...localContent.matchAll(/const\s+app\s*=\s*express\(\)/g)];
5
+ const match = matches.at(0);
6
+
7
+ return match ? match.index + match[0].length : localContent.length;
8
+ };
9
+
10
+ export default findUseInsertIndex;
@@ -0,0 +1,42 @@
1
+ import readFile from "../common/readFile.js";
2
+ import writeFile from "../common/writeFile.js";
3
+
4
+ import buildUseLine from "./buildUseLine.js";
5
+ import checkDuplicate from "./checkDuplicate.js";
6
+ import findUseInsertIndex from "./findInsertIndex.js";
7
+
8
+ const updateAppUse = ({ appJsPath, endpoint, showLog = false }) => {
9
+ const summary = {
10
+ use: { added: false, skipped: false, line: null }
11
+ };
12
+
13
+ const content = readFile(appJsPath);
14
+ const useLine = buildUseLine(endpoint);
15
+
16
+ if (checkDuplicate(content, endpoint)) {
17
+ summary.use.skipped = true;
18
+
19
+ if (showLog) console.log(summary);
20
+
21
+ return summary;
22
+ };
23
+
24
+ const index = findUseInsertIndex(content);
25
+
26
+ const before = content.slice(0, index);
27
+ const lineNumber = before.split("\n").length + 1;
28
+
29
+ const updated =
30
+ before + "\n" + useLine + content.slice(index);
31
+
32
+ writeFile(appJsPath, updated);
33
+
34
+ summary.use.added = true;
35
+ summary.use.line = lineNumber;
36
+
37
+ if (showLog) console.log(summary);
38
+
39
+ return summary;
40
+ };
41
+
42
+ export default updateAppUse;
@@ -0,0 +1,9 @@
1
+ export const announce = ({ inResolvedFolderName }) => {
2
+ console.log(`[keshavsoft] Folder created: ${inResolvedFolderName}`);
3
+
4
+ console.log("");
5
+ console.log("Next:");
6
+ console.log("");
7
+ console.log(`cd ${inResolvedFolderName}`);
8
+ console.log(`npx kschema-api-gen-appjs AddSubRoute`);
9
+ };
@@ -0,0 +1,3 @@
1
+ export const decideTemplate = ({ inTemplate }) => {
2
+ return inTemplate || "basic";
3
+ };
@@ -0,0 +1,7 @@
1
+ import path from "path";
2
+
3
+ export const locateDestination = ({ inResolvedFolderName , toPath }) => {
4
+ const localToPath = toPath;
5
+
6
+ return path.join(localToPath, inResolvedFolderName);
7
+ };
@@ -0,0 +1,13 @@
1
+ import path from "path";
2
+ import { fileURLToPath } from "url";
3
+
4
+ const __filename = fileURLToPath(import.meta.url);
5
+ const __dirname = path.dirname(__filename);
6
+
7
+ export const locateSource = () => {
8
+ return path.join(
9
+ __dirname,
10
+ "..",
11
+ "template"
12
+ );
13
+ };
@@ -0,0 +1,20 @@
1
+ import fs from "fs";
2
+
3
+ export default function resolveFolderName({ name }) {
4
+ const defaultFolerName = "Api";
5
+
6
+ // case 1: force new → timestamp
7
+ if (name === null) {
8
+ name = defaultFolerName;
9
+ };
10
+
11
+ // case 2: user provided → strict
12
+ if (fs.existsSync(name)) {
13
+ return {
14
+ KTF: false,
15
+ KReason: `Folder already exists : ${name}`
16
+ };
17
+ };
18
+
19
+ return name;
20
+ };
@@ -0,0 +1,5 @@
1
+ import express from 'express';
2
+
3
+ const router = express.Router();
4
+
5
+ export { router };
@@ -0,0 +1,49 @@
1
+ import { locateSource } from "./Helpers/steps/locateSource.js";
2
+ import { locateDestination } from "./Helpers/steps/locateDestination.js";
3
+ import { createFolder } from "../core/createFolder.js";
4
+ import updateAppJs from "./Helpers/steps/UpdateAppJs/index.js";
5
+
6
+ import { announce } from "./Helpers/steps/announce.js";
7
+
8
+ import resolveFolderName from "./Helpers/steps/resolveFolderName.js";
9
+
10
+ export default ({ folderName = "", toPath, isAnnounce = true,
11
+ checkBeforeCreate = true, showLog = false
12
+ }) => {
13
+ console.log("aaaaaaaaaa : ", folderName);
14
+
15
+ const localToPath = toPath;
16
+
17
+ const resolvedFolderName = resolveFolderName({
18
+ name: folderName
19
+ });
20
+
21
+ if (resolvedFolderName.KTF === false) {
22
+ return folderName;
23
+ };
24
+
25
+ const source = locateSource();
26
+ const destination = locateDestination({
27
+ inResolvedFolderName: resolvedFolderName,
28
+ toPath: localToPath
29
+ });
30
+
31
+ const createFolderResponse = createFolder({
32
+ source, destination,
33
+ isAnnounce, checkBeforeCreate
34
+ });
35
+
36
+ if (createFolderResponse.KTF) {
37
+ const fromUpdate = updateAppJs({
38
+ appJsPath: `${localToPath}/app.js`,
39
+ endpoint: resolvedFolderName,
40
+ showLog
41
+ });
42
+
43
+ if (showLog) console.log("fromUpdate : ", fromUpdate);
44
+ };
45
+
46
+ if (isAnnounce) announce({ inResolvedFolderName: resolvedFolderName });
47
+
48
+ return resolvedFolderName;
49
+ };
@@ -0,0 +1,8 @@
1
+ import fs from "fs";
2
+
3
+ const readFile = (inAppJsPath) => {
4
+ const localPath = inAppJsPath;
5
+ return fs.readFileSync(localPath, "utf-8");
6
+ };
7
+
8
+ export default readFile;
@@ -0,0 +1,10 @@
1
+ import fs from "fs";
2
+
3
+ const writeFile = (inAppJsPath, inContent) => {
4
+ const localPath = inAppJsPath;
5
+ const localContent = inContent;
6
+
7
+ fs.writeFileSync(localPath, localContent);
8
+ };
9
+
10
+ export default writeFile;
@@ -0,0 +1,9 @@
1
+ const buildImport = (inEndpoint) => {
2
+ const localEndpoint = inEndpoint;
3
+
4
+ const safeName = localEndpoint.replace(/[^a-zA-Z0-9]/g, "_");
5
+
6
+ return `import { router as routerFrom${safeName} } from "./${localEndpoint}/routes.js";`;
7
+ };
8
+
9
+ export default buildImport;
@@ -0,0 +1,8 @@
1
+ const checkUseDuplicate = (inContent, inEndpoint) => {
2
+ const localContent = inContent;
3
+ const localEndpoint = inEndpoint;
4
+
5
+ return localContent.includes(`app.use("/${localEndpoint}"`);
6
+ };
7
+
8
+ export default checkUseDuplicate;
@@ -0,0 +1,10 @@
1
+ const findInsertIndex = (inContent) => {
2
+ const localContent = inContent;
3
+
4
+ const matches = [...localContent.matchAll(/^\s*import.*$/gm)];
5
+ const last = matches.at(-1);
6
+
7
+ return last ? last.index + last[0].length : 0;
8
+ };
9
+
10
+ export default findInsertIndex;
@@ -0,0 +1,41 @@
1
+ import readFile from "../common/readFile.js";
2
+ import buildImport from "./buildImport.js";
3
+ import checkDuplicate from "./checkDuplicate.js";
4
+ import findInsertIndex from "./findInsertIndex.js";
5
+ import writeFile from "../common/writeFile.js";
6
+
7
+ const updateImports = ({ appJsPath, endpoint, showLog }) => {
8
+ const summary = {
9
+ import: { added: false, line: null },
10
+ };
11
+
12
+ const content = readFile(appJsPath);
13
+ const importLine = buildImport(endpoint);
14
+
15
+ if (checkDuplicate(content, endpoint)) {
16
+ summary.import.skipped = true;
17
+
18
+ if (showLog) console.log(summary);
19
+
20
+ return summary;
21
+ };
22
+
23
+ const index = findInsertIndex(content);
24
+
25
+ const before = content.slice(0, index);
26
+ const lineNumber = before.split("\n").length + 1;
27
+
28
+ const updated =
29
+ before + "\n" + importLine + content.slice(index);
30
+
31
+ writeFile(appJsPath, updated);
32
+
33
+ summary.import.added = true;
34
+ summary.import.line = lineNumber;
35
+
36
+ if (showLog) console.log(summary);
37
+
38
+ return summary;
39
+ };
40
+
41
+ export default updateImports;
@@ -0,0 +1,11 @@
1
+ const appOrRouter = "router";
2
+
3
+ const buildUseLine = (inEndpoint) => {
4
+ const localEndpoint = inEndpoint;
5
+
6
+ const safeName = localEndpoint.replace(/[^a-zA-Z0-9]/g, "_");
7
+
8
+ return `${appOrRouter}.use("/${localEndpoint}", routerFrom${safeName});`;
9
+ };
10
+
11
+ export default buildUseLine;
@@ -0,0 +1,8 @@
1
+ const checkDuplicate = (inContent, inEndpoint) => {
2
+ const localContent = inContent;
3
+ const localEndpoint = inEndpoint;
4
+
5
+ return localContent.includes(`app.use("/${localEndpoint}"`);
6
+ };
7
+
8
+ export default checkDuplicate;
@@ -0,0 +1,10 @@
1
+ const findUseInsertIndex = (inContent) => {
2
+ const localContent = inContent;
3
+
4
+ const matches = [...localContent.matchAll(/const\s+router\s*=\s*express\.Router\(\)/g)];
5
+ const match = matches.at(0);
6
+
7
+ return match ? match.index + match[0].length : localContent.length;
8
+ };
9
+
10
+ export default findUseInsertIndex;
@@ -0,0 +1,42 @@
1
+ import readFile from "../common/readFile.js";
2
+ import writeFile from "../common/writeFile.js";
3
+
4
+ import buildUseLine from "./buildUseLine.js";
5
+ import checkDuplicate from "./checkDuplicate.js";
6
+ import findUseInsertIndex from "./findInsertIndex.js";
7
+
8
+ const updateAppUse = ({ appJsPath, endpoint, showLog }) => {
9
+ const summary = {
10
+ use: { added: false, skipped: false, line: null }
11
+ };
12
+
13
+ const content = readFile(appJsPath);
14
+ const useLine = buildUseLine(endpoint);
15
+
16
+ if (checkDuplicate(content, endpoint)) {
17
+ summary.use.skipped = true;
18
+
19
+ if (showLog) console.log(summary);
20
+
21
+ return summary;
22
+ };
23
+
24
+ const index = findUseInsertIndex(content);
25
+
26
+ const before = content.slice(0, index);
27
+ const lineNumber = before.split("\n").length + 1;
28
+
29
+ const updated =
30
+ before + "\n" + useLine + content.slice(index);
31
+
32
+ writeFile(appJsPath, updated);
33
+
34
+ summary.use.added = true;
35
+ summary.use.line = lineNumber;
36
+
37
+ if (showLog) console.log(summary);
38
+
39
+ return summary;
40
+ };
41
+
42
+ export default updateAppUse;
@@ -0,0 +1,9 @@
1
+ export const announce = ({ inResolvedFolderName }) => {
2
+ console.log(`[keshavsoft] Folder created: ${inResolvedFolderName}`);
3
+
4
+ console.log("");
5
+ console.log("Next:");
6
+ console.log("");
7
+ console.log(`cd ${inResolvedFolderName}`);
8
+ console.log(`npx kschema-api-gen AddSubRoute`);
9
+ };
@@ -0,0 +1,7 @@
1
+ import fs from "fs";
2
+
3
+ export const createFolder = ({ source, destination }) => {
4
+ fs.mkdirSync(destination, { recursive: true });
5
+
6
+ fs.cpSync(source, destination, { recursive: true });
7
+ };
@@ -0,0 +1,3 @@
1
+ export const decideTemplate = ({ inTemplate }) => {
2
+ return inTemplate || "basic";
3
+ };
@@ -0,0 +1,5 @@
1
+ import path from "path";
2
+
3
+ export const locateDestination = ({ inResolvedFolderName, toPath }) => {
4
+ return path.join(toPath, inResolvedFolderName);
5
+ };
@@ -0,0 +1,13 @@
1
+ import path from "path";
2
+ import { fileURLToPath } from "url";
3
+
4
+ const __filename = fileURLToPath(import.meta.url);
5
+ const __dirname = path.dirname(__filename);
6
+
7
+ export const locateSource = () => {
8
+ return path.join(
9
+ __dirname,
10
+ "..",
11
+ "template"
12
+ );
13
+ };
@@ -0,0 +1,20 @@
1
+ import fs from "fs";
2
+
3
+ export default function resolveFolderName({ name }) {
4
+ const defaultFolerName = "V1";
5
+
6
+ // case 1: force new → timestamp
7
+ if (name === null) {
8
+ name = defaultFolerName;
9
+ };
10
+
11
+ // case 2: user provided → strict
12
+ if (fs.existsSync(name)) {
13
+ return {
14
+ KTF: false,
15
+ KReason: `Folder already exists : ${name}`
16
+ };
17
+ };
18
+
19
+ return name;
20
+ };
@@ -0,0 +1,7 @@
1
+ import updateImports from "./UpdateRoutesJs/updateImports/index.js";
2
+ import updateAppUse from "./UpdateRoutesJs/updateUse/index.js";
3
+
4
+ export function updateAppJs({ appJsPath, endpoint }) {
5
+ updateImports({ appJsPath, endpoint });
6
+ updateAppUse({ appJsPath, endpoint });
7
+ };
@@ -0,0 +1,5 @@
1
+ import express from 'express';
2
+
3
+ const router = express.Router();
4
+
5
+ export { router };
@@ -0,0 +1,8 @@
1
+ import fs from "fs";
2
+
3
+ const readFile = (inAppJsPath) => {
4
+ const localPath = inAppJsPath;
5
+ return fs.readFileSync(localPath, "utf-8");
6
+ };
7
+
8
+ export default readFile;
@@ -0,0 +1,10 @@
1
+ import fs from "fs";
2
+
3
+ const writeFile = (inAppJsPath, inContent) => {
4
+ const localPath = inAppJsPath;
5
+ const localContent = inContent;
6
+
7
+ fs.writeFileSync(localPath, localContent);
8
+ };
9
+
10
+ export default writeFile;
@@ -0,0 +1,11 @@
1
+ import fs from "fs";
2
+
3
+ import updateImports from "./updateImports/index.js";
4
+ import updateAppUse from "./updateUse/index.js";
5
+
6
+ export function updateAppJs({ appJsPath, endpoint }) {
7
+ if (!fs.existsSync(appJsPath)) return;
8
+
9
+ updateImports({ appJsPath, endpoint });
10
+ updateAppUse({ appJsPath, endpoint });
11
+ };
@@ -0,0 +1,9 @@
1
+ const buildImport = (inEndpoint) => {
2
+ const localEndpoint = inEndpoint;
3
+
4
+ const safeName = localEndpoint.replace(/[^a-zA-Z0-9]/g, "_");
5
+
6
+ return `import { router as routerFrom${safeName} } from "./${localEndpoint}/routes.js";`;
7
+ };
8
+
9
+ export default buildImport;
@@ -0,0 +1,8 @@
1
+ const checkUseDuplicate = (inContent, inEndpoint) => {
2
+ const localContent = inContent;
3
+ const localEndpoint = inEndpoint;
4
+
5
+ return localContent.includes(`app.use("/${localEndpoint}"`);
6
+ };
7
+
8
+ export default checkUseDuplicate;
@@ -0,0 +1,10 @@
1
+ const findInsertIndex = (inContent) => {
2
+ const localContent = inContent;
3
+
4
+ const matches = [...localContent.matchAll(/^\s*import.*$/gm)];
5
+ const last = matches.at(-1);
6
+
7
+ return last ? last.index + last[0].length : 0;
8
+ };
9
+
10
+ export default findInsertIndex;
@@ -0,0 +1,41 @@
1
+ import readFile from "../common/readFile.js";
2
+ import buildImport from "./buildImport.js";
3
+ import checkDuplicate from "./checkDuplicate.js";
4
+ import findInsertIndex from "./findInsertIndex.js";
5
+ import writeFile from "../common/writeFile.js";
6
+
7
+ const updateImports = ({ appJsPath, endpoint, showLog }) => {
8
+ const summary = {
9
+ import: { added: false, line: null },
10
+ };
11
+
12
+ const content = readFile(appJsPath);
13
+ const importLine = buildImport(endpoint);
14
+
15
+ if (checkDuplicate(content, endpoint)) {
16
+ summary.import.skipped = true;
17
+
18
+ if (showLog) console.log(summary);
19
+
20
+ return summary;
21
+ };
22
+
23
+ const index = findInsertIndex(content);
24
+
25
+ const before = content.slice(0, index);
26
+ const lineNumber = before.split("\n").length + 1;
27
+
28
+ const updated =
29
+ before + "\n" + importLine + content.slice(index);
30
+
31
+ writeFile(appJsPath, updated);
32
+
33
+ summary.import.added = true;
34
+ summary.import.line = lineNumber;
35
+
36
+ if (showLog) console.log(summary);
37
+
38
+ return summary;
39
+ };
40
+
41
+ export default updateImports;
@@ -0,0 +1,9 @@
1
+ const buildUseLine = (inEndpoint) => {
2
+ const localEndpoint = inEndpoint;
3
+
4
+ const safeName = localEndpoint.replace(/[^a-zA-Z0-9]/g, "_");
5
+
6
+ return `app.use("/${localEndpoint}", routerFrom${safeName});`;
7
+ };
8
+
9
+ export default buildUseLine;
@@ -0,0 +1,8 @@
1
+ const checkDuplicate = (inContent, inEndpoint) => {
2
+ const localContent = inContent;
3
+ const localEndpoint = inEndpoint;
4
+
5
+ return localContent.includes(`app.use("/${localEndpoint}"`);
6
+ };
7
+
8
+ export default checkDuplicate;
@@ -0,0 +1,10 @@
1
+ const findUseInsertIndex = (inContent) => {
2
+ const localContent = inContent;
3
+
4
+ const matches = [...localContent.matchAll(/const\s+app\s*=\s*express\(\)/g)];
5
+ const match = matches.at(0);
6
+
7
+ return match ? match.index + match[0].length : localContent.length;
8
+ };
9
+
10
+ export default findUseInsertIndex;
@@ -0,0 +1,42 @@
1
+ import readFile from "../common/readFile.js";
2
+ import writeFile from "../common/writeFile.js";
3
+
4
+ import buildUseLine from "./buildUseLine.js";
5
+ import checkDuplicate from "./checkDuplicate.js";
6
+ import findUseInsertIndex from "./findInsertIndex.js";
7
+
8
+ const updateAppUse = ({ appJsPath, endpoint, showLog = false }) => {
9
+ const summary = {
10
+ use: { added: false, skipped: false, line: null }
11
+ };
12
+
13
+ const content = readFile(appJsPath);
14
+ const useLine = buildUseLine(endpoint);
15
+
16
+ if (checkDuplicate(content, endpoint)) {
17
+ summary.use.skipped = true;
18
+
19
+ if (showLog) console.log(summary);
20
+
21
+ return summary;
22
+ };
23
+
24
+ const index = findUseInsertIndex(content);
25
+
26
+ const before = content.slice(0, index);
27
+ const lineNumber = before.split("\n").length + 1;
28
+
29
+ const updated =
30
+ before + "\n" + useLine + content.slice(index);
31
+
32
+ writeFile(appJsPath, updated);
33
+
34
+ summary.use.added = true;
35
+ summary.use.line = lineNumber;
36
+
37
+ if (showLog) console.log(summary);
38
+
39
+ return summary;
40
+ };
41
+
42
+ export default updateAppUse;
@@ -0,0 +1,9 @@
1
+ export const announce = ({ inResolvedFolderName }) => {
2
+ console.log(`[keshavsoft] Folder created: ${inResolvedFolderName}`);
3
+
4
+ console.log("");
5
+ console.log("Next:");
6
+ console.log("");
7
+ console.log(`cd ${inResolvedFolderName}`);
8
+ console.log(`npx kschema-api-gen AddSubRoute`);
9
+ };
@@ -0,0 +1,3 @@
1
+ export const decideTemplate = ({ inTemplate }) => {
2
+ return inTemplate || "basic";
3
+ };
@@ -0,0 +1,7 @@
1
+ import path from "path";
2
+
3
+ export const locateDestination = ({ inResolvedFolderName , toPath }) => {
4
+ const localToPath = toPath;
5
+
6
+ return path.join(localToPath, inResolvedFolderName);
7
+ };
@@ -0,0 +1,13 @@
1
+ import path from "path";
2
+ import { fileURLToPath } from "url";
3
+
4
+ const __filename = fileURLToPath(import.meta.url);
5
+ const __dirname = path.dirname(__filename);
6
+
7
+ export const locateSource = () => {
8
+ return path.join(
9
+ __dirname,
10
+ "..",
11
+ "template"
12
+ );
13
+ };
@@ -0,0 +1,20 @@
1
+ import fs from "fs";
2
+
3
+ export default function resolveFolderName({ name }) {
4
+ const defaultFolerName = "Api";
5
+
6
+ // case 1: force new → timestamp
7
+ if (name === null) {
8
+ name = defaultFolerName;
9
+ };
10
+
11
+ // case 2: user provided → strict
12
+ if (fs.existsSync(name)) {
13
+ return {
14
+ KTF: false,
15
+ KReason: `Folder already exists : ${name}`
16
+ };
17
+ };
18
+
19
+ return name;
20
+ };
@@ -0,0 +1,5 @@
1
+ import express from 'express';
2
+
3
+ const router = express.Router();
4
+
5
+ export { router };
@@ -0,0 +1,42 @@
1
+ import { locateSource } from "./AddSubRoute/steps/locateSource.js";
2
+ import { locateDestination } from "./AddSubRoute/steps/locateDestination.js";
3
+ import { createFolder } from "../core/createFolder.js";
4
+ import { updateAppJs } from "./AddSubRoute/steps/updateAppJs.js";
5
+ import { announce } from "./AddSubRoute/steps/announce.js";
6
+
7
+ import resolveFolderName from "./AddSubRoute/steps/resolveFolderName.js";
8
+
9
+ export default ({ folderName = "", argsAsIs, toPath, isAnnounce = true, checkBeforeCreate = true }) => {
10
+ const localToPath = toPath;
11
+ const resolvedFolderName = resolveFolderName({
12
+ name: folderName
13
+ });
14
+
15
+ if (resolvedFolderName.KTF === false) {
16
+ console.log(resolvedFolderName.KReason);
17
+
18
+ return folderName;
19
+ };
20
+
21
+ const source = locateSource();
22
+ const destination = locateDestination({
23
+ inResolvedFolderName: resolvedFolderName,
24
+ toPath: localToPath
25
+ });
26
+
27
+ const createFolderResponse = createFolder({
28
+ source, destination,
29
+ isAnnounce, checkBeforeCreate
30
+ });
31
+
32
+ if (createFolderResponse.KTF) {
33
+ updateAppJs({
34
+ appJsPath: `${localToPath}/routes.js`,
35
+ endpoint: resolvedFolderName
36
+ });
37
+ };
38
+
39
+ if (isAnnounce) announce({ inResolvedFolderName: resolvedFolderName });
40
+
41
+ return resolvedFolderName;
42
+ };
@@ -0,0 +1,44 @@
1
+ import { locateSource } from "./StartEndPoint/steps/locateSource.js";
2
+ import { locateDestination } from "./StartEndPoint/steps/locateDestination.js";
3
+ import { createFolder } from "../core/createFolder.js";
4
+ import { updateAppJs } from "./StartEndPoint/steps/UpdateAppJs/index.js";
5
+
6
+ import { announce } from "./StartEndPoint/steps/announce.js";
7
+
8
+ import resolveFolderName from "./StartEndPoint/steps/resolveFolderName.js";
9
+
10
+ export default ({ folderName = "", toPath, isAnnounce = true, checkBeforeCreate = true }) => {
11
+ const localToPath = toPath;
12
+
13
+ const resolvedFolderName = resolveFolderName({
14
+ name: folderName
15
+ });
16
+
17
+ if (resolvedFolderName.KTF === false) {
18
+ return folderName;
19
+ };
20
+
21
+ const source = locateSource();
22
+ const destination = locateDestination({
23
+ inResolvedFolderName: resolvedFolderName,
24
+ toPath: localToPath
25
+ });
26
+
27
+ const createFolderResponse = createFolder({
28
+ source, destination,
29
+ isAnnounce, checkBeforeCreate
30
+ });
31
+
32
+ // console.log("bbbbbbbbbbbbbbbb : ", createFolderResponse);
33
+
34
+ if (createFolderResponse.KTF) {
35
+ updateAppJs({
36
+ appJsPath: `${localToPath}/app.js`,
37
+ endpoint: resolvedFolderName
38
+ });
39
+ };
40
+
41
+ if (isAnnounce) announce({ inResolvedFolderName: resolvedFolderName });
42
+
43
+ return resolvedFolderName;
44
+ };
@@ -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,16 @@
1
+ export default function parseInput() {
2
+ const [...args] = process.argv.slice(2);
3
+
4
+ const flags = Object.fromEntries(
5
+ args
6
+ .filter(a => a.includes("="))
7
+ .map(a => a.split("="))
8
+ );
9
+
10
+ return {
11
+ folderName: flags?.folderName || null,
12
+ isAnnounce: flags?.isAnnounce || true,
13
+ showLog: flags?.showLog || false,
14
+ toPath: process.cwd()
15
+ };
16
+ };
@@ -0,0 +1,14 @@
1
+ import StartEndPoint from "../StartEndPoint/index.js";
2
+ import AddSubRoute from "../AddSubRoute/index.js";
3
+ import AddTableName from "../Tables/index.js";
4
+
5
+ // resolveCommand.js
6
+ const map = {
7
+ StartEndPoint,
8
+ AddSubRoute,
9
+ AddTableName
10
+ };
11
+
12
+ export default function resolveCommand(cmd) {
13
+ return map[cmd] || null;
14
+ };
@@ -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
+ }
@@ -0,0 +1,19 @@
1
+ import parseInput from "./core/parseInput.js";
2
+ import resolveCommand from "./core/resolveCommand.js";
3
+ import showUsage from './core/showUsage.js';
4
+
5
+ import startEndPoint from "./StartEndPoint/index.js";
6
+
7
+ import pkg from '../../package.json' with { type: 'json' };
8
+
9
+ const version = pkg.version;
10
+
11
+ const run = () => {
12
+ const input = parseInput();
13
+
14
+ if (input.cmd === "--help" || input.cmd === "-h" || input.cmd === "help") return showUsage(version);
15
+
16
+ startEndPoint(input);
17
+ };
18
+
19
+ export default run;
package/index.js CHANGED
@@ -1,10 +1,11 @@
1
1
  import getLatestVersion from "./bin/core/getLatestVersion.js";
2
2
 
3
- const load = async (cmd) => {
3
+ const load = async () => {
4
4
  const v = getLatestVersion();
5
- return (await import(`./bin/${v}/commands/exportCommands/${cmd}.js`)).default;
5
+
6
+ const module = await import(`./bin/${v}/StartEndPoint/index.js`);
7
+
8
+ return module.default;
6
9
  };
7
10
 
8
- export const StartEndPoint = async (...a) => (await load("StartEndPoint"))(...a);
9
- export const init = async (...a) => (await load("init"))(...a);
10
- export const tally = async (...a) => (await load("tally"))(...a);
11
+ export default load;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kschema-api-gen-appjs",
3
- "version": "1.3.5",
3
+ "version": "1.4.1",
4
4
  "description": "CLI to build for appjs, the endpoints",
5
5
  "keywords": [
6
6
  "cli",