react-fs-router 0.2.0 → 0.2.5

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,8 +1,7 @@
1
1
  #!/usr/bin/env node
2
-
3
- const genPath = require("./dis/genPath/index.js");
2
+ const genPath = require("./dist/genPath");
4
3
  const { Command } = require("commander");
5
- const { version, name } = require("./package.json");
4
+ const { version, name } = require("../package.json");
6
5
 
7
6
  const program = new Command();
8
7
 
File without changes
@@ -13,9 +13,9 @@ var __assign = (this && this.__assign) || function () {
13
13
  exports.__esModule = true;
14
14
  var jsx_runtime_1 = require("react/jsx-runtime");
15
15
  //@ts-ignore
16
- var routeConfigs_1 = require("dist/src/generated/routeConfigs");
16
+ var routeConfigs_1 = require("dist/generated/routeConfigs");
17
17
  //@ts-ignore
18
- var Pages = require("dist/src/generated/routes");
18
+ var Pages = require("dist/generated/routes");
19
19
  var react_1 = require("react");
20
20
  var react_router_dom_1 = require("react-router-dom");
21
21
  function Route() {
@@ -2,4 +2,3 @@ declare const fs: any;
2
2
  declare const prettier: any;
3
3
  declare const chokidar: any;
4
4
  declare let isNotThefirstTimeRunning: boolean;
5
- declare const genPath: (page_directory: any) => void;
@@ -2,7 +2,7 @@ var fs = require("fs");
2
2
  var prettier = require("prettier");
3
3
  var chokidar = require("chokidar");
4
4
  var isNotThefirstTimeRunning = true;
5
- var genPath = function (page_directory) {
5
+ module.exports = function (page_directory) {
6
6
  var pathMaker = function () {
7
7
  var readDirectory = function (directory, includeDirectory) {
8
8
  var pages = {};
@@ -136,4 +136,3 @@ var genPath = function (page_directory) {
136
136
  console.error("Error happened", error);
137
137
  });
138
138
  };
139
- module.exports = genPath;
File without changes
File without changes
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "react-fs-router",
3
- "version": "0.2.0",
3
+ "version": "0.2.5",
4
4
  "description": "File-system based routing for React.",
5
- "main": "./dist/index.js",
5
+ "main": "./bin/index.js",
6
6
  "git repository": "https://github.com/Mehran9675/react-filesystem-routing",
7
7
  "types": "./dist/index.d.ts",
8
8
  "scripts": {
@@ -25,8 +25,8 @@
25
25
  "react-router-dom": "^5.3.0"
26
26
  },
27
27
  "bin": {
28
- "react-fs-router": "./dist/index.js",
29
- "rfr": "./dist/index.js"
28
+ "react-fs-router": "./bin/index.js",
29
+ "rfr": "./bin/index.js"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@types/node": "^16.10.5",
@@ -1,7 +1,7 @@
1
1
  //@ts-ignore
2
- import { routes } from "dist/src/generated/routeConfigs";
2
+ import { routes } from "dist/generated/routeConfigs";
3
3
  //@ts-ignore
4
- import * as Pages from "dist/src/generated/routes";
4
+ import * as Pages from "dist/generated/routes";
5
5
  import React, { ReactElement, ReactNode, useState, useEffect } from "react";
6
6
  import { Route, Switch } from "react-router-dom";
7
7
 
@@ -4,7 +4,7 @@ const chokidar = require("chokidar");
4
4
 
5
5
  let isNotThefirstTimeRunning = true;
6
6
 
7
- const genPath = (page_directory) => {
7
+ module.exports = (page_directory) => {
8
8
  const pathMaker = () => {
9
9
  const readDirectory = (directory: string, includeDirectory?: boolean) => {
10
10
  const pages: Record<string, any> = {};
@@ -159,5 +159,3 @@ const genPath = (page_directory) => {
159
159
  console.error("Error happened", error);
160
160
  });
161
161
  };
162
-
163
- module.exports = genPath;
package/tsconfig.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  // Change this to match your project
3
- "include": ["src/**/*", "index.js"],
4
- "exclude": ["dist/**/*"],
3
+ "include": ["src/**/*"],
4
+ "exclude": ["dist/**/*", "bin/index.js"],
5
5
  "compilerOptions": {
6
6
  "allowJs": true,
7
7
  "declaration": true,
package/dist/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env node
2
- export {};
package/dist/index.js DELETED
@@ -1,10 +0,0 @@
1
- #!/usr/bin/env node
2
- var genPath = require("./dis/genPath/index.js");
3
- var Command = require("commander").Command;
4
- var _a = require("./package.json"), version = _a.version, name = _a.name;
5
- var program = new Command();
6
- program.storeOptionsAsProperties(name);
7
- program.version(version, "-v --version", "Output the current version");
8
- program.requiredOption("-p,--path <path>", "Path to your pages folder");
9
- program.parse(process.argv);
10
- genPath(program.path);
@@ -1,5 +0,0 @@
1
- declare const fs: any;
2
- declare const prettier: any;
3
- declare const chokidar: any;
4
- declare let isNotThefirstTimeRunning: boolean;
5
- declare const genPath: (page_directory: any) => void;
package/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env node
2
- export {};
@@ -1,150 +0,0 @@
1
- var fs = require("fs");
2
- var prettier = require("prettier");
3
- var chokidar = require("chokidar");
4
- var isNotThefirstTimeRunning = true;
5
- var genPath = 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("./src/config/_config.ts", prettier.format(_config, { semi: false, parser: "babel" }), function (err) {
106
- if (err)
107
- return console.error(err);
108
- });
109
- fs.writeFileSync("./src/config/_routes.ts", 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
- // let fsWait: boolean | any = false;
119
- // fs.watch(page_, (event, filename) => {
120
- // if (filename) {
121
- // if (fsWait) return;
122
- // fsWait = setTimeout(() => {
123
- // fsWait = false;
124
- // }, 100);
125
- // console.log(`${filename} file Changed`);
126
- // pathMaker();
127
- // }
128
- // });
129
- var watcher = chokidar.watch(page_directory, {
130
- ignored: /^\./,
131
- persistent: true
132
- });
133
- watcher
134
- .on("add", function (path) {
135
- pathMaker();
136
- console.log("File", path, "has been added");
137
- })
138
- .on("change", function (path) {
139
- pathMaker();
140
- console.log("File", path, "has been changed");
141
- })
142
- .on("unlink", function (path) {
143
- pathMaker();
144
- console.log("File", path, "has been removed");
145
- })
146
- .on("error", function (error) {
147
- console.error("Error happened", error);
148
- });
149
- };
150
- module.exports = genPath;