ts-file-router 1.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.
- package/dist/file-router.d.ts +3 -0
- package/dist/file-router.d.ts.map +1 -0
- package/dist/file-router.js +84 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/types.d.ts +13 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/package.json +20 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-router.d.ts","sourceRoot":"","sources":["../src/file-router.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAe,MAAM,SAAS,CAAC;AA+BrD,eAAO,MAAM,KAAK,GAAI,4CAInB,aAAa,SAsEf,CAAC"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.start = void 0;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const promises_1 = __importDefault(require("fs/promises"));
|
|
9
|
+
// Serialize routes objects to TS file output
|
|
10
|
+
const serializeRoutes = (obj, ident = 2) => {
|
|
11
|
+
const pad = ' '.repeat(ident);
|
|
12
|
+
let str = '{\n';
|
|
13
|
+
for (const key in obj) {
|
|
14
|
+
const value = obj[key];
|
|
15
|
+
// Manipulate JS keys
|
|
16
|
+
const keyStr = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key) ? key : `"${key}"`;
|
|
17
|
+
if (typeof value === 'object' &&
|
|
18
|
+
value !== null &&
|
|
19
|
+
!('path' in value && 'import' in value)) {
|
|
20
|
+
// Create sub folders
|
|
21
|
+
str += `${pad}${keyStr}: ${serializeRoutes(value, ident + 2)},\n`;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
// Routes
|
|
25
|
+
str += `${pad}${keyStr}: { path: "${value.path}", import: "${value.import}" },\n`;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
str += ' '.repeat(ident - 2) + '}';
|
|
29
|
+
return str;
|
|
30
|
+
};
|
|
31
|
+
const start = ({ baseFolder, outputFile, routeFileName = 'page.ts', }) => {
|
|
32
|
+
// Get the pages dir to resolve routes
|
|
33
|
+
const basePath = path_1.default.resolve(__dirname, baseFolder);
|
|
34
|
+
// Output file for routes
|
|
35
|
+
const output = path_1.default.resolve(__dirname, outputFile);
|
|
36
|
+
const generateRouter = async () => {
|
|
37
|
+
const mapRoutes = async (dir) => {
|
|
38
|
+
const routes = {};
|
|
39
|
+
const directory = await promises_1.default.readdir(dir);
|
|
40
|
+
if (!directory.includes(routeFileName)) {
|
|
41
|
+
throw new Error(`Invalid pages structure: The folder "${dir}" must contain a ${routeFileName} file.`);
|
|
42
|
+
}
|
|
43
|
+
for (const file of directory) {
|
|
44
|
+
const fullPath = path_1.default.join(dir, file);
|
|
45
|
+
// Get directory info to control file or folder
|
|
46
|
+
const dirInfo = await promises_1.default.stat(fullPath);
|
|
47
|
+
// Normalize import path to esm pattern
|
|
48
|
+
const importPath = './' + path_1.default.relative(basePath, fullPath).replaceAll(/\\/g, '/');
|
|
49
|
+
const relativePath = './' + path_1.default.relative(basePath, dir).replaceAll(/\\/g, '/');
|
|
50
|
+
// Remove extension from file to naming the route
|
|
51
|
+
const key = file.replace(/\.ts$/, '');
|
|
52
|
+
if (dirInfo.isDirectory()) {
|
|
53
|
+
// Dev friendly when enter in this conditional file is a directory(folder)
|
|
54
|
+
const directory = file;
|
|
55
|
+
// Recursion for sub directories
|
|
56
|
+
routes[directory] = await mapRoutes(fullPath);
|
|
57
|
+
// Skip this directory iteration
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
routes[key] = {
|
|
61
|
+
path: relativePath,
|
|
62
|
+
import: importPath,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return routes;
|
|
66
|
+
};
|
|
67
|
+
try {
|
|
68
|
+
// Create routes
|
|
69
|
+
const routes = await mapRoutes(basePath);
|
|
70
|
+
// Cria arquivo TS exportando JSON
|
|
71
|
+
const tsContent = `// AUTO-GENERATED ROUTES\n\nconst routes = ${serializeRoutes(routes)};\n\nexport default routes;\n`;
|
|
72
|
+
// Write .ts file
|
|
73
|
+
await promises_1.default.writeFile(output, tsContent, 'utf-8');
|
|
74
|
+
console.log('🚀 Routes generated successfully!\n', output);
|
|
75
|
+
process.exit(0);
|
|
76
|
+
}
|
|
77
|
+
catch (err) {
|
|
78
|
+
console.error('❌ Error generating routes:', err);
|
|
79
|
+
process.exit(1);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
generateRouter();
|
|
83
|
+
};
|
|
84
|
+
exports.start = start;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,cAAc,SAAS,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.start = void 0;
|
|
18
|
+
var file_router_1 = require("./file-router");
|
|
19
|
+
Object.defineProperty(exports, "start", { enumerable: true, get: function () { return file_router_1.start; } });
|
|
20
|
+
__exportStar(require("./types"), exports);
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type TStartConfigs = {
|
|
2
|
+
baseFolder: string;
|
|
3
|
+
outputFile: string;
|
|
4
|
+
routeFileName?: string;
|
|
5
|
+
};
|
|
6
|
+
export type TRoute = {
|
|
7
|
+
path: string;
|
|
8
|
+
import: string;
|
|
9
|
+
};
|
|
10
|
+
export type TRoutesTree = {
|
|
11
|
+
[key: string]: TRoute | TRoutesTree;
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,aAAa,GAAG;IAC1B,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,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"}
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ts-file-router",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "router based on project files using typescript",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc"
|
|
12
|
+
},
|
|
13
|
+
"author": "MatheusF10",
|
|
14
|
+
"license": "ISC",
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/node": "^24.10.1",
|
|
17
|
+
"ts-node": "^10.9.2",
|
|
18
|
+
"typescript": "^5.9.3"
|
|
19
|
+
}
|
|
20
|
+
}
|