ts-file-router 4.0.2 → 4.1.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,SA6HvB,CAAC"}
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"}
package/dist/generator.js CHANGED
@@ -1,6 +1,10 @@
1
+ import { serialize } from './serialize.js';
1
2
  import fs from 'fs/promises';
2
3
  import path from 'path';
3
- import { serialize } from './serialize.js';
4
+ const getIgnoredOutputFile = (file, output) => file.includes(output);
5
+ const getIgnoredFiles = (file, output) => file.includes('index') ||
6
+ file.startsWith('_') ||
7
+ getIgnoredOutputFile(file, output);
4
8
  export const generateRoutes = ({ baseFolder, outputFile, routeFileName = 'page.tsx', options = { exitCodeOnResolution: true }, }) => {
5
9
  // Get the pages dir to resolve routes
6
10
  const basePath = path.resolve(process.cwd(), baseFolder);
@@ -14,9 +18,7 @@ export const generateRoutes = ({ baseFolder, outputFile, routeFileName = 'page.t
14
18
  }
15
19
  for (const file of directory) {
16
20
  // ignore index files, underscore marked, or route file generated
17
- if (file.includes('index') ||
18
- file.startsWith('_') ||
19
- file.includes(outputFile)) {
21
+ if (getIgnoredFiles(file, outputFile)) {
20
22
  continue;
21
23
  }
22
24
  const fullPath = path.join(dir, file);
@@ -73,11 +75,10 @@ export const generateRoutes = ({ baseFolder, outputFile, routeFileName = 'page.t
73
75
  const { watch } = await import('chokidar');
74
76
  let timeoutId;
75
77
  const watcher = watch(baseFolder, {
76
- ignoreInitial: true,
77
- ignored: `${baseFolder}/${outputFile}`,
78
+ ignoreInitial: false,
78
79
  persistent: true,
79
80
  });
80
- console.log(`👀 Watching folder: ${baseFolder} for changes...`);
81
+ console.log(`👀 Watching folder: "${baseFolder}" for changes...`);
81
82
  const runFromWatcher = () => {
82
83
  if (timeoutId) {
83
84
  clearTimeout(timeoutId);
@@ -86,9 +87,27 @@ export const generateRoutes = ({ baseFolder, outputFile, routeFileName = 'page.t
86
87
  createRoutes();
87
88
  }, debounce);
88
89
  };
89
- watcher.on('add', runFromWatcher);
90
- watcher.on('unlink', runFromWatcher);
91
- watcher.on('unlinkDir', runFromWatcher);
90
+ watcher.on('all', (ev, file) => {
91
+ const ignoredOuput = getIgnoredOutputFile(file, outputFile);
92
+ // If output file as deleted regenerate it
93
+ if (ignoredOuput && ev === 'unlink') {
94
+ console.log(`🚨 ${outputFile} was deleted, regenerating...`);
95
+ runFromWatcher();
96
+ return;
97
+ }
98
+ const watchOnEvents = [
99
+ 'add',
100
+ 'change',
101
+ 'unlink',
102
+ 'unlinkDir',
103
+ ];
104
+ const ignoredGeneralFiles = getIgnoredFiles(file, outputFile);
105
+ // If added, or change (renamed) or deleted, update routes
106
+ if (watchOnEvents.includes(ev) && !ignoredGeneralFiles) {
107
+ console.log(`👀 Watching files from path: "${file}" for changes...`);
108
+ runFromWatcher();
109
+ }
110
+ });
92
111
  return () => watcher.close();
93
112
  };
94
113
  if (!!options.watcher) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-file-router",
3
- "version": "4.0.2",
3
+ "version": "4.1.1",
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",