@arc-js/initiator 0.0.7 → 0.0.81
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 +265 -172
- 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,160 @@ 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 parts = dirName.split(path.sep);
|
|
194931
|
+
let startIndex = 0;
|
|
194932
|
+
if (moduleName) {
|
|
194933
|
+
const modulePagesPath = path.sep + 'modules' + path.sep + moduleName + path.sep + 'pages';
|
|
194934
|
+
startIndex = parts.findIndex((part, index, arr) => {
|
|
194935
|
+
const pathSoFar = arr.slice(0, index + 1).join(path.sep);
|
|
194936
|
+
return pathSoFar.endsWith(modulePagesPath);
|
|
194937
|
+
}) + 1;
|
|
194938
|
+
}
|
|
194939
|
+
else {
|
|
194940
|
+
const pagesPath = path.sep + 'pages';
|
|
194941
|
+
startIndex = parts.findIndex((part, index, arr) => {
|
|
194942
|
+
const pathSoFar = arr.slice(0, index + 1).join(path.sep);
|
|
194943
|
+
return pathSoFar.endsWith(pagesPath);
|
|
194944
|
+
}) + 1;
|
|
194945
|
+
}
|
|
194946
|
+
if (startIndex < 0)
|
|
194947
|
+
startIndex = 0;
|
|
194948
|
+
const relevantParts = parts.slice(startIndex);
|
|
194949
|
+
let componentName = moduleName ? this.toPascalCase(moduleName) : '';
|
|
194950
|
+
relevantParts.forEach(part => {
|
|
194951
|
+
if (part && part !== 'pages') {
|
|
194952
|
+
componentName += this.toPascalCase(part);
|
|
194953
|
+
}
|
|
194954
|
+
});
|
|
194955
|
+
let fileNamePart = fileName;
|
|
194956
|
+
if (fileName.startsWith('{') && fileName.endsWith('}')) {
|
|
194957
|
+
const paramName = fileName.substring(1, fileName.length - 1);
|
|
194958
|
+
fileNamePart = this.toPascalCase(paramName) + 'Params';
|
|
194959
|
+
}
|
|
194960
|
+
else {
|
|
194961
|
+
fileNamePart = this.toPascalCase(fileName);
|
|
194962
|
+
}
|
|
194963
|
+
if (!fileNamePart.endsWith('Page')) {
|
|
194964
|
+
fileNamePart += 'Page';
|
|
194965
|
+
}
|
|
194966
|
+
const reservedNames = ['Layout', 'ErrorBoundary', 'NotFound'];
|
|
194967
|
+
if (reservedNames.includes(fileNamePart)) {
|
|
194968
|
+
fileNamePart += 'Component';
|
|
194969
|
+
}
|
|
194970
|
+
return componentName + fileNamePart;
|
|
194971
|
+
}
|
|
194972
|
+
toPascalCase(str) {
|
|
194973
|
+
if (!str)
|
|
194974
|
+
return '';
|
|
194975
|
+
return str
|
|
194923
194976
|
.split(/[-_]/)
|
|
194924
194977
|
.map(part => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase())
|
|
194925
194978
|
.join('');
|
|
194926
|
-
const reservedNames = ['Layout', 'Error', 'NotFound', 'ErrorBoundary'];
|
|
194927
|
-
const finalName = reservedNames.includes(pascalName) ? `${pascalName}Page` : pascalName;
|
|
194928
|
-
return `${finalName}Page`;
|
|
194929
194979
|
}
|
|
194930
|
-
|
|
194980
|
+
findLayoutComponent(filePath, moduleName) {
|
|
194931
194981
|
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
194982
|
let currentDir = dir;
|
|
194937
194983
|
while (currentDir !== this.config.srcDir && currentDir !== path.dirname(this.config.srcDir)) {
|
|
194938
|
-
const
|
|
194939
|
-
if (fs.existsSync(
|
|
194940
|
-
return
|
|
194984
|
+
const layoutPath = path.join(currentDir, `${this.config.layoutFileName}.tsx`);
|
|
194985
|
+
if (fs.existsSync(layoutPath)) {
|
|
194986
|
+
return this.generateComponentName(layoutPath, moduleName, 'Layout');
|
|
194941
194987
|
}
|
|
194942
194988
|
if (moduleName) {
|
|
194943
|
-
const
|
|
194944
|
-
if (
|
|
194945
|
-
|
|
194989
|
+
const modulePath = path.join(this.config.modulesDir, moduleName);
|
|
194990
|
+
if (currentDir === modulePath) {
|
|
194991
|
+
break;
|
|
194946
194992
|
}
|
|
194947
194993
|
}
|
|
194948
194994
|
currentDir = path.dirname(currentDir);
|
|
194949
194995
|
}
|
|
194950
194996
|
const globalLayoutPath = path.join(this.config.pagesDir, `${this.config.layoutFileName}.tsx`);
|
|
194951
|
-
|
|
194997
|
+
if (fs.existsSync(globalLayoutPath)) {
|
|
194998
|
+
return 'Layout';
|
|
194999
|
+
}
|
|
195000
|
+
return undefined;
|
|
194952
195001
|
}
|
|
194953
|
-
|
|
195002
|
+
findErrorComponent(filePath, moduleName) {
|
|
194954
195003
|
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
195004
|
let currentDir = dir;
|
|
194960
195005
|
while (currentDir !== this.config.srcDir && currentDir !== path.dirname(this.config.srcDir)) {
|
|
194961
|
-
const
|
|
194962
|
-
if (fs.existsSync(
|
|
194963
|
-
return
|
|
195006
|
+
const errorPath = path.join(currentDir, `${this.config.errorFileName}.tsx`);
|
|
195007
|
+
if (fs.existsSync(errorPath)) {
|
|
195008
|
+
return this.generateComponentName(errorPath, moduleName, 'ErrorBoundary');
|
|
194964
195009
|
}
|
|
194965
195010
|
if (moduleName) {
|
|
194966
|
-
const
|
|
194967
|
-
if (
|
|
194968
|
-
|
|
195011
|
+
const modulePath = path.join(this.config.modulesDir, moduleName);
|
|
195012
|
+
if (currentDir === modulePath) {
|
|
195013
|
+
break;
|
|
194969
195014
|
}
|
|
194970
195015
|
}
|
|
194971
195016
|
currentDir = path.dirname(currentDir);
|
|
194972
195017
|
}
|
|
194973
195018
|
const globalErrorPath = path.join(this.config.pagesDir, `${this.config.errorFileName}.tsx`);
|
|
194974
|
-
|
|
195019
|
+
if (fs.existsSync(globalErrorPath)) {
|
|
195020
|
+
return 'ErrorBoundary';
|
|
195021
|
+
}
|
|
195022
|
+
return undefined;
|
|
195023
|
+
}
|
|
195024
|
+
findNotFoundComponent(filePath, moduleName) {
|
|
195025
|
+
if (!moduleName)
|
|
195026
|
+
return undefined;
|
|
195027
|
+
const modulePagesDir = path.join(this.config.modulesDir, moduleName, 'pages');
|
|
195028
|
+
const notFoundPath = path.join(modulePagesDir, `${this.config.notFoundFileName}.tsx`);
|
|
195029
|
+
if (fs.existsSync(notFoundPath)) {
|
|
195030
|
+
return this.generateComponentName(notFoundPath, moduleName, 'NotFound');
|
|
195031
|
+
}
|
|
195032
|
+
return undefined;
|
|
195033
|
+
}
|
|
195034
|
+
generateComponentName(filePath, moduleName, suffix) {
|
|
195035
|
+
if (!moduleName) {
|
|
195036
|
+
return suffix;
|
|
195037
|
+
}
|
|
195038
|
+
const fileName = path.basename(filePath, path.extname(filePath));
|
|
195039
|
+
const dirName = path.dirname(filePath);
|
|
195040
|
+
const modulePagesPath = path.join(this.config.modulesDir, moduleName, 'pages');
|
|
195041
|
+
const relativePath = path.relative(modulePagesPath, dirName);
|
|
195042
|
+
let componentName = this.toPascalCase(moduleName);
|
|
195043
|
+
if (relativePath && relativePath !== '.') {
|
|
195044
|
+
const dirParts = relativePath.split(path.sep).filter(part => part && part !== '.');
|
|
195045
|
+
dirParts.forEach(part => {
|
|
195046
|
+
componentName += this.toPascalCase(part);
|
|
195047
|
+
});
|
|
195048
|
+
}
|
|
195049
|
+
if (fileName !== this.config.layoutFileName &&
|
|
195050
|
+
fileName !== this.config.errorFileName &&
|
|
195051
|
+
fileName !== this.config.notFoundFileName) {
|
|
195052
|
+
componentName += this.toPascalCase(fileName);
|
|
195053
|
+
}
|
|
195054
|
+
return componentName + suffix;
|
|
194975
195055
|
}
|
|
194976
195056
|
generateAutoRoutesFile() {
|
|
194977
195057
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -194993,166 +195073,179 @@ class RouteGenerator {
|
|
|
194993
195073
|
importPath = importPath.replace(/\.(tsx|jsx)$/, '');
|
|
194994
195074
|
return importPath;
|
|
194995
195075
|
}
|
|
194996
|
-
escapeTemplateString(str) {
|
|
194997
|
-
return str
|
|
194998
|
-
.replace(/\\/g, '\\\\')
|
|
194999
|
-
.replace(/`/g, '\\`')
|
|
195000
|
-
.replace(/\${/g, '\\${');
|
|
195001
|
-
}
|
|
195002
195076
|
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);
|
|
195077
|
+
const components = new Set();
|
|
195022
195078
|
const imports = [
|
|
195023
195079
|
`import { lazy } from 'react';`,
|
|
195024
195080
|
`import type { RouteObject } from 'react-router-dom';`,
|
|
195025
195081
|
``
|
|
195026
195082
|
];
|
|
195027
|
-
|
|
195083
|
+
const globalLayoutPath = path.join(this.config.pagesDir, `${this.config.layoutFileName}.tsx`);
|
|
195084
|
+
const globalErrorPath = path.join(this.config.pagesDir, `${this.config.errorFileName}.tsx`);
|
|
195085
|
+
const global404Path = path.join(this.config.pagesDir, `${this.config.notFoundFileName}.tsx`);
|
|
195086
|
+
if (fs.existsSync(globalLayoutPath)) {
|
|
195028
195087
|
const importPath = this.getRelativeImportPath(globalLayoutPath);
|
|
195029
|
-
imports.push(`const Layout = lazy(() => import('${
|
|
195088
|
+
imports.push(`const Layout = lazy(() => import('${importPath}'));`);
|
|
195089
|
+
components.add('Layout');
|
|
195030
195090
|
}
|
|
195031
|
-
|
|
195032
|
-
imports.push(`// No global layout found`);
|
|
195033
|
-
}
|
|
195034
|
-
if (hasGlobalError) {
|
|
195091
|
+
if (fs.existsSync(globalErrorPath)) {
|
|
195035
195092
|
const importPath = this.getRelativeImportPath(globalErrorPath);
|
|
195036
|
-
imports.push(`const ErrorBoundary = lazy(() => import('${
|
|
195037
|
-
|
|
195038
|
-
else {
|
|
195039
|
-
imports.push(`// No global error boundary found`);
|
|
195093
|
+
imports.push(`const ErrorBoundary = lazy(() => import('${importPath}'));`);
|
|
195094
|
+
components.add('ErrorBoundary');
|
|
195040
195095
|
}
|
|
195041
|
-
if (
|
|
195096
|
+
if (fs.existsSync(global404Path)) {
|
|
195042
195097
|
const importPath = this.getRelativeImportPath(global404Path);
|
|
195043
|
-
imports.push(`const NotFound = lazy(() => import('${
|
|
195044
|
-
|
|
195045
|
-
else {
|
|
195046
|
-
imports.push(`// No 404 page found`);
|
|
195098
|
+
imports.push(`const NotFound = lazy(() => import('${importPath}'));`);
|
|
195099
|
+
components.add('NotFound');
|
|
195047
195100
|
}
|
|
195048
195101
|
imports.push(``);
|
|
195049
|
-
|
|
195050
|
-
|
|
195051
|
-
|
|
195052
|
-
|
|
195053
|
-
const
|
|
195054
|
-
|
|
195055
|
-
|
|
195056
|
-
|
|
195057
|
-
|
|
195058
|
-
|
|
195059
|
-
|
|
195060
|
-
|
|
195061
|
-
|
|
195062
|
-
|
|
195063
|
-
|
|
195064
|
-
|
|
195065
|
-
|
|
195066
|
-
|
|
195102
|
+
const specialComponents = new Map();
|
|
195103
|
+
const findSpecialFiles = (baseDir, isModule = false, moduleName) => {
|
|
195104
|
+
if (!fs.existsSync(baseDir))
|
|
195105
|
+
return;
|
|
195106
|
+
const walkDir = (dir) => {
|
|
195107
|
+
try {
|
|
195108
|
+
const items = fs.readdirSync(dir, { withFileTypes: true });
|
|
195109
|
+
for (const item of items) {
|
|
195110
|
+
const fullPath = path.join(dir, item.name);
|
|
195111
|
+
if (item.isDirectory()) {
|
|
195112
|
+
walkDir(fullPath);
|
|
195113
|
+
}
|
|
195114
|
+
else if (item.isFile()) {
|
|
195115
|
+
const ext = path.extname(item.name).toLowerCase();
|
|
195116
|
+
if (['.tsx', '.jsx'].includes(ext)) {
|
|
195117
|
+
const fileName = path.basename(item.name, ext);
|
|
195118
|
+
if (fileName === this.config.layoutFileName ||
|
|
195119
|
+
fileName === this.config.errorFileName ||
|
|
195120
|
+
fileName === this.config.notFoundFileName) {
|
|
195121
|
+
const componentName = this.generateComponentName(fullPath, moduleName, fileName === this.config.layoutFileName ? 'Layout' :
|
|
195122
|
+
fileName === this.config.errorFileName ? 'ErrorBoundary' : 'NotFound');
|
|
195123
|
+
if (!components.has(componentName)) {
|
|
195124
|
+
specialComponents.set(fullPath, componentName);
|
|
195125
|
+
components.add(componentName);
|
|
195126
|
+
}
|
|
195127
|
+
}
|
|
195128
|
+
}
|
|
195129
|
+
}
|
|
195130
|
+
}
|
|
195131
|
+
}
|
|
195132
|
+
catch (error) {
|
|
195133
|
+
console.error(`Error walking directory ${dir}:`, error);
|
|
195134
|
+
}
|
|
195135
|
+
};
|
|
195136
|
+
walkDir(baseDir);
|
|
195137
|
+
};
|
|
195138
|
+
findSpecialFiles(this.config.pagesDir, false);
|
|
195139
|
+
this.modules.forEach(moduleName => {
|
|
195140
|
+
const modulePagesDir = path.join(this.config.modulesDir, moduleName, 'pages');
|
|
195141
|
+
if (fs.existsSync(modulePagesDir)) {
|
|
195142
|
+
findSpecialFiles(modulePagesDir, true, moduleName);
|
|
195067
195143
|
}
|
|
195068
|
-
imports.push(``);
|
|
195069
195144
|
});
|
|
195070
|
-
|
|
195071
|
-
|
|
195072
|
-
|
|
195073
|
-
|
|
195074
|
-
|
|
195075
|
-
});
|
|
195145
|
+
specialComponents.forEach((componentName, filePath) => {
|
|
195146
|
+
const importPath = this.getRelativeImportPath(filePath);
|
|
195147
|
+
imports.push(`const ${componentName} = lazy(() => import('${importPath}'));`);
|
|
195148
|
+
});
|
|
195149
|
+
if (specialComponents.size > 0) {
|
|
195076
195150
|
imports.push(``);
|
|
195077
195151
|
}
|
|
195078
|
-
|
|
195079
|
-
|
|
195080
|
-
|
|
195081
|
-
|
|
195082
|
-
const importPath = this.getRelativeImportPath(route.filePath);
|
|
195083
|
-
imports.push(`const ${route.componentName} = lazy(() => import('${this.escapeTemplateString(importPath)}'));`);
|
|
195084
|
-
});
|
|
195085
|
-
imports.push(``);
|
|
195086
|
-
}
|
|
195152
|
+
this.routeFiles.forEach(route => {
|
|
195153
|
+
const importPath = this.getRelativeImportPath(route.filePath);
|
|
195154
|
+
imports.push(`const ${route.componentName} = lazy(() => import('${importPath}'));`);
|
|
195155
|
+
components.add(route.componentName);
|
|
195087
195156
|
});
|
|
195157
|
+
imports.push(``);
|
|
195088
195158
|
const routes = ['export const routes: RouteObject[] = ['];
|
|
195089
|
-
|
|
195090
|
-
|
|
195091
|
-
|
|
195092
|
-
|
|
195093
|
-
|
|
195094
|
-
|
|
195095
|
-
|
|
195096
|
-
|
|
195097
|
-
|
|
195098
|
-
|
|
195099
|
-
|
|
195100
|
-
|
|
195159
|
+
const sortedRoutes = this.routeFiles.sort((a, b) => {
|
|
195160
|
+
if (a.routePath === '/')
|
|
195161
|
+
return -1;
|
|
195162
|
+
if (b.routePath === '/')
|
|
195163
|
+
return 1;
|
|
195164
|
+
if (!a.moduleName && b.moduleName)
|
|
195165
|
+
return -1;
|
|
195166
|
+
if (a.moduleName && !b.moduleName)
|
|
195167
|
+
return 1;
|
|
195168
|
+
if (a.moduleName === b.moduleName) {
|
|
195169
|
+
return a.routePath.localeCompare(b.routePath);
|
|
195170
|
+
}
|
|
195171
|
+
return 0;
|
|
195172
|
+
});
|
|
195173
|
+
let lastModuleName = undefined;
|
|
195174
|
+
sortedRoutes.forEach((route, index) => {
|
|
195175
|
+
if (route.moduleName !== lastModuleName) {
|
|
195176
|
+
if (lastModuleName !== undefined) {
|
|
195177
|
+
routes.push(``);
|
|
195101
195178
|
}
|
|
195102
|
-
|
|
195103
|
-
|
|
195104
|
-
routeObj[routeObj.length - 1] = lastLine + ',';
|
|
195179
|
+
if (route.moduleName) {
|
|
195180
|
+
routes.push(` // ${route.moduleName} module routes`);
|
|
195105
195181
|
}
|
|
195106
|
-
|
|
195107
|
-
|
|
195108
|
-
|
|
195182
|
+
lastModuleName = route.moduleName;
|
|
195183
|
+
}
|
|
195184
|
+
const routeLines = [' {'];
|
|
195185
|
+
routeLines.push(` path: '${route.routePath}',`);
|
|
195186
|
+
let layoutComponent = route.layoutComponent;
|
|
195187
|
+
if (!layoutComponent && route.moduleName) {
|
|
195188
|
+
const moduleLayoutPath = path.join(this.config.modulesDir, route.moduleName, 'pages', `${this.config.layoutFileName}.tsx`);
|
|
195189
|
+
if (fs.existsSync(moduleLayoutPath)) {
|
|
195190
|
+
layoutComponent = this.generateComponentName(moduleLayoutPath, route.moduleName, 'Layout');
|
|
195109
195191
|
}
|
|
195110
|
-
|
|
195111
|
-
|
|
195112
|
-
|
|
195113
|
-
|
|
195114
|
-
|
|
195115
|
-
|
|
195116
|
-
|
|
195192
|
+
}
|
|
195193
|
+
if (layoutComponent) {
|
|
195194
|
+
routeLines.push(` element: <${layoutComponent}><${route.componentName} /></${layoutComponent}>,`);
|
|
195195
|
+
}
|
|
195196
|
+
else {
|
|
195197
|
+
routeLines.push(` element: <${route.componentName} />,`);
|
|
195198
|
+
}
|
|
195199
|
+
let errorComponent = route.errorComponent;
|
|
195200
|
+
if (!errorComponent && route.moduleName) {
|
|
195201
|
+
const moduleErrorPath = path.join(this.config.modulesDir, route.moduleName, 'pages', `${this.config.errorFileName}.tsx`);
|
|
195202
|
+
if (fs.existsSync(moduleErrorPath)) {
|
|
195203
|
+
errorComponent = this.generateComponentName(moduleErrorPath, route.moduleName, 'ErrorBoundary');
|
|
195117
195204
|
}
|
|
195118
|
-
|
|
195119
|
-
|
|
195120
|
-
|
|
195121
|
-
|
|
195122
|
-
|
|
195123
|
-
|
|
195124
|
-
|
|
195125
|
-
|
|
195126
|
-
|
|
195127
|
-
|
|
195128
|
-
|
|
195129
|
-
|
|
195130
|
-
|
|
195131
|
-
|
|
195132
|
-
|
|
195133
|
-
|
|
195134
|
-
|
|
195135
|
-
|
|
195136
|
-
|
|
195137
|
-
|
|
195138
|
-
const
|
|
195139
|
-
|
|
195140
|
-
|
|
195141
|
-
|
|
195205
|
+
}
|
|
195206
|
+
if (errorComponent) {
|
|
195207
|
+
routeLines.push(` errorElement: <${errorComponent} />`);
|
|
195208
|
+
}
|
|
195209
|
+
if (routeLines[routeLines.length - 1].endsWith(',')) {
|
|
195210
|
+
routeLines[routeLines.length - 1] = routeLines[routeLines.length - 1].slice(0, -1);
|
|
195211
|
+
}
|
|
195212
|
+
routeLines.push(' }');
|
|
195213
|
+
if (index < sortedRoutes.length - 1) {
|
|
195214
|
+
routeLines[routeLines.length - 1] += ',';
|
|
195215
|
+
}
|
|
195216
|
+
routes.push(routeLines.join('\n'));
|
|
195217
|
+
});
|
|
195218
|
+
const modulesWith404 = new Set();
|
|
195219
|
+
specialComponents.forEach((componentName, filePath) => {
|
|
195220
|
+
const fileName = path.basename(filePath, path.extname(filePath));
|
|
195221
|
+
if (fileName === this.config.notFoundFileName) {
|
|
195222
|
+
const pathParts = filePath.split(path.sep);
|
|
195223
|
+
const modulesIndex = pathParts.indexOf('modules');
|
|
195224
|
+
if (modulesIndex !== -1 && modulesIndex + 1 < pathParts.length) {
|
|
195225
|
+
const moduleName = pathParts[modulesIndex + 1];
|
|
195226
|
+
if (!modulesWith404.has(moduleName)) {
|
|
195227
|
+
modulesWith404.add(moduleName);
|
|
195228
|
+
if (routes[routes.length - 1].endsWith(',')) {
|
|
195229
|
+
routes[routes.length - 1] = routes[routes.length - 1].slice(0, -1);
|
|
195230
|
+
}
|
|
195231
|
+
routes.push(`,`);
|
|
195232
|
+
routes.push(` {`);
|
|
195233
|
+
routes.push(` path: '/${moduleName}/*',`);
|
|
195234
|
+
routes.push(` element: <${componentName} />`);
|
|
195235
|
+
routes.push(` }`);
|
|
195142
195236
|
}
|
|
195143
|
-
|
|
195144
|
-
});
|
|
195237
|
+
}
|
|
195145
195238
|
}
|
|
195146
195239
|
});
|
|
195147
|
-
if (
|
|
195148
|
-
if (
|
|
195149
|
-
routes.
|
|
195240
|
+
if (components.has('NotFound')) {
|
|
195241
|
+
if (routes[routes.length - 1].endsWith(',')) {
|
|
195242
|
+
routes[routes.length - 1] = routes[routes.length - 1].slice(0, -1);
|
|
195150
195243
|
}
|
|
195151
|
-
routes.push(
|
|
195152
|
-
routes.push(
|
|
195244
|
+
routes.push(`,`);
|
|
195245
|
+
routes.push(` {`);
|
|
195153
195246
|
routes.push(` path: '*',`);
|
|
195154
195247
|
routes.push(` element: <NotFound />`);
|
|
195155
|
-
routes.push(
|
|
195248
|
+
routes.push(` }`);
|
|
195156
195249
|
}
|
|
195157
195250
|
routes.push('];');
|
|
195158
195251
|
routes.push('');
|
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);let i=0;if(t){let n=path.sep+"modules"+path.sep+t+path.sep+"pages";i=e.findIndex((e,t,r)=>r.slice(0,t+1).join(path.sep).endsWith(n))+1}else{let n=path.sep+"pages";i=e.findIndex((e,t,r)=>r.slice(0,t+1).join(path.sep).endsWith(n))+1}i<0&&(i=0);e=e.slice(i);let n=t?this.toPascalCase(t):"",a=(e.forEach(e=>{e&&"pages"!==e&&(n+=this.toPascalCase(e))}),r);(a=r.startsWith("{")&&r.endsWith("}")?(t=r.substring(1,r.length-1),this.toPascalCase(t)+"Params"):this.toPascalCase(r)).endsWith("Page")||(a+="Page");return["Layout","ErrorBoundary","NotFound"].includes(a)&&(a+="Component"),n+a}toPascalCase(e){return e?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;var n=path.basename(e,path.extname(e)),e=path.dirname(e),i=path.join(this.config.modulesDir,t,"pages"),i=path.relative(i,e);let a=this.toPascalCase(t);return i&&"."!==i&&i.split(path.sep).filter(e=>e&&"."!==e).forEach(e=>{a+=this.toPascalCase(e)}),n!==this.config.layoutFileName&&n!==this.config.errorFileName&&n!==this.config.notFoundFileName&&(a+=this.toPascalCase(n)),a+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 c=new Set,r=["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"),n=path.join(this.config.pagesDir,this.config.notFoundFileName+".tsx");fs.existsSync(e)&&(e=this.getRelativeImportPath(e),r.push(`const Layout = lazy(() => import('${e}'));`),c.add("Layout")),fs.existsSync(t)&&(e=this.getRelativeImportPath(t),r.push(`const ErrorBoundary = lazy(() => import('${e}'));`),c.add("ErrorBoundary")),fs.existsSync(n)&&(t=this.getRelativeImportPath(n),r.push(`const NotFound = lazy(() => import('${t}'));`),c.add("NotFound")),r.push("");let l=new Map,i=(e,t=0,s)=>{if(fs.existsSync(e)){let o=e=>{try{var t;for(t of fs.readdirSync(e,{withFileTypes:!0})){var r,n,i,a=path.join(e,t.name);t.isDirectory()?o(a):t.isFile()&&(r=path.extname(t.name).toLowerCase(),![".tsx",".jsx"].includes(r)||(n=path.basename(t.name,r))!==this.config.layoutFileName&&n!==this.config.errorFileName&&n!==this.config.notFoundFileName||(i=this.generateComponentName(a,s,n===this.config.layoutFileName?"Layout":n===this.config.errorFileName?"ErrorBoundary":"NotFound"),c.has(i))||(l.set(a,i),c.add(i)))}}catch(e){}};o(e)}},o=(i(this.config.pagesDir,!1),this.modules.forEach(e=>{var t=path.join(this.config.modulesDir,e,"pages");fs.existsSync(t)&&i(t,!0,e)}),l.forEach((e,t)=>{t=this.getRelativeImportPath(t);r.push(`const ${e} = lazy(() => import('${t}'));`)}),0<l.size&&r.push(""),this.routeFiles.forEach(e=>{var t=this.getRelativeImportPath(e.filePath);r.push(`const ${e.componentName} = lazy(() => import('${t}'));`),c.add(e.componentName)}),r.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),_=void 0,a=(s.forEach((e,t)=>{e.moduleName!==_&&(void 0!==_&&o.push(""),e.moduleName&&o.push(` // ${e.moduleName} module routes`),_=e.moduleName);var r,n=[" {"];n.push(` path: '${e.routePath}',`);let i=e.layoutComponent,a=((i=!i&&e.moduleName&&(r=path.join(this.config.modulesDir,e.moduleName,"pages",this.config.layoutFileName+".tsx"),fs.existsSync(r))?this.generateComponentName(r,e.moduleName,"Layout"):i)?n.push(` element: <${i}><${e.componentName} /></${i}>,`):n.push(` element: <${e.componentName} />,`),e.errorComponent);(a=!a&&e.moduleName&&(r=path.join(this.config.modulesDir,e.moduleName,"pages",this.config.errorFileName+".tsx"),fs.existsSync(r))?this.generateComponentName(r,e.moduleName,"ErrorBoundary"):a)&&n.push(` errorElement: <${a} />`),n[n.length-1].endsWith(",")&&(n[n.length-1]=n[n.length-1].slice(0,-1)),n.push(" }"),t<s.length-1&&(n[n.length-1]+=","),o.push(n.join("\n"))}),new Set);return l.forEach((e,t)=>{var r;path.basename(t,path.extname(t))===this.config.notFoundFileName&&-1!==(r=(t=t.split(path.sep)).indexOf("modules"))&&r+1<t.length&&(t=t[r+1],a.has(t)||(a.add(t),o[o.length-1].endsWith(",")&&(o[o.length-1]=o[o.length-1].slice(0,-1)),o.push(","),o.push(" {"),o.push(` path: '/${t}/*',`),o.push(` element: <${e} />`),o.push(" }")))}),c.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;"),[...r,"",...o].join("\n")}}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.81",
|
|
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": [],
|