react-fs-router 0.2.16 → 0.2.17
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/README.md +185 -2
- package/genPath.js +36 -36
- package/index.js +7 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,2 +1,185 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
1
|
+
# React-fs-router
|
|
2
|
+
|
|
3
|
+
React-fs-router is a simple library that watches a user-provided route for changes in files and generates two files in a user-provided route to be used with react-router.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Use yarn or npm to install react-fs-router
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
yarn add react-fs-router
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install react-fs-router
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
The name of each file will be used as its path. The path for index files is "/". Each folder is treated as a route and each file inside a folder is treated as a subroute of its parent folder.
|
|
20
|
+
|
|
21
|
+
As of yet, react-fs-router runs via a command that must be added to the script section of your package.json file.
|
|
22
|
+
The path to pages folder and the path for the output files must be passes via command:
|
|
23
|
+
|
|
24
|
+
```JSON
|
|
25
|
+
"scripts": {
|
|
26
|
+
"rfr": "rfr -p <path to pages folder> -c <path to place output files>"
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
-b can be passed to tell the program to run only once. Intended to be used with the build script.
|
|
31
|
+
|
|
32
|
+
Run the development server
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm run start
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Then run react-fs-router
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
npm run rfr
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
The program will then generate two files one containing imports and exports of all the files inside the pages path and one containing an array of objects each containing:
|
|
45
|
+
|
|
46
|
+
```javascript
|
|
47
|
+
{
|
|
48
|
+
component://Name of the default export of the file
|
|
49
|
+
name: //Name of the component mentioned in the file
|
|
50
|
+
icon: //Name of the icon mentioned in the file
|
|
51
|
+
index: //A number by which the objects in this array are sorted by
|
|
52
|
+
file: // The name of the file this object is for
|
|
53
|
+
path: // The path of this file in relation to its position in the folder structure.
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Name, icon and index can be written in each file and react-fs-router will read and place them in the output file.
|
|
58
|
+
|
|
59
|
+
```javascript
|
|
60
|
+
// name: Home
|
|
61
|
+
// icon: HomeOutlined
|
|
62
|
+
// index: 0
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
To use with the react-router:
|
|
66
|
+
|
|
67
|
+
```javascript xml react
|
|
68
|
+
// importing all of the files from the imports-exports file
|
|
69
|
+
import * as Pages from "src/config/route";
|
|
70
|
+
// importing the route objects
|
|
71
|
+
import { routes } from "src/config/routeConfigs";
|
|
72
|
+
|
|
73
|
+
import { Route, Switch } from "react-router-dom";
|
|
74
|
+
|
|
75
|
+
function App() {
|
|
76
|
+
const [pages, setPages] = useState();
|
|
77
|
+
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
// calling the function to genarate routes.
|
|
80
|
+
setPages(renderConponents(Pages));
|
|
81
|
+
}, []);
|
|
82
|
+
|
|
83
|
+
function renderComponent(key, module) {
|
|
84
|
+
return module[key] || module["default"] || module;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function renderConponents(imports: Record<string, ReactNode>) {
|
|
88
|
+
let renders = [];
|
|
89
|
+
// Looping through the Pages import
|
|
90
|
+
for (const [key, Value] of Object.entries(imports)) {
|
|
91
|
+
const Component = renderComponent(key, Value);
|
|
92
|
+
// Finding the correct route object.
|
|
93
|
+
const route = routes.filter(
|
|
94
|
+
(config) => config.component === Component.name
|
|
95
|
+
)[0];
|
|
96
|
+
|
|
97
|
+
renders.push(
|
|
98
|
+
<Route exact={true} key={route.path} path={route?.path || ""}>
|
|
99
|
+
<Component />
|
|
100
|
+
</Route>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
return renders;
|
|
104
|
+
}
|
|
105
|
+
return <Switch>{pages}</Switch>;
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Place this component inside <BrowserRouter>
|
|
110
|
+
|
|
111
|
+
```xml
|
|
112
|
+
<BrowserRouter>
|
|
113
|
+
<App />
|
|
114
|
+
</BrowserRouter>
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Using with AntPro Layout
|
|
118
|
+
|
|
119
|
+
The routes array generated by this library can be easily used with Ant designs pro layout component.
|
|
120
|
+
The following is an an example using an hoc:
|
|
121
|
+
|
|
122
|
+
```javascript xml
|
|
123
|
+
import { routes } from "../../config/routeConfigs";
|
|
124
|
+
import * as Icons from "@ant-design/icons";
|
|
125
|
+
import { Row } from "antd";
|
|
126
|
+
import ProLayout, { PageContainer } from "@ant-design/pro-layout";
|
|
127
|
+
import { Link } from "react-router-dom";
|
|
128
|
+
function Layout(Component) {
|
|
129
|
+
return class extends React.Component {
|
|
130
|
+
state = {
|
|
131
|
+
path: "",
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
render() {
|
|
135
|
+
const makeIcon = (Icon) => <Icon />;
|
|
136
|
+
const loopMenuItem = (menus) =>
|
|
137
|
+
// If name and icon are found in the file of the component, they will appear in the
|
|
138
|
+
// pro layout menu with the specified name and icon.
|
|
139
|
+
menus.map(({ icon, children, ...item }) => {
|
|
140
|
+
return {
|
|
141
|
+
...item,
|
|
142
|
+
icon: icon && makeIcon(Icons[icon]),
|
|
143
|
+
children: children && loopMenuItem(children),
|
|
144
|
+
};
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
return (
|
|
148
|
+
<ProLayout
|
|
149
|
+
menuItemRender={(item, dom) => (
|
|
150
|
+
<Link
|
|
151
|
+
onClick={() => this.setState({ path: item.path || "/" })}
|
|
152
|
+
to={item.path}
|
|
153
|
+
>
|
|
154
|
+
{dom}
|
|
155
|
+
</Link>
|
|
156
|
+
)}
|
|
157
|
+
menu={{ request: async () => loopMenuItem(routes) }}
|
|
158
|
+
style={{ height: "100vh" }}
|
|
159
|
+
location={{ pathname: this.state.path || window.location.pathname }}
|
|
160
|
+
>
|
|
161
|
+
<PageContainer>
|
|
162
|
+
<Component {...this.props} />
|
|
163
|
+
</PageContainer>
|
|
164
|
+
</ProLayout>
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Caveats
|
|
172
|
+
|
|
173
|
+
1. Since all of the files are wrriten into imports and exports file, having two files with the same name as default export will cause an error.
|
|
174
|
+
2. Running this package with the start command is not recommended as it interferes with react script unless -b is passed to run the main function of the package once.
|
|
175
|
+
3. This library is a work in progress. Feel free to discuss your issues in the github page of the library.
|
|
176
|
+
|
|
177
|
+
## Contributing
|
|
178
|
+
|
|
179
|
+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
|
|
180
|
+
|
|
181
|
+
Please make sure to update tests as appropriate.
|
|
182
|
+
|
|
183
|
+
## License
|
|
184
|
+
|
|
185
|
+
[MIT](https://choosealicense.com/licenses/mit/)
|
package/genPath.js
CHANGED
|
@@ -3,9 +3,8 @@ const prettier = require("prettier");
|
|
|
3
3
|
const chokidar = require("chokidar");
|
|
4
4
|
|
|
5
5
|
let isNotThefirstTimeRunning = true;
|
|
6
|
-
|
|
7
|
-
const genPath = (page_directory, configs_directory) => {
|
|
8
|
-
configs_path = configs_directory;
|
|
6
|
+
|
|
7
|
+
const genPath = (page_directory, configs_directory, runOnce) => {
|
|
9
8
|
const pathMaker = () => {
|
|
10
9
|
const readDirectory = (directory, includeDirectory) => {
|
|
11
10
|
const pages = {};
|
|
@@ -96,9 +95,9 @@ const genPath = (page_directory, configs_directory) => {
|
|
|
96
95
|
return result;
|
|
97
96
|
};
|
|
98
97
|
|
|
99
|
-
const routes = makePathsArray(pages)
|
|
100
|
-
makeRouteObject(page.path, page.file)
|
|
101
|
-
|
|
98
|
+
const routes = makePathsArray(pages)
|
|
99
|
+
.map((page) => makeRouteObject(page.path, page.file))
|
|
100
|
+
.sort((a, b) => b.index - a.index);
|
|
102
101
|
|
|
103
102
|
const _config = `export const routes = ${JSON.stringify(routes)}`;
|
|
104
103
|
|
|
@@ -139,35 +138,36 @@ const genPath = (page_directory, configs_directory) => {
|
|
|
139
138
|
ignored: /^\./,
|
|
140
139
|
persistent: true,
|
|
141
140
|
});
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
141
|
+
if (!runOnce) {
|
|
142
|
+
watcher
|
|
143
|
+
.on("add", function (path) {
|
|
144
|
+
try {
|
|
145
|
+
pathMaker();
|
|
146
|
+
} catch (e) {
|
|
147
|
+
console.log(e);
|
|
148
|
+
}
|
|
149
|
+
console.log("File", path, "has been added");
|
|
150
|
+
})
|
|
151
|
+
.on("change", function (path) {
|
|
152
|
+
try {
|
|
153
|
+
pathMaker();
|
|
154
|
+
} catch (e) {
|
|
155
|
+
console.log(e);
|
|
156
|
+
}
|
|
157
|
+
console.log("File", path, "has been changed");
|
|
158
|
+
})
|
|
159
|
+
.on("unlink", function (path) {
|
|
160
|
+
try {
|
|
161
|
+
pathMaker();
|
|
162
|
+
} catch (e) {
|
|
163
|
+
console.log(e);
|
|
164
|
+
}
|
|
165
|
+
console.log("File", path, "has been removed");
|
|
166
|
+
})
|
|
167
|
+
.on("error", function (error) {
|
|
168
|
+
console.error("Error happened", error);
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
171
|
};
|
|
172
172
|
|
|
173
|
-
module.exports = { genPath
|
|
173
|
+
module.exports = { genPath };
|
package/index.js
CHANGED
|
@@ -5,7 +5,6 @@ const { Command } = require("commander");
|
|
|
5
5
|
const { version, name } = require("./package.json");
|
|
6
6
|
|
|
7
7
|
const program = new Command();
|
|
8
|
-
|
|
9
8
|
program.storeOptionsAsProperties(name);
|
|
10
9
|
|
|
11
10
|
program.version(version, "-v --version", "Output the current version");
|
|
@@ -15,6 +14,12 @@ program.requiredOption(
|
|
|
15
14
|
"-c,--configs <configs>",
|
|
16
15
|
"Path to store generated files"
|
|
17
16
|
);
|
|
18
|
-
|
|
17
|
+
|
|
18
|
+
program.option(
|
|
19
|
+
"-b,--build [type]",
|
|
20
|
+
"boolean Option to run the program once. Intended to be with build commands."
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
program.parse();
|
|
19
24
|
|
|
20
25
|
genPath(program.page, program.configs);
|