json-api-builder-create-app 1.0.0 → 1.2.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.
- package/package.json +42 -39
- package/src/index.js +1 -0
- package/src/v2/create-app.js +26 -0
- package/src/v2/emit-app.js +29 -0
- package/src/v2/hook-app-strategies.js +71 -0
- package/src/v2/index.js +31 -0
- package/src/v2/start-server.js +1 -0
- package/src/v2/templates/app.json +55 -0
- package/src/v2/templates/snippets/api-routes.json +27 -0
- package/src/v2/templates/snippets/fs-sync.json +39 -0
- package/src/v2/templates/snippets/start-server.json +105 -0
package/package.json
CHANGED
|
@@ -1,39 +1,42 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "json-api-builder-create-app",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Create app and hook app strategies stage for JSON API Builder.",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "./src/index.js",
|
|
7
|
-
"exports": {
|
|
8
|
-
".": "./src/index.js"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"url": "https://github.com/keshavsoft/json-api-builder-create-app
|
|
37
|
-
},
|
|
38
|
-
"
|
|
39
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "json-api-builder-create-app",
|
|
3
|
+
"version": "1.2.1",
|
|
4
|
+
"description": "Create app and hook app strategies stage for JSON API Builder.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.js",
|
|
9
|
+
"./v1": "./src/v1/index.js",
|
|
10
|
+
"./v2": "./src/v2/index.js"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"src",
|
|
14
|
+
"README.md",
|
|
15
|
+
"LICENSE",
|
|
16
|
+
"docs"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"check": "node scripts/check.mjs",
|
|
20
|
+
"test": "node --test \"test/**/*.test.js\""
|
|
21
|
+
},
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=20"
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@hono/node-server": "^1.19.6",
|
|
30
|
+
"astring": "^1.9.0"
|
|
31
|
+
},
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"author": "KeshavSoft",
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "git+https://github.com/keshavsoft/json-api-builder-create-app.git"
|
|
37
|
+
},
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/keshavsoft/json-api-builder-create-app/issues"
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://keshavsoft.github.io/json-api-builder-create-app/"
|
|
42
|
+
}
|
package/src/index.js
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
|
|
5
|
+
const defaultTemplatePath = fileURLToPath(new URL("./templates/app.json", import.meta.url));
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Step 1: Loads and returns the base app JSON.
|
|
9
|
+
*
|
|
10
|
+
* @param {object} options
|
|
11
|
+
* @returns {object} The base app AST JSON.
|
|
12
|
+
*/
|
|
13
|
+
export function createApp(options = {}) {
|
|
14
|
+
const clientRoot = path.resolve(options.clientRoot ?? process.cwd());
|
|
15
|
+
const templateFile = options.templatePath
|
|
16
|
+
? path.resolve(clientRoot, options.templatePath)
|
|
17
|
+
: defaultTemplatePath;
|
|
18
|
+
|
|
19
|
+
if (!fs.existsSync(templateFile)) {
|
|
20
|
+
throw new Error(`Base app JSON template not found: ${templateFile}`);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return JSON.parse(fs.readFileSync(templateFile, "utf8"));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export default createApp;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { generate } from "astring";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Step 3: Takes the altered AST JSON, serializes to app.js via astring, and writes to disk.
|
|
7
|
+
*
|
|
8
|
+
* @param {object} ast The altered app AST JSON.
|
|
9
|
+
* @param {object} options
|
|
10
|
+
* @returns {{ code: string, appPath: string }}
|
|
11
|
+
*/
|
|
12
|
+
export function emitApp(ast, options = {}) {
|
|
13
|
+
if (!ast) {
|
|
14
|
+
throw new Error("emitApp requires an AST JSON object.");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const clientRoot = path.resolve(options.clientRoot ?? process.cwd());
|
|
18
|
+
const outputRoot = path.resolve(options.outputRoot ?? path.join(clientRoot, "output"));
|
|
19
|
+
const appPath = path.resolve(options.appPath ?? path.join(outputRoot, "app.js"));
|
|
20
|
+
|
|
21
|
+
fs.mkdirSync(outputRoot, { recursive: true });
|
|
22
|
+
|
|
23
|
+
const code = generate(ast);
|
|
24
|
+
fs.writeFileSync(appPath, code, "utf8");
|
|
25
|
+
|
|
26
|
+
return { code, appPath };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export default emitApp;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
|
|
5
|
+
const snippetsDir = fileURLToPath(new URL("./templates/snippets", import.meta.url));
|
|
6
|
+
|
|
7
|
+
function loadSnippet(fileName) {
|
|
8
|
+
const filePath = path.join(snippetsDir, fileName);
|
|
9
|
+
return JSON.parse(fs.readFileSync(filePath, "utf8"));
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Step 2: Receives an app AST JSON, alters it based on api.json, and returns the altered JSON.
|
|
14
|
+
* Pure transformation: JSON in -> Altered JSON out.
|
|
15
|
+
*
|
|
16
|
+
* @param {object} ast The base app AST JSON.
|
|
17
|
+
* @param {object} options
|
|
18
|
+
* @returns {object} The altered app AST JSON.
|
|
19
|
+
*/
|
|
20
|
+
export function hookAppStrategies(ast, options = {}) {
|
|
21
|
+
if (!ast?.body) {
|
|
22
|
+
throw new Error("hookAppStrategies requires an AST JSON with a body array.");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const clientRoot = path.resolve(options.clientRoot ?? process.cwd());
|
|
26
|
+
const apiPath = path.resolve(clientRoot, options.apiPath ?? "input/api.json");
|
|
27
|
+
const api = options.api ?? JSON.parse(fs.readFileSync(apiPath, "utf8"));
|
|
28
|
+
|
|
29
|
+
// 1. API Route Snippet
|
|
30
|
+
if (api.root) {
|
|
31
|
+
const snippet = loadSnippet("api-routes.json");
|
|
32
|
+
if (api.root !== "api") {
|
|
33
|
+
snippet.import.source.value = `./${api.root}/routes.js`;
|
|
34
|
+
snippet.import.source.raw = JSON.stringify(`./${api.root}/routes.js`);
|
|
35
|
+
snippet.mount.expression.arguments[0].value = `/${api.root}`;
|
|
36
|
+
snippet.mount.expression.arguments[0].raw = JSON.stringify(`/${api.root}`);
|
|
37
|
+
}
|
|
38
|
+
ast.body.unshift(snippet.import);
|
|
39
|
+
|
|
40
|
+
const exportIdx = ast.body.findIndex(n => n.type === "ExportDefaultDeclaration");
|
|
41
|
+
ast.body.splice(exportIdx >= 0 ? exportIdx : ast.body.length, 0, snippet.mount);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// 2. File-Sync Strategies (e.g. fs -> file-sync)
|
|
45
|
+
for (const [strategyName, strategyConfig] of Object.entries(api.strategies ?? {})) {
|
|
46
|
+
if (strategyName === "fs" && strategyConfig?.["file-sync"]) {
|
|
47
|
+
const fsSnippet = loadSnippet("fs-sync.json");
|
|
48
|
+
ast.body.unshift(...fsSnippet.imports);
|
|
49
|
+
|
|
50
|
+
const exportIdx = ast.body.findIndex(n => n.type === "ExportDefaultDeclaration");
|
|
51
|
+
ast.body.splice(exportIdx >= 0 ? exportIdx : ast.body.length, 0, fsSnippet.mount);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// 3. Start Server Lifecycle Snippet
|
|
56
|
+
const defaultPort = Number(api.port ?? 3000);
|
|
57
|
+
const serverSnippet = loadSnippet("start-server.json");
|
|
58
|
+
|
|
59
|
+
if (defaultPort !== 3000) {
|
|
60
|
+
serverSnippet[0].declaration.params[0].right.arguments[0].right.value = defaultPort;
|
|
61
|
+
serverSnippet[0].declaration.params[0].right.arguments[0].right.raw = String(defaultPort);
|
|
62
|
+
serverSnippet[1].expression.arguments[0].arguments[0].right.value = defaultPort;
|
|
63
|
+
serverSnippet[1].expression.arguments[0].arguments[0].right.raw = String(defaultPort);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
ast.body.push(...serverSnippet);
|
|
67
|
+
|
|
68
|
+
return ast;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export default hookAppStrategies;
|
package/src/v2/index.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { createApp } from "./create-app.js";
|
|
2
|
+
import { hookAppStrategies } from "./hook-app-strategies.js";
|
|
3
|
+
import { emitApp } from "./emit-app.js";
|
|
4
|
+
import { startServer } from "./start-server.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Pure Linear Pipeline:
|
|
8
|
+
* const baseJson = createApp(options);
|
|
9
|
+
* const alteredJson = hookAppStrategies(baseJson, options);
|
|
10
|
+
* return emitApp(alteredJson, options);
|
|
11
|
+
*/
|
|
12
|
+
export function buildApp(options = {}) {
|
|
13
|
+
const baseJson = createApp(options);
|
|
14
|
+
const alteredJson = hookAppStrategies(baseJson, options);
|
|
15
|
+
return emitApp(alteredJson, options);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export {
|
|
19
|
+
createApp,
|
|
20
|
+
hookAppStrategies,
|
|
21
|
+
emitApp,
|
|
22
|
+
startServer
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export default {
|
|
26
|
+
createApp,
|
|
27
|
+
hookAppStrategies,
|
|
28
|
+
emitApp,
|
|
29
|
+
startServer,
|
|
30
|
+
buildApp
|
|
31
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { startServer } from "../v1/start-server.js";
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "Program",
|
|
3
|
+
"sourceType": "module",
|
|
4
|
+
"body": [
|
|
5
|
+
{
|
|
6
|
+
"type": "ImportDeclaration",
|
|
7
|
+
"specifiers": [
|
|
8
|
+
{
|
|
9
|
+
"type": "ImportSpecifier",
|
|
10
|
+
"imported": {
|
|
11
|
+
"type": "Identifier",
|
|
12
|
+
"name": "Hono"
|
|
13
|
+
},
|
|
14
|
+
"local": {
|
|
15
|
+
"type": "Identifier",
|
|
16
|
+
"name": "Hono"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
],
|
|
20
|
+
"source": {
|
|
21
|
+
"type": "Literal",
|
|
22
|
+
"value": "hono",
|
|
23
|
+
"raw": "\"hono\""
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
"type": "VariableDeclaration",
|
|
28
|
+
"kind": "const",
|
|
29
|
+
"declarations": [
|
|
30
|
+
{
|
|
31
|
+
"type": "VariableDeclarator",
|
|
32
|
+
"id": {
|
|
33
|
+
"type": "Identifier",
|
|
34
|
+
"name": "app"
|
|
35
|
+
},
|
|
36
|
+
"init": {
|
|
37
|
+
"type": "NewExpression",
|
|
38
|
+
"callee": {
|
|
39
|
+
"type": "Identifier",
|
|
40
|
+
"name": "Hono"
|
|
41
|
+
},
|
|
42
|
+
"arguments": []
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
]
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
"type": "ExportDefaultDeclaration",
|
|
49
|
+
"declaration": {
|
|
50
|
+
"type": "Identifier",
|
|
51
|
+
"name": "app"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
]
|
|
55
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"import": {
|
|
3
|
+
"type": "ImportDeclaration",
|
|
4
|
+
"specifiers": [
|
|
5
|
+
{
|
|
6
|
+
"type": "ImportDefaultSpecifier",
|
|
7
|
+
"local": { "type": "Identifier", "name": "apiRoutes" }
|
|
8
|
+
}
|
|
9
|
+
],
|
|
10
|
+
"source": { "type": "Literal", "value": "./api/routes.js", "raw": "\"./api/routes.js\"" }
|
|
11
|
+
},
|
|
12
|
+
"mount": {
|
|
13
|
+
"type": "ExpressionStatement",
|
|
14
|
+
"expression": {
|
|
15
|
+
"type": "CallExpression",
|
|
16
|
+
"callee": {
|
|
17
|
+
"type": "MemberExpression",
|
|
18
|
+
"object": { "type": "Identifier", "name": "app" },
|
|
19
|
+
"property": { "type": "Identifier", "name": "route" }
|
|
20
|
+
},
|
|
21
|
+
"arguments": [
|
|
22
|
+
{ "type": "Literal", "value": "/api", "raw": "\"/api\"" },
|
|
23
|
+
{ "type": "Identifier", "name": "apiRoutes" }
|
|
24
|
+
]
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"imports": [
|
|
3
|
+
{
|
|
4
|
+
"type": "ImportDeclaration",
|
|
5
|
+
"specifiers": [{ "type": "ImportDefaultSpecifier", "local": { "type": "Identifier", "name": "fsRoutes" } }],
|
|
6
|
+
"source": { "type": "Literal", "value": "./endpoints/fs/routes.js", "raw": "\"./endpoints/fs/routes.js\"" }
|
|
7
|
+
},
|
|
8
|
+
{
|
|
9
|
+
"type": "ImportDeclaration",
|
|
10
|
+
"specifiers": [{ "type": "ImportSpecifier", "imported": { "type": "Identifier", "name": "serve" }, "local": { "type": "Identifier", "name": "serve" } }],
|
|
11
|
+
"source": { "type": "Literal", "value": "@hono/node-server", "raw": "\"@hono/node-server\"" }
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"type": "ImportDeclaration",
|
|
15
|
+
"specifiers": [{ "type": "ImportSpecifier", "imported": { "type": "Identifier", "name": "pathToFileURL" }, "local": { "type": "Identifier", "name": "pathToFileURL" } }],
|
|
16
|
+
"source": { "type": "Literal", "value": "node:url", "raw": "\"node:url\"" }
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"type": "ImportDeclaration",
|
|
20
|
+
"specifiers": [{ "type": "ImportDefaultSpecifier", "local": { "type": "Identifier", "name": "path" } }],
|
|
21
|
+
"source": { "type": "Literal", "value": "node:path", "raw": "\"node:path\"" }
|
|
22
|
+
}
|
|
23
|
+
],
|
|
24
|
+
"mount": {
|
|
25
|
+
"type": "ExpressionStatement",
|
|
26
|
+
"expression": {
|
|
27
|
+
"type": "CallExpression",
|
|
28
|
+
"callee": {
|
|
29
|
+
"type": "MemberExpression",
|
|
30
|
+
"object": { "type": "Identifier", "name": "app" },
|
|
31
|
+
"property": { "type": "Identifier", "name": "route" }
|
|
32
|
+
},
|
|
33
|
+
"arguments": [
|
|
34
|
+
{ "type": "Literal", "value": "/fs", "raw": "\"/fs\"" },
|
|
35
|
+
{ "type": "Identifier", "name": "fsRoutes" }
|
|
36
|
+
]
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"type": "ExportNamedDeclaration",
|
|
4
|
+
"declaration": {
|
|
5
|
+
"type": "FunctionDeclaration",
|
|
6
|
+
"id": { "type": "Identifier", "name": "startServer" },
|
|
7
|
+
"params": [
|
|
8
|
+
{
|
|
9
|
+
"type": "AssignmentPattern",
|
|
10
|
+
"left": { "type": "Identifier", "name": "port" },
|
|
11
|
+
"right": {
|
|
12
|
+
"type": "CallExpression",
|
|
13
|
+
"callee": { "type": "Identifier", "name": "Number" },
|
|
14
|
+
"arguments": [
|
|
15
|
+
{
|
|
16
|
+
"type": "LogicalExpression",
|
|
17
|
+
"left": {
|
|
18
|
+
"type": "MemberExpression",
|
|
19
|
+
"object": {
|
|
20
|
+
"type": "MemberExpression",
|
|
21
|
+
"object": { "type": "Identifier", "name": "process" },
|
|
22
|
+
"property": { "type": "Identifier", "name": "env" }
|
|
23
|
+
},
|
|
24
|
+
"property": { "type": "Identifier", "name": "PORT" }
|
|
25
|
+
},
|
|
26
|
+
"operator": "??",
|
|
27
|
+
"right": { "type": "Literal", "value": 3000, "raw": "3000" }
|
|
28
|
+
}
|
|
29
|
+
]
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
],
|
|
33
|
+
"body": {
|
|
34
|
+
"type": "BlockStatement",
|
|
35
|
+
"body": [
|
|
36
|
+
{
|
|
37
|
+
"type": "ReturnStatement",
|
|
38
|
+
"argument": {
|
|
39
|
+
"type": "CallExpression",
|
|
40
|
+
"callee": { "type": "Identifier", "name": "serve" },
|
|
41
|
+
"arguments": [
|
|
42
|
+
{
|
|
43
|
+
"type": "ObjectExpression",
|
|
44
|
+
"properties": [
|
|
45
|
+
{
|
|
46
|
+
"type": "Property",
|
|
47
|
+
"key": { "type": "Identifier", "name": "fetch" },
|
|
48
|
+
"value": {
|
|
49
|
+
"type": "MemberExpression",
|
|
50
|
+
"object": { "type": "Identifier", "name": "app" },
|
|
51
|
+
"property": { "type": "Identifier", "name": "fetch" }
|
|
52
|
+
},
|
|
53
|
+
"kind": "init"
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
"type": "Property",
|
|
57
|
+
"key": { "type": "Identifier", "name": "port" },
|
|
58
|
+
"value": { "type": "Identifier", "name": "port" },
|
|
59
|
+
"kind": "init"
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
"type": "Property",
|
|
63
|
+
"key": { "type": "Identifier", "name": "hostname" },
|
|
64
|
+
"value": { "type": "Literal", "value": "0.0.0.0", "raw": "\"0.0.0.0\"" },
|
|
65
|
+
"kind": "init"
|
|
66
|
+
}
|
|
67
|
+
]
|
|
68
|
+
}
|
|
69
|
+
]
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
]
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
"type": "ExpressionStatement",
|
|
78
|
+
"expression": {
|
|
79
|
+
"type": "CallExpression",
|
|
80
|
+
"callee": { "type": "Identifier", "name": "startServer" },
|
|
81
|
+
"arguments": [
|
|
82
|
+
{
|
|
83
|
+
"type": "CallExpression",
|
|
84
|
+
"callee": { "type": "Identifier", "name": "Number" },
|
|
85
|
+
"arguments": [
|
|
86
|
+
{
|
|
87
|
+
"type": "LogicalExpression",
|
|
88
|
+
"left": {
|
|
89
|
+
"type": "MemberExpression",
|
|
90
|
+
"object": {
|
|
91
|
+
"type": "MemberExpression",
|
|
92
|
+
"object": { "type": "Identifier", "name": "process" },
|
|
93
|
+
"property": { "type": "Identifier", "name": "env" }
|
|
94
|
+
},
|
|
95
|
+
"property": { "type": "Identifier", "name": "PORT" }
|
|
96
|
+
},
|
|
97
|
+
"operator": "??",
|
|
98
|
+
"right": { "type": "Literal", "value": 3000, "raw": "3000" }
|
|
99
|
+
}
|
|
100
|
+
]
|
|
101
|
+
}
|
|
102
|
+
]
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
]
|