ts-file-router 4.0.0 → 4.0.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.
@@ -1 +1 @@
1
- {"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../src/generator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAe,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAMrE,eAAO,MAAM,cAAc,GAAI,qDAK5B,qBAAqB,SAyHvB,CAAC"}
1
+ {"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../src/generator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAe,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAMrE,eAAO,MAAM,cAAc,GAAI,qDAK5B,qBAAqB,SA6HvB,CAAC"}
package/dist/generator.js CHANGED
@@ -25,7 +25,11 @@ export const generateRoutes = ({ baseFolder, outputFile, routeFileName = 'page.t
25
25
  // Path to browser sync if necessary
26
26
  const relativePath = '/' + path.relative(basePath, dir).replaceAll(/\\/g, '/');
27
27
  // Normalize import path to esm pattern
28
- const importPath = './' + path.relative(basePath, fullPath).replaceAll(/\\/g, '/');
28
+ const importPath = './' +
29
+ path
30
+ .relative(basePath, fullPath)
31
+ .replaceAll(/\\/gi, '/')
32
+ .replaceAll(/.(tsx|ts|jsx|js)/gi, '');
29
33
  // Remove extension from file to naming the route
30
34
  const key = path.basename(file, path.extname(file));
31
35
  if (dirInfo.isDirectory()) {
@@ -49,7 +53,7 @@ export const generateRoutes = ({ baseFolder, outputFile, routeFileName = 'page.t
49
53
  // Create routes
50
54
  const routes = await mapRoutes(basePath);
51
55
  // Create ts file
52
- serialize(routes, output);
56
+ await serialize(routes, output);
53
57
  // Promise writeFile was successfully resolved
54
58
  console.log('🚀 Routes generated successfully!\n');
55
59
  if (options.exitCodeOnResolution) {
@@ -1 +1 @@
1
- {"version":3,"file":"vite.d.ts","sourceRoot":"","sources":["../../src/plugins/vite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AACnC,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAGpD,eAAO,MAAM,oBAAoB,GAAI,sCAIlC,qBAAqB,KAAG,MAY1B,CAAC"}
1
+ {"version":3,"file":"vite.d.ts","sourceRoot":"","sources":["../../src/plugins/vite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AACnC,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAGpD,eAAO,MAAM,oBAAoB,GAAI,sCAOlC,qBAAqB,KAAG,MAY1B,CAAC"}
@@ -1,5 +1,8 @@
1
1
  import { generateRoutes } from '../generator.js';
2
- export const generateRoutesPlugin = ({ baseFolder, outputFile, options = { watcher: { debounce: 500 }, exitCodeOnResolution: false }, }) => {
2
+ export const generateRoutesPlugin = ({ baseFolder, outputFile, options = {
3
+ watcher: { watch: true, debounce: 500 },
4
+ exitCodeOnResolution: false,
5
+ }, }) => {
3
6
  return {
4
7
  name: 'file-router-plugin',
5
8
  apply: 'serve',
@@ -1,3 +1,3 @@
1
1
  import type { TRoutesTree } from './types.js';
2
- export declare const serialize: (routes: TRoutesTree, outputPath: string) => void;
2
+ export declare const serialize: (routes: TRoutesTree, outputPath: string) => Promise<void>;
3
3
  //# sourceMappingURL=serialize.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"serialize.d.ts","sourceRoot":"","sources":["../src/serialize.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAsB9C,eAAO,MAAM,SAAS,GAAI,QAAQ,WAAW,EAAE,YAAY,MAAM,SAMhE,CAAC"}
1
+ {"version":3,"file":"serialize.d.ts","sourceRoot":"","sources":["../src/serialize.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AA2C9C,eAAO,MAAM,SAAS,GAAU,QAAQ,WAAW,EAAE,YAAY,MAAM,kBActE,CAAC"}
package/dist/serialize.js CHANGED
@@ -1,21 +1,45 @@
1
+ import { Biome, Distribution } from '@biomejs/js-api';
1
2
  import * as fs from 'node:fs';
2
3
  import * as path from 'node:path';
3
- const stringifyTS = (obj, indent = 0) => {
4
- const spaces = ' '.repeat(indent);
5
- const innerSpaces = ' '.repeat(indent + 2);
6
- if (typeof obj !== 'object' || obj === null) {
7
- return JSON.stringify(obj);
8
- }
4
+ const stringifyRoutes = (obj) => {
9
5
  const entries = Object.entries(obj).map(([key, value]) => {
10
- const validKey = /^[a-z_$][a-z0-9_$]*$/i.test(key)
11
- ? key
12
- : JSON.stringify(key);
13
- return `${innerSpaces}${validKey}: ${stringifyTS(value, indent + 2)}`;
6
+ if (typeof value === 'object' &&
7
+ value !== null &&
8
+ 'path' in value &&
9
+ 'import' in value) {
10
+ return `'${key}': {
11
+ path: '${value.path}',
12
+ import: () => import('${value.import}')
13
+ }`;
14
+ }
15
+ return `'${key}': ${stringifyRoutes(value)}`;
16
+ });
17
+ return `{ ${entries.join(',\n')} }`;
18
+ };
19
+ const format = async (filePath, content) => {
20
+ const biome = await Biome.create({ distribution: Distribution.NODE });
21
+ const project = biome.openProject();
22
+ biome.applyConfiguration(project.projectKey, {
23
+ formatter: { enabled: true, indentStyle: 'space', lineWidth: 100 },
24
+ javascript: { formatter: { quoteStyle: 'single' } },
14
25
  });
15
- return `{\n${entries.join(',\n')}\n${spaces}}`;
26
+ const formatted = biome.formatContent(project.projectKey, content, {
27
+ filePath: path.basename(filePath),
28
+ });
29
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
30
+ fs.writeFileSync(filePath, formatted.content, 'utf8');
16
31
  };
17
- export const serialize = (routes, outputPath) => {
18
- const content = `export const routes = ${stringifyTS(routes)} as const;\n`;
19
- fs.mkdirSync(path.dirname(outputPath), { recursive: true });
20
- fs.writeFileSync(outputPath, content, 'utf8');
32
+ export const serialize = async (routes, outputPath) => {
33
+ const rawCode = `
34
+ // GENERATED WITH TS-FILE-ROUTER DO NOT EDIT
35
+
36
+ export const routes = ${stringifyRoutes(routes)} as const;
37
+ \n`;
38
+ try {
39
+ await format(path.resolve(outputPath), rawCode);
40
+ console.log('✨ File was parsed succesfully');
41
+ }
42
+ catch (err) {
43
+ console.error('❌ Error parsing file:\n', err);
44
+ }
21
45
  };
package/dist/types.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  type TWatcherConfig = {
2
+ watch: boolean;
2
3
  debounce?: number;
3
4
  };
4
5
  type TGenerateRoutesOptions = {
@@ -11,12 +12,12 @@ export type TGenerateRoutesConfig = {
11
12
  routeFileName?: string;
12
13
  options?: TGenerateRoutesOptions;
13
14
  };
14
- export type TRoute = {
15
+ export type TRouteLeaf = {
15
16
  path: string;
16
17
  import: string;
17
18
  };
18
19
  export type TRoutesTree = {
19
- [key: string]: TRoute | TRoutesTree;
20
+ [key: string]: TRouteLeaf | TRoutesTree;
20
21
  };
21
22
  export {};
22
23
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,KAAK,cAAc,GAAG;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,KAAK,sBAAsB,GAAG;IAC5B,OAAO,CAAC,EAAE,cAAc,CAAC;IACzB,oBAAoB,CAAC,EAAE,OAAO,CAAC;CAChC,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IAClC,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,OAAO,CAAC,EAAE,sBAAsB,CAAC;CAClC,CAAC;AAEF,MAAM,MAAM,MAAM,GAAG;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,WAAW,GAAG;IACxB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,WAAW,CAAC;CACrC,CAAC"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,KAAK,cAAc,GAAG;IACpB,KAAK,EAAE,OAAO,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,KAAK,sBAAsB,GAAG;IAC5B,OAAO,CAAC,EAAE,cAAc,CAAC;IACzB,oBAAoB,CAAC,EAAE,OAAO,CAAC;CAChC,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IAClC,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,OAAO,CAAC,EAAE,sBAAsB,CAAC;CAClC,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,WAAW,GAAG;IACxB,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,GAAG,WAAW,CAAC;CACzC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-file-router",
3
- "version": "4.0.0",
3
+ "version": "4.0.1",
4
4
  "description": "router based on project files using typescript",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",