kschema-fs-api-gen-get-actions 1.3.2

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 (73) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +323 -0
  3. package/bin/cli.js +12 -0
  4. package/bin/core/getLatestVersion.js +13 -0
  5. package/bin/core/loadRunner.js +9 -0
  6. package/bin/v2/core/createFolder.js +34 -0
  7. package/bin/v2/core/parseInput.js +9 -0
  8. package/bin/v2/core/resolveCommand.js +11 -0
  9. package/bin/v2/core/showUsage.js +49 -0
  10. package/bin/v2/start.js +22 -0
  11. package/bin/v2/tasks/actions/ShowAll/actions.json +26 -0
  12. package/bin/v2/tasks/actions/ShowAll/steps/UpdateRoutesJs/common/readFile.js +8 -0
  13. package/bin/v2/tasks/actions/ShowAll/steps/UpdateRoutesJs/common/writeFile.js +10 -0
  14. package/bin/v2/tasks/actions/ShowAll/steps/UpdateRoutesJs/updateImports/buildImport.js +9 -0
  15. package/bin/v2/tasks/actions/ShowAll/steps/UpdateRoutesJs/updateImports/checkDuplicate.js +8 -0
  16. package/bin/v2/tasks/actions/ShowAll/steps/UpdateRoutesJs/updateImports/findInsertIndex.js +10 -0
  17. package/bin/v2/tasks/actions/ShowAll/steps/UpdateRoutesJs/updateImports/index.js +47 -0
  18. package/bin/v2/tasks/actions/ShowAll/steps/UpdateRoutesJs/updateUse/buildUseLine.js +5 -0
  19. package/bin/v2/tasks/actions/ShowAll/steps/UpdateRoutesJs/updateUse/checkDuplicate.js +9 -0
  20. package/bin/v2/tasks/actions/ShowAll/steps/UpdateRoutesJs/updateUse/findInsertIndex.js +10 -0
  21. package/bin/v2/tasks/actions/ShowAll/steps/UpdateRoutesJs/updateUse/index.js +56 -0
  22. package/bin/v2/tasks/actions/ShowAll/steps/announce.js +9 -0
  23. package/bin/v2/tasks/actions/ShowAll/steps/createFolder.js +7 -0
  24. package/bin/v2/tasks/actions/ShowAll/steps/decideTemplate.js +3 -0
  25. package/bin/v2/tasks/actions/ShowAll/steps/locateDestination.js +7 -0
  26. package/bin/v2/tasks/actions/ShowAll/steps/locateSource.js +18 -0
  27. package/bin/v2/tasks/actions/ShowAll/steps/resolveFolderName.js +20 -0
  28. package/bin/v2/tasks/actions/ShowAll/steps/updateAppJs.js +7 -0
  29. package/bin/v2/tasks/actions/ShowAll/steps/updateEndPointsFile.js +30 -0
  30. package/bin/v2/tasks/actions/ShowAll/template/v2/controller.js +24 -0
  31. package/bin/v2/tasks/actions/ShowAll/template/v2/errors.js +11 -0
  32. package/bin/v2/tasks/actions/ShowAll/template/v2/getData.js +11 -0
  33. package/bin/v2/tasks/actions/ShowAll/template/v2/service.js +9 -0
  34. package/bin/v2/tasks/actions/showAll.js +63 -0
  35. package/bin/v2/tasks/core/createFolder.js +34 -0
  36. package/bin/v2/tasks/core/parseInput.js +12 -0
  37. package/bin/v2/tasks/core/resolveCommand.js +24 -0
  38. package/bin/v2/tasks/core/showUsage.js +43 -0
  39. package/bin/v3/core/createFolder.js +34 -0
  40. package/bin/v3/core/parseInput.js +9 -0
  41. package/bin/v3/core/resolveCommand.js +11 -0
  42. package/bin/v3/core/showUsage.js +49 -0
  43. package/bin/v3/start.js +22 -0
  44. package/bin/v3/tasks/actions/ShowAll/actions.json +26 -0
  45. package/bin/v3/tasks/actions/ShowAll/steps/UpdateRoutesJs/common/readFile.js +8 -0
  46. package/bin/v3/tasks/actions/ShowAll/steps/UpdateRoutesJs/common/writeFile.js +10 -0
  47. package/bin/v3/tasks/actions/ShowAll/steps/UpdateRoutesJs/updateImports/buildImport.js +9 -0
  48. package/bin/v3/tasks/actions/ShowAll/steps/UpdateRoutesJs/updateImports/checkDuplicate.js +8 -0
  49. package/bin/v3/tasks/actions/ShowAll/steps/UpdateRoutesJs/updateImports/findInsertIndex.js +10 -0
  50. package/bin/v3/tasks/actions/ShowAll/steps/UpdateRoutesJs/updateImports/index.js +47 -0
  51. package/bin/v3/tasks/actions/ShowAll/steps/UpdateRoutesJs/updateUse/buildUseLine.js +5 -0
  52. package/bin/v3/tasks/actions/ShowAll/steps/UpdateRoutesJs/updateUse/checkDuplicate.js +9 -0
  53. package/bin/v3/tasks/actions/ShowAll/steps/UpdateRoutesJs/updateUse/findInsertIndex.js +10 -0
  54. package/bin/v3/tasks/actions/ShowAll/steps/UpdateRoutesJs/updateUse/index.js +56 -0
  55. package/bin/v3/tasks/actions/ShowAll/steps/announce.js +9 -0
  56. package/bin/v3/tasks/actions/ShowAll/steps/createFolder.js +7 -0
  57. package/bin/v3/tasks/actions/ShowAll/steps/decideTemplate.js +3 -0
  58. package/bin/v3/tasks/actions/ShowAll/steps/locateDestination.js +7 -0
  59. package/bin/v3/tasks/actions/ShowAll/steps/locateSource.js +18 -0
  60. package/bin/v3/tasks/actions/ShowAll/steps/resolveFolderName.js +20 -0
  61. package/bin/v3/tasks/actions/ShowAll/steps/updateAppJs.js +7 -0
  62. package/bin/v3/tasks/actions/ShowAll/steps/updateEndPointsFile.js +30 -0
  63. package/bin/v3/tasks/actions/ShowAll/template/v2/controller.js +24 -0
  64. package/bin/v3/tasks/actions/ShowAll/template/v2/errors.js +11 -0
  65. package/bin/v3/tasks/actions/ShowAll/template/v2/getData.js +11 -0
  66. package/bin/v3/tasks/actions/ShowAll/template/v2/service.js +9 -0
  67. package/bin/v3/tasks/actions/showAll.js +63 -0
  68. package/bin/v3/tasks/core/createFolder.js +34 -0
  69. package/bin/v3/tasks/core/parseInput.js +12 -0
  70. package/bin/v3/tasks/core/resolveCommand.js +24 -0
  71. package/bin/v3/tasks/core/showUsage.js +43 -0
  72. package/index.js +22 -0
  73. package/package.json +37 -0
@@ -0,0 +1,5 @@
1
+ const buildUseLine = ({ inAppOrRouter, inEndpoint, inFuncName }) => {
2
+ return `${inAppOrRouter}.get('/${inEndpoint}', (req, res) => ${inFuncName}({ res, inTableName : tableName}));`;
3
+ };
4
+
5
+ export default buildUseLine;
@@ -0,0 +1,9 @@
1
+ const checkDuplicate = ({ inContent, inEndpoint, inAppOrRouter }) => {
2
+ const localContent = inContent;
3
+ const localEndpoint = inEndpoint;
4
+ const stringToSearch = `${inAppOrRouter}.get('/${localEndpoint}'`;
5
+
6
+ return localContent.includes(stringToSearch);
7
+ };
8
+
9
+ 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,56 @@
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 appOrRouter = "router";
9
+
10
+ const updateAppUse = ({ appJsPath, endpoint, showLog, inFuncName }) => {
11
+ const summary = {
12
+ use: { added: false, skipped: false, line: null }
13
+ };
14
+
15
+ const content = readFile(appJsPath);
16
+
17
+ const useLine = buildUseLine({
18
+ inAppOrRouter: appOrRouter,
19
+ inEndpoint: endpoint,
20
+ inFuncName
21
+ });
22
+
23
+ const fromCheckDuplicate = checkDuplicate({
24
+ inContent: content,
25
+ inEndpoint: endpoint,
26
+ inAppOrRouter: appOrRouter
27
+ });
28
+ console.log("fromCheckDuplicate : ", fromCheckDuplicate);
29
+
30
+ if (fromCheckDuplicate) {
31
+ summary.use.skipped = true;
32
+
33
+ if (showLog) console.log(summary);
34
+
35
+ return summary;
36
+ };
37
+
38
+ const index = findUseInsertIndex(content);
39
+
40
+ const before = content.slice(0, index);
41
+ const lineNumber = before.split("\n").length + 1;
42
+
43
+ const updated =
44
+ before + "\n" + useLine + content.slice(index);
45
+
46
+ writeFile(appJsPath, updated);
47
+
48
+ summary.use.added = true;
49
+ summary.use.line = lineNumber;
50
+
51
+ if (showLog) console.log(summary);
52
+
53
+ return summary;
54
+ };
55
+
56
+ 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,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,18 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import { fileURLToPath } from "url";
4
+
5
+ const __filename = fileURLToPath(import.meta.url);
6
+ const __dirname = path.dirname(__filename);
7
+
8
+ export const locateSource = () => {
9
+ const templatePath = path.join(__dirname, "..", "template");
10
+
11
+ const versions = fs.readdirSync(templatePath);
12
+
13
+ const maxVersion = Math.max(
14
+ ...versions.map(v => Number(v.replace("v", "")))
15
+ );
16
+
17
+ return path.join(templatePath, `v${maxVersion}`);
18
+ };
@@ -0,0 +1,20 @@
1
+ import fs from "fs";
2
+
3
+ export default function resolveFolderName({ name, inDefaultFolderName = "InsertGenPk" }) {
4
+ const defaultFolderName = inDefaultFolderName;
5
+
6
+ // case 1: force new → timestamp
7
+ if (name === null) {
8
+ name = defaultFolderName;
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 default ({ appJsPath, endpoint }) => {
5
+ updateImports({ appJsPath, endpoint });
6
+ updateAppUse({ appJsPath, endpoint });
7
+ };
@@ -0,0 +1,30 @@
1
+ import fs from 'fs';
2
+
3
+ function updateEndPointsFile({ filePath, inTableName }) {
4
+ try {
5
+ let content = readFile(filePath);
6
+
7
+ content = content.replace("<TABLE_NAME>", inTableName);
8
+
9
+ writeFile(filePath, content);
10
+ } catch (e) {
11
+ handleError(e);
12
+ };
13
+ };
14
+
15
+ // read
16
+ function readFile(filePath) {
17
+ return fs.readFileSync(filePath, "utf-8");
18
+ };
19
+
20
+ // write
21
+ function writeFile(filePath, content) {
22
+ fs.writeFileSync(filePath, content);
23
+ };
24
+
25
+ // error handler
26
+ function handleError(e) {
27
+ console.error("ROUTE USE ERROR:", e.message);
28
+ };
29
+
30
+ export { updateEndPointsFile };
@@ -0,0 +1,24 @@
1
+ import { startFunc as Service } from "./service.js";
2
+ import { ConflictError, StorageError } from "./errors.js";
3
+
4
+ const getFunc = async ({ req, res, inTablePath }) => {
5
+ try {
6
+ const fromService = await Service({
7
+ inTablePath
8
+ });
9
+
10
+ res.type("application/json").send(fromService);
11
+ } catch (err) {
12
+
13
+ if (err instanceof ConflictError)
14
+ return res.status(409).send(err.message);
15
+
16
+ if (err instanceof StorageError)
17
+ return res.status(500).send("Failed to persist data");
18
+
19
+ console.error(err);
20
+ res.status(500).send("Unexpected error");
21
+ }
22
+ };
23
+
24
+ export default getFunc;
@@ -0,0 +1,11 @@
1
+ class AppError extends Error {
2
+ constructor(message) {
3
+ super(message);
4
+ this.name = this.constructor.name;
5
+ }
6
+ }
7
+
8
+ class ConflictError extends AppError { }
9
+ class StorageError extends AppError { }
10
+
11
+ export { AppError, ConflictError, StorageError };
@@ -0,0 +1,11 @@
1
+ import { JSONFilePreset } from 'lowdb/node'
2
+
3
+ const startFunc = async ({ inTablePath }) => {
4
+ const db = await JSONFilePreset(inTablePath, []);
5
+
6
+ await db.read();
7
+
8
+ return await db.data;
9
+ };
10
+
11
+ export default startFunc;
@@ -0,0 +1,9 @@
1
+ import getData from "./getData.js";
2
+
3
+ const startFunc = async ({ inTablePath }) => {
4
+ const dataAsArray = await getData({ inTablePath });
5
+
6
+ return await dataAsArray;
7
+ };
8
+
9
+ export { startFunc };
@@ -0,0 +1,63 @@
1
+ import path from "path";
2
+
3
+ import generateRest from "kschema-fs-api-gen-rest";
4
+ import fixEndpointsJs from "express-fix-endpoints-get-js";
5
+
6
+ import { locateSource } from "./ShowAll/steps/locateSource.js";
7
+ import { locateDestination } from "./ShowAll/steps/locateDestination.js";
8
+ import { createFolder } from "../../core/createFolder.js";
9
+
10
+ import { announce } from "./ShowAll/steps/announce.js";
11
+
12
+ import resolveFolderName from "./ShowAll/steps/resolveFolderName.js";
13
+ import actions from "./ShowAll/actions.json" with { type: "json" };
14
+
15
+ const startFunc = async ({ cmd = "", toPath, isAnnounce = true, checkBeforeCreate = true,
16
+ toConfigPath, inTargetPath, inGenerateRest = false, inFolderName
17
+ }) => {
18
+
19
+ const matched = actions;
20
+
21
+ const localToPath = toPath;
22
+
23
+ const resolvedFolderName = resolveFolderName({
24
+ name: cmd
25
+ });
26
+
27
+ if (resolvedFolderName.KTF === false) {
28
+ console.log(resolvedFolderName.KReason);
29
+
30
+ return;
31
+ };
32
+
33
+ const source = locateSource();
34
+ const destination = locateDestination({
35
+ inResolvedFolderName: resolvedFolderName,
36
+ toPath: localToPath
37
+ });
38
+
39
+ const createFolderResponse = createFolder({
40
+ source, destination,
41
+ isAnnounce, checkBeforeCreate
42
+ });
43
+
44
+ if (createFolderResponse.KTF) {
45
+ const fromEndPointsJs = await fixEndpointsJs({
46
+ endPointsJsPath: path.join(localToPath, "end-points.js"),
47
+ inActionName: cmd, inFolderName
48
+ });
49
+
50
+ if (inGenerateRest) {
51
+ generateRest({
52
+ toConfigPath, inTargetPath,
53
+ toPath: path.join(localToPath, resolvedFolderName),
54
+ });
55
+ };
56
+ };
57
+
58
+ if (isAnnounce) announce({ inResolvedFolderName: resolvedFolderName });
59
+
60
+ return resolvedFolderName;
61
+ };
62
+
63
+ export default startFunc;
@@ -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,12 @@
1
+ export default function parseInput() {
2
+ const [cmd, ...args] = process.argv.slice(2);
3
+
4
+ return {
5
+ cmd: cmd || null,
6
+ args,
7
+ template: cmd || null,
8
+ folderName: args[0] || null,
9
+ argsAsIs: args[0] || null,
10
+ toPath: process.cwd()
11
+ };
12
+ };
@@ -0,0 +1,24 @@
1
+ import StartEndPoint from "../commands/startEndPoint.js";
2
+ import AddSubRoute from "../commands/addSubRoute.js";
3
+ import AddTableName from "../tasks/tables/addTableName.js";
4
+ // import ShowAll from "../commands/showAll.js";
5
+ import Insert from "../tasks/actions/insert.js";
6
+ import ShowAll from "../tasks/actions/showAll.js";
7
+
8
+ import CreateApi from "../Orchestration/CreateApi/createApi.js";
9
+ import InsertApi from "../Orchestration/InsertApi/insertApi.js";
10
+
11
+ // resolveCommand.js
12
+ const map = {
13
+ StartEndPoint,
14
+ AddSubRoute,
15
+ AddTableName,
16
+ ShowAll,
17
+ CreateApi,
18
+ InsertApi,
19
+ Insert
20
+ };
21
+
22
+ export default function resolveCommand(cmd) {
23
+ return map[cmd] || null;
24
+ };
@@ -0,0 +1,43 @@
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}CreateApi${r} Creates new end point and hooks to app.js
33
+ ${g}InsertApi${r} Creates new InsertApi end point and hooks to app.js
34
+
35
+ ${y}Examples:${r}
36
+ ${gray}npx @keshavsoft/kschema-api-gen StartEndPoint${r}
37
+ ${gray}npx @keshavsoft/kschema-api-gen CreateApi Api/V1/journals/ShowAll${r}
38
+ ${gray}npx @keshavsoft/kschema-api-gen InsertApi Api/V1/journals/Insert${r}
39
+
40
+ ${y}Tip:${r}
41
+ ${gray}npm i -g @keshavsoft/kschema-api-gen${r}
42
+ `);
43
+ }
package/index.js ADDED
@@ -0,0 +1,22 @@
1
+ import getLatestVersion from "./bin/core/getLatestVersion.js";
2
+
3
+ const load = async (cmd) => {
4
+ const module = await import(`./bin/${getLatestVersion()}/tasks/actions/${cmd}.js`);
5
+
6
+ return module.default; // Returns a function
7
+ };
8
+
9
+ const showAll = async ({ toPath, showLog, inTargetPath }) => {
10
+ const commandToSend = "showAll";
11
+
12
+ const commandFunction = await load(commandToSend);
13
+ // console.log(" ...args :", args);
14
+ await commandFunction({
15
+ toPath, cmd: commandToSend, inTargetPath,
16
+ inFolderName: commandToSend
17
+ });
18
+ };
19
+
20
+ export {
21
+ showAll
22
+ };
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "kschema-fs-api-gen-get-actions",
3
+ "version": "1.3.2",
4
+ "description": "start from end points js, generate post methods",
5
+ "keywords": [
6
+ "cli",
7
+ "scaffold",
8
+ "templates",
9
+ "node",
10
+ "project-generator"
11
+ ],
12
+ "dependencies": {
13
+ "kschema-fs-api-gen-rest": "^1.4.2",
14
+ "express-fix-endpoints-get-js": "^1.2.2"
15
+ },
16
+ "type": "module",
17
+ "exports": {
18
+ ".": "./index.js"
19
+ },
20
+ "bin": {
21
+ "kschema-fs-api-gen-post-actions": "./bin/cli.js"
22
+ },
23
+ "files": [
24
+ "bin/",
25
+ "index.js",
26
+ "README.md",
27
+ "LICENSE"
28
+ ],
29
+ "homepage": "https://cli.keshavsoft.com",
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "https://github.com/keshavsoft/kschema-fs-api-gen-get-actions"
33
+ },
34
+ "bugs": {
35
+ "url": "https://github.com/keshavsoft/kschema-fs-api-gen-get-actions/issues"
36
+ }
37
+ }