@pracht/cli 1.3.2 → 1.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/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# @pracht/cli
|
|
2
2
|
|
|
3
|
+
## 1.4.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#139](https://github.com/JoviDeCroock/pracht/pull/139) [`97594bd`](https://github.com/JoviDeCroock/pracht/commit/97594bd57b14fd5b527de647ba254b77f77912ca) Thanks [@JoviDeCroock](https://github.com/JoviDeCroock)! - Add typed route href helpers, `<Link route="...">`, route-object `useNavigate()`, and `pracht typegen` for generated route id/param declarations.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [[`5578791`](https://github.com/JoviDeCroock/pracht/commit/5578791b3abd6c808f5af78d88224667f483b32c), [`5938cb5`](https://github.com/JoviDeCroock/pracht/commit/5938cb56dd053fc8725efae0b7392dd65866b37b), [`97594bd`](https://github.com/JoviDeCroock/pracht/commit/97594bd57b14fd5b527de647ba254b77f77912ca)]:
|
|
12
|
+
- @pracht/core@0.7.0
|
|
13
|
+
|
|
14
|
+
## 1.3.3
|
|
15
|
+
|
|
16
|
+
### Patch Changes
|
|
17
|
+
|
|
18
|
+
- [`64242a9`](https://github.com/JoviDeCroock/pracht/commit/64242a9dd01348c29e08e22b54581ebce28208d6) Thanks [@JoviDeCroock](https://github.com/JoviDeCroock)! - Add npm package descriptions and keywords so Pracht packages are easier to discover in registries and AI-assisted tooling.
|
|
19
|
+
|
|
20
|
+
- Updated dependencies [[`64242a9`](https://github.com/JoviDeCroock/pracht/commit/64242a9dd01348c29e08e22b54581ebce28208d6)]:
|
|
21
|
+
- @pracht/core@0.6.1
|
|
22
|
+
|
|
3
23
|
## 1.3.2
|
|
4
24
|
|
|
5
25
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -79,6 +79,17 @@ pracht inspect api --json
|
|
|
79
79
|
pracht inspect build --json
|
|
80
80
|
```
|
|
81
81
|
|
|
82
|
+
### `pracht typegen`
|
|
83
|
+
|
|
84
|
+
Generate `src/pracht-routes.d.ts` and `src/pracht-routes.ts` from the resolved
|
|
85
|
+
route graph for typed `<Link>`, route-object `useNavigate()`, and `href()`
|
|
86
|
+
helpers. Use `--check` in CI to fail when generated files are stale.
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
pracht typegen
|
|
90
|
+
pracht typegen --check
|
|
91
|
+
```
|
|
92
|
+
|
|
82
93
|
### `pracht doctor`
|
|
83
94
|
|
|
84
95
|
Validate the local app wiring across the whole project. Use `--json` for
|
package/dist/index.mjs
CHANGED
|
@@ -45,8 +45,9 @@ runMain(defineCommand({
|
|
|
45
45
|
dev: () => import("./dev-BCFe3g38.mjs").then((m) => m.default),
|
|
46
46
|
doctor: () => import("./doctor-DyOWEA42.mjs").then((m) => m.default),
|
|
47
47
|
generate: () => import("./generate-V7pQExlW.mjs").then((m) => m.default),
|
|
48
|
-
inspect: () => import("./inspect-
|
|
49
|
-
|
|
48
|
+
inspect: () => import("./inspect-WF08uLOl.mjs").then((m) => m.default),
|
|
49
|
+
typegen: () => import("./typegen-Bl4fYQIy.mjs").then((m) => m.default),
|
|
50
|
+
verify: () => import("./verify-FwVbdlxh.mjs").then((m) => m.default)
|
|
50
51
|
}
|
|
51
52
|
}));
|
|
52
53
|
//#endregion
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { f as ensureTrailingNewline, n as displayPath, p as handleCliError } from "./project-voPTS9UC.mjs";
|
|
2
|
+
import { runInspect } from "./inspect-WF08uLOl.mjs";
|
|
3
|
+
import { defineCommand } from "citty";
|
|
4
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { dirname, isAbsolute, relative, resolve } from "node:path";
|
|
6
|
+
//#region src/commands/typegen.ts
|
|
7
|
+
const DEFAULT_DECLARATION_OUT = "src/pracht-routes.d.ts";
|
|
8
|
+
const DEFAULT_RUNTIME_OUT = "src/pracht-routes.ts";
|
|
9
|
+
var typegen_default = defineCommand({
|
|
10
|
+
meta: {
|
|
11
|
+
name: "typegen",
|
|
12
|
+
description: "Generate typed route declarations and href helpers"
|
|
13
|
+
},
|
|
14
|
+
args: {
|
|
15
|
+
out: {
|
|
16
|
+
type: "string",
|
|
17
|
+
description: `Declaration output path (default: ${DEFAULT_DECLARATION_OUT})`
|
|
18
|
+
},
|
|
19
|
+
"runtime-out": {
|
|
20
|
+
type: "string",
|
|
21
|
+
description: `Runtime href helper output path (default: ${DEFAULT_RUNTIME_OUT})`
|
|
22
|
+
},
|
|
23
|
+
check: {
|
|
24
|
+
type: "boolean",
|
|
25
|
+
description: "Check whether generated route files are up to date without writing"
|
|
26
|
+
},
|
|
27
|
+
json: {
|
|
28
|
+
type: "boolean",
|
|
29
|
+
description: "Output as JSON"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
async run({ args }) {
|
|
33
|
+
const json = Boolean(args.json);
|
|
34
|
+
try {
|
|
35
|
+
const result = await runTypegen({
|
|
36
|
+
check: Boolean(args.check),
|
|
37
|
+
declarationOut: typeof args.out === "string" ? args.out : DEFAULT_DECLARATION_OUT,
|
|
38
|
+
root: process.cwd(),
|
|
39
|
+
runtimeOut: typeof args["runtime-out"] === "string" ? args["runtime-out"] : DEFAULT_RUNTIME_OUT
|
|
40
|
+
});
|
|
41
|
+
if (json) {
|
|
42
|
+
console.log(JSON.stringify({
|
|
43
|
+
ok: true,
|
|
44
|
+
...result
|
|
45
|
+
}, null, 2));
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (result.check) {
|
|
49
|
+
console.log("Generated route files are up to date.");
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
console.log("Generated typed routes:");
|
|
53
|
+
for (const file of result.files) console.log(` ${file}`);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
handleCliError(error, { json });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
async function runTypegen(options) {
|
|
60
|
+
const report = await runInspect(options.root, { target: "routes" });
|
|
61
|
+
const routes = report.routes ?? [];
|
|
62
|
+
validateRoutes(routes);
|
|
63
|
+
const declarationPath = resolveOutputPath(options.root, options.declarationOut);
|
|
64
|
+
const runtimePath = resolveOutputPath(options.root, options.runtimeOut);
|
|
65
|
+
const outputs = [{
|
|
66
|
+
path: declarationPath,
|
|
67
|
+
source: buildDeclarationSource(routes)
|
|
68
|
+
}, {
|
|
69
|
+
path: runtimePath,
|
|
70
|
+
source: buildRuntimeSource(routes)
|
|
71
|
+
}];
|
|
72
|
+
if (options.check) {
|
|
73
|
+
const stale = outputs.filter((output) => !fileMatches(output.path, output.source));
|
|
74
|
+
if (stale.length > 0) {
|
|
75
|
+
const files = stale.map((output) => displayPath(options.root, output.path)).join(", ");
|
|
76
|
+
throw new Error(`Generated route files are out of date: ${files}. Run \`pracht typegen\`.`);
|
|
77
|
+
}
|
|
78
|
+
} else for (const output of outputs) {
|
|
79
|
+
mkdirSync(dirname(output.path), { recursive: true });
|
|
80
|
+
writeFileSync(output.path, output.source, "utf-8");
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
check: options.check,
|
|
84
|
+
files: outputs.map((output) => displayPath(options.root, output.path)),
|
|
85
|
+
mode: report.mode,
|
|
86
|
+
routes: routes.length
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function validateRoutes(routes) {
|
|
90
|
+
const seen = /* @__PURE__ */ new Map();
|
|
91
|
+
for (const route of routes) {
|
|
92
|
+
if (!route.id) throw new Error(`Route ${route.path} resolved without an id.`);
|
|
93
|
+
const previousPath = seen.get(route.id);
|
|
94
|
+
if (previousPath) throw new Error(`Duplicate route id "${route.id}" for ${previousPath} and ${route.path}. Add explicit unique ids.`);
|
|
95
|
+
seen.set(route.id, route.path);
|
|
96
|
+
inferRouteParams(route.path);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function buildDeclarationSource(routes) {
|
|
100
|
+
const lines = [
|
|
101
|
+
"// Generated by `pracht typegen`. Do not edit manually.",
|
|
102
|
+
"import \"@pracht/core\";",
|
|
103
|
+
"import type { RouteParamInput, SearchParamsInput } from \"@pracht/core\";",
|
|
104
|
+
"",
|
|
105
|
+
"declare module \"@pracht/core\" {",
|
|
106
|
+
" interface Register {",
|
|
107
|
+
" routes: {"
|
|
108
|
+
];
|
|
109
|
+
for (const route of routes) {
|
|
110
|
+
lines.push(` ${JSON.stringify(route.id)}: {`);
|
|
111
|
+
lines.push(` path: ${JSON.stringify(route.path)};`);
|
|
112
|
+
lines.push(` params: ${formatParamsType(inferRouteParams(route.path))};`);
|
|
113
|
+
lines.push(" search: SearchParamsInput;");
|
|
114
|
+
lines.push(" };");
|
|
115
|
+
}
|
|
116
|
+
lines.push(" };");
|
|
117
|
+
lines.push(" }");
|
|
118
|
+
lines.push("}");
|
|
119
|
+
lines.push("");
|
|
120
|
+
lines.push("export {};");
|
|
121
|
+
lines.push("");
|
|
122
|
+
return lines.join("\n");
|
|
123
|
+
}
|
|
124
|
+
function buildRuntimeSource(routes) {
|
|
125
|
+
const lines = [
|
|
126
|
+
"// Generated by `pracht typegen`. Do not edit manually.",
|
|
127
|
+
"import { createHref } from \"@pracht/core\";",
|
|
128
|
+
"import type { HrefRouteDefinition } from \"@pracht/core\";",
|
|
129
|
+
"",
|
|
130
|
+
"export const routes = ["
|
|
131
|
+
];
|
|
132
|
+
for (const route of routes) {
|
|
133
|
+
lines.push(" {");
|
|
134
|
+
lines.push(` id: ${JSON.stringify(route.id)},`);
|
|
135
|
+
lines.push(` path: ${JSON.stringify(route.path)},`);
|
|
136
|
+
lines.push(" },");
|
|
137
|
+
}
|
|
138
|
+
lines.push("] as const satisfies readonly HrefRouteDefinition[];");
|
|
139
|
+
lines.push("");
|
|
140
|
+
lines.push("export const href = createHref(routes);");
|
|
141
|
+
lines.push("");
|
|
142
|
+
return lines.join("\n");
|
|
143
|
+
}
|
|
144
|
+
function inferRouteParams(path) {
|
|
145
|
+
const params = [];
|
|
146
|
+
const seen = /* @__PURE__ */ new Set();
|
|
147
|
+
for (const segment of path.split("/").filter(Boolean)) {
|
|
148
|
+
let name = null;
|
|
149
|
+
if (segment === "*") name = "*";
|
|
150
|
+
else if (segment.startsWith(":")) name = segment.endsWith("*") ? segment.slice(1, -1) || "*" : segment.slice(1);
|
|
151
|
+
if (!name) continue;
|
|
152
|
+
if (seen.has(name)) throw new Error(`Route ${path} declares duplicate param "${name}".`);
|
|
153
|
+
seen.add(name);
|
|
154
|
+
params.push(name);
|
|
155
|
+
}
|
|
156
|
+
return params;
|
|
157
|
+
}
|
|
158
|
+
function formatParamsType(params) {
|
|
159
|
+
if (params.length === 0) return "Record<never, never>";
|
|
160
|
+
return `{ ${params.map((param) => `${JSON.stringify(param)}: RouteParamInput;`).join(" ")} }`;
|
|
161
|
+
}
|
|
162
|
+
function resolveOutputPath(root, outputPath) {
|
|
163
|
+
const absolutePath = isAbsolute(outputPath) ? outputPath : resolve(root, outputPath);
|
|
164
|
+
const relativePath = relative(root, absolutePath);
|
|
165
|
+
if (relativePath === "" || !relativePath.startsWith("..") && !isAbsolute(relativePath)) return absolutePath;
|
|
166
|
+
throw new Error(`Refusing to write outside the project root: ${outputPath}.`);
|
|
167
|
+
}
|
|
168
|
+
function fileMatches(path, source) {
|
|
169
|
+
return existsSync(path) && readFileSync(path, "utf-8") === ensureTrailingNewline(source);
|
|
170
|
+
}
|
|
171
|
+
//#endregion
|
|
172
|
+
export { typegen_default as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pracht/cli",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
|
+
"description": "CLI for developing, building, validating, inspecting, and scaffolding Pracht apps.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"pracht",
|
|
7
|
+
"preact",
|
|
8
|
+
"cli",
|
|
9
|
+
"vite",
|
|
10
|
+
"ssr",
|
|
11
|
+
"scaffold",
|
|
12
|
+
"inspect",
|
|
13
|
+
"doctor",
|
|
14
|
+
"framework"
|
|
15
|
+
],
|
|
4
16
|
"license": "MIT",
|
|
5
17
|
"homepage": "https://github.com/JoviDeCroock/pracht/tree/main/packages/cli",
|
|
6
18
|
"bugs": {
|
|
@@ -27,7 +39,7 @@
|
|
|
27
39
|
},
|
|
28
40
|
"dependencies": {
|
|
29
41
|
"citty": "^0.1.6",
|
|
30
|
-
"@pracht/core": "0.
|
|
42
|
+
"@pracht/core": "0.7.0"
|
|
31
43
|
},
|
|
32
44
|
"scripts": {
|
|
33
45
|
"build": "tsdown"
|
|
File without changes
|