@arc-js/initiator 0.0.7 → 0.0.8
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/index.js +212 -176
- package/index.min.js +1 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -194872,18 +194872,19 @@ class RouteGenerator {
|
|
|
194872
194872
|
processRouteFile(filePath, moduleName) {
|
|
194873
194873
|
return __awaiter(this, void 0, void 0, function* () {
|
|
194874
194874
|
try {
|
|
194875
|
-
const relativePath = path.relative(this.config.srcDir, filePath);
|
|
194876
194875
|
let routePath = this.convertFilePathToRoutePath(filePath, moduleName);
|
|
194877
|
-
const componentName = this.extractComponentName(filePath);
|
|
194878
|
-
const
|
|
194879
|
-
const
|
|
194876
|
+
const componentName = this.extractComponentName(filePath, moduleName);
|
|
194877
|
+
const layoutComponent = this.findLayoutComponent(filePath, moduleName);
|
|
194878
|
+
const errorComponent = this.findErrorComponent(filePath, moduleName);
|
|
194879
|
+
const notFoundComponent = this.findNotFoundComponent(filePath, moduleName);
|
|
194880
194880
|
const routeFile = {
|
|
194881
194881
|
filePath,
|
|
194882
|
-
relativePath,
|
|
194882
|
+
relativePath: path.relative(this.config.srcDir, filePath),
|
|
194883
194883
|
routePath,
|
|
194884
194884
|
componentName,
|
|
194885
|
-
|
|
194886
|
-
|
|
194885
|
+
layoutComponent,
|
|
194886
|
+
errorComponent,
|
|
194887
|
+
notFoundComponent,
|
|
194887
194888
|
moduleName
|
|
194888
194889
|
};
|
|
194889
194890
|
this.routeFiles.push(routeFile);
|
|
@@ -194897,81 +194898,128 @@ class RouteGenerator {
|
|
|
194897
194898
|
let routePath = filePath;
|
|
194898
194899
|
routePath = routePath.replace(this.config.srcDir, '');
|
|
194899
194900
|
if (moduleName) {
|
|
194900
|
-
|
|
194901
|
-
|
|
194901
|
+
const modulePagesPrefix = `modules${path.sep}${moduleName}${path.sep}pages`;
|
|
194902
|
+
if (routePath.includes(modulePagesPrefix)) {
|
|
194903
|
+
routePath = routePath.substring(routePath.indexOf(modulePagesPrefix) + modulePagesPrefix.length);
|
|
194904
|
+
routePath = `/${moduleName}${routePath}`;
|
|
194905
|
+
}
|
|
194902
194906
|
}
|
|
194903
194907
|
else {
|
|
194904
|
-
|
|
194908
|
+
const pagesPrefix = 'pages';
|
|
194909
|
+
if (routePath.includes(pagesPrefix)) {
|
|
194910
|
+
routePath = routePath.substring(routePath.indexOf(pagesPrefix) + pagesPrefix.length);
|
|
194911
|
+
}
|
|
194905
194912
|
}
|
|
194906
194913
|
routePath = routePath.replace(/\.(tsx|jsx)$/, '');
|
|
194907
194914
|
routePath = routePath.replace(/\\/g, '/');
|
|
194908
194915
|
if (routePath.endsWith('/index')) {
|
|
194909
194916
|
routePath = routePath.replace(/\/index$/, '');
|
|
194910
194917
|
}
|
|
194911
|
-
routePath = routePath.replace(/\[([^\]]+)\]/g, '
|
|
194918
|
+
routePath = routePath.replace(/\[([^\]]+)\]/g, '{$1}');
|
|
194912
194919
|
if (!routePath.startsWith('/')) {
|
|
194913
194920
|
routePath = '/' + routePath;
|
|
194914
194921
|
}
|
|
194915
|
-
if (routePath === '/') {
|
|
194922
|
+
if (routePath === '/' || routePath === '//') {
|
|
194916
194923
|
return '/';
|
|
194917
194924
|
}
|
|
194918
194925
|
return routePath;
|
|
194919
194926
|
}
|
|
194920
|
-
extractComponentName(filePath) {
|
|
194927
|
+
extractComponentName(filePath, moduleName) {
|
|
194921
194928
|
const fileName = path.basename(filePath, path.extname(filePath));
|
|
194922
|
-
const
|
|
194929
|
+
const dirName = path.dirname(filePath);
|
|
194930
|
+
const parentDirs = dirName.split(path.sep).filter(dir => dir && dir !== 'src' && dir !== 'modules' && dir !== 'pages' && dir !== moduleName);
|
|
194931
|
+
let componentName = moduleName ? this.toPascalCase(moduleName) : '';
|
|
194932
|
+
parentDirs.forEach(dir => {
|
|
194933
|
+
if (dir !== 'pages') {
|
|
194934
|
+
componentName += this.toPascalCase(dir);
|
|
194935
|
+
}
|
|
194936
|
+
});
|
|
194937
|
+
let cleanedFileName = fileName;
|
|
194938
|
+
if (fileName.startsWith('{') && fileName.endsWith('}')) {
|
|
194939
|
+
const paramName = fileName.substring(1, fileName.length - 1);
|
|
194940
|
+
cleanedFileName = `${this.toPascalCase(paramName)}Params`;
|
|
194941
|
+
}
|
|
194942
|
+
else {
|
|
194943
|
+
cleanedFileName = this.toPascalCase(fileName);
|
|
194944
|
+
}
|
|
194945
|
+
if (!cleanedFileName.endsWith('Page')) {
|
|
194946
|
+
cleanedFileName += 'Page';
|
|
194947
|
+
}
|
|
194948
|
+
return componentName + cleanedFileName;
|
|
194949
|
+
}
|
|
194950
|
+
toPascalCase(str) {
|
|
194951
|
+
return str
|
|
194923
194952
|
.split(/[-_]/)
|
|
194924
194953
|
.map(part => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase())
|
|
194925
194954
|
.join('');
|
|
194926
|
-
const reservedNames = ['Layout', 'Error', 'NotFound', 'ErrorBoundary'];
|
|
194927
|
-
const finalName = reservedNames.includes(pascalName) ? `${pascalName}Page` : pascalName;
|
|
194928
|
-
return `${finalName}Page`;
|
|
194929
194955
|
}
|
|
194930
|
-
|
|
194956
|
+
findLayoutComponent(filePath, moduleName) {
|
|
194931
194957
|
const dir = path.dirname(filePath);
|
|
194932
|
-
const layoutPath = path.join(dir, `${this.config.layoutFileName}.tsx`);
|
|
194933
|
-
if (fs.existsSync(layoutPath)) {
|
|
194934
|
-
return true;
|
|
194935
|
-
}
|
|
194936
194958
|
let currentDir = dir;
|
|
194937
194959
|
while (currentDir !== this.config.srcDir && currentDir !== path.dirname(this.config.srcDir)) {
|
|
194938
|
-
const
|
|
194939
|
-
if (fs.existsSync(
|
|
194940
|
-
return
|
|
194960
|
+
const layoutPath = path.join(currentDir, `${this.config.layoutFileName}.tsx`);
|
|
194961
|
+
if (fs.existsSync(layoutPath)) {
|
|
194962
|
+
return this.generateComponentName(layoutPath, moduleName, 'Layout');
|
|
194941
194963
|
}
|
|
194942
194964
|
if (moduleName) {
|
|
194943
|
-
const
|
|
194944
|
-
if (
|
|
194945
|
-
|
|
194965
|
+
const modulePath = path.join(this.config.modulesDir, moduleName);
|
|
194966
|
+
if (currentDir === modulePath) {
|
|
194967
|
+
break;
|
|
194946
194968
|
}
|
|
194947
194969
|
}
|
|
194948
194970
|
currentDir = path.dirname(currentDir);
|
|
194949
194971
|
}
|
|
194950
194972
|
const globalLayoutPath = path.join(this.config.pagesDir, `${this.config.layoutFileName}.tsx`);
|
|
194951
|
-
|
|
194973
|
+
if (fs.existsSync(globalLayoutPath)) {
|
|
194974
|
+
return 'Layout';
|
|
194975
|
+
}
|
|
194976
|
+
return undefined;
|
|
194952
194977
|
}
|
|
194953
|
-
|
|
194978
|
+
findErrorComponent(filePath, moduleName) {
|
|
194954
194979
|
const dir = path.dirname(filePath);
|
|
194955
|
-
const errorPath = path.join(dir, `${this.config.errorFileName}.tsx`);
|
|
194956
|
-
if (fs.existsSync(errorPath)) {
|
|
194957
|
-
return true;
|
|
194958
|
-
}
|
|
194959
194980
|
let currentDir = dir;
|
|
194960
194981
|
while (currentDir !== this.config.srcDir && currentDir !== path.dirname(this.config.srcDir)) {
|
|
194961
|
-
const
|
|
194962
|
-
if (fs.existsSync(
|
|
194963
|
-
return
|
|
194982
|
+
const errorPath = path.join(currentDir, `${this.config.errorFileName}.tsx`);
|
|
194983
|
+
if (fs.existsSync(errorPath)) {
|
|
194984
|
+
return this.generateComponentName(errorPath, moduleName, 'ErrorBoundary');
|
|
194964
194985
|
}
|
|
194965
194986
|
if (moduleName) {
|
|
194966
|
-
const
|
|
194967
|
-
if (
|
|
194968
|
-
|
|
194987
|
+
const modulePath = path.join(this.config.modulesDir, moduleName);
|
|
194988
|
+
if (currentDir === modulePath) {
|
|
194989
|
+
break;
|
|
194969
194990
|
}
|
|
194970
194991
|
}
|
|
194971
194992
|
currentDir = path.dirname(currentDir);
|
|
194972
194993
|
}
|
|
194973
194994
|
const globalErrorPath = path.join(this.config.pagesDir, `${this.config.errorFileName}.tsx`);
|
|
194974
|
-
|
|
194995
|
+
if (fs.existsSync(globalErrorPath)) {
|
|
194996
|
+
return 'ErrorBoundary';
|
|
194997
|
+
}
|
|
194998
|
+
return undefined;
|
|
194999
|
+
}
|
|
195000
|
+
findNotFoundComponent(filePath, moduleName) {
|
|
195001
|
+
if (!moduleName)
|
|
195002
|
+
return undefined;
|
|
195003
|
+
const modulePagesDir = path.join(this.config.modulesDir, moduleName, 'pages');
|
|
195004
|
+
const notFoundPath = path.join(modulePagesDir, `${this.config.notFoundFileName}.tsx`);
|
|
195005
|
+
if (fs.existsSync(notFoundPath)) {
|
|
195006
|
+
return this.generateComponentName(notFoundPath, moduleName, 'NotFound');
|
|
195007
|
+
}
|
|
195008
|
+
return undefined;
|
|
195009
|
+
}
|
|
195010
|
+
generateComponentName(filePath, moduleName, suffix) {
|
|
195011
|
+
if (!moduleName) {
|
|
195012
|
+
return suffix;
|
|
195013
|
+
}
|
|
195014
|
+
const relativePath = path.relative(path.join(this.config.modulesDir, moduleName, 'pages'), path.dirname(filePath));
|
|
195015
|
+
let componentName = this.toPascalCase(moduleName);
|
|
195016
|
+
if (relativePath && relativePath !== '.') {
|
|
195017
|
+
const dirNames = relativePath.split(path.sep).filter(dir => dir && dir !== '.');
|
|
195018
|
+
dirNames.forEach(dir => {
|
|
195019
|
+
componentName += this.toPascalCase(dir);
|
|
195020
|
+
});
|
|
195021
|
+
}
|
|
195022
|
+
return componentName + suffix;
|
|
194975
195023
|
}
|
|
194976
195024
|
generateAutoRoutesFile() {
|
|
194977
195025
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -194993,166 +195041,130 @@ class RouteGenerator {
|
|
|
194993
195041
|
importPath = importPath.replace(/\.(tsx|jsx)$/, '');
|
|
194994
195042
|
return importPath;
|
|
194995
195043
|
}
|
|
194996
|
-
escapeTemplateString(str) {
|
|
194997
|
-
return str
|
|
194998
|
-
.replace(/\\/g, '\\\\')
|
|
194999
|
-
.replace(/`/g, '\\`')
|
|
195000
|
-
.replace(/\${/g, '\\${');
|
|
195001
|
-
}
|
|
195002
195044
|
generateFileContent() {
|
|
195003
|
-
const
|
|
195004
|
-
const globalRoutes = [];
|
|
195005
|
-
this.routeFiles.forEach(route => {
|
|
195006
|
-
if (route.moduleName) {
|
|
195007
|
-
if (!moduleRoutes[route.moduleName]) {
|
|
195008
|
-
moduleRoutes[route.moduleName] = [];
|
|
195009
|
-
}
|
|
195010
|
-
moduleRoutes[route.moduleName].push(route);
|
|
195011
|
-
}
|
|
195012
|
-
else {
|
|
195013
|
-
globalRoutes.push(route);
|
|
195014
|
-
}
|
|
195015
|
-
});
|
|
195016
|
-
const globalLayoutPath = path.join(this.config.pagesDir, `${this.config.layoutFileName}.tsx`);
|
|
195017
|
-
const globalErrorPath = path.join(this.config.pagesDir, `${this.config.errorFileName}.tsx`);
|
|
195018
|
-
const global404Path = path.join(this.config.pagesDir, `${this.config.notFoundFileName}.tsx`);
|
|
195019
|
-
const hasGlobalLayout = fs.existsSync(globalLayoutPath);
|
|
195020
|
-
const hasGlobalError = fs.existsSync(globalErrorPath);
|
|
195021
|
-
const hasGlobal404 = fs.existsSync(global404Path);
|
|
195045
|
+
const components = new Set();
|
|
195022
195046
|
const imports = [
|
|
195023
195047
|
`import { lazy } from 'react';`,
|
|
195024
195048
|
`import type { RouteObject } from 'react-router-dom';`,
|
|
195025
195049
|
``
|
|
195026
195050
|
];
|
|
195027
|
-
|
|
195051
|
+
const globalLayoutPath = path.join(this.config.pagesDir, `${this.config.layoutFileName}.tsx`);
|
|
195052
|
+
const globalErrorPath = path.join(this.config.pagesDir, `${this.config.errorFileName}.tsx`);
|
|
195053
|
+
const global404Path = path.join(this.config.pagesDir, `${this.config.notFoundFileName}.tsx`);
|
|
195054
|
+
if (fs.existsSync(globalLayoutPath)) {
|
|
195028
195055
|
const importPath = this.getRelativeImportPath(globalLayoutPath);
|
|
195029
|
-
imports.push(`const Layout = lazy(() => import('${
|
|
195030
|
-
|
|
195031
|
-
else {
|
|
195032
|
-
imports.push(`// No global layout found`);
|
|
195056
|
+
imports.push(`const Layout = lazy(() => import('${importPath}'));`);
|
|
195057
|
+
components.add('Layout');
|
|
195033
195058
|
}
|
|
195034
|
-
if (
|
|
195059
|
+
if (fs.existsSync(globalErrorPath)) {
|
|
195035
195060
|
const importPath = this.getRelativeImportPath(globalErrorPath);
|
|
195036
|
-
imports.push(`const ErrorBoundary = lazy(() => import('${
|
|
195061
|
+
imports.push(`const ErrorBoundary = lazy(() => import('${importPath}'));`);
|
|
195062
|
+
components.add('ErrorBoundary');
|
|
195037
195063
|
}
|
|
195038
|
-
|
|
195039
|
-
imports.push(`// No global error boundary found`);
|
|
195040
|
-
}
|
|
195041
|
-
if (hasGlobal404) {
|
|
195064
|
+
if (fs.existsSync(global404Path)) {
|
|
195042
195065
|
const importPath = this.getRelativeImportPath(global404Path);
|
|
195043
|
-
imports.push(`const NotFound = lazy(() => import('${
|
|
195044
|
-
|
|
195045
|
-
else {
|
|
195046
|
-
imports.push(`// No 404 page found`);
|
|
195066
|
+
imports.push(`const NotFound = lazy(() => import('${importPath}'));`);
|
|
195067
|
+
components.add('NotFound');
|
|
195047
195068
|
}
|
|
195048
195069
|
imports.push(``);
|
|
195049
|
-
|
|
195050
|
-
|
|
195051
|
-
|
|
195052
|
-
|
|
195053
|
-
|
|
195054
|
-
|
|
195055
|
-
|
|
195056
|
-
|
|
195070
|
+
const moduleComponents = new Map();
|
|
195071
|
+
this.routeFiles.forEach(route => {
|
|
195072
|
+
if (route.layoutComponent && !components.has(route.layoutComponent)) {
|
|
195073
|
+
components.add(route.layoutComponent);
|
|
195074
|
+
const layoutFile = this.findComponentFile(route.filePath, this.config.layoutFileName, route.moduleName);
|
|
195075
|
+
if (layoutFile) {
|
|
195076
|
+
const importPath = this.getRelativeImportPath(layoutFile);
|
|
195077
|
+
imports.push(`const ${route.layoutComponent} = lazy(() => import('${importPath}'));`);
|
|
195078
|
+
}
|
|
195057
195079
|
}
|
|
195058
|
-
|
|
195059
|
-
|
|
195080
|
+
if (route.errorComponent && !components.has(route.errorComponent)) {
|
|
195081
|
+
components.add(route.errorComponent);
|
|
195082
|
+
const errorFile = this.findComponentFile(route.filePath, this.config.errorFileName, route.moduleName);
|
|
195083
|
+
if (errorFile) {
|
|
195084
|
+
const importPath = this.getRelativeImportPath(errorFile);
|
|
195085
|
+
imports.push(`const ${route.errorComponent} = lazy(() => import('${importPath}'));`);
|
|
195086
|
+
}
|
|
195060
195087
|
}
|
|
195061
|
-
if (
|
|
195062
|
-
|
|
195063
|
-
|
|
195088
|
+
if (route.notFoundComponent && !components.has(route.notFoundComponent)) {
|
|
195089
|
+
components.add(route.notFoundComponent);
|
|
195090
|
+
const notFoundFile = this.findComponentFile(route.filePath, this.config.notFoundFileName, route.moduleName);
|
|
195091
|
+
if (notFoundFile) {
|
|
195092
|
+
const importPath = this.getRelativeImportPath(notFoundFile);
|
|
195093
|
+
imports.push(`const ${route.notFoundComponent} = lazy(() => import('${importPath}'));`);
|
|
195094
|
+
}
|
|
195064
195095
|
}
|
|
195065
|
-
|
|
195066
|
-
|
|
195096
|
+
const importPath = this.getRelativeImportPath(route.filePath);
|
|
195097
|
+
imports.push(`const ${route.componentName} = lazy(() => import('${importPath}'));`);
|
|
195098
|
+
components.add(route.componentName);
|
|
195099
|
+
if (route.moduleName) {
|
|
195100
|
+
if (!moduleComponents.has(route.moduleName)) {
|
|
195101
|
+
moduleComponents.set(route.moduleName, new Set());
|
|
195102
|
+
}
|
|
195103
|
+
moduleComponents.get(route.moduleName).add(route.componentName);
|
|
195067
195104
|
}
|
|
195068
|
-
imports.push(``);
|
|
195069
195105
|
});
|
|
195070
|
-
|
|
195071
|
-
|
|
195072
|
-
|
|
195073
|
-
|
|
195074
|
-
|
|
195075
|
-
|
|
195076
|
-
|
|
195077
|
-
|
|
195078
|
-
|
|
195079
|
-
if (
|
|
195080
|
-
|
|
195081
|
-
|
|
195082
|
-
|
|
195083
|
-
imports.push(`const ${route.componentName} = lazy(() => import('${this.escapeTemplateString(importPath)}'));`);
|
|
195084
|
-
});
|
|
195085
|
-
imports.push(``);
|
|
195106
|
+
imports.push(``);
|
|
195107
|
+
const routes = ['export const routes: RouteObject[] = ['];
|
|
195108
|
+
const sortedRoutes = this.routeFiles.sort((a, b) => {
|
|
195109
|
+
if (a.routePath === '/')
|
|
195110
|
+
return -1;
|
|
195111
|
+
if (b.routePath === '/')
|
|
195112
|
+
return 1;
|
|
195113
|
+
if (!a.moduleName && b.moduleName)
|
|
195114
|
+
return -1;
|
|
195115
|
+
if (a.moduleName && !b.moduleName)
|
|
195116
|
+
return 1;
|
|
195117
|
+
if (a.moduleName === b.moduleName) {
|
|
195118
|
+
return a.routePath.localeCompare(b.routePath);
|
|
195086
195119
|
}
|
|
195120
|
+
return 0;
|
|
195087
195121
|
});
|
|
195088
|
-
|
|
195089
|
-
|
|
195090
|
-
|
|
195091
|
-
|
|
195092
|
-
|
|
195093
|
-
|
|
195094
|
-
|
|
195095
|
-
|
|
195096
|
-
|
|
195097
|
-
|
|
195098
|
-
|
|
195099
|
-
|
|
195100
|
-
|
|
195101
|
-
|
|
195102
|
-
|
|
195103
|
-
|
|
195104
|
-
|
|
195105
|
-
|
|
195106
|
-
|
|
195107
|
-
|
|
195108
|
-
|
|
195109
|
-
|
|
195110
|
-
routes.push(
|
|
195111
|
-
}
|
|
195112
|
-
|
|
195113
|
-
|
|
195114
|
-
|
|
195115
|
-
|
|
195116
|
-
|
|
195117
|
-
|
|
195118
|
-
routes.push(
|
|
195119
|
-
|
|
195120
|
-
|
|
195121
|
-
|
|
195122
|
-
|
|
195123
|
-
const hasModuleError = fs.existsSync(path.join(this.config.modulesDir, moduleName, 'pages', `${this.config.errorFileName}.tsx`));
|
|
195124
|
-
if (route.hasLayout && hasModuleLayout) {
|
|
195125
|
-
routeObj.push(` element: <${moduleName}Layout><${route.componentName} /></${moduleName}Layout>,`);
|
|
195126
|
-
}
|
|
195127
|
-
else {
|
|
195128
|
-
routeObj.push(` element: <${route.componentName} />,`);
|
|
195129
|
-
}
|
|
195130
|
-
if (route.hasError && hasModuleError) {
|
|
195131
|
-
routeObj.push(` errorElement: <${moduleName}ErrorBoundary />`);
|
|
195132
|
-
}
|
|
195133
|
-
const lastLine = routeObj[routeObj.length - 1];
|
|
195134
|
-
if (!lastLine.endsWith(',')) {
|
|
195135
|
-
routeObj[routeObj.length - 1] = lastLine + ',';
|
|
195136
|
-
}
|
|
195137
|
-
routeObj.push(' }');
|
|
195138
|
-
const isLastModule = moduleIndex === Object.keys(moduleRoutes).length - 1;
|
|
195139
|
-
const isLastRoute = routeIndex === moduleRoutesList.length - 1;
|
|
195140
|
-
if (!(isLastModule && isLastRoute && !hasGlobal404)) {
|
|
195141
|
-
routeObj[routeObj.length - 1] += ',';
|
|
195142
|
-
}
|
|
195143
|
-
routes.push(routeObj.join('\n'));
|
|
195144
|
-
});
|
|
195122
|
+
sortedRoutes.forEach((route, index) => {
|
|
195123
|
+
var _a;
|
|
195124
|
+
const routeLines = [' {'];
|
|
195125
|
+
routeLines.push(` path: '${route.routePath}',`);
|
|
195126
|
+
if (route.layoutComponent) {
|
|
195127
|
+
routeLines.push(` element: <${route.layoutComponent}><${route.componentName} /></${route.layoutComponent}>,`);
|
|
195128
|
+
}
|
|
195129
|
+
else {
|
|
195130
|
+
routeLines.push(` element: <${route.componentName} />,`);
|
|
195131
|
+
}
|
|
195132
|
+
if (route.errorComponent) {
|
|
195133
|
+
routeLines.push(` errorElement: <${route.errorComponent} />`);
|
|
195134
|
+
}
|
|
195135
|
+
if (routeLines[routeLines.length - 1].endsWith(',')) {
|
|
195136
|
+
routeLines[routeLines.length - 1] = routeLines[routeLines.length - 1].slice(0, -1);
|
|
195137
|
+
}
|
|
195138
|
+
routeLines.push(' }');
|
|
195139
|
+
if (index < sortedRoutes.length - 1) {
|
|
195140
|
+
routeLines[routeLines.length - 1] += ',';
|
|
195141
|
+
}
|
|
195142
|
+
if (route.moduleName && (index === 0 ||
|
|
195143
|
+
((_a = sortedRoutes[index - 1]) === null || _a === void 0 ? void 0 : _a.moduleName) !== route.moduleName)) {
|
|
195144
|
+
routes.push(` // ${route.moduleName} module routes`);
|
|
195145
|
+
}
|
|
195146
|
+
routes.push(routeLines.join('\n'));
|
|
195147
|
+
});
|
|
195148
|
+
const modulesWith404 = new Set();
|
|
195149
|
+
this.routeFiles.forEach(route => {
|
|
195150
|
+
if (route.notFoundComponent && route.moduleName && !modulesWith404.has(route.moduleName)) {
|
|
195151
|
+
modulesWith404.add(route.moduleName);
|
|
195152
|
+
routes.push(`,`);
|
|
195153
|
+
routes.push(` {`);
|
|
195154
|
+
routes.push(` path: '/${route.moduleName}/*',`);
|
|
195155
|
+
routes.push(` element: <${route.notFoundComponent} />`);
|
|
195156
|
+
routes.push(` }`);
|
|
195145
195157
|
}
|
|
195146
195158
|
});
|
|
195147
|
-
if (
|
|
195148
|
-
if (
|
|
195149
|
-
routes.
|
|
195159
|
+
if (components.has('NotFound')) {
|
|
195160
|
+
if (routes[routes.length - 1].endsWith(',')) {
|
|
195161
|
+
routes[routes.length - 1] = routes[routes.length - 1].slice(0, -1);
|
|
195150
195162
|
}
|
|
195151
|
-
routes.push(
|
|
195152
|
-
routes.push(
|
|
195163
|
+
routes.push(`,`);
|
|
195164
|
+
routes.push(` {`);
|
|
195153
195165
|
routes.push(` path: '*',`);
|
|
195154
195166
|
routes.push(` element: <NotFound />`);
|
|
195155
|
-
routes.push(
|
|
195167
|
+
routes.push(` }`);
|
|
195156
195168
|
}
|
|
195157
195169
|
routes.push('];');
|
|
195158
195170
|
routes.push('');
|
|
@@ -195163,6 +195175,30 @@ class RouteGenerator {
|
|
|
195163
195175
|
...routes
|
|
195164
195176
|
].join('\n');
|
|
195165
195177
|
}
|
|
195178
|
+
findComponentFile(startPath, fileName, moduleName) {
|
|
195179
|
+
const dir = path.dirname(startPath);
|
|
195180
|
+
let currentDir = dir;
|
|
195181
|
+
while (currentDir !== this.config.srcDir && currentDir !== path.dirname(this.config.srcDir)) {
|
|
195182
|
+
const componentPath = path.join(currentDir, `${fileName}.tsx`);
|
|
195183
|
+
if (fs.existsSync(componentPath)) {
|
|
195184
|
+
return componentPath;
|
|
195185
|
+
}
|
|
195186
|
+
if (moduleName) {
|
|
195187
|
+
const modulePath = path.join(this.config.modulesDir, moduleName);
|
|
195188
|
+
if (currentDir === modulePath) {
|
|
195189
|
+
break;
|
|
195190
|
+
}
|
|
195191
|
+
}
|
|
195192
|
+
currentDir = path.dirname(currentDir);
|
|
195193
|
+
}
|
|
195194
|
+
if (!moduleName) {
|
|
195195
|
+
const globalPath = path.join(this.config.pagesDir, `${fileName}.tsx`);
|
|
195196
|
+
if (fs.existsSync(globalPath)) {
|
|
195197
|
+
return globalPath;
|
|
195198
|
+
}
|
|
195199
|
+
}
|
|
195200
|
+
return undefined;
|
|
195201
|
+
}
|
|
195166
195202
|
}
|
|
195167
195203
|
function RouteInitiator() {
|
|
195168
195204
|
return __awaiter(this, arguments, void 0, function* (dirname = __dirname$1) {
|
package/index.min.js
CHANGED
|
@@ -385,5 +385,5 @@ ${0<i.length?i:" // No modules with config.json found"}
|
|
|
385
385
|
}
|
|
386
386
|
};
|
|
387
387
|
|
|
388
|
-
export default configs;`}}function ConfigInitiator(){return __awaiter(this,arguments,void 0,function*(e=__dirname$2){new ConfigGenerator({},e).generate().then(()=>{}).catch(e=>{})})}let __filename$1=url.fileURLToPath("undefined"==typeof document?require("url").pathToFileURL(__filename).href:_documentCurrentScript&&"SCRIPT"===_documentCurrentScript.tagName.toUpperCase()&&_documentCurrentScript.src||new URL("index.js",document.baseURI).href),__dirname$1=path.dirname(__filename$1);class RouteGenerator{constructor(e={},t=__dirname$1){this.routeFiles=[],this.modules=new Set,this.config={srcDir:e.srcDir||pajoExports.Pajo.join(t,"src")||"",modulesDir:e.modulesDir||pajoExports.Pajo.join(t,"src\\modules")||"",pagesDir:e.pagesDir||pajoExports.Pajo.join(t,"src\\pages")||"",outputFile:e.outputFile||pajoExports.Pajo.join(t,"src\\auto-routes.ts")||"",layoutFileName:e.layoutFileName||"_layout",errorFileName:e.errorFileName||"_error",notFoundFileName:e.notFoundFileName||"_404"}}generate(){return __awaiter(this,void 0,void 0,function*(){yield this.findRouteFiles(),yield this.generateAutoRoutesFile()})}findRouteFiles(){return __awaiter(this,void 0,void 0,function*(){var e;if(this.routeFiles=[],yield this.scanDirectoryForRoutes(this.config.pagesDir),fs.existsSync(this.config.modulesDir))for(e of fs.readdirSync(this.config.modulesDir,{withFileTypes:!0}).filter(e=>e.isDirectory()).map(e=>e.name)){var t=path.join(this.config.modulesDir,e,"pages");fs.existsSync(t)&&(this.modules.add(e),yield this.scanDirectoryForRoutes(t,e))}})}scanDirectoryForRoutes(i,a){return __awaiter(this,void 0,void 0,function*(){try{var e;for(e of fs.readdirSync(i,{withFileTypes:!0})){var t,r,n=path.join(i,e.name);"node_modules"===e.name||"dist"===e.name||"build"===e.name||e.name.startsWith(".")||(e.isDirectory()?yield this.scanDirectoryForRoutes(n,a):e.isFile()&&(t=path.extname(e.name).toLowerCase(),[".tsx",".jsx"].includes(t))&&(r=path.basename(e.name,t))!==this.config.layoutFileName&&r!==this.config.errorFileName&&r!==this.config.notFoundFileName&&(yield this.processRouteFile(n,a)))}}catch(e){}})}processRouteFile(o,s){return __awaiter(this,void 0,void 0,function*(){try{var e=
|
|
388
|
+
export default configs;`}}function ConfigInitiator(){return __awaiter(this,arguments,void 0,function*(e=__dirname$2){new ConfigGenerator({},e).generate().then(()=>{}).catch(e=>{})})}let __filename$1=url.fileURLToPath("undefined"==typeof document?require("url").pathToFileURL(__filename).href:_documentCurrentScript&&"SCRIPT"===_documentCurrentScript.tagName.toUpperCase()&&_documentCurrentScript.src||new URL("index.js",document.baseURI).href),__dirname$1=path.dirname(__filename$1);class RouteGenerator{constructor(e={},t=__dirname$1){this.routeFiles=[],this.modules=new Set,this.config={srcDir:e.srcDir||pajoExports.Pajo.join(t,"src")||"",modulesDir:e.modulesDir||pajoExports.Pajo.join(t,"src\\modules")||"",pagesDir:e.pagesDir||pajoExports.Pajo.join(t,"src\\pages")||"",outputFile:e.outputFile||pajoExports.Pajo.join(t,"src\\auto-routes.ts")||"",layoutFileName:e.layoutFileName||"_layout",errorFileName:e.errorFileName||"_error",notFoundFileName:e.notFoundFileName||"_404"}}generate(){return __awaiter(this,void 0,void 0,function*(){yield this.findRouteFiles(),yield this.generateAutoRoutesFile()})}findRouteFiles(){return __awaiter(this,void 0,void 0,function*(){var e;if(this.routeFiles=[],yield this.scanDirectoryForRoutes(this.config.pagesDir),fs.existsSync(this.config.modulesDir))for(e of fs.readdirSync(this.config.modulesDir,{withFileTypes:!0}).filter(e=>e.isDirectory()).map(e=>e.name)){var t=path.join(this.config.modulesDir,e,"pages");fs.existsSync(t)&&(this.modules.add(e),yield this.scanDirectoryForRoutes(t,e))}})}scanDirectoryForRoutes(i,a){return __awaiter(this,void 0,void 0,function*(){try{var e;for(e of fs.readdirSync(i,{withFileTypes:!0})){var t,r,n=path.join(i,e.name);"node_modules"===e.name||"dist"===e.name||"build"===e.name||e.name.startsWith(".")||(e.isDirectory()?yield this.scanDirectoryForRoutes(n,a):e.isFile()&&(t=path.extname(e.name).toLowerCase(),[".tsx",".jsx"].includes(t))&&(r=path.basename(e.name,t))!==this.config.layoutFileName&&r!==this.config.errorFileName&&r!==this.config.notFoundFileName&&(yield this.processRouteFile(n,a)))}}catch(e){}})}processRouteFile(o,s){return __awaiter(this,void 0,void 0,function*(){try{var e=this.convertFilePathToRoutePath(o,s),t=this.extractComponentName(o,s),r=this.findLayoutComponent(o,s),n=this.findErrorComponent(o,s),i=this.findNotFoundComponent(o,s),a={filePath:o,relativePath:path.relative(this.config.srcDir,o),routePath:e,componentName:t,layoutComponent:r,errorComponent:n,notFoundComponent:i,moduleName:s};this.routeFiles.push(a)}catch(e){}})}convertFilePathToRoutePath(e,t){let r=e;return r=r.replace(this.config.srcDir,""),t?(e=`modules${path.sep}${t}${path.sep}pages`,r.includes(e)&&(r="/"+t+(r=r.substring(r.indexOf(e)+e.length)))):r.includes("pages")&&(r=r.substring(r.indexOf("pages")+"pages".length)),"/"===(r=(r=(r=(r=(r=r.replace(/\.(tsx|jsx)$/,"")).replace(/\\/g,"/")).endsWith("/index")?r.replace(/\/index$/,""):r).replace(/\[([^\]]+)\]/g,"{$1}")).startsWith("/")?r:"/"+r)||"//"===r?"/":r}extractComponentName(e,t){var r=path.basename(e,path.extname(e)),e=path.dirname(e).split(path.sep).filter(e=>e&&"src"!==e&&"modules"!==e&&"pages"!==e&&e!==t);let n=t?this.toPascalCase(t):"",i=(e.forEach(e=>{"pages"!==e&&(n+=this.toPascalCase(e))}),r);return(i=r.startsWith("{")&&r.endsWith("}")?(e=r.substring(1,r.length-1),this.toPascalCase(e)+"Params"):this.toPascalCase(r)).endsWith("Page")||(i+="Page"),n+i}toPascalCase(e){return e.split(/[-_]/).map(e=>e.charAt(0).toUpperCase()+e.slice(1).toLowerCase()).join("")}findLayoutComponent(e,t){let r=path.dirname(e);for(;r!==this.config.srcDir&&r!==path.dirname(this.config.srcDir);){var n=path.join(r,this.config.layoutFileName+".tsx");if(fs.existsSync(n))return this.generateComponentName(n,t,"Layout");if(t){n=path.join(this.config.modulesDir,t);if(r===n)break}r=path.dirname(r)}e=path.join(this.config.pagesDir,this.config.layoutFileName+".tsx");if(fs.existsSync(e))return"Layout"}findErrorComponent(e,t){let r=path.dirname(e);for(;r!==this.config.srcDir&&r!==path.dirname(this.config.srcDir);){var n=path.join(r,this.config.errorFileName+".tsx");if(fs.existsSync(n))return this.generateComponentName(n,t,"ErrorBoundary");if(t){n=path.join(this.config.modulesDir,t);if(r===n)break}r=path.dirname(r)}e=path.join(this.config.pagesDir,this.config.errorFileName+".tsx");if(fs.existsSync(e))return"ErrorBoundary"}findNotFoundComponent(e,t){var r;if(t)return r=path.join(this.config.modulesDir,t,"pages"),r=path.join(r,this.config.notFoundFileName+".tsx"),fs.existsSync(r)?this.generateComponentName(r,t,"NotFound"):void 0}generateComponentName(e,t,r){if(!t)return r;e=path.relative(path.join(this.config.modulesDir,t,"pages"),path.dirname(e));let n=this.toPascalCase(t);return e&&"."!==e&&e.split(path.sep).filter(e=>e&&"."!==e).forEach(e=>{n+=this.toPascalCase(e)}),n+r}generateAutoRoutesFile(){return __awaiter(this,void 0,void 0,function*(){var e=path.dirname(this.config.outputFile),e=(fs.existsSync(e)||fs.mkdirSync(e,{recursive:!0}),this.generateFileContent());fs.writeFileSync(this.config.outputFile,e,"utf-8")})}getRelativeImportPath(e){var t=path.dirname(this.config.outputFile);let r=path.relative(t,e).replace(/\\/g,"/");return r=(r=r.startsWith(".")||r.startsWith("/")?r:"./"+r).replace(/\.(tsx|jsx)$/,"")}generateFileContent(){let n=new Set,i=["import { lazy } from 'react';","import type { RouteObject } from 'react-router-dom';",""];var e=path.join(this.config.pagesDir,this.config.layoutFileName+".tsx"),t=path.join(this.config.pagesDir,this.config.errorFileName+".tsx"),r=path.join(this.config.pagesDir,this.config.notFoundFileName+".tsx");fs.existsSync(e)&&(e=this.getRelativeImportPath(e),i.push(`const Layout = lazy(() => import('${e}'));`),n.add("Layout")),fs.existsSync(t)&&(e=this.getRelativeImportPath(t),i.push(`const ErrorBoundary = lazy(() => import('${e}'));`),n.add("ErrorBoundary")),fs.existsSync(r)&&(t=this.getRelativeImportPath(r),i.push(`const NotFound = lazy(() => import('${t}'));`),n.add("NotFound")),i.push("");let a=new Map,o=(this.routeFiles.forEach(t=>{if(t.layoutComponent&&!n.has(t.layoutComponent)){n.add(t.layoutComponent);var r=this.findComponentFile(t.filePath,this.config.layoutFileName,t.moduleName);if(r){let e=this.getRelativeImportPath(r);i.push(`const ${t.layoutComponent} = lazy(() => import('${e}'));`)}}if(t.errorComponent&&!n.has(t.errorComponent)){n.add(t.errorComponent);r=this.findComponentFile(t.filePath,this.config.errorFileName,t.moduleName);if(r){let e=this.getRelativeImportPath(r);i.push(`const ${t.errorComponent} = lazy(() => import('${e}'));`)}}if(t.notFoundComponent&&!n.has(t.notFoundComponent)){n.add(t.notFoundComponent);r=this.findComponentFile(t.filePath,this.config.notFoundFileName,t.moduleName);if(r){let e=this.getRelativeImportPath(r);i.push(`const ${t.notFoundComponent} = lazy(() => import('${e}'));`)}}let e=this.getRelativeImportPath(t.filePath);i.push(`const ${t.componentName} = lazy(() => import('${e}'));`),n.add(t.componentName),t.moduleName&&(a.has(t.moduleName)||a.set(t.moduleName,new Set),a.get(t.moduleName).add(t.componentName))}),i.push(""),["export const routes: RouteObject[] = ["]),s=this.routeFiles.sort((e,t)=>"/"===e.routePath?-1:"/"===t.routePath?1:!e.moduleName&&t.moduleName?-1:e.moduleName&&!t.moduleName?1:e.moduleName===t.moduleName?e.routePath.localeCompare(t.routePath):0),c=(s.forEach((e,t)=>{var r=[" {"];r.push(` path: '${e.routePath}',`),e.layoutComponent?r.push(` element: <${e.layoutComponent}><${e.componentName} /></${e.layoutComponent}>,`):r.push(` element: <${e.componentName} />,`),e.errorComponent&&r.push(` errorElement: <${e.errorComponent} />`),r[r.length-1].endsWith(",")&&(r[r.length-1]=r[r.length-1].slice(0,-1)),r.push(" }"),t<s.length-1&&(r[r.length-1]+=","),!e.moduleName||0!==t&&(null==(t=s[t-1])?void 0:t.moduleName)===e.moduleName||o.push(` // ${e.moduleName} module routes`),o.push(r.join("\n"))}),new Set);return this.routeFiles.forEach(e=>{e.notFoundComponent&&e.moduleName&&!c.has(e.moduleName)&&(c.add(e.moduleName),o.push(","),o.push(" {"),o.push(` path: '/${e.moduleName}/*',`),o.push(` element: <${e.notFoundComponent} />`),o.push(" }"))}),n.has("NotFound")&&(o[o.length-1].endsWith(",")&&(o[o.length-1]=o[o.length-1].slice(0,-1)),o.push(","),o.push(" {"),o.push(" path: '*',"),o.push(" element: <NotFound />"),o.push(" }")),o.push("];"),o.push(""),o.push("export default routes;"),[...i,"",...o].join("\n")}findComponentFile(e,t,r){let n=path.dirname(e);for(;n!==this.config.srcDir&&n!==path.dirname(this.config.srcDir);){var i=path.join(n,t+".tsx");if(fs.existsSync(i))return i;if(r){i=path.join(this.config.modulesDir,r);if(n===i)break}n=path.dirname(n)}if(!r){e=path.join(this.config.pagesDir,t+".tsx");if(fs.existsSync(e))return e}}}function RouteInitiator(){return __awaiter(this,arguments,void 0,function*(e=__dirname$1){e=new RouteGenerator({},e);try{yield e.generate()}catch(e){}})}function index(e=__dirname){TranslationInitiator(e),ConfigInitiator(e),RouteInitiator(e)}module.exports=index;
|
|
389
389
|
//# sourceMappingURL=index.min.js.map
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "0.0.
|
|
6
|
+
"version": "0.0.8",
|
|
7
7
|
"description": "INITIATOR est un système de gestion de configuration modulaire pour les applications React avec TypeScript/JavaScript. Il fournit une gestion centralisée des configurations, un chargement dynamique des modules, et une intégration transparente avec les variables d'environnement.",
|
|
8
8
|
"main": "index.js",
|
|
9
9
|
"keywords": [],
|