create-backon 0.6.2 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +141 -12
- package/dist/cli.js +1675 -0
- package/package.json +10 -6
- package/src/add-method.ts +101 -0
- package/src/add-route.ts +11 -9
- package/src/cli.ts +512 -3
- package/src/colors.ts +35 -0
- package/src/db-status.ts +117 -0
- package/src/generate.ts +17 -6
- package/src/prompts.ts +26 -0
- package/src/remove-method.ts +100 -0
- package/src/route-methods.ts +100 -0
- package/src/templates/base.ts +95 -5
- package/src/templates/route.ts +143 -64
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-backon",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "Scaffolds a new backend project on backon — like `bun create vite@latest`, but for a consistent controller/routes/services backend layout.",
|
|
5
|
+
"description": "Scaffolds a new backend project on backon — like `bun create vite@latest`, but for a consistent controller/routes/services backend layout. Runs via bunx/bun create or npx/npm create.",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/mednours/backon/tree/main/packages/create-backon#readme",
|
|
8
8
|
"repository": {
|
|
@@ -12,22 +12,26 @@
|
|
|
12
12
|
},
|
|
13
13
|
"bugs": "https://github.com/mednours/backon/issues",
|
|
14
14
|
"engines": {
|
|
15
|
-
"bun": ">=1.3.0"
|
|
15
|
+
"bun": ">=1.3.0",
|
|
16
|
+
"node": ">=18.0.0"
|
|
16
17
|
},
|
|
17
18
|
"bin": {
|
|
18
|
-
"create-backon": "./
|
|
19
|
+
"create-backon": "./dist/cli.js"
|
|
19
20
|
},
|
|
20
|
-
"files": ["src"],
|
|
21
|
+
"files": ["src", "dist"],
|
|
21
22
|
"scripts": {
|
|
22
23
|
"start": "bun run src/cli.ts",
|
|
23
24
|
"test": "bun test",
|
|
24
|
-
"typecheck": "tsc --noEmit"
|
|
25
|
+
"typecheck": "tsc --noEmit",
|
|
26
|
+
"build": "rm -rf dist && esbuild src/cli.ts --bundle --platform=node --format=esm --target=node18 --packages=external --banner:js=\"#!/usr/bin/env node\" --outfile=dist/cli.js && chmod +x dist/cli.js",
|
|
27
|
+
"prepublishOnly": "bun run typecheck && bun run test && bun run build"
|
|
25
28
|
},
|
|
26
29
|
"dependencies": {
|
|
27
30
|
"@clack/prompts": "^0.9.1"
|
|
28
31
|
},
|
|
29
32
|
"devDependencies": {
|
|
30
33
|
"@types/bun": "latest",
|
|
34
|
+
"esbuild": "^0.28.2",
|
|
31
35
|
"typescript": "^5.7.2"
|
|
32
36
|
}
|
|
33
37
|
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { detectProject } from "./detect";
|
|
4
|
+
import { parseMountedRoutes, readEndpointsIndex } from "./endpoints-index";
|
|
5
|
+
import { parseRouteMethods } from "./route-methods";
|
|
6
|
+
import {
|
|
7
|
+
controllerImports,
|
|
8
|
+
renderControllerMethod,
|
|
9
|
+
renderServiceMethod,
|
|
10
|
+
routeMountLine,
|
|
11
|
+
serviceImports,
|
|
12
|
+
type MethodSpec,
|
|
13
|
+
} from "./templates/route";
|
|
14
|
+
|
|
15
|
+
export interface AddMethodResult {
|
|
16
|
+
route: string;
|
|
17
|
+
root: string;
|
|
18
|
+
filesWritten: string[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// The exact placeholder comment each blank file starts with — see
|
|
22
|
+
// templates/route.ts. Stripped the first time a real method lands, wherever
|
|
23
|
+
// it happens to sit in the file (start of controller.ts/services.ts, but
|
|
24
|
+
// mid-file in routes.ts, after the Router()/Hono() line).
|
|
25
|
+
const PLACEHOLDER_PATTERN = /\/\/ No (?:routes|methods) yet[^\n]*\n\n?/;
|
|
26
|
+
|
|
27
|
+
function stripPlaceholder(content: string): string {
|
|
28
|
+
return content.replace(PLACEHOLDER_PATTERN, "");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Inserts any of importLines not already present, right after the file's existing leading import block. */
|
|
32
|
+
function ensureImports(content: string, importLines: string[]): string {
|
|
33
|
+
const stripped = stripPlaceholder(content);
|
|
34
|
+
const missing = importLines.filter((line) => !stripped.includes(line));
|
|
35
|
+
if (missing.length === 0) return stripped;
|
|
36
|
+
|
|
37
|
+
const lines = stripped.split("\n");
|
|
38
|
+
let insertAt = 0;
|
|
39
|
+
while (insertAt < lines.length && lines[insertAt]?.startsWith("import ")) insertAt++;
|
|
40
|
+
lines.splice(insertAt, 0, ...missing);
|
|
41
|
+
return lines.join("\n");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function appendBlock(content: string, block: string): string {
|
|
45
|
+
const trimmed = content.replace(/\n+$/, "");
|
|
46
|
+
return trimmed ? `${trimmed}\n\n${block}\n` : `${block}\n`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Adds one HTTP method to an already-mounted, already-blank-or-partial
|
|
51
|
+
* endpoint: appends a defineRoute() export (with whatever params/body/query
|
|
52
|
+
* schemas this method declares — params auto-derived from the path, body
|
|
53
|
+
* and query left as TODO placeholders same as the old fixed scaffold did)
|
|
54
|
+
* to the controller, a matching service function typed via z.infer, and
|
|
55
|
+
* the route mount line. All three files are read fresh off disk first, so
|
|
56
|
+
* this is safe to call repeatedly, or after the previous method's stub was
|
|
57
|
+
* hand-edited.
|
|
58
|
+
*/
|
|
59
|
+
export async function addMethod(route: string, spec: MethodSpec, cwd: string = process.cwd()): Promise<AddMethodResult> {
|
|
60
|
+
const { root, framework, language, ext } = await detectProject(cwd);
|
|
61
|
+
|
|
62
|
+
const { contents: indexContents } = await readEndpointsIndex(root, ext);
|
|
63
|
+
const mounted = parseMountedRoutes(indexContents);
|
|
64
|
+
if (!mounted.includes(route)) {
|
|
65
|
+
throw new Error(`"${route}" isn't mounted in src/endpoints/index.${ext} — add it first with \`bun run add-route\`.`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const routeDir = join(root, "src/endpoints", route);
|
|
69
|
+
const controllerPath = join(routeDir, `${route}.controller.${ext}`);
|
|
70
|
+
const routesPath = join(routeDir, `${route}.routes.${ext}`);
|
|
71
|
+
const servicesPath = join(routeDir, `${route}.services.${ext}`);
|
|
72
|
+
|
|
73
|
+
const [controllerContents, routesContents, servicesContents] = await Promise.all([
|
|
74
|
+
readFile(controllerPath, "utf-8"),
|
|
75
|
+
readFile(routesPath, "utf-8"),
|
|
76
|
+
readFile(servicesPath, "utf-8"),
|
|
77
|
+
]);
|
|
78
|
+
|
|
79
|
+
const existing = parseRouteMethods(routesContents);
|
|
80
|
+
if (existing.some((m) => m.name === spec.name)) {
|
|
81
|
+
throw new Error(`"${spec.name}" is already used in ${controllerPath} — pick a different name.`);
|
|
82
|
+
}
|
|
83
|
+
if (existing.some((m) => m.verb === spec.verb && m.path === spec.path)) {
|
|
84
|
+
throw new Error(`${spec.verb.toUpperCase()} ${spec.path} is already mounted in ${routesPath}.`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const newController = appendBlock(ensureImports(controllerContents, controllerImports(route, framework)), renderControllerMethod(spec));
|
|
88
|
+
const newRoutes = appendBlock(
|
|
89
|
+
ensureImports(routesContents, [`import * as controller from "./${route}.controller";`]),
|
|
90
|
+
routeMountLine(route, spec)
|
|
91
|
+
);
|
|
92
|
+
const newServices = appendBlock(ensureImports(servicesContents, serviceImports(route, spec, language)), renderServiceMethod(spec, language));
|
|
93
|
+
|
|
94
|
+
await Promise.all([
|
|
95
|
+
writeFile(controllerPath, newController),
|
|
96
|
+
writeFile(routesPath, newRoutes),
|
|
97
|
+
writeFile(servicesPath, newServices),
|
|
98
|
+
]);
|
|
99
|
+
|
|
100
|
+
return { route, root, filesWritten: [controllerPath, routesPath, servicesPath] };
|
|
101
|
+
}
|
package/src/add-route.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { detectProject } from "./detect";
|
|
|
4
4
|
import { parseMountedRoutes, readEndpointsIndex } from "./endpoints-index";
|
|
5
5
|
import { toKebabCase } from "./prompts";
|
|
6
6
|
import { renderEndpointsIndex } from "./templates/base";
|
|
7
|
-
import {
|
|
7
|
+
import { renderBlankController, renderBlankRoutes, renderBlankServices } from "./templates/route";
|
|
8
8
|
|
|
9
9
|
export interface AddRouteResult {
|
|
10
10
|
route: string;
|
|
@@ -18,11 +18,13 @@ async function write(path: string, contents: string): Promise<void> {
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
|
-
* Adds a single route to an already-scaffolded backon project: detects
|
|
22
|
-
* project's framework/language from its package.json, writes the same
|
|
23
|
-
* controller/routes/services trio generateProject() would have
|
|
24
|
-
* this route at scaffold time
|
|
25
|
-
*
|
|
21
|
+
* Adds a single blank route to an already-scaffolded backon project: detects
|
|
22
|
+
* the project's framework/language from its package.json, writes the same
|
|
23
|
+
* blank controller/routes/services trio generateProject() would have
|
|
24
|
+
* written for this route at scaffold time (zero methods — see
|
|
25
|
+
* templates/route.ts), and regenerates src/endpoints/index.<ext> so the new
|
|
26
|
+
* route is mounted alongside whatever was already there. Use
|
|
27
|
+
* `add-method` afterward to give it an actual GET/POST/etc.
|
|
26
28
|
*/
|
|
27
29
|
export async function addRoute(rawName: string, cwd: string = process.cwd()): Promise<AddRouteResult> {
|
|
28
30
|
const route = toKebabCase(rawName);
|
|
@@ -55,9 +57,9 @@ export async function addRoute(rawName: string, cwd: string = process.cwd()): Pr
|
|
|
55
57
|
const routeDir = join(endpointsDir, route);
|
|
56
58
|
const filesWritten: string[] = [];
|
|
57
59
|
const files: Array<[string, string]> = [
|
|
58
|
-
[join(routeDir, `${route}.controller.${ext}`),
|
|
59
|
-
[join(routeDir, `${route}.routes.${ext}`),
|
|
60
|
-
[join(routeDir, `${route}.services.${ext}`),
|
|
60
|
+
[join(routeDir, `${route}.controller.${ext}`), renderBlankController()],
|
|
61
|
+
[join(routeDir, `${route}.routes.${ext}`), renderBlankRoutes(route, framework, language)],
|
|
62
|
+
[join(routeDir, `${route}.services.${ext}`), renderBlankServices()],
|
|
61
63
|
];
|
|
62
64
|
|
|
63
65
|
for (const [path, contents] of files) {
|