create-backon 0.2.0 → 0.4.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 +69 -2
- package/package.json +1 -1
- package/src/add-route.ts +73 -0
- package/src/cli.ts +98 -1
- package/src/detect.ts +49 -0
- package/src/doctor.ts +73 -0
- package/src/endpoints-index.ts +37 -0
- package/src/generate.ts +1 -0
- package/src/list-routes.ts +14 -0
- package/src/prompts.ts +9 -1
- package/src/remove-route.ts +41 -0
package/README.md
CHANGED
|
@@ -20,8 +20,9 @@ bunx create-backon
|
|
|
20
20
|
Prompts for a project name (or `.` to scaffold into the current directory,
|
|
21
21
|
same as `bun create vite@latest .`), a framework (Hono or Express), a
|
|
22
22
|
language (TypeScript or JavaScript), a database (Neon, Postgres, SQLite, or
|
|
23
|
-
none — via [Drizzle](https://orm.drizzle.team)),
|
|
24
|
-
|
|
23
|
+
none — via [Drizzle](https://orm.drizzle.team)), whether to create `.env`
|
|
24
|
+
from `.env.example` right away (default yes — skips the manual `cp` step),
|
|
25
|
+
and any number of routes, then generates:
|
|
25
26
|
|
|
26
27
|
```
|
|
27
28
|
my-api/
|
|
@@ -57,6 +58,72 @@ already has real files in it — a few common exceptions (`.git`,
|
|
|
57
58
|
`.gitignore`, `README.md`, `LICENSE`, editor folders) are allowed through so
|
|
58
59
|
running this right after `git init` on a fresh repo doesn't get blocked.
|
|
59
60
|
|
|
61
|
+
## Adding a route to an existing project
|
|
62
|
+
|
|
63
|
+
Re-running the full generator isn't the way to add one more route to a
|
|
64
|
+
project you already scaffolded. Use the `add-route` subcommand instead,
|
|
65
|
+
from inside the project:
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
bunx create-backon add-route users
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
(`bun create backon add-route users` also works — `bun create` forwards
|
|
72
|
+
extra arguments straight through. `route` works as a shorthand for
|
|
73
|
+
`add-route` either way.) Leave the name off and it'll prompt for one:
|
|
74
|
+
|
|
75
|
+
```
|
|
76
|
+
bunx create-backon add-route
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
It reads the project's own `package.json` to figure out whether it's
|
|
80
|
+
Hono or Express, TypeScript or JavaScript — no need to answer those again
|
|
81
|
+
— then writes `src/endpoints/users/users.controller.ts` (or `.routes`,
|
|
82
|
+
`.services`, matching whatever the rest of the project already generates),
|
|
83
|
+
and regenerates `src/endpoints/index.ts` so the new route is mounted next
|
|
84
|
+
to the ones that were already there. It refuses to run outside a backon
|
|
85
|
+
project, refuses a route name whose folder already exists, and refuses a
|
|
86
|
+
name that's already mounted.
|
|
87
|
+
|
|
88
|
+
## Removing a route
|
|
89
|
+
|
|
90
|
+
The undo for `add-route`:
|
|
91
|
+
|
|
92
|
+
```
|
|
93
|
+
bunx create-backon remove-route users
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
(`rm-route` also works.) Leave the name off to be prompted for one. Either
|
|
97
|
+
way you'll be asked to confirm before anything is deleted — this removes
|
|
98
|
+
`src/endpoints/users/` and unmounts it from `src/endpoints/index.ts`.
|
|
99
|
+
Refuses if the route isn't currently mounted, rather than guessing at
|
|
100
|
+
deleting a folder that was never wired up.
|
|
101
|
+
|
|
102
|
+
## Listing routes
|
|
103
|
+
|
|
104
|
+
```
|
|
105
|
+
bunx create-backon list-routes
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
(`routes` also works.) Read-only — prints every route currently mounted in
|
|
109
|
+
`src/endpoints/index.ts`.
|
|
110
|
+
|
|
111
|
+
## Checking a project for drift
|
|
112
|
+
|
|
113
|
+
```
|
|
114
|
+
bunx create-backon doctor
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Compares what's actually in `src/endpoints/` against what `index.<ext>`
|
|
118
|
+
mounts, and flags three things: a route folder that exists but isn't
|
|
119
|
+
mounted (an orphan), a mount in `index.<ext>` pointing at a folder that's
|
|
120
|
+
gone (dangling), and a route folder missing one of its three expected
|
|
121
|
+
files. This kind of drift usually comes from hand-editing `index.ts`,
|
|
122
|
+
deleting a route folder without using `remove-route`, or copy-pasting a
|
|
123
|
+
route folder without wiring it up. `doctor` only reports issues — it
|
|
124
|
+
doesn't fix them — and exits non-zero if it finds any, so it's usable in
|
|
125
|
+
a pre-commit hook or CI step.
|
|
126
|
+
|
|
60
127
|
## Requirements
|
|
61
128
|
|
|
62
129
|
Bun >= 1.3.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-backon",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.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,73 @@
|
|
|
1
|
+
import { mkdir, readdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { detectProject } from "./detect";
|
|
4
|
+
import { parseMountedRoutes, readEndpointsIndex } from "./endpoints-index";
|
|
5
|
+
import { toKebabCase } from "./prompts";
|
|
6
|
+
import { renderEndpointsIndex } from "./templates/base";
|
|
7
|
+
import { renderController, renderRoutes, renderServices } from "./templates/route";
|
|
8
|
+
|
|
9
|
+
export interface AddRouteResult {
|
|
10
|
+
route: string;
|
|
11
|
+
root: string;
|
|
12
|
+
filesWritten: string[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async function write(path: string, contents: string): Promise<void> {
|
|
16
|
+
await mkdir(dirname(path), { recursive: true });
|
|
17
|
+
await writeFile(path, contents);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Adds a single route to an already-scaffolded backon project: detects the
|
|
22
|
+
* project's framework/language from its package.json, writes the same
|
|
23
|
+
* controller/routes/services trio generateProject() would have written for
|
|
24
|
+
* this route at scaffold time, and regenerates src/endpoints/index.<ext> so
|
|
25
|
+
* the new route is mounted alongside whatever was already there.
|
|
26
|
+
*/
|
|
27
|
+
export async function addRoute(rawName: string, cwd: string = process.cwd()): Promise<AddRouteResult> {
|
|
28
|
+
const route = toKebabCase(rawName);
|
|
29
|
+
if (!route) {
|
|
30
|
+
throw new Error("Route name can't be empty.");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const { root, framework, language, ext } = await detectProject(cwd);
|
|
34
|
+
|
|
35
|
+
const endpointsDir = join(root, "src/endpoints");
|
|
36
|
+
let dirEntries: string[];
|
|
37
|
+
try {
|
|
38
|
+
dirEntries = await readdir(endpointsDir);
|
|
39
|
+
} catch (err) {
|
|
40
|
+
if ((err as NodeJS.ErrnoException).code === "ENOENT") {
|
|
41
|
+
throw new Error(`${endpointsDir} doesn't exist — is this a backon project?`);
|
|
42
|
+
}
|
|
43
|
+
throw err;
|
|
44
|
+
}
|
|
45
|
+
if (dirEntries.includes(route)) {
|
|
46
|
+
throw new Error(`src/endpoints/${route} already exists.`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const { path: indexPath, contents: indexContents } = await readEndpointsIndex(root, ext);
|
|
50
|
+
const existingRoutes = parseMountedRoutes(indexContents);
|
|
51
|
+
if (existingRoutes.includes(route)) {
|
|
52
|
+
throw new Error(`"${route}" is already mounted in ${indexPath}.`);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const routeDir = join(endpointsDir, route);
|
|
56
|
+
const filesWritten: string[] = [];
|
|
57
|
+
const files: Array<[string, string]> = [
|
|
58
|
+
[join(routeDir, `${route}.controller.${ext}`), renderController(route, framework)],
|
|
59
|
+
[join(routeDir, `${route}.routes.${ext}`), renderRoutes(route, framework, language)],
|
|
60
|
+
[join(routeDir, `${route}.services.${ext}`), renderServices(route, language)],
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
for (const [path, contents] of files) {
|
|
64
|
+
await write(path, contents);
|
|
65
|
+
filesWritten.push(path);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const allRoutes = [...existingRoutes, route];
|
|
69
|
+
await writeFile(indexPath, renderEndpointsIndex(framework, allRoutes));
|
|
70
|
+
filesWritten.push(indexPath);
|
|
71
|
+
|
|
72
|
+
return { route, root, filesWritten };
|
|
73
|
+
}
|
package/src/cli.ts
CHANGED
|
@@ -2,8 +2,82 @@
|
|
|
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";
|
|
6
|
+
import { removeRoute } from "./remove-route";
|
|
7
|
+
import { listRoutes } from "./list-routes";
|
|
8
|
+
import { runDoctor } from "./doctor";
|
|
5
9
|
|
|
6
|
-
|
|
10
|
+
const ADD_ROUTE_SUBCOMMANDS = new Set(["add-route", "route"]);
|
|
11
|
+
const REMOVE_ROUTE_SUBCOMMANDS = new Set(["remove-route", "rm-route"]);
|
|
12
|
+
const LIST_ROUTES_SUBCOMMANDS = new Set(["list-routes", "routes"]);
|
|
13
|
+
const DOCTOR_SUBCOMMANDS = new Set(["doctor"]);
|
|
14
|
+
|
|
15
|
+
async function promptForRouteName(introText: string): Promise<string> {
|
|
16
|
+
p.intro(introText);
|
|
17
|
+
const answer = await p.text({
|
|
18
|
+
message: "Route name",
|
|
19
|
+
placeholder: "e.g. users",
|
|
20
|
+
validate: (value) => (!value ? "Required." : undefined),
|
|
21
|
+
});
|
|
22
|
+
if (p.isCancel(answer)) {
|
|
23
|
+
p.cancel("Cancelled.");
|
|
24
|
+
process.exit(0);
|
|
25
|
+
}
|
|
26
|
+
return answer;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function runAddRoute(args: string[]): Promise<void> {
|
|
30
|
+
let [name] = args;
|
|
31
|
+
|
|
32
|
+
if (!name) {
|
|
33
|
+
name = await promptForRouteName("create-backon add-route");
|
|
34
|
+
p.outro(`Adding ${name}...`);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const { route, root, filesWritten } = await addRoute(name);
|
|
38
|
+
p.log.success(`Added src/endpoints/${route} (${filesWritten.length} files) in ${root}`);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function runRemoveRoute(args: string[]): Promise<void> {
|
|
42
|
+
let [name] = args;
|
|
43
|
+
|
|
44
|
+
if (!name) {
|
|
45
|
+
name = await promptForRouteName("create-backon remove-route");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const shouldRemove = await p.confirm({
|
|
49
|
+
message: `Remove src/endpoints/${name} and unmount it? This deletes the folder.`,
|
|
50
|
+
initialValue: false,
|
|
51
|
+
});
|
|
52
|
+
if (p.isCancel(shouldRemove) || !shouldRemove) {
|
|
53
|
+
p.cancel("Cancelled.");
|
|
54
|
+
process.exit(0);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const { route, root, removedDir } = await removeRoute(name);
|
|
58
|
+
p.log.success(`Removed ${removedDir} and unmounted "${route}" in ${root}`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function runListRoutes(): Promise<void> {
|
|
62
|
+
const { root, routes } = await listRoutes();
|
|
63
|
+
if (routes.length === 0) {
|
|
64
|
+
p.log.info(`No routes mounted in ${root}.`);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
p.log.info(`Routes mounted in ${root}:\n${routes.map((route) => ` - ${route}`).join("\n")}`);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function runDoctorCommand(): Promise<void> {
|
|
71
|
+
const { root, issues } = await runDoctor();
|
|
72
|
+
if (issues.length === 0) {
|
|
73
|
+
p.log.success(`No issues found in ${root}.`);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
p.log.warn(`${issues.length} issue(s) found in ${root}:\n${issues.map((issue) => ` - ${issue.detail}`).join("\n")}`);
|
|
77
|
+
process.exitCode = 1;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function runScaffold(): Promise<void> {
|
|
7
81
|
const answers = await runPrompts();
|
|
8
82
|
const { root, filesWritten } = await generateProject(answers);
|
|
9
83
|
|
|
@@ -13,6 +87,29 @@ async function main() {
|
|
|
13
87
|
p.log.success(`Wrote ${filesWritten.length} files to ${root}`);
|
|
14
88
|
}
|
|
15
89
|
|
|
90
|
+
async function main() {
|
|
91
|
+
const [subcommand, ...rest] = process.argv.slice(2);
|
|
92
|
+
|
|
93
|
+
if (subcommand && ADD_ROUTE_SUBCOMMANDS.has(subcommand)) {
|
|
94
|
+
await runAddRoute(rest);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (subcommand && REMOVE_ROUTE_SUBCOMMANDS.has(subcommand)) {
|
|
98
|
+
await runRemoveRoute(rest);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (subcommand && LIST_ROUTES_SUBCOMMANDS.has(subcommand)) {
|
|
102
|
+
await runListRoutes();
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (subcommand && DOCTOR_SUBCOMMANDS.has(subcommand)) {
|
|
106
|
+
await runDoctorCommand();
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
await runScaffold();
|
|
111
|
+
}
|
|
112
|
+
|
|
16
113
|
main().catch((err) => {
|
|
17
114
|
console.error(err);
|
|
18
115
|
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
|
+
}
|
package/src/doctor.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { access, readdir } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { detectProject } from "./detect";
|
|
4
|
+
import { parseMountedRoutes, readEndpointsIndex } from "./endpoints-index";
|
|
5
|
+
|
|
6
|
+
export type DoctorIssueKind = "dangling-mount" | "orphaned-folder" | "missing-file";
|
|
7
|
+
|
|
8
|
+
export interface DoctorIssue {
|
|
9
|
+
kind: DoctorIssueKind;
|
|
10
|
+
route: string;
|
|
11
|
+
detail: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface DoctorResult {
|
|
15
|
+
root: string;
|
|
16
|
+
issues: DoctorIssue[];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const ROUTE_FILE_SUFFIXES = ["controller", "routes", "services"] as const;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Checks src/endpoints/ for drift between what's on disk and what
|
|
23
|
+
* index.<ext> actually mounts — the kind of thing that creeps in from
|
|
24
|
+
* hand-editing index.ts, an interrupted addRoute()/removeRoute(), or
|
|
25
|
+
* copy-pasting a route folder without wiring it up. Read-only; reports
|
|
26
|
+
* issues, doesn't fix them.
|
|
27
|
+
*/
|
|
28
|
+
export async function runDoctor(cwd: string = process.cwd()): Promise<DoctorResult> {
|
|
29
|
+
const { root, ext } = await detectProject(cwd);
|
|
30
|
+
const endpointsDir = join(root, "src/endpoints");
|
|
31
|
+
const { contents } = await readEndpointsIndex(root, ext);
|
|
32
|
+
const mounted = new Set(parseMountedRoutes(contents));
|
|
33
|
+
|
|
34
|
+
const dirents = await readdir(endpointsDir, { withFileTypes: true }).catch((err) => {
|
|
35
|
+
if ((err as NodeJS.ErrnoException).code === "ENOENT") return [];
|
|
36
|
+
throw err;
|
|
37
|
+
});
|
|
38
|
+
const folders = dirents.filter((d) => d.isDirectory()).map((d) => d.name);
|
|
39
|
+
const folderSet = new Set(folders);
|
|
40
|
+
|
|
41
|
+
const issues: DoctorIssue[] = [];
|
|
42
|
+
|
|
43
|
+
for (const route of mounted) {
|
|
44
|
+
if (!folderSet.has(route)) {
|
|
45
|
+
issues.push({
|
|
46
|
+
kind: "dangling-mount",
|
|
47
|
+
route,
|
|
48
|
+
detail: `index.${ext} mounts "/${route}" but src/endpoints/${route}/ doesn't exist.`,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
for (const route of folders) {
|
|
54
|
+
if (!mounted.has(route)) {
|
|
55
|
+
issues.push({
|
|
56
|
+
kind: "orphaned-folder",
|
|
57
|
+
route,
|
|
58
|
+
detail: `src/endpoints/${route}/ exists but isn't mounted in index.${ext}.`,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
for (const suffix of ROUTE_FILE_SUFFIXES) {
|
|
63
|
+
const filePath = join(endpointsDir, route, `${route}.${suffix}.${ext}`);
|
|
64
|
+
try {
|
|
65
|
+
await access(filePath);
|
|
66
|
+
} catch {
|
|
67
|
+
issues.push({ kind: "missing-file", route, detail: `${filePath} is missing.` });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return { root, issues };
|
|
73
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
|
|
4
|
+
// Matches the two shapes renderEndpointsIndex() ever generates:
|
|
5
|
+
// endpoints.route("/telemetry", telemetryRoutes); (hono)
|
|
6
|
+
// endpoints.use("/telemetry", telemetryRoutes); (express)
|
|
7
|
+
// Shared by add-route/remove-route/list-routes/doctor so all four agree on
|
|
8
|
+
// what "mounted" means by reading the same file the same way, instead of
|
|
9
|
+
// four copies of this regex drifting apart over time.
|
|
10
|
+
const MOUNT_PATTERN = /endpoints\.(?:route|use)\("\/([a-z0-9-]+)"/g;
|
|
11
|
+
|
|
12
|
+
export function parseMountedRoutes(indexContents: string): string[] {
|
|
13
|
+
const routes: string[] = [];
|
|
14
|
+
for (const match of indexContents.matchAll(MOUNT_PATTERN)) {
|
|
15
|
+
routes.push(match[1]);
|
|
16
|
+
}
|
|
17
|
+
return routes;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface EndpointsIndexFile {
|
|
21
|
+
path: string;
|
|
22
|
+
contents: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function readEndpointsIndex(root: string, ext: "ts" | "js"): Promise<EndpointsIndexFile> {
|
|
26
|
+
const path = join(root, "src/endpoints", `index.${ext}`);
|
|
27
|
+
let contents: string;
|
|
28
|
+
try {
|
|
29
|
+
contents = await readFile(path, "utf-8");
|
|
30
|
+
} catch (err) {
|
|
31
|
+
if ((err as NodeJS.ErrnoException).code === "ENOENT") {
|
|
32
|
+
throw new Error(`${path} doesn't exist — is this a backon project?`);
|
|
33
|
+
}
|
|
34
|
+
throw err;
|
|
35
|
+
}
|
|
36
|
+
return { path, contents };
|
|
37
|
+
}
|
package/src/generate.ts
CHANGED
|
@@ -84,6 +84,7 @@ export async function generateProject(answers: Answers, cwd: string = process.cw
|
|
|
84
84
|
const files: Array<[string, string]> = [
|
|
85
85
|
[join(root, `index.${ext}`), renderIndexTs(answers.framework)],
|
|
86
86
|
[join(root, ".env.example"), renderEnvExample(answers.db)],
|
|
87
|
+
...(answers.copyEnv ? ([[join(root, ".env"), renderEnvExample(answers.db)]] as Array<[string, string]>) : []),
|
|
87
88
|
[join(root, "package.json"), renderPackageJson(packageName, answers.framework, answers.language, answers.db)],
|
|
88
89
|
answers.language === "javascript"
|
|
89
90
|
? [join(root, "jsconfig.json"), renderJsconfig()]
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { detectProject } from "./detect";
|
|
2
|
+
import { parseMountedRoutes, readEndpointsIndex } from "./endpoints-index";
|
|
3
|
+
|
|
4
|
+
export interface ListRoutesResult {
|
|
5
|
+
root: string;
|
|
6
|
+
routes: string[];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** Read-only: what's actually mounted in src/endpoints/index.<ext> right now. */
|
|
10
|
+
export async function listRoutes(cwd: string = process.cwd()): Promise<ListRoutesResult> {
|
|
11
|
+
const { root, ext } = await detectProject(cwd);
|
|
12
|
+
const { contents } = await readEndpointsIndex(root, ext);
|
|
13
|
+
return { root, routes: parseMountedRoutes(contents) };
|
|
14
|
+
}
|
package/src/prompts.ts
CHANGED
|
@@ -10,6 +10,7 @@ export interface Answers {
|
|
|
10
10
|
language: Language;
|
|
11
11
|
db: DbChoice;
|
|
12
12
|
routes: string[];
|
|
13
|
+
copyEnv: boolean;
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
function bail(): never {
|
|
@@ -17,7 +18,7 @@ function bail(): never {
|
|
|
17
18
|
process.exit(0);
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
function toKebabCase(input: string): string {
|
|
21
|
+
export function toKebabCase(input: string): string {
|
|
21
22
|
return input
|
|
22
23
|
.trim()
|
|
23
24
|
.replace(/([a-z0-9])([A-Z])/g, "$1-$2")
|
|
@@ -71,6 +72,12 @@ export async function runPrompts(): Promise<Answers> {
|
|
|
71
72
|
});
|
|
72
73
|
if (p.isCancel(db)) bail();
|
|
73
74
|
|
|
75
|
+
const copyEnv = await p.confirm({
|
|
76
|
+
message: "Create .env from .env.example now?",
|
|
77
|
+
initialValue: true,
|
|
78
|
+
});
|
|
79
|
+
if (p.isCancel(copyEnv)) bail();
|
|
80
|
+
|
|
74
81
|
const routes: string[] = [];
|
|
75
82
|
p.log.step("Add routes one at a time. Leave the name blank when you're done.");
|
|
76
83
|
// eslint-disable-next-line no-constant-condition
|
|
@@ -92,5 +99,6 @@ export async function runPrompts(): Promise<Answers> {
|
|
|
92
99
|
language: language as Language,
|
|
93
100
|
db: db as DbChoice,
|
|
94
101
|
routes,
|
|
102
|
+
copyEnv: copyEnv as boolean,
|
|
95
103
|
};
|
|
96
104
|
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { rm, 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 { toKebabCase } from "./prompts";
|
|
6
|
+
import { renderEndpointsIndex } from "./templates/base";
|
|
7
|
+
|
|
8
|
+
export interface RemoveRouteResult {
|
|
9
|
+
route: string;
|
|
10
|
+
root: string;
|
|
11
|
+
removedDir: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The undo for addRoute(): unmounts the route from src/endpoints/index.<ext>
|
|
16
|
+
* and deletes its folder. Refuses if the route isn't currently mounted,
|
|
17
|
+
* rather than silently deleting an orphaned folder that was never wired up
|
|
18
|
+
* — that's what `doctor` is for.
|
|
19
|
+
*/
|
|
20
|
+
export async function removeRoute(rawName: string, cwd: string = process.cwd()): Promise<RemoveRouteResult> {
|
|
21
|
+
const route = toKebabCase(rawName);
|
|
22
|
+
if (!route) {
|
|
23
|
+
throw new Error("Route name can't be empty.");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const { root, framework, ext } = await detectProject(cwd);
|
|
27
|
+
const { path: indexPath, contents } = await readEndpointsIndex(root, ext);
|
|
28
|
+
|
|
29
|
+
const mounted = parseMountedRoutes(contents);
|
|
30
|
+
if (!mounted.includes(route)) {
|
|
31
|
+
throw new Error(`"${route}" isn't mounted in ${indexPath} — nothing to remove.`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const remaining = mounted.filter((name) => name !== route);
|
|
35
|
+
await writeFile(indexPath, renderEndpointsIndex(framework, remaining));
|
|
36
|
+
|
|
37
|
+
const removedDir = join(root, "src/endpoints", route);
|
|
38
|
+
await rm(removedDir, { recursive: true, force: true });
|
|
39
|
+
|
|
40
|
+
return { route, root, removedDir };
|
|
41
|
+
}
|