react-fs-router 0.2.8 → 0.2.12
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/.babelrc +3 -0
- package/bin/index.js +9 -3
- package/package.json +2 -5
- package/src/Route.d.ts +1 -0
- package/src/Route.js +34 -0
- package/src/{Routes/index.tsx → Route.tsx} +9 -8
- package/src/genPath.d.ts +2 -0
- package/src/{genPath/index.ts → genPath.js} +10 -20
- package/src/routeConfigs.d.ts +1 -0
- package/src/routeConfigs.js +1 -0
- package/src/routes.d.ts +1 -0
- package/src/routes.js +1 -0
- package/tsconfig.json +4 -4
- package/src/Routes/index.d.ts +0 -2
- package/src/Routes/index.js +0 -44
- package/src/genPath/index.d.ts +0 -4
- package/src/genPath/index.js +0 -138
- package/src/generated/routeConfigs.d.ts +0 -9
- package/src/generated/routeConfigs.js +0 -2
- package/src/generated/routeConfigs.ts +0 -9
- package/src/generated/routes.d.ts +0 -0
- package/src/generated/routes.js +0 -0
- package/src/generated/routes.ts +0 -0
package/.babelrc
ADDED
package/bin/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
const genPath = require(
|
|
2
|
+
const genPath = require("../src/genPath");
|
|
3
3
|
const { Command } = require("commander");
|
|
4
4
|
const { version, name } = require("../package.json");
|
|
5
5
|
|
|
@@ -9,7 +9,13 @@ program.storeOptionsAsProperties(name);
|
|
|
9
9
|
|
|
10
10
|
program.version(version, "-v --version", "Output the current version");
|
|
11
11
|
|
|
12
|
-
program.requiredOption("-p,--
|
|
12
|
+
program.requiredOption("-p,--page <page>", "Path to your pages folder");
|
|
13
|
+
program.requiredOption(
|
|
14
|
+
"-c,--configs <configs>",
|
|
15
|
+
"Path to store generated files"
|
|
16
|
+
);
|
|
13
17
|
program.parse(process.argv);
|
|
14
18
|
|
|
15
|
-
genPath(program.
|
|
19
|
+
genPath(program.page, program.configs);
|
|
20
|
+
|
|
21
|
+
module.exports = { configs_path: program.configs };
|
package/package.json
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-fs-router",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.12",
|
|
4
4
|
"description": "File-system based routing for React.",
|
|
5
5
|
"main": "./bin/index.js",
|
|
6
6
|
"git repository": "https://github.com/Mehran9675/react-filesystem-routing",
|
|
7
|
-
"types": "./dist/index.d.ts",
|
|
8
7
|
"scripts": {
|
|
9
|
-
"test": "node index.js -p ./pages",
|
|
8
|
+
"test": "node ./bin/index.js -p ./pages -c ./configs",
|
|
10
9
|
"build": "npx -p typescript tsc"
|
|
11
10
|
},
|
|
12
11
|
"keywords": [
|
|
@@ -20,7 +19,6 @@
|
|
|
20
19
|
"dependencies": {
|
|
21
20
|
"chokidar": "^3.5.2",
|
|
22
21
|
"commander": "^8.2.0",
|
|
23
|
-
"node": "^16.10.0",
|
|
24
22
|
"react": "^17.0.2",
|
|
25
23
|
"react-router-dom": "^5.3.0"
|
|
26
24
|
},
|
|
@@ -29,7 +27,6 @@
|
|
|
29
27
|
"rfr": "./bin/index.js"
|
|
30
28
|
},
|
|
31
29
|
"devDependencies": {
|
|
32
|
-
"@types/node": "^16.10.5",
|
|
33
30
|
"typescript": "^4.4.4",
|
|
34
31
|
"prettier": "^2.4.1"
|
|
35
32
|
}
|
package/src/Route.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function Route(): any;
|
package/src/Route.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
exports.__esModule = true;
|
|
3
|
+
var configs_path = require("../bin/index").configs_path;
|
|
4
|
+
var routes = require(configs_path + "/routeConfigs.js").routes;
|
|
5
|
+
var Pages = require(configs_path + "/route.js");
|
|
6
|
+
var react_1 = require("react");
|
|
7
|
+
var react_router_dom_1 = require("react-router-dom");
|
|
8
|
+
function Route() {
|
|
9
|
+
var _a = (0, react_1.useState)(), pages = _a[0], setPages = _a[1];
|
|
10
|
+
var randomStr = function () { return (Math.random() + 1).toString(36).substring(7); };
|
|
11
|
+
(0, react_1.useEffect)(function () {
|
|
12
|
+
setPages(renderConponents(Pages));
|
|
13
|
+
}, []);
|
|
14
|
+
function renderComponent(key, module) {
|
|
15
|
+
return module[key] || module["default"] || module;
|
|
16
|
+
}
|
|
17
|
+
function renderConponents(imports) {
|
|
18
|
+
var renders = [];
|
|
19
|
+
var _loop_1 = function (key, Value) {
|
|
20
|
+
var Component = renderComponent(key, Value);
|
|
21
|
+
var route = routes.filter(function (config) { return (config === null || config === void 0 ? void 0 : config.component) === (Component === null || Component === void 0 ? void 0 : Component.name); })[0];
|
|
22
|
+
renders.push(<react_router_dom_1.Route exact={true} key={randomStr()} path={(route === null || route === void 0 ? void 0 : route.path) || ""}>
|
|
23
|
+
<Component />
|
|
24
|
+
</react_router_dom_1.Route>);
|
|
25
|
+
};
|
|
26
|
+
for (var _i = 0, _a = Object.entries(imports); _i < _a.length; _i++) {
|
|
27
|
+
var _b = _a[_i], key = _b[0], Value = _b[1];
|
|
28
|
+
_loop_1(key, Value);
|
|
29
|
+
}
|
|
30
|
+
return renders;
|
|
31
|
+
}
|
|
32
|
+
return <react_router_dom_1.Switch>{pages}</react_router_dom_1.Switch>;
|
|
33
|
+
}
|
|
34
|
+
exports["default"] = Route;
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
const { configs_path } = require("../bin/index");
|
|
2
|
+
const { routes } = require(configs_path + "/routeConfigs.js");
|
|
3
|
+
const Pages = require(configs_path + "/route.js");
|
|
4
|
+
|
|
5
|
+
import React, { useState, useEffect } from "react";
|
|
4
6
|
import { Route, Switch } from "react-router-dom";
|
|
5
7
|
|
|
6
|
-
export default function Route()
|
|
7
|
-
const [pages, setPages] = useState
|
|
8
|
+
export default function Route() {
|
|
9
|
+
const [pages, setPages] = useState();
|
|
8
10
|
const randomStr = () => (Math.random() + 1).toString(36).substring(7);
|
|
9
11
|
|
|
10
12
|
useEffect(() => {
|
|
@@ -15,15 +17,14 @@ export default function Route(): ReactElement {
|
|
|
15
17
|
return module[key] || module["default"] || module;
|
|
16
18
|
}
|
|
17
19
|
|
|
18
|
-
function renderConponents(imports
|
|
19
|
-
let renders
|
|
20
|
+
function renderConponents(imports) {
|
|
21
|
+
let renders = [];
|
|
20
22
|
for (const [key, Value] of Object.entries(imports)) {
|
|
21
23
|
const Component = renderComponent(key, Value);
|
|
22
24
|
const route = routes.filter(
|
|
23
25
|
(config) => config?.component === Component?.name
|
|
24
26
|
)[0];
|
|
25
27
|
|
|
26
|
-
//@ts-ignore
|
|
27
28
|
renders.push(
|
|
28
29
|
<Route exact={true} key={randomStr()} path={route?.path || ""}>
|
|
29
30
|
<Component />
|
package/src/genPath.d.ts
ADDED
|
@@ -4,10 +4,10 @@ const chokidar = require("chokidar");
|
|
|
4
4
|
|
|
5
5
|
let isNotThefirstTimeRunning = true;
|
|
6
6
|
|
|
7
|
-
module.exports = (page_directory) => {
|
|
7
|
+
module.exports = (page_directory, configs_directory) => {
|
|
8
8
|
const pathMaker = () => {
|
|
9
|
-
const readDirectory = (directory
|
|
10
|
-
const pages
|
|
9
|
+
const readDirectory = (directory, includeDirectory) => {
|
|
10
|
+
const pages = {};
|
|
11
11
|
const read = fs.readdirSync(directory);
|
|
12
12
|
read.forEach((page) => {
|
|
13
13
|
const name = page.split(".")[0];
|
|
@@ -23,7 +23,7 @@ module.exports = (page_directory) => {
|
|
|
23
23
|
};
|
|
24
24
|
const pages = readDirectory(page_directory);
|
|
25
25
|
|
|
26
|
-
const readFileProperties = (filepath
|
|
26
|
+
const readFileProperties = (filepath) => {
|
|
27
27
|
const file = String(fs.readFileSync(page_directory + filepath)); // todo: put in try catch
|
|
28
28
|
const component = file.match(/(export default .*)\w/g); //todo: export default function *
|
|
29
29
|
const props = file.match(
|
|
@@ -51,15 +51,8 @@ module.exports = (page_directory) => {
|
|
|
51
51
|
return result;
|
|
52
52
|
};
|
|
53
53
|
|
|
54
|
-
const makeRouteObject = (filepath
|
|
55
|
-
const route
|
|
56
|
-
component?: string;
|
|
57
|
-
name?: string;
|
|
58
|
-
icon?: string;
|
|
59
|
-
index?: number;
|
|
60
|
-
path?: string;
|
|
61
|
-
file?: string;
|
|
62
|
-
} = {
|
|
54
|
+
const makeRouteObject = (filepath, filename) => {
|
|
55
|
+
const route = {
|
|
63
56
|
component: null,
|
|
64
57
|
name: null,
|
|
65
58
|
icon: null,
|
|
@@ -82,14 +75,11 @@ module.exports = (page_directory) => {
|
|
|
82
75
|
return route;
|
|
83
76
|
};
|
|
84
77
|
|
|
85
|
-
const makePathsArray = (
|
|
86
|
-
paths: Record<string, any>,
|
|
87
|
-
prevPath?: boolean
|
|
88
|
-
): Record<string, string>[] => {
|
|
78
|
+
const makePathsArray = (paths, prevPath) => {
|
|
89
79
|
const result = [];
|
|
90
80
|
for (const [file, path] of Object.entries(paths)) {
|
|
91
81
|
if (typeof path === "string") {
|
|
92
|
-
const oj
|
|
82
|
+
const oj = { file };
|
|
93
83
|
if (prevPath) oj.path = path.replace(page_directory, "");
|
|
94
84
|
else oj.path = path;
|
|
95
85
|
result.push(oj);
|
|
@@ -117,7 +107,7 @@ module.exports = (page_directory) => {
|
|
|
117
107
|
.join("")}};`;
|
|
118
108
|
|
|
119
109
|
fs.writeFileSync(
|
|
120
|
-
"
|
|
110
|
+
configs_directory + "/routeConfigs.js",
|
|
121
111
|
prettier.format(_config, { semi: false, parser: "babel" }),
|
|
122
112
|
(err) => {
|
|
123
113
|
if (err) return console.error(err);
|
|
@@ -125,7 +115,7 @@ module.exports = (page_directory) => {
|
|
|
125
115
|
);
|
|
126
116
|
|
|
127
117
|
fs.writeFileSync(
|
|
128
|
-
"
|
|
118
|
+
configs_directory + "/route.js",
|
|
129
119
|
prettier.format(_routes, { semi: false, parser: "babel" }),
|
|
130
120
|
(err) => {
|
|
131
121
|
if (err) return console.error(err);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const routes: {}[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const routes = [{}];
|
package/src/routes.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/src/routes.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/tsconfig.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
// Change this to match your project
|
|
3
|
-
"include": ["src
|
|
4
|
-
"exclude": ["
|
|
3
|
+
"include": ["./src"],
|
|
4
|
+
"exclude": ["bin/index.js", "*.d.ts"],
|
|
5
5
|
"compilerOptions": {
|
|
6
6
|
"allowJs": true,
|
|
7
7
|
"declaration": true,
|
|
8
8
|
// This compiler run should
|
|
9
9
|
// only output d.ts files
|
|
10
|
-
"emitDeclarationOnly":
|
|
11
|
-
"
|
|
10
|
+
"emitDeclarationOnly": true,
|
|
11
|
+
"outDir": "./src",
|
|
12
12
|
"jsx": "react-jsx"
|
|
13
13
|
}
|
|
14
14
|
}
|
package/src/Routes/index.d.ts
DELETED
package/src/Routes/index.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
exports.__esModule = true;
|
|
14
|
-
var jsx_runtime_1 = require("react/jsx-runtime");
|
|
15
|
-
var routeConfigs_1 = require("src/generated/routeConfigs"); //@ts-ignore
|
|
16
|
-
var Pages = require("src/generated/routes");
|
|
17
|
-
var react_1 = require("react");
|
|
18
|
-
var react_router_dom_1 = require("react-router-dom");
|
|
19
|
-
function Route() {
|
|
20
|
-
var _a = (0, react_1.useState)(), pages = _a[0], setPages = _a[1];
|
|
21
|
-
var randomStr = function () { return (Math.random() + 1).toString(36).substring(7); };
|
|
22
|
-
(0, react_1.useEffect)(function () {
|
|
23
|
-
setPages(renderConponents(Pages));
|
|
24
|
-
}, []);
|
|
25
|
-
function renderComponent(key, module) {
|
|
26
|
-
return module[key] || module["default"] || module;
|
|
27
|
-
}
|
|
28
|
-
function renderConponents(imports) {
|
|
29
|
-
var renders = [];
|
|
30
|
-
var _loop_1 = function (key, Value) {
|
|
31
|
-
var Component = renderComponent(key, Value);
|
|
32
|
-
var route = routeConfigs_1.routes.filter(function (config) { return (config === null || config === void 0 ? void 0 : config.component) === (Component === null || Component === void 0 ? void 0 : Component.name); })[0];
|
|
33
|
-
//@ts-ignore
|
|
34
|
-
renders.push((0, jsx_runtime_1.jsx)(react_router_dom_1.Route, __assign({ exact: true, path: (route === null || route === void 0 ? void 0 : route.path) || "" }, { children: (0, jsx_runtime_1.jsx)(Component, {}, void 0) }), randomStr()));
|
|
35
|
-
};
|
|
36
|
-
for (var _i = 0, _a = Object.entries(imports); _i < _a.length; _i++) {
|
|
37
|
-
var _b = _a[_i], key = _b[0], Value = _b[1];
|
|
38
|
-
_loop_1(key, Value);
|
|
39
|
-
}
|
|
40
|
-
return renders;
|
|
41
|
-
}
|
|
42
|
-
return (0, jsx_runtime_1.jsx)(react_router_dom_1.Switch, { children: pages }, void 0);
|
|
43
|
-
}
|
|
44
|
-
exports["default"] = Route;
|
package/src/genPath/index.d.ts
DELETED
package/src/genPath/index.js
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
var fs = require("fs");
|
|
2
|
-
var prettier = require("prettier");
|
|
3
|
-
var chokidar = require("chokidar");
|
|
4
|
-
var isNotThefirstTimeRunning = true;
|
|
5
|
-
module.exports = function (page_directory) {
|
|
6
|
-
var pathMaker = function () {
|
|
7
|
-
var readDirectory = function (directory, includeDirectory) {
|
|
8
|
-
var pages = {};
|
|
9
|
-
var read = fs.readdirSync(directory);
|
|
10
|
-
read.forEach(function (page) {
|
|
11
|
-
var name = page.split(".")[0];
|
|
12
|
-
if (page.includes(".")) {
|
|
13
|
-
pages[page.replace(".", "_")] = (includeDirectory ? directory : "") + "/" + (name === "index" ? "" : name);
|
|
14
|
-
}
|
|
15
|
-
else {
|
|
16
|
-
pages[name] = readDirectory(directory + "/" + page, true);
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
return pages;
|
|
20
|
-
};
|
|
21
|
-
var pages = readDirectory(page_directory);
|
|
22
|
-
var readFileProperties = function (filepath) {
|
|
23
|
-
var file = String(fs.readFileSync(page_directory + filepath)); // todo: put in try catch
|
|
24
|
-
var component = file.match(/(export default .*)\w/g); //todo: export default function *
|
|
25
|
-
var props = file.match(/((name|icon|index):( |).*[^{}\[\]-_=\/\\\(\)\*&^%$#@!~`|?><,;:"'])/g);
|
|
26
|
-
var result = {
|
|
27
|
-
component: null,
|
|
28
|
-
name: null,
|
|
29
|
-
icon: null,
|
|
30
|
-
index: null
|
|
31
|
-
};
|
|
32
|
-
if (!file)
|
|
33
|
-
return result;
|
|
34
|
-
if (component === null || component === void 0 ? void 0 : component.length) {
|
|
35
|
-
result.component = component[0].split("default")[1].trim();
|
|
36
|
-
}
|
|
37
|
-
if (props === null || props === void 0 ? void 0 : props.length) {
|
|
38
|
-
var name_1 = props.filter(function (match) { return match.includes("name"); });
|
|
39
|
-
var icon = props.filter(function (match) { return match.includes("icon"); });
|
|
40
|
-
var index = props.filter(function (match) { return match.includes("index"); });
|
|
41
|
-
if (name_1.length)
|
|
42
|
-
result.name = name_1[0].split(":")[1].trim();
|
|
43
|
-
if (icon.length)
|
|
44
|
-
result.icon = icon[0].split(":")[1].trim();
|
|
45
|
-
if (index.length)
|
|
46
|
-
result.index = Number(index[0].split(":")[1].trim()) || 0;
|
|
47
|
-
}
|
|
48
|
-
return result;
|
|
49
|
-
};
|
|
50
|
-
var makeRouteObject = function (filepath, filename) {
|
|
51
|
-
var route = {
|
|
52
|
-
component: null,
|
|
53
|
-
name: null,
|
|
54
|
-
icon: null,
|
|
55
|
-
index: 0,
|
|
56
|
-
path: filepath
|
|
57
|
-
};
|
|
58
|
-
var _a = readFileProperties("" + (filepath.match(/(\/)/g).length ? filepath : "") + (filename.includes("index")
|
|
59
|
-
? "/" + filename.replace("_", ".")
|
|
60
|
-
: "." + filename.split("_")[1])), component = _a.component, name = _a.name, icon = _a.icon, index = _a.index;
|
|
61
|
-
if (component)
|
|
62
|
-
route.component = component;
|
|
63
|
-
if (name)
|
|
64
|
-
route.name = name;
|
|
65
|
-
if (icon)
|
|
66
|
-
route.icon = icon;
|
|
67
|
-
if (index)
|
|
68
|
-
route.index = index;
|
|
69
|
-
if (filepath)
|
|
70
|
-
route.path = filepath;
|
|
71
|
-
if (filename)
|
|
72
|
-
route.file = filename;
|
|
73
|
-
return route;
|
|
74
|
-
};
|
|
75
|
-
var makePathsArray = function (paths, prevPath) {
|
|
76
|
-
var result = [];
|
|
77
|
-
for (var _i = 0, _a = Object.entries(paths); _i < _a.length; _i++) {
|
|
78
|
-
var _b = _a[_i], file = _b[0], path = _b[1];
|
|
79
|
-
if (typeof path === "string") {
|
|
80
|
-
var oj = { file: file };
|
|
81
|
-
if (prevPath)
|
|
82
|
-
oj.path = path.replace(page_directory, "");
|
|
83
|
-
else
|
|
84
|
-
oj.path = path;
|
|
85
|
-
result.push(oj);
|
|
86
|
-
}
|
|
87
|
-
else
|
|
88
|
-
result.push.apply(result, makePathsArray(path, true));
|
|
89
|
-
}
|
|
90
|
-
return result;
|
|
91
|
-
};
|
|
92
|
-
var routes = makePathsArray(pages).map(function (page) {
|
|
93
|
-
return makeRouteObject(page.path, page.file);
|
|
94
|
-
});
|
|
95
|
-
var _config = "export const routes = " + JSON.stringify(routes);
|
|
96
|
-
var _routes = routes
|
|
97
|
-
.map(function (node) {
|
|
98
|
-
return (node === null || node === void 0 ? void 0 : node.component)
|
|
99
|
-
? "import " + node.component + " from \"" + (process.env.PWD + page_directory.replace(".", "")) + (node.path === "/" ? "" : node.path) + "\";"
|
|
100
|
-
: "";
|
|
101
|
-
})
|
|
102
|
-
.join("") + " \n \n export{" + routes
|
|
103
|
-
.map(function (node) { return ((node === null || node === void 0 ? void 0 : node.component) ? node.component + "," : ""); })
|
|
104
|
-
.join("") + "};";
|
|
105
|
-
fs.writeFileSync("./node_modules/react-fs-router/dist/src/generated/routeConfigs.js", prettier.format(_config, { semi: false, parser: "babel" }), function (err) {
|
|
106
|
-
if (err)
|
|
107
|
-
return console.error(err);
|
|
108
|
-
});
|
|
109
|
-
fs.writeFileSync("./node_modules/react-fs-router/dist/src/generated/route.js", prettier.format(_routes, { semi: false, parser: "babel" }), function (err) {
|
|
110
|
-
if (err)
|
|
111
|
-
return console.error(err);
|
|
112
|
-
});
|
|
113
|
-
};
|
|
114
|
-
if (isNotThefirstTimeRunning) {
|
|
115
|
-
pathMaker();
|
|
116
|
-
isNotThefirstTimeRunning = false;
|
|
117
|
-
}
|
|
118
|
-
var watcher = chokidar.watch(page_directory, {
|
|
119
|
-
ignored: /^\./,
|
|
120
|
-
persistent: true
|
|
121
|
-
});
|
|
122
|
-
watcher
|
|
123
|
-
.on("add", function (path) {
|
|
124
|
-
pathMaker();
|
|
125
|
-
console.log("File", path, "has been added");
|
|
126
|
-
})
|
|
127
|
-
.on("change", function (path) {
|
|
128
|
-
pathMaker();
|
|
129
|
-
console.log("File", path, "has been changed");
|
|
130
|
-
})
|
|
131
|
-
.on("unlink", function (path) {
|
|
132
|
-
pathMaker();
|
|
133
|
-
console.log("File", path, "has been removed");
|
|
134
|
-
})
|
|
135
|
-
.on("error", function (error) {
|
|
136
|
-
console.error("Error happened", error);
|
|
137
|
-
});
|
|
138
|
-
};
|
|
File without changes
|
package/src/generated/routes.js
DELETED
|
File without changes
|
package/src/generated/routes.ts
DELETED
|
File without changes
|