ts-file-router 4.1.0 → 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;AAKrE,eAAO,MAAM,cAAc,GAAI,qDAK5B,qBAAqB,SAqJvB,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
@@ -1,6 +1,11 @@
1
1
  import { serialize } from './serialize.js';
2
2
  import fs from 'fs/promises';
3
3
  import path from 'path';
4
+ const getIgnoredOutputFile = (file, output) => file.includes(output);
5
+ const getIgnoredFiles = (file, output) => file.includes('index') ||
6
+ file.startsWith('_') ||
7
+ getIgnoredOutputFile(file, output);
8
+ const cleanPaths = (path) => path.replaceAll(/\\/gi, '/').replaceAll(/.(tsx|ts|jsx|js)/gi, '');
4
9
  export const generateRoutes = ({ baseFolder, outputFile, routeFileName = 'page.tsx', options = { exitCodeOnResolution: true }, }) => {
5
10
  // Get the pages dir to resolve routes
6
11
  const basePath = path.resolve(process.cwd(), baseFolder);
@@ -14,22 +19,16 @@ export const generateRoutes = ({ baseFolder, outputFile, routeFileName = 'page.t
14
19
  }
15
20
  for (const file of directory) {
16
21
  // ignore index files, underscore marked, or route file generated
17
- if (file.includes('index') ||
18
- file.startsWith('_') ||
19
- file.includes(outputFile)) {
22
+ if (getIgnoredFiles(file, outputFile)) {
20
23
  continue;
21
24
  }
22
25
  const fullPath = path.join(dir, file);
23
26
  // Get directory info to control file or folder
24
27
  const dirInfo = await fs.stat(fullPath);
25
28
  // Path to browser sync if necessary
26
- const relativePath = '/' + path.relative(basePath, dir).replaceAll(/\\/g, '/');
29
+ const relativePath = '/' + path.relative(basePath, dir);
27
30
  // Normalize import path to esm pattern
28
- const importPath = './' +
29
- path
30
- .relative(basePath, fullPath)
31
- .replaceAll(/\\/gi, '/')
32
- .replaceAll(/.(tsx|ts|jsx|js)/gi, '');
31
+ const importPath = './' + path.relative(basePath, fullPath);
33
32
  // Remove extension from file to naming the route
34
33
  const key = path.basename(file, path.extname(file));
35
34
  if (dirInfo.isDirectory()) {
@@ -42,8 +41,8 @@ export const generateRoutes = ({ baseFolder, outputFile, routeFileName = 'page.t
42
41
  // Mount the route object with path like "/folder" and import
43
42
  // import will be like "(./baseFolder/file or ./baseFolder/folders).extension"
44
43
  routes[key] = {
45
- path: relativePath,
46
- import: importPath,
44
+ path: cleanPaths(relativePath),
45
+ import: cleanPaths(importPath),
47
46
  };
48
47
  }
49
48
  return routes;
@@ -71,26 +70,24 @@ export const generateRoutes = ({ baseFolder, outputFile, routeFileName = 'page.t
71
70
  };
72
71
  const watcher = async ({ debounce = 500 }) => {
73
72
  const { watch } = await import('chokidar');
74
- const output = path.resolve(baseFolder, outputFile);
75
73
  let timeoutId;
76
74
  const watcher = watch(baseFolder, {
77
75
  ignoreInitial: false,
78
76
  persistent: true,
79
77
  });
80
- console.log(`👀 Watching folder: ${baseFolder} for changes...`);
78
+ console.log(`👀 Watching folder: "${baseFolder}" for changes...`);
81
79
  const runFromWatcher = () => {
82
80
  if (timeoutId) {
83
81
  clearTimeout(timeoutId);
84
82
  }
85
83
  timeoutId = setTimeout(() => {
86
- console.log(`👀 Files changed, regenerating routes...`);
87
84
  createRoutes();
88
85
  }, debounce);
89
86
  };
90
87
  watcher.on('all', (ev, file) => {
91
- const resolvedPath = path.resolve(file);
88
+ const ignoredOuput = getIgnoredOutputFile(file, outputFile);
92
89
  // If output file as deleted regenerate it
93
- if (resolvedPath === output && ev === 'unlink') {
90
+ if (ignoredOuput && ev === 'unlink') {
94
91
  console.log(`🚨 ${outputFile} was deleted, regenerating...`);
95
92
  runFromWatcher();
96
93
  return;
@@ -101,8 +98,10 @@ export const generateRoutes = ({ baseFolder, outputFile, routeFileName = 'page.t
101
98
  'unlink',
102
99
  'unlinkDir',
103
100
  ];
104
- // If added, or change (renamed) or deleted update de routes
105
- if (watchOnEvents.includes(ev) && resolvedPath !== output) {
101
+ const ignoredGeneralFiles = getIgnoredFiles(file, outputFile);
102
+ // If added, or change (renamed) or deleted, update routes
103
+ if (watchOnEvents.includes(ev) && !ignoredGeneralFiles) {
104
+ console.log(`🔎 Watching files from path: "${file}" for changes...`);
106
105
  runFromWatcher();
107
106
  }
108
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.0",
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",
@@ -11,7 +11,8 @@
11
11
  "scripts": {
12
12
  "start": "tsx ./src/generator.ts",
13
13
  "prebuild": "rimraf dist",
14
- "build": "tsc"
14
+ "build": "tsc",
15
+ "publish:package": "npm run build && npm publish"
15
16
  },
16
17
  "author": "MatheusF10",
17
18
  "license": "ISC",