-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path_routes.js
54 lines (43 loc) · 1.09 KB
/
_routes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { walk } from "fs";
function formatRoute(origin) {
console.log(origin);
const pathsToIgnore = [
"src/pages/_error.jsx",
];
if (pathsToIgnore.includes(origin)) {
return null;
}
const paths = origin.split("/");
let [name, extension] = paths[paths.length - 1].split(".");
paths.shift(); // Remove /src
paths.shift(); // Remove /pages
paths.pop(); // Remove file
if (name !== "index") paths.push(name); // Add name to path without index if isn't index
const path = "/" + paths.join("/");
const type = paths[0] === "api" ? "api" : "page";
origin = "/" + origin;
return {
name,
path,
extension,
origin,
type,
};
}
const pageRoutes = [];
const apiRoutes = [];
// Walk thought pages
for await (const file of walk("./src/pages")) {
if (file.isFile) {
const routeObject = formatRoute(file.path);
if (routeObject) {
if (routeObject.type === "page") {
pageRoutes.push(routeObject);
} else {
apiRoutes.push(routeObject);
}
}
}
}
export { pageRoutes, apiRoutes };
export default { pageRoutes, apiRoutes };