@mpen/rerouter 0.3.0 → 0.3.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.
Files changed (66) hide show
  1. package/README.md +4 -0
  2. package/{src → cli}/bin.test.ts +24 -2
  3. package/{src → cli}/bin.ts +27 -18
  4. package/cli/tsconfig.json +9 -0
  5. package/dist/acorn-k7ED_tOl.js +4968 -0
  6. package/dist/angular--Iqdw9UJ.js +4057 -0
  7. package/dist/babel-hfWAujRY.js +9878 -0
  8. package/dist/bin.d.ts +1 -1
  9. package/dist/bin.js +28 -23
  10. package/dist/estree-C1Zjnvlw.js +7266 -0
  11. package/dist/flow-BaD9LyIP.js +52912 -0
  12. package/dist/glimmer-CvCjW_1V.js +7541 -0
  13. package/dist/graphql-BdtzBuWh.js +1945 -0
  14. package/dist/html-DkZtUVbo.js +7137 -0
  15. package/dist/index.d.ts +19 -6
  16. package/dist/index.js +135 -27
  17. package/dist/markdown-Z8Vrc69e.js +6876 -0
  18. package/dist/meriyah-DeO4stuH.js +7590 -0
  19. package/dist/postcss-BmgGJ0E5.js +6777 -0
  20. package/dist/prettier-BT_F8kIx.js +15629 -0
  21. package/dist/typescript-DtIxStjy.js +22936 -0
  22. package/dist/yaml-CWOPBY0q.js +5281 -0
  23. package/examples/App.tsx +18 -49
  24. package/examples/dist/BlogPost-c10d9w2p.js +1 -0
  25. package/examples/dist/FetchLoading-534mdrgz.js +1 -0
  26. package/examples/dist/FetchLoading-sbxbdkre.js +1 -0
  27. package/examples/dist/Home-a1258p25.js +1 -0
  28. package/examples/dist/KitchenSink-821mjg0h.js +1 -0
  29. package/examples/dist/Login-wywx6bp7.js +1 -0
  30. package/examples/dist/Match-1e72jm5w.js +1 -0
  31. package/examples/dist/NotFound-smxj24jw.js +1 -0
  32. package/examples/dist/SlowLoading-59xxmbfk.js +1 -0
  33. package/examples/dist/index-0d4kj0rv.js +2 -0
  34. package/examples/dist/index-3x197sbt.js +9 -0
  35. package/examples/dist/index-a2hkfx1n.js +9 -0
  36. package/examples/dist/index-d21me1mc.js +9 -0
  37. package/examples/dist/index-ktqdknsn.js +2 -0
  38. package/examples/dist/index-p53qxxzd.js +2 -0
  39. package/examples/dist/index.html +67 -0
  40. package/examples/routes.gen.ts +66 -86
  41. package/examples/routes.ts +2 -2
  42. package/examples/server/serve-dist.ts +33 -0
  43. package/examples/server/tsconfig.json +9 -0
  44. package/package.json +11 -6
  45. package/src/components/Link.tsx +8 -6
  46. package/src/components/Router.test.tsx +183 -0
  47. package/src/components/Router.tsx +161 -29
  48. package/src/lib/routes.ts +2 -0
  49. package/tsconfig.json +3 -2
  50. package/tsdown.config.ts +3 -4
  51. package/dist/hooks-Dlwcb0sV.js +0 -20
  52. package/dist/hooks.d.ts +0 -2
  53. package/dist/hooks.js +0 -2
  54. package/dist/index-BYXpNitc.d.ts +0 -5
  55. /package/{src → cli}/fixtures/bin/kitchen-sink.tsx +0 -0
  56. /package/{src → cli}/fixtures/bin/optional.tsx +0 -0
  57. /package/{src → cli}/fixtures/bin/pages/Home.tsx +0 -0
  58. /package/{src → cli}/fixtures/bin/pages/KitchenSink.tsx +0 -0
  59. /package/{src → cli}/fixtures/bin/pages/Login.tsx +0 -0
  60. /package/{src → cli}/fixtures/bin/pages/Match.tsx +0 -0
  61. /package/{src → cli}/fixtures/bin/pages/NotFound.tsx +0 -0
  62. /package/{src → cli}/fixtures/bin/pages/Optional.tsx +0 -0
  63. /package/{src → cli}/fixtures/bin/regexp-groups.tsx +0 -0
  64. /package/{src → cli}/fixtures/bin/simple.tsx +0 -0
  65. /package/{src → cli}/fixtures/bin/unnamed.tsx +0 -0
  66. /package/dist/{routes-Hpf6cwcZ.js → routes-PW-bNm8e.js} +0 -0
package/dist/bin.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- //#region src/bin.d.ts
1
+ //#region cli/bin.d.ts
2
2
  type RunOptions = {
3
3
  cwd?: string;
4
4
  commandName?: string;
package/dist/bin.js CHANGED
@@ -1,12 +1,12 @@
1
1
  #!/usr/bin/env node
2
- import { n as normalizeRoutes, t as normalizeLegacyPathToRegexpSyntax } from "./routes-Hpf6cwcZ.js";
2
+ import { n as normalizeRoutes, t as normalizeLegacyPathToRegexpSyntax } from "./routes-PW-bNm8e.js";
3
3
  import { parse } from "path-to-regexp";
4
4
  import fs from "node:fs/promises";
5
5
  import path from "node:path";
6
6
  import process from "node:process";
7
7
  import { fileURLToPath, pathToFileURL } from "node:url";
8
8
  import { parseArgs } from "node:util";
9
- //#region src/bin.ts
9
+ //#region cli/bin.ts
10
10
  const PARSE_CONFIG = {
11
11
  options: {
12
12
  output: {
@@ -17,6 +17,10 @@ const PARSE_CONFIG = {
17
17
  type: "boolean",
18
18
  short: "w"
19
19
  },
20
+ pretty: {
21
+ type: "boolean",
22
+ short: "p"
23
+ },
20
24
  "wildcard-delimiter": { type: "string" },
21
25
  "encode-function": { type: "string" }
22
26
  },
@@ -39,6 +43,7 @@ function compilePathGenerator(pattern, { delimiter = "/", encode = "encodeURICom
39
43
  name,
40
44
  type: typeOfParam(t)
41
45
  });
46
+ const renderPropsType = (props) => props.length ? `{ ${props.map((p) => `${escapeString(p.name)}: ${p.type}`).join("; ")} }` : "{}";
42
47
  function collectGroupProps(ts2) {
43
48
  const props = [];
44
49
  for (const t of ts2) if (t.type === "param" || t.type === "wildcard") props.push(makeProp(t.name, t));
@@ -49,23 +54,12 @@ function compilePathGenerator(pattern, { delimiter = "/", encode = "encodeURICom
49
54
  for (const t of ts2) if ((t.type === "param" || t.type === "wildcard") && intoBase) baseProps.push(makeProp(t.name, t));
50
55
  else if (t.type === "group") {
51
56
  const groupProps = collectGroupProps(t.tokens);
52
- if (groupProps.length) {
53
- const some = [
54
- "{",
55
- ...groupProps.map((p) => ` ${escapeString(p.name)}: ${p.type}`),
56
- "}"
57
- ].join("\n");
58
- groupTypes.push(`AllOrNone<${some}>`);
59
- }
57
+ if (groupProps.length) groupTypes.push(`AllOrNone<${renderPropsType(groupProps)}>`);
60
58
  collectTypes(t.tokens, false);
61
59
  }
62
60
  }
63
61
  collectTypes(tokens);
64
- const baseParamsType = [
65
- "{",
66
- ...baseProps.map((p) => ` ${escapeString(p.name)}: ${p.type}`),
67
- "}"
68
- ].join("\n");
62
+ const baseParamsType = renderPropsType(baseProps);
69
63
  const paramsType = groupTypes.length ? `${baseParamsType} & ${groupTypes.join(" & ")}` : baseParamsType;
70
64
  const lines = [];
71
65
  const indentUnit = " ";
@@ -74,11 +68,8 @@ function compilePathGenerator(pattern, { delimiter = "/", encode = "encodeURICom
74
68
  else lines.push(indentUnit.repeat(indentLevel) + text);
75
69
  };
76
70
  const hasAnyParams = baseProps.length > 0 || groupTypes.length > 0;
77
- if (hasAnyParams) {
78
- lines.push(`export function ${functionName}(`);
79
- lines.push(` params: ${paramsType}`);
80
- lines.push(`): string {`);
81
- } else lines.push(`export function ${functionName}(): string {`);
71
+ if (hasAnyParams) lines.push(`export function ${functionName}(params: ${paramsType}): string {`);
72
+ else lines.push(`export function ${functionName}(): string {`);
82
73
  line(1, `let sb = ""`);
83
74
  line(0);
84
75
  const delim = escapeString(delimiter);
@@ -122,6 +113,19 @@ async function importRoutes(routesPath) {
122
113
  if (!Array.isArray(mod.default)) throw new Error("Routes file must default export an array of routes.");
123
114
  return mod.default;
124
115
  }
116
+ async function formatWithPrettier(source, outputPath) {
117
+ let prettier;
118
+ try {
119
+ prettier = await import("./prettier-BT_F8kIx.js");
120
+ } catch (cause) {
121
+ throw new Error("The --pretty option requires prettier to be installed.", { cause });
122
+ }
123
+ const options = await prettier.resolveConfig(outputPath) ?? {};
124
+ return prettier.format(source, {
125
+ ...options,
126
+ filepath: outputPath
127
+ });
128
+ }
125
129
  function extractRoutes(routes) {
126
130
  return normalizeRoutes(routes).flatMap((route) => {
127
131
  if (!route.name || typeof route.pattern !== "string") return [];
@@ -136,7 +140,7 @@ async function main(options, positionals, { cwd = process.cwd(), commandName = "
136
140
  if (!routesPathArg) return {
137
141
  exitCode: 1,
138
142
  stdout: "",
139
- stderr: "Usage: rerouter <routes-file> [-o <output-file>] [-w] [--wildcard-delimiter <string>] [--encode-function <identifier>]\n"
143
+ stderr: "Usage: rerouter <routes-file> [-o <output-file>] [-w] [-p|--pretty] [--wildcard-delimiter <string>] [--encode-function <identifier>]\n"
140
144
  };
141
145
  const routesPath = path.resolve(cwd, routesPathArg);
142
146
  let outputPath;
@@ -179,12 +183,13 @@ async function main(options, positionals, { cwd = process.cwd(), commandName = "
179
183
  out.push(``);
180
184
  }
181
185
  }
182
- const finalOutput = out.join("\n");
186
+ let finalOutput = out.join("\n");
183
187
  if (outputPath) {
188
+ if (options.pretty) finalOutput = await formatWithPrettier(finalOutput, outputPath);
184
189
  await fs.writeFile(outputPath, finalOutput, "utf8");
185
190
  return {
186
191
  stdout: "",
187
- stderr: `Wrote ${outputPath}\n`
192
+ stderr: `Wrote ${path.relative(cwd, outputPath) || "."}\n`
188
193
  };
189
194
  } else return {
190
195
  stdout: finalOutput,