@sealcode/sealgen 0.18.6 → 0.18.7

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.
@@ -1,7 +1,6 @@
1
1
  import { promises as fs } from "fs";
2
2
  import { extname, resolve } from "node:path";
3
3
  import { embeddable_file_extensions } from "../embeddable-file-extensions.js";
4
- import { compile } from "@sealcode/tempstream-esbuild";
5
4
  const rewrite_asset_imports_plugin = (project_dir) => ({
6
5
  name: "sealgen-rewrite-asset-imports",
7
6
  setup(build) {
@@ -31,9 +30,6 @@ const rewrite_asset_imports_plugin = (project_dir) => ({
31
30
  return replaced;
32
31
  }
33
32
  );
34
- if (extname(args.path) === ".tsx") {
35
- contents = compile(contents, args.path);
36
- }
37
33
  return {
38
34
  contents,
39
35
  loader: extname(args.path) === ".tsx" ? "tsx" : "ts"
@@ -34,9 +34,21 @@ function generateURL(actionName, url) {
34
34
  ${actionName}URL.rawURL = "${encodeURI(url)}";`;
35
35
  }
36
36
  function sortRoutes(urls) {
37
- return urls.sort(
38
- ({ url: url1 }, { url: url2 }) => url1.includes(":") ? 1 : url2.includes(":") ? -1 : url1 < url2 ? -1 : 1
39
- );
37
+ return urls.sort(({ url: url1 }, { url: url2 }) => {
38
+ const [segments1, segments2] = [url1, url2].map(
39
+ (url) => url.split("/")
40
+ );
41
+ if (segments1.length < segments2.length) {
42
+ return -1;
43
+ }
44
+ if (segments1.length > segments2.length) {
45
+ return 1;
46
+ }
47
+ const [encoded1, encoded2] = [url1, url2].map(
48
+ (url) => url.split("/").map((e) => e.startsWith(":") ? "1" : "0")
49
+ );
50
+ return encoded1 < encoded2 ? -1 : 1;
51
+ });
40
52
  }
41
53
  async function generateRoutes() {
42
54
  const files = await Promise.all(
@@ -1,4 +1,4 @@
1
- import { generateURL } from "./generate-routes.js";
1
+ import { generateURL, sortRoutes } from "./generate-routes.js";
2
2
  import * as prettier from "prettier";
3
3
  import { getPrettierConfig } from "./utils/prettier.js";
4
4
  import assert from "node:assert";
@@ -37,5 +37,29 @@ EditUserURL.rawURL = "/admin/u%C5%BCytkownicy/:id/edit/";`,
37
37
  )
38
38
  );
39
39
  });
40
+ it("sorts routes from most specific first to least specific last, considering directory structure", () => {
41
+ assert.deepStrictEqual(
42
+ sortRoutes(
43
+ [
44
+ "/home",
45
+ "/foo/:bar/:baz",
46
+ "/:param1/:param2/:param3",
47
+ "/foo/bar/baz",
48
+ "/foo/:bar",
49
+ "/:param1"
50
+ ].map((url) => ({
51
+ url
52
+ }))
53
+ ).map(({ url }) => url),
54
+ [
55
+ "/home",
56
+ "/:param1",
57
+ "/foo/:bar",
58
+ "/foo/bar/baz",
59
+ "/foo/:bar/:baz",
60
+ "/:param1/:param2/:param3"
61
+ ]
62
+ );
63
+ });
40
64
  });
41
65
  //# sourceMappingURL=generate-routes.test.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sealcode/sealgen",
3
- "version": "0.18.6",
3
+ "version": "0.18.7",
4
4
  "description": "Module to automate adding routes and collections to a sealious application",
5
5
  "main": "lib/index.js",
6
6
  "type": "module",
@@ -62,7 +62,6 @@
62
62
  "dependencies": {
63
63
  "@koa/router": "^12.0.1",
64
64
  "@sealcode/file-manager": "^1.0.2",
65
- "@sealcode/tempstream-esbuild": "^0.1.0",
66
65
  "@sealcode/ts-predicates": "^0.4.3",
67
66
  "chokidar": "^3.6.0",
68
67
  "colord": "^2.9.3",
@@ -2,7 +2,6 @@ import { promises as fs } from "fs";
2
2
  import { extname, resolve } from "node:path";
3
3
  import { embeddable_file_extensions } from "../embeddable-file-extensions.js";
4
4
  import { PluginBuild } from "esbuild";
5
- import { compile } from "@sealcode/tempstream-esbuild";
6
5
 
7
6
  export const rewrite_asset_imports_plugin = (project_dir: string) => ({
8
7
  name: "sealgen-rewrite-asset-imports",
@@ -34,9 +33,6 @@ export const rewrite_asset_imports_plugin = (project_dir: string) => ({
34
33
  }
35
34
  );
36
35
 
37
- if (extname(args.path) === ".tsx") {
38
- contents = compile(contents, args.path);
39
- }
40
36
  return {
41
37
  contents,
42
38
  loader: extname(args.path) === ".tsx" ? "tsx" : "ts",
@@ -1,4 +1,4 @@
1
- import { generateURL } from "./generate-routes.js";
1
+ import { generateURL, sortRoutes } from "./generate-routes.js";
2
2
  import * as prettier from "prettier";
3
3
  import { getPrettierConfig } from "./utils/prettier.js";
4
4
  import assert from "node:assert";
@@ -40,4 +40,29 @@ EditUserURL.rawURL = "/admin/u%C5%BCytkownicy/:id/edit/";`,
40
40
  )
41
41
  );
42
42
  });
43
+
44
+ it("sorts routes from most specific first to least specific last, considering directory structure", () => {
45
+ assert.deepStrictEqual(
46
+ sortRoutes(
47
+ [
48
+ "/home",
49
+ "/foo/:bar/:baz",
50
+ "/:param1/:param2/:param3",
51
+ "/foo/bar/baz",
52
+ "/foo/:bar",
53
+ "/:param1",
54
+ ].map((url) => ({
55
+ url,
56
+ }))
57
+ ).map(({ url }: { url: string }) => url),
58
+ [
59
+ "/home",
60
+ "/:param1",
61
+ "/foo/:bar",
62
+ "/foo/bar/baz",
63
+ "/foo/:bar/:baz",
64
+ "/:param1/:param2/:param3",
65
+ ]
66
+ );
67
+ });
43
68
  });
@@ -48,15 +48,24 @@ export function generateURL(actionName: string, url: string) {
48
48
  }
49
49
 
50
50
  export function sortRoutes<T extends { url: string }>(urls: T[]): T[] {
51
- return urls.sort(({ url: url1 }, { url: url2 }) =>
52
- url1.includes(":") // make sure the parametrized ones come later, so the more concrete ones show up first. This is not a full solution, but works for single-parameter urls
53
- ? 1
54
- : url2.includes(":")
55
- ? -1
56
- : url1 < url2
57
- ? -1
58
- : 1
59
- );
51
+ return urls.sort(({ url: url1 }, { url: url2 }) => {
52
+ const [segments1, segments2] = [url1, url2].map((url) =>
53
+ url.split("/")
54
+ );
55
+ if (segments1.length < segments2.length) {
56
+ return -1;
57
+ }
58
+ if (segments1.length > segments2.length) {
59
+ return 1;
60
+ }
61
+ // if they are the same length, map each url to a string like 00010
62
+ // where "0" is for static (so should be sorted earlier, and "1" is for
63
+ // dynamic parts
64
+ const [encoded1, encoded2] = [url1, url2].map((url) =>
65
+ url.split("/").map((e) => (e.startsWith(":") ? "1" : "0"))
66
+ );
67
+ return encoded1 < encoded2 ? -1 : 1;
68
+ });
60
69
  }
61
70
 
62
71
  export async function generateRoutes(): Promise<void> {