ts-file-router 4.1.1 → 5.0.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.
@@ -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;AAarE,eAAO,MAAM,cAAc,GAAI,qDAK5B,qBAAqB,SAiJvB,CAAC"}
1
+ {"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../src/generator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAe,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAgBrE,eAAO,MAAM,cAAc,GAAI,qDAK5B,qBAAqB,SA2IvB,CAAC"}
package/dist/generator.js CHANGED
@@ -5,6 +5,7 @@ const getIgnoredOutputFile = (file, output) => file.includes(output);
5
5
  const getIgnoredFiles = (file, output) => file.includes('index') ||
6
6
  file.startsWith('_') ||
7
7
  getIgnoredOutputFile(file, output);
8
+ const cleanPaths = (path) => path.replaceAll(/\\/gi, '/').replaceAll(/.(tsx|ts|jsx|js)/gi, '');
8
9
  export const generateRoutes = ({ baseFolder, outputFile, routeFileName = 'page.tsx', options = { exitCodeOnResolution: true }, }) => {
9
10
  // Get the pages dir to resolve routes
10
11
  const basePath = path.resolve(process.cwd(), baseFolder);
@@ -25,13 +26,9 @@ export const generateRoutes = ({ baseFolder, outputFile, routeFileName = 'page.t
25
26
  // Get directory info to control file or folder
26
27
  const dirInfo = await fs.stat(fullPath);
27
28
  // Path to browser sync if necessary
28
- const relativePath = '/' + path.relative(basePath, dir).replaceAll(/\\/g, '/');
29
+ const relativePath = '/' + path.relative(basePath, dir);
29
30
  // Normalize import path to esm pattern
30
- const importPath = './' +
31
- path
32
- .relative(basePath, fullPath)
33
- .replaceAll(/\\/gi, '/')
34
- .replaceAll(/.(tsx|ts|jsx|js)/gi, '');
31
+ const importPath = './' + path.relative(basePath, fullPath);
35
32
  // Remove extension from file to naming the route
36
33
  const key = path.basename(file, path.extname(file));
37
34
  if (dirInfo.isDirectory()) {
@@ -44,8 +41,8 @@ export const generateRoutes = ({ baseFolder, outputFile, routeFileName = 'page.t
44
41
  // Mount the route object with path like "/folder" and import
45
42
  // import will be like "(./baseFolder/file or ./baseFolder/folders).extension"
46
43
  routes[key] = {
47
- path: relativePath,
48
- import: importPath,
44
+ path: cleanPaths(relativePath),
45
+ import: cleanPaths(importPath),
49
46
  };
50
47
  }
51
48
  return routes;
@@ -104,7 +101,7 @@ export const generateRoutes = ({ baseFolder, outputFile, routeFileName = 'page.t
104
101
  const ignoredGeneralFiles = getIgnoredFiles(file, outputFile);
105
102
  // If added, or change (renamed) or deleted, update routes
106
103
  if (watchOnEvents.includes(ev) && !ignoredGeneralFiles) {
107
- console.log(`👀 Watching files from path: "${file}" for changes...`);
104
+ console.log(`🔎 Watching files from path: "${file}" for changes...`);
108
105
  runFromWatcher();
109
106
  }
110
107
  });
@@ -1 +1 @@
1
- {"version":3,"file":"serialize.d.ts","sourceRoot":"","sources":["../src/serialize.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AA+C9C,eAAO,MAAM,SAAS,GAAU,QAAQ,WAAW,EAAE,YAAY,MAAM,kBActE,CAAC"}
1
+ {"version":3,"file":"serialize.d.ts","sourceRoot":"","sources":["../src/serialize.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAc,WAAW,EAAE,MAAM,YAAY,CAAC;AAmD1D,eAAO,MAAM,SAAS,GAAU,QAAQ,WAAW,EAAE,YAAY,MAAM,kBActE,CAAC"}
package/dist/serialize.js CHANGED
@@ -1,15 +1,18 @@
1
1
  import { Biome, Distribution } from '@biomejs/js-api';
2
2
  import * as fs from 'node:fs';
3
3
  import * as path from 'node:path';
4
+ const isRouteLeaf = (value) => {
5
+ return (typeof value === 'object' &&
6
+ value !== null &&
7
+ 'path' in value &&
8
+ 'import' in value);
9
+ };
4
10
  const stringifyRoutes = (obj) => {
5
11
  const entries = Object.entries(obj).map(([key, value]) => {
6
- if (typeof value === 'object' &&
7
- value !== null &&
8
- 'path' in value &&
9
- 'import' in value) {
12
+ if (isRouteLeaf(value)) {
10
13
  return `'${key}': {
11
14
  path: '${value.path}',
12
- import: () => import('${value.import}')
15
+ import: import('${value.import}')
13
16
  }`;
14
17
  }
15
18
  return `'${key}': ${stringifyRoutes(value)}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-file-router",
3
- "version": "4.1.1",
3
+ "version": "5.0.0",
4
4
  "description": "router based on project files using typescript",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",