create-backon 0.2.0 → 0.3.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 +27 -0
- package/package.json +1 -1
- package/src/add-route.ts +90 -0
- package/src/cli.ts +37 -1
- package/src/detect.ts +49 -0
- package/src/prompts.ts +1 -1
package/README.md
CHANGED
|
@@ -57,6 +57,33 @@ already has real files in it — a few common exceptions (`.git`,
|
|
|
57
57
|
`.gitignore`, `README.md`, `LICENSE`, editor folders) are allowed through so
|
|
58
58
|
running this right after `git init` on a fresh repo doesn't get blocked.
|
|
59
59
|
|
|
60
|
+
## Adding a route to an existing project
|
|
61
|
+
|
|
62
|
+
Re-running the full generator isn't the way to add one more route to a
|
|
63
|
+
project you already scaffolded. Use the `add-route` subcommand instead,
|
|
64
|
+
from inside the project:
|
|
65
|
+
|
|
66
|
+
```
|
|
67
|
+
bunx create-backon add-route users
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
(`bun create backon add-route users` also works — `bun create` forwards
|
|
71
|
+
extra arguments straight through. `route` works as a shorthand for
|
|
72
|
+
`add-route` either way.) Leave the name off and it'll prompt for one:
|
|
73
|
+
|
|
74
|
+
```
|
|
75
|
+
bunx create-backon add-route
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
It reads the project's own `package.json` to figure out whether it's
|
|
79
|
+
Hono or Express, TypeScript or JavaScript — no need to answer those again
|
|
80
|
+
— then writes `src/endpoints/users/users.controller.ts` (or `.routes`,
|
|
81
|
+
`.services`, matching whatever the rest of the project already generates),
|
|
82
|
+
and regenerates `src/endpoints/index.ts` so the new route is mounted next
|
|
83
|
+
to the ones that were already there. It refuses to run outside a backon
|
|
84
|
+
project, refuses a route name whose folder already exists, and refuses a
|
|
85
|
+
name that's already mounted.
|
|
86
|
+
|
|
60
87
|
## Requirements
|
|
61
88
|
|
|
62
89
|
Bun >= 1.3.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-backon",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Scaffolds a new backend project on backon — like `bun create vite@latest`, but for a consistent controller/routes/services backend layout.",
|
|
6
6
|
"license": "MIT",
|
package/src/add-route.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { mkdir, readFile, readdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { detectProject } from "./detect";
|
|
4
|
+
import { toKebabCase } from "./prompts";
|
|
5
|
+
import { renderEndpointsIndex } from "./templates/base";
|
|
6
|
+
import { renderController, renderRoutes, renderServices } from "./templates/route";
|
|
7
|
+
|
|
8
|
+
// Matches the two shapes renderEndpointsIndex() ever generates:
|
|
9
|
+
// endpoints.route("/telemetry", telemetryRoutes); (hono)
|
|
10
|
+
// endpoints.use("/telemetry", telemetryRoutes); (express)
|
|
11
|
+
const MOUNT_PATTERN = /endpoints\.(?:route|use)\("\/([a-z0-9-]+)"/g;
|
|
12
|
+
|
|
13
|
+
export interface AddRouteResult {
|
|
14
|
+
route: string;
|
|
15
|
+
root: string;
|
|
16
|
+
filesWritten: string[];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function write(path: string, contents: string): Promise<void> {
|
|
20
|
+
await mkdir(dirname(path), { recursive: true });
|
|
21
|
+
await writeFile(path, contents);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Adds a single route to an already-scaffolded backon project: detects the
|
|
26
|
+
* project's framework/language from its package.json, writes the same
|
|
27
|
+
* controller/routes/services trio generateProject() would have written for
|
|
28
|
+
* this route at scaffold time, and regenerates src/endpoints/index.<ext> so
|
|
29
|
+
* the new route is mounted alongside whatever was already there.
|
|
30
|
+
*/
|
|
31
|
+
export async function addRoute(rawName: string, cwd: string = process.cwd()): Promise<AddRouteResult> {
|
|
32
|
+
const route = toKebabCase(rawName);
|
|
33
|
+
if (!route) {
|
|
34
|
+
throw new Error("Route name can't be empty.");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const { root, framework, language, ext } = await detectProject(cwd);
|
|
38
|
+
|
|
39
|
+
const endpointsDir = join(root, "src/endpoints");
|
|
40
|
+
let dirEntries: string[];
|
|
41
|
+
try {
|
|
42
|
+
dirEntries = await readdir(endpointsDir);
|
|
43
|
+
} catch (err) {
|
|
44
|
+
if ((err as NodeJS.ErrnoException).code === "ENOENT") {
|
|
45
|
+
throw new Error(`${endpointsDir} doesn't exist — is this a backon project?`);
|
|
46
|
+
}
|
|
47
|
+
throw err;
|
|
48
|
+
}
|
|
49
|
+
if (dirEntries.includes(route)) {
|
|
50
|
+
throw new Error(`src/endpoints/${route} already exists.`);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const indexPath = join(endpointsDir, `index.${ext}`);
|
|
54
|
+
let indexContents: string;
|
|
55
|
+
try {
|
|
56
|
+
indexContents = await readFile(indexPath, "utf-8");
|
|
57
|
+
} catch (err) {
|
|
58
|
+
if ((err as NodeJS.ErrnoException).code === "ENOENT") {
|
|
59
|
+
throw new Error(`${indexPath} doesn't exist — is this a backon project?`);
|
|
60
|
+
}
|
|
61
|
+
throw err;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const existingRoutes: string[] = [];
|
|
65
|
+
for (const match of indexContents.matchAll(MOUNT_PATTERN)) {
|
|
66
|
+
existingRoutes.push(match[1]);
|
|
67
|
+
}
|
|
68
|
+
if (existingRoutes.includes(route)) {
|
|
69
|
+
throw new Error(`"${route}" is already mounted in ${indexPath}.`);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const routeDir = join(endpointsDir, route);
|
|
73
|
+
const filesWritten: string[] = [];
|
|
74
|
+
const files: Array<[string, string]> = [
|
|
75
|
+
[join(routeDir, `${route}.controller.${ext}`), renderController(route, framework)],
|
|
76
|
+
[join(routeDir, `${route}.routes.${ext}`), renderRoutes(route, framework, language)],
|
|
77
|
+
[join(routeDir, `${route}.services.${ext}`), renderServices(route, language)],
|
|
78
|
+
];
|
|
79
|
+
|
|
80
|
+
for (const [path, contents] of files) {
|
|
81
|
+
await write(path, contents);
|
|
82
|
+
filesWritten.push(path);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const allRoutes = [...existingRoutes, route];
|
|
86
|
+
await writeFile(indexPath, renderEndpointsIndex(framework, allRoutes));
|
|
87
|
+
filesWritten.push(indexPath);
|
|
88
|
+
|
|
89
|
+
return { route, root, filesWritten };
|
|
90
|
+
}
|
package/src/cli.ts
CHANGED
|
@@ -2,8 +2,33 @@
|
|
|
2
2
|
import * as p from "@clack/prompts";
|
|
3
3
|
import { runPrompts } from "./prompts";
|
|
4
4
|
import { generateProject } from "./generate";
|
|
5
|
+
import { addRoute } from "./add-route";
|
|
5
6
|
|
|
6
|
-
|
|
7
|
+
const ADD_ROUTE_SUBCOMMANDS = new Set(["add-route", "route"]);
|
|
8
|
+
|
|
9
|
+
async function runAddRoute(args: string[]): Promise<void> {
|
|
10
|
+
let [name] = args;
|
|
11
|
+
|
|
12
|
+
if (!name) {
|
|
13
|
+
p.intro("create-backon add-route");
|
|
14
|
+
const answer = await p.text({
|
|
15
|
+
message: "Route name",
|
|
16
|
+
placeholder: "e.g. users",
|
|
17
|
+
validate: (value) => (!value ? "Required." : undefined),
|
|
18
|
+
});
|
|
19
|
+
if (p.isCancel(answer)) {
|
|
20
|
+
p.cancel("Cancelled.");
|
|
21
|
+
process.exit(0);
|
|
22
|
+
}
|
|
23
|
+
name = answer;
|
|
24
|
+
p.outro(`Adding ${name}...`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const { route, root, filesWritten } = await addRoute(name);
|
|
28
|
+
p.log.success(`Added src/endpoints/${route} (${filesWritten.length} files) in ${root}`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function runScaffold(): Promise<void> {
|
|
7
32
|
const answers = await runPrompts();
|
|
8
33
|
const { root, filesWritten } = await generateProject(answers);
|
|
9
34
|
|
|
@@ -13,6 +38,17 @@ async function main() {
|
|
|
13
38
|
p.log.success(`Wrote ${filesWritten.length} files to ${root}`);
|
|
14
39
|
}
|
|
15
40
|
|
|
41
|
+
async function main() {
|
|
42
|
+
const [subcommand, ...rest] = process.argv.slice(2);
|
|
43
|
+
|
|
44
|
+
if (subcommand && ADD_ROUTE_SUBCOMMANDS.has(subcommand)) {
|
|
45
|
+
await runAddRoute(rest);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
await runScaffold();
|
|
50
|
+
}
|
|
51
|
+
|
|
16
52
|
main().catch((err) => {
|
|
17
53
|
console.error(err);
|
|
18
54
|
process.exit(1);
|
package/src/detect.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import type { Framework, Language } from "./prompts";
|
|
4
|
+
|
|
5
|
+
export interface DetectedProject {
|
|
6
|
+
root: string;
|
|
7
|
+
framework: Framework;
|
|
8
|
+
language: Language;
|
|
9
|
+
ext: "ts" | "js";
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Figures out framework/language for an already-scaffolded backon project
|
|
14
|
+
* by reading its package.json, rather than asking again — the answers are
|
|
15
|
+
* already implied by what generateProject() wrote the first time around.
|
|
16
|
+
*/
|
|
17
|
+
export async function detectProject(cwd: string = process.cwd()): Promise<DetectedProject> {
|
|
18
|
+
const pkgPath = join(cwd, "package.json");
|
|
19
|
+
|
|
20
|
+
let raw: string;
|
|
21
|
+
try {
|
|
22
|
+
raw = await readFile(pkgPath, "utf-8");
|
|
23
|
+
} catch (err) {
|
|
24
|
+
if ((err as NodeJS.ErrnoException).code === "ENOENT") {
|
|
25
|
+
throw new Error(`No package.json found in ${cwd} — run this from a project created by \`bun create backon\`.`);
|
|
26
|
+
}
|
|
27
|
+
throw err;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let pkg: { dependencies?: Record<string, string>; devDependencies?: Record<string, string> };
|
|
31
|
+
try {
|
|
32
|
+
pkg = JSON.parse(raw);
|
|
33
|
+
} catch {
|
|
34
|
+
throw new Error(`Couldn't parse ${pkgPath} as JSON.`);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const dependencies = pkg.dependencies ?? {};
|
|
38
|
+
const devDependencies = pkg.devDependencies ?? {};
|
|
39
|
+
|
|
40
|
+
if (!dependencies["@mednours/backon"]) {
|
|
41
|
+
throw new Error(`${cwd} doesn't look like a backon project — package.json has no "@mednours/backon" dependency.`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const framework: Framework = dependencies.express ? "express" : "hono";
|
|
45
|
+
const language: Language = devDependencies.typescript ? "typescript" : "javascript";
|
|
46
|
+
const ext: "ts" | "js" = language === "javascript" ? "js" : "ts";
|
|
47
|
+
|
|
48
|
+
return { root: cwd, framework, language, ext };
|
|
49
|
+
}
|