@arcpkg/initiator 0.0.1-beta.0.1
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 +524 -0
- package/config.ts +153 -0
- package/index.ts +23 -0
- package/intl.ts +366 -0
- package/js/config.js +110 -0
- package/js/index.js +17 -0
- package/js/intl.js +287 -0
- package/js/native-routing.js +696 -0
- package/js/provider.js +303 -0
- package/js/routing.js +533 -0
- package/native-routing.ts +871 -0
- package/package.json +21 -0
- package/provider.ts +403 -0
- package/routing.ts +691 -0
- package/tsconfig.json +27 -0
|
@@ -0,0 +1,696 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
import { Pajo } from '@arcpkg/pajo';
|
|
5
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = path.dirname(__filename);
|
|
7
|
+
export class NativeRouteGenerator {
|
|
8
|
+
config;
|
|
9
|
+
routeFiles = [];
|
|
10
|
+
modules = new Set();
|
|
11
|
+
modulePathOverrides = new Map();
|
|
12
|
+
constructor(config = {}, dirname = __dirname) {
|
|
13
|
+
this.config = {
|
|
14
|
+
srcDir: config.srcDir || Pajo.join(dirname, 'src') || '',
|
|
15
|
+
modulesDir: config.modulesDir || Pajo.join(dirname, 'src\\modules') || '',
|
|
16
|
+
pagesDir: config.pagesDir || Pajo.join(dirname, 'src\\pages') || '',
|
|
17
|
+
outputFile: config.outputFile || Pajo.join(dirname, 'src\\auto-routes.tsx') || '',
|
|
18
|
+
layoutFileName: config.layoutFileName || '_layout',
|
|
19
|
+
errorFileName: config.errorFileName || '_error',
|
|
20
|
+
notFoundFileName: config.notFoundFileName || '_404'
|
|
21
|
+
};
|
|
22
|
+
console.log(`--> NativeRouteGenerator config:: `, this.config);
|
|
23
|
+
}
|
|
24
|
+
async generate() {
|
|
25
|
+
console.log('🔍 Scanning for native route files...');
|
|
26
|
+
await this.loadModulePathOverrides();
|
|
27
|
+
await this.findRouteFiles();
|
|
28
|
+
await this.generateNativeRoutesFile();
|
|
29
|
+
console.log(`✅ Generated ${this.config.outputFile} with ${this.routeFiles.length} native routes`);
|
|
30
|
+
console.log(`📦 Found modules with routes: ${Array.from(this.modules).join(', ')}`);
|
|
31
|
+
console.log(`🔄 Module path overrides: ${Array.from(this.modulePathOverrides.entries()).map(([k, v]) => `${k} -> ${v}`).join(', ')}`);
|
|
32
|
+
}
|
|
33
|
+
async loadModulePathOverrides() {
|
|
34
|
+
try {
|
|
35
|
+
if (!fs.existsSync(this.config.modulesDir)) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const moduleDirs = fs.readdirSync(this.config.modulesDir, { withFileTypes: true })
|
|
39
|
+
.filter(dirent => dirent.isDirectory())
|
|
40
|
+
.map(dirent => dirent.name);
|
|
41
|
+
for (const moduleName of moduleDirs) {
|
|
42
|
+
const configPath = path.join(this.config.modulesDir, moduleName, 'config.json');
|
|
43
|
+
if (fs.existsSync(configPath)) {
|
|
44
|
+
try {
|
|
45
|
+
const configContent = fs.readFileSync(configPath, 'utf-8');
|
|
46
|
+
const config = JSON.parse(configContent);
|
|
47
|
+
if (config.path && typeof config.path === 'string') {
|
|
48
|
+
let modulePath = config.path.trim();
|
|
49
|
+
if (modulePath.startsWith('/')) {
|
|
50
|
+
modulePath = modulePath.substring(1);
|
|
51
|
+
}
|
|
52
|
+
if (modulePath.endsWith('/')) {
|
|
53
|
+
modulePath = modulePath.slice(0, -1);
|
|
54
|
+
}
|
|
55
|
+
if (modulePath) {
|
|
56
|
+
this.modulePathOverrides.set(moduleName, modulePath);
|
|
57
|
+
console.log(`🔄 Module ${moduleName} path override: ${modulePath}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
console.error(`Error reading config.json for module ${moduleName}:`, error);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
console.error('Error loading module path overrides:', error);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async findRouteFiles() {
|
|
72
|
+
this.routeFiles = [];
|
|
73
|
+
await this.scanDirectoryForRoutes(this.config.pagesDir);
|
|
74
|
+
if (fs.existsSync(this.config.modulesDir)) {
|
|
75
|
+
const moduleDirs = fs.readdirSync(this.config.modulesDir, { withFileTypes: true })
|
|
76
|
+
.filter(dirent => dirent.isDirectory())
|
|
77
|
+
.map(dirent => dirent.name);
|
|
78
|
+
for (const moduleName of moduleDirs) {
|
|
79
|
+
const modulePagesDir = path.join(this.config.modulesDir, moduleName, 'pages');
|
|
80
|
+
if (fs.existsSync(modulePagesDir)) {
|
|
81
|
+
this.modules.add(moduleName);
|
|
82
|
+
await this.scanDirectoryForRoutes(modulePagesDir, moduleName);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
async scanDirectoryForRoutes(dir, moduleName) {
|
|
88
|
+
try {
|
|
89
|
+
const items = fs.readdirSync(dir, { withFileTypes: true });
|
|
90
|
+
for (const item of items) {
|
|
91
|
+
const fullPath = path.join(dir, item.name);
|
|
92
|
+
if (item.name === 'node_modules' ||
|
|
93
|
+
item.name === 'dist' ||
|
|
94
|
+
item.name === 'build' ||
|
|
95
|
+
item.name.startsWith('.')) {
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (item.isDirectory()) {
|
|
99
|
+
await this.scanDirectoryForRoutes(fullPath, moduleName);
|
|
100
|
+
}
|
|
101
|
+
else if (item.isFile()) {
|
|
102
|
+
const ext = path.extname(item.name).toLowerCase();
|
|
103
|
+
if (['.tsx', '.jsx'].includes(ext)) {
|
|
104
|
+
const fileName = path.basename(item.name, ext);
|
|
105
|
+
if (fileName === this.config.layoutFileName ||
|
|
106
|
+
fileName === this.config.errorFileName ||
|
|
107
|
+
fileName === this.config.notFoundFileName) {
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
await this.processRouteFile(fullPath, moduleName);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
console.error(`Error scanning directory ${dir}:`, error);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
async processRouteFile(filePath, moduleName) {
|
|
120
|
+
try {
|
|
121
|
+
let routePath = this.convertFilePathToRoutePath(filePath, moduleName);
|
|
122
|
+
const componentName = this.extractComponentName(filePath, moduleName);
|
|
123
|
+
const layoutComponent = this.findLayoutComponent(filePath, moduleName);
|
|
124
|
+
const errorComponent = this.findErrorComponent(filePath, moduleName);
|
|
125
|
+
const notFoundComponent = this.findNotFoundComponent(filePath, moduleName);
|
|
126
|
+
const routeFile = {
|
|
127
|
+
filePath,
|
|
128
|
+
relativePath: path.relative(this.config.srcDir, filePath),
|
|
129
|
+
routePath,
|
|
130
|
+
componentName,
|
|
131
|
+
layoutComponent,
|
|
132
|
+
errorComponent,
|
|
133
|
+
notFoundComponent,
|
|
134
|
+
moduleName
|
|
135
|
+
};
|
|
136
|
+
this.routeFiles.push(routeFile);
|
|
137
|
+
}
|
|
138
|
+
catch (error) {
|
|
139
|
+
console.error(`Error processing route file ${filePath}:`, error);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
convertFilePathToRoutePath(filePath, moduleName) {
|
|
143
|
+
let routePath = filePath;
|
|
144
|
+
routePath = routePath.replace(this.config.srcDir, '');
|
|
145
|
+
if (moduleName) {
|
|
146
|
+
const modulePagesPrefix = `modules${path.sep}${moduleName}${path.sep}pages`;
|
|
147
|
+
if (routePath.includes(modulePagesPrefix)) {
|
|
148
|
+
routePath = routePath.substring(routePath.indexOf(modulePagesPrefix) + modulePagesPrefix.length);
|
|
149
|
+
let modulePrefix = `/${moduleName}`;
|
|
150
|
+
if (this.modulePathOverrides.has(moduleName)) {
|
|
151
|
+
const overridePath = this.modulePathOverrides.get(moduleName);
|
|
152
|
+
if (overridePath) {
|
|
153
|
+
modulePrefix = `/${overridePath}`;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
routePath = modulePrefix + routePath;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
const pagesPrefix = 'pages';
|
|
161
|
+
if (routePath.includes(pagesPrefix)) {
|
|
162
|
+
routePath = routePath.substring(routePath.indexOf(pagesPrefix) + pagesPrefix.length);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
routePath = routePath.replace(/\.(tsx|jsx)$/, '');
|
|
166
|
+
routePath = routePath.replace(/\\/g, '/');
|
|
167
|
+
if (routePath.endsWith('/index')) {
|
|
168
|
+
routePath = routePath.replace(/\/index$/, '');
|
|
169
|
+
}
|
|
170
|
+
routePath = routePath.replace(/\{([^}]+)\}/g, ':$1');
|
|
171
|
+
if (!routePath.startsWith('/')) {
|
|
172
|
+
routePath = '/' + routePath;
|
|
173
|
+
}
|
|
174
|
+
if (routePath === '/' || routePath === '//') {
|
|
175
|
+
return '/';
|
|
176
|
+
}
|
|
177
|
+
return routePath;
|
|
178
|
+
}
|
|
179
|
+
extractComponentName(filePath, moduleName) {
|
|
180
|
+
const fileName = path.basename(filePath, path.extname(filePath));
|
|
181
|
+
const dirName = path.dirname(filePath);
|
|
182
|
+
const parts = dirName.split(path.sep);
|
|
183
|
+
let startIndex = 0;
|
|
184
|
+
if (moduleName) {
|
|
185
|
+
const modulePagesPath = path.sep + 'modules' + path.sep + moduleName + path.sep + 'pages';
|
|
186
|
+
startIndex = parts.findIndex((part, index, arr) => {
|
|
187
|
+
const pathSoFar = arr.slice(0, index + 1).join(path.sep);
|
|
188
|
+
return pathSoFar.endsWith(modulePagesPath);
|
|
189
|
+
}) + 1;
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
const pagesPath = path.sep + 'pages';
|
|
193
|
+
startIndex = parts.findIndex((part, index, arr) => {
|
|
194
|
+
const pathSoFar = arr.slice(0, index + 1).join(path.sep);
|
|
195
|
+
return pathSoFar.endsWith(pagesPath);
|
|
196
|
+
}) + 1;
|
|
197
|
+
}
|
|
198
|
+
if (startIndex < 0)
|
|
199
|
+
startIndex = 0;
|
|
200
|
+
const relevantParts = parts.slice(startIndex);
|
|
201
|
+
let componentName = moduleName ? this.toPascalCase(moduleName) : '';
|
|
202
|
+
relevantParts.forEach(part => {
|
|
203
|
+
if (part && part !== 'pages') {
|
|
204
|
+
componentName += this.toPascalCase(part);
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
let fileNamePart = fileName;
|
|
208
|
+
if (fileName.startsWith('{') && fileName.endsWith('}')) {
|
|
209
|
+
const paramName = fileName.substring(1, fileName.length - 1);
|
|
210
|
+
fileNamePart = this.toPascalCase(paramName) + 'Params';
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
fileNamePart = this.toPascalCase(fileName);
|
|
214
|
+
}
|
|
215
|
+
if (!fileNamePart.endsWith('Page')) {
|
|
216
|
+
fileNamePart += 'Page';
|
|
217
|
+
}
|
|
218
|
+
const reservedNames = ['Layout', 'ErrorBoundary', 'NotFound'];
|
|
219
|
+
if (reservedNames.includes(fileNamePart)) {
|
|
220
|
+
fileNamePart += 'Component';
|
|
221
|
+
}
|
|
222
|
+
return componentName + fileNamePart;
|
|
223
|
+
}
|
|
224
|
+
toPascalCase(str) {
|
|
225
|
+
if (!str)
|
|
226
|
+
return '';
|
|
227
|
+
return str
|
|
228
|
+
.split(/[-_]/)
|
|
229
|
+
.map(part => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase())
|
|
230
|
+
.join('');
|
|
231
|
+
}
|
|
232
|
+
findLayoutComponent(filePath, moduleName) {
|
|
233
|
+
const dir = path.dirname(filePath);
|
|
234
|
+
let currentDir = dir;
|
|
235
|
+
while (currentDir !== this.config.srcDir && currentDir !== path.dirname(this.config.srcDir)) {
|
|
236
|
+
const layoutPath = path.join(currentDir, `${this.config.layoutFileName}.tsx`);
|
|
237
|
+
if (fs.existsSync(layoutPath)) {
|
|
238
|
+
return this.generateComponentName(layoutPath, moduleName, 'Layout');
|
|
239
|
+
}
|
|
240
|
+
if (moduleName) {
|
|
241
|
+
const modulePath = path.join(this.config.modulesDir, moduleName);
|
|
242
|
+
if (currentDir === modulePath) {
|
|
243
|
+
break;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
currentDir = path.dirname(currentDir);
|
|
247
|
+
}
|
|
248
|
+
const globalLayoutPath = path.join(this.config.pagesDir, `${this.config.layoutFileName}.tsx`);
|
|
249
|
+
if (fs.existsSync(globalLayoutPath)) {
|
|
250
|
+
return 'Layout';
|
|
251
|
+
}
|
|
252
|
+
return undefined;
|
|
253
|
+
}
|
|
254
|
+
findErrorComponent(filePath, moduleName) {
|
|
255
|
+
const dir = path.dirname(filePath);
|
|
256
|
+
let currentDir = dir;
|
|
257
|
+
while (currentDir !== this.config.srcDir && currentDir !== path.dirname(this.config.srcDir)) {
|
|
258
|
+
const errorPath = path.join(currentDir, `${this.config.errorFileName}.tsx`);
|
|
259
|
+
if (fs.existsSync(errorPath)) {
|
|
260
|
+
return this.generateComponentName(errorPath, moduleName, 'ErrorBoundary');
|
|
261
|
+
}
|
|
262
|
+
if (moduleName) {
|
|
263
|
+
const modulePath = path.join(this.config.modulesDir, moduleName);
|
|
264
|
+
if (currentDir === modulePath) {
|
|
265
|
+
break;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
currentDir = path.dirname(currentDir);
|
|
269
|
+
}
|
|
270
|
+
const globalErrorPath = path.join(this.config.pagesDir, `${this.config.errorFileName}.tsx`);
|
|
271
|
+
if (fs.existsSync(globalErrorPath)) {
|
|
272
|
+
return 'ErrorBoundary';
|
|
273
|
+
}
|
|
274
|
+
return undefined;
|
|
275
|
+
}
|
|
276
|
+
findNotFoundComponent(filePath, moduleName) {
|
|
277
|
+
if (!moduleName)
|
|
278
|
+
return undefined;
|
|
279
|
+
const modulePagesDir = path.join(this.config.modulesDir, moduleName, 'pages');
|
|
280
|
+
const notFoundPath = path.join(modulePagesDir, `${this.config.notFoundFileName}.tsx`);
|
|
281
|
+
if (fs.existsSync(notFoundPath)) {
|
|
282
|
+
return this.generateComponentName(notFoundPath, moduleName, 'NotFound');
|
|
283
|
+
}
|
|
284
|
+
return undefined;
|
|
285
|
+
}
|
|
286
|
+
generateComponentName(filePath, moduleName, suffix) {
|
|
287
|
+
if (!moduleName) {
|
|
288
|
+
return suffix;
|
|
289
|
+
}
|
|
290
|
+
const fileName = path.basename(filePath, path.extname(filePath));
|
|
291
|
+
const dirName = path.dirname(filePath);
|
|
292
|
+
const modulePagesPath = path.join(this.config.modulesDir, moduleName, 'pages');
|
|
293
|
+
const relativePath = path.relative(modulePagesPath, dirName);
|
|
294
|
+
let componentName = this.toPascalCase(moduleName);
|
|
295
|
+
if (relativePath && relativePath !== '.') {
|
|
296
|
+
const dirParts = relativePath.split(path.sep).filter(part => part && part !== '.');
|
|
297
|
+
dirParts.forEach(part => {
|
|
298
|
+
componentName += this.toPascalCase(part);
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
if (fileName !== this.config.layoutFileName &&
|
|
302
|
+
fileName !== this.config.errorFileName &&
|
|
303
|
+
fileName !== this.config.notFoundFileName) {
|
|
304
|
+
componentName += this.toPascalCase(fileName);
|
|
305
|
+
}
|
|
306
|
+
return componentName + suffix;
|
|
307
|
+
}
|
|
308
|
+
async generateNativeRoutesFile() {
|
|
309
|
+
const outputDir = path.dirname(this.config.outputFile);
|
|
310
|
+
if (!fs.existsSync(outputDir)) {
|
|
311
|
+
fs.mkdirSync(outputDir, { recursive: true });
|
|
312
|
+
}
|
|
313
|
+
const content = this.generateFileContent();
|
|
314
|
+
fs.writeFileSync(this.config.outputFile, content, 'utf-8');
|
|
315
|
+
}
|
|
316
|
+
getRelativeImportPath(targetPath) {
|
|
317
|
+
const outputDir = path.dirname(this.config.outputFile);
|
|
318
|
+
const relative = path.relative(outputDir, targetPath);
|
|
319
|
+
let importPath = relative.replace(/\\/g, '/');
|
|
320
|
+
if (!importPath.startsWith('.') && !importPath.startsWith('/')) {
|
|
321
|
+
importPath = './' + importPath;
|
|
322
|
+
}
|
|
323
|
+
importPath = importPath.replace(/\.(tsx|jsx)$/, '');
|
|
324
|
+
return importPath;
|
|
325
|
+
}
|
|
326
|
+
generateFileContent() {
|
|
327
|
+
const components = new Set();
|
|
328
|
+
const imports = [
|
|
329
|
+
`import React from 'react';`,
|
|
330
|
+
`import { View, StyleSheet, Text } from 'react-native';`,
|
|
331
|
+
`import type { NativeStackNavigationOptions } from '@react-navigation/native-stack';`,
|
|
332
|
+
``
|
|
333
|
+
];
|
|
334
|
+
const globalLayoutPath = path.join(this.config.pagesDir, `${this.config.layoutFileName}.tsx`);
|
|
335
|
+
const globalErrorPath = path.join(this.config.pagesDir, `${this.config.errorFileName}.tsx`);
|
|
336
|
+
const global404Path = path.join(this.config.pagesDir, `${this.config.notFoundFileName}.tsx`);
|
|
337
|
+
if (fs.existsSync(globalLayoutPath)) {
|
|
338
|
+
const importPath = this.getRelativeImportPath(globalLayoutPath);
|
|
339
|
+
imports.push(`import Layout from '${importPath}';`);
|
|
340
|
+
components.add('Layout');
|
|
341
|
+
}
|
|
342
|
+
if (fs.existsSync(globalErrorPath)) {
|
|
343
|
+
const importPath = this.getRelativeImportPath(globalErrorPath);
|
|
344
|
+
imports.push(`import ErrorBoundary from '${importPath}';`);
|
|
345
|
+
components.add('ErrorBoundary');
|
|
346
|
+
}
|
|
347
|
+
if (fs.existsSync(global404Path)) {
|
|
348
|
+
const importPath = this.getRelativeImportPath(global404Path);
|
|
349
|
+
imports.push(`import NotFound from '${importPath}';`);
|
|
350
|
+
components.add('NotFound');
|
|
351
|
+
}
|
|
352
|
+
imports.push(``);
|
|
353
|
+
const specialComponents = new Map();
|
|
354
|
+
const findSpecialFiles = (baseDir, isModule = false, moduleName) => {
|
|
355
|
+
if (!fs.existsSync(baseDir))
|
|
356
|
+
return;
|
|
357
|
+
const walkDir = (dir) => {
|
|
358
|
+
try {
|
|
359
|
+
const items = fs.readdirSync(dir, { withFileTypes: true });
|
|
360
|
+
for (const item of items) {
|
|
361
|
+
const fullPath = path.join(dir, item.name);
|
|
362
|
+
if (item.isDirectory()) {
|
|
363
|
+
walkDir(fullPath);
|
|
364
|
+
}
|
|
365
|
+
else if (item.isFile()) {
|
|
366
|
+
const ext = path.extname(item.name).toLowerCase();
|
|
367
|
+
if (['.tsx', '.jsx'].includes(ext)) {
|
|
368
|
+
const fileName = path.basename(item.name, ext);
|
|
369
|
+
if (fileName === this.config.layoutFileName ||
|
|
370
|
+
fileName === this.config.errorFileName ||
|
|
371
|
+
fileName === this.config.notFoundFileName) {
|
|
372
|
+
const componentName = this.generateComponentName(fullPath, moduleName, fileName === this.config.layoutFileName ? 'Layout' :
|
|
373
|
+
fileName === this.config.errorFileName ? 'ErrorBoundary' : 'NotFound');
|
|
374
|
+
if (!components.has(componentName)) {
|
|
375
|
+
specialComponents.set(fullPath, componentName);
|
|
376
|
+
components.add(componentName);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
catch (error) {
|
|
384
|
+
console.error(`Error walking directory ${dir}:`, error);
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
walkDir(baseDir);
|
|
388
|
+
};
|
|
389
|
+
findSpecialFiles(this.config.pagesDir, false);
|
|
390
|
+
this.modules.forEach(moduleName => {
|
|
391
|
+
const modulePagesDir = path.join(this.config.modulesDir, moduleName, 'pages');
|
|
392
|
+
if (fs.existsSync(modulePagesDir)) {
|
|
393
|
+
findSpecialFiles(modulePagesDir, true, moduleName);
|
|
394
|
+
}
|
|
395
|
+
});
|
|
396
|
+
specialComponents.forEach((componentName, filePath) => {
|
|
397
|
+
const importPath = this.getRelativeImportPath(filePath);
|
|
398
|
+
imports.push(`import ${componentName} from '${importPath}';`);
|
|
399
|
+
});
|
|
400
|
+
if (specialComponents.size > 0) {
|
|
401
|
+
imports.push(``);
|
|
402
|
+
}
|
|
403
|
+
this.routeFiles.forEach(route => {
|
|
404
|
+
const importPath = this.getRelativeImportPath(route.filePath);
|
|
405
|
+
imports.push(`import ${route.componentName} from '${importPath}';`);
|
|
406
|
+
components.add(route.componentName);
|
|
407
|
+
});
|
|
408
|
+
imports.push(``);
|
|
409
|
+
const content = [
|
|
410
|
+
...imports,
|
|
411
|
+
this.generateExpoRouteInterface(),
|
|
412
|
+
this.generateDefaultErrorPage(),
|
|
413
|
+
this.generateErrorBoundaryWrapper(),
|
|
414
|
+
this.generateCreateScreenFunction(),
|
|
415
|
+
this.generateRoutesArray(components, specialComponents)
|
|
416
|
+
].join('\n');
|
|
417
|
+
return content;
|
|
418
|
+
}
|
|
419
|
+
generateExpoRouteInterface() {
|
|
420
|
+
return `
|
|
421
|
+
export interface ExpoRoute {
|
|
422
|
+
name: string;
|
|
423
|
+
component: React.ComponentType<any>;
|
|
424
|
+
path?: string;
|
|
425
|
+
options?: NativeStackNavigationOptions;
|
|
426
|
+
layout?: React.ComponentType<any>;
|
|
427
|
+
errorBoundary?: React.ComponentType<any>;
|
|
428
|
+
}
|
|
429
|
+
`;
|
|
430
|
+
}
|
|
431
|
+
generateDefaultErrorPage() {
|
|
432
|
+
return `
|
|
433
|
+
const DefaultErrorPage: ({ error }: { error: any }) => React.JSX.Element = ({ error }) => {
|
|
434
|
+
return (
|
|
435
|
+
<View style={defaultErrorStyles.container}>
|
|
436
|
+
<Text style={defaultErrorStyles.title}>ERROR DETECTED</Text>
|
|
437
|
+
{!!error && (
|
|
438
|
+
<View style={defaultErrorStyles.error_container}>
|
|
439
|
+
<Text style={defaultErrorStyles.error_title}>{error.message}</Text>
|
|
440
|
+
</View>
|
|
441
|
+
)}
|
|
442
|
+
</View>
|
|
443
|
+
);
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
const defaultErrorStyles = StyleSheet.create({
|
|
447
|
+
container: {
|
|
448
|
+
flex: 1,
|
|
449
|
+
flexDirection: 'column',
|
|
450
|
+
justifyContent: 'center',
|
|
451
|
+
alignItems: 'center',
|
|
452
|
+
padding: 10,
|
|
453
|
+
gap: 5,
|
|
454
|
+
backgroundColor: '#f5f5f5',
|
|
455
|
+
},
|
|
456
|
+
title: {
|
|
457
|
+
fontSize: 24,
|
|
458
|
+
fontWeight: 'bold',
|
|
459
|
+
color: '#333',
|
|
460
|
+
},
|
|
461
|
+
error_container: {
|
|
462
|
+
display: 'flex',
|
|
463
|
+
flexDirection: 'column',
|
|
464
|
+
justifyContent: 'center',
|
|
465
|
+
alignItems: 'center',
|
|
466
|
+
gap: 3,
|
|
467
|
+
backgroundColor: '#d32f2f',
|
|
468
|
+
color: '#fff',
|
|
469
|
+
padding: 15,
|
|
470
|
+
borderRadius: 5,
|
|
471
|
+
},
|
|
472
|
+
error_title: {
|
|
473
|
+
fontWeight: 'medium',
|
|
474
|
+
fontSize: 18,
|
|
475
|
+
color: '#fff',
|
|
476
|
+
},
|
|
477
|
+
});
|
|
478
|
+
`;
|
|
479
|
+
}
|
|
480
|
+
generateErrorBoundaryWrapper() {
|
|
481
|
+
return `
|
|
482
|
+
class ErrorBoundaryWrapper extends React.Component<{
|
|
483
|
+
children: React.ReactNode;
|
|
484
|
+
ErrorBoundaryComponent?: React.ComponentType<{ error: Error }>;
|
|
485
|
+
}> {
|
|
486
|
+
state = { hasError: false, error: null as Error | null };
|
|
487
|
+
|
|
488
|
+
static getDerivedStateFromError(error: Error) {
|
|
489
|
+
return { hasError: true, error };
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
|
|
493
|
+
console.log(\`[src -> auto-route] ErrorBoundaryWrapper | error:: \`, error);
|
|
494
|
+
console.log(\`[src -> auto-route] ErrorBoundaryWrapper | errorInfo:: \`, errorInfo);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
render() {
|
|
498
|
+
if (this.state.hasError && this.props.ErrorBoundaryComponent) {
|
|
499
|
+
return React.createElement(this.props.ErrorBoundaryComponent, {
|
|
500
|
+
error: this.state.error as Error
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
return this.props.children;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
`;
|
|
508
|
+
}
|
|
509
|
+
generateCreateScreenFunction() {
|
|
510
|
+
return `
|
|
511
|
+
const createScreen = (
|
|
512
|
+
Component: React.ComponentType<any>,
|
|
513
|
+
layout?: React.ComponentType<any>,
|
|
514
|
+
errorBoundary?: React.ComponentType<any>
|
|
515
|
+
) => {
|
|
516
|
+
return function WrappedScreen(props: any) {
|
|
517
|
+
const Content = layout ?
|
|
518
|
+
React.createElement(layout, null, React.createElement(Component, props)) :
|
|
519
|
+
React.createElement(Component, props);
|
|
520
|
+
|
|
521
|
+
if (errorBoundary) {
|
|
522
|
+
return React.createElement(
|
|
523
|
+
ErrorBoundaryWrapper,
|
|
524
|
+
{
|
|
525
|
+
ErrorBoundaryComponent: errorBoundary,
|
|
526
|
+
children: Content
|
|
527
|
+
}
|
|
528
|
+
);
|
|
529
|
+
} else {
|
|
530
|
+
return React.createElement(
|
|
531
|
+
ErrorBoundaryWrapper,
|
|
532
|
+
{
|
|
533
|
+
ErrorBoundaryComponent: DefaultErrorPage,
|
|
534
|
+
children: Content
|
|
535
|
+
}
|
|
536
|
+
);
|
|
537
|
+
}
|
|
538
|
+
};
|
|
539
|
+
};
|
|
540
|
+
`;
|
|
541
|
+
}
|
|
542
|
+
generateRoutesArray(components, specialComponents) {
|
|
543
|
+
const routes = [];
|
|
544
|
+
routes.push(`export let routes: ExpoRoute[] = [`);
|
|
545
|
+
const sortedRoutes = this.routeFiles.sort((a, b) => {
|
|
546
|
+
if (a.routePath === '/')
|
|
547
|
+
return -1;
|
|
548
|
+
if (b.routePath === '/')
|
|
549
|
+
return 1;
|
|
550
|
+
if (!a.moduleName && b.moduleName)
|
|
551
|
+
return -1;
|
|
552
|
+
if (a.moduleName && !b.moduleName)
|
|
553
|
+
return 1;
|
|
554
|
+
if (a.moduleName === b.moduleName) {
|
|
555
|
+
return a.routePath.localeCompare(b.routePath);
|
|
556
|
+
}
|
|
557
|
+
return 0;
|
|
558
|
+
});
|
|
559
|
+
let lastModuleName = undefined;
|
|
560
|
+
sortedRoutes.forEach((route, index) => {
|
|
561
|
+
if (route.moduleName !== lastModuleName) {
|
|
562
|
+
if (lastModuleName !== undefined) {
|
|
563
|
+
routes.push(``);
|
|
564
|
+
}
|
|
565
|
+
if (route.moduleName) {
|
|
566
|
+
let displayName = route.moduleName;
|
|
567
|
+
if (this.modulePathOverrides.has(route.moduleName)) {
|
|
568
|
+
const overridePath = this.modulePathOverrides.get(route.moduleName);
|
|
569
|
+
displayName = `${route.moduleName} (path: ${overridePath})`;
|
|
570
|
+
}
|
|
571
|
+
routes.push(` // ${displayName} module routes`);
|
|
572
|
+
}
|
|
573
|
+
lastModuleName = route.moduleName;
|
|
574
|
+
}
|
|
575
|
+
let layoutComponent = route.layoutComponent;
|
|
576
|
+
if (!layoutComponent && route.moduleName) {
|
|
577
|
+
const moduleLayoutPath = path.join(this.config.modulesDir, route.moduleName, 'pages', `${this.config.layoutFileName}.tsx`);
|
|
578
|
+
if (fs.existsSync(moduleLayoutPath)) {
|
|
579
|
+
layoutComponent = this.generateComponentName(moduleLayoutPath, route.moduleName, 'Layout');
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
let errorComponent = route.errorComponent;
|
|
583
|
+
if (!errorComponent && route.moduleName) {
|
|
584
|
+
const moduleErrorPath = path.join(this.config.modulesDir, route.moduleName, 'pages', `${this.config.errorFileName}.tsx`);
|
|
585
|
+
if (fs.existsSync(moduleErrorPath)) {
|
|
586
|
+
errorComponent = this.generateComponentName(moduleErrorPath, route.moduleName, 'ErrorBoundary');
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
const screenName = this.generateScreenName(route);
|
|
590
|
+
const routeLines = [' {'];
|
|
591
|
+
routeLines.push(` name: '${screenName}',`);
|
|
592
|
+
routeLines.push(` component: createScreen(${route.componentName}, ${layoutComponent || 'undefined'}, ${errorComponent || 'undefined'}),`);
|
|
593
|
+
routeLines.push(` path: '${route.routePath}',`);
|
|
594
|
+
routeLines.push(` options: { headerShown: false }`);
|
|
595
|
+
routeLines.push(' }');
|
|
596
|
+
if (index < sortedRoutes.length - 1) {
|
|
597
|
+
routeLines[routeLines.length - 1] += ',';
|
|
598
|
+
}
|
|
599
|
+
routes.push(routeLines.join('\n'));
|
|
600
|
+
});
|
|
601
|
+
const modulesWith404 = new Set();
|
|
602
|
+
this.modules.forEach(moduleName => {
|
|
603
|
+
const modulePagesDir = path.join(this.config.modulesDir, moduleName, 'pages');
|
|
604
|
+
if (fs.existsSync(modulePagesDir)) {
|
|
605
|
+
const notFoundPath = path.join(modulePagesDir, `${this.config.notFoundFileName}.tsx`);
|
|
606
|
+
if (fs.existsSync(notFoundPath)) {
|
|
607
|
+
modulesWith404.add(moduleName);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
});
|
|
611
|
+
modulesWith404.forEach(moduleName => {
|
|
612
|
+
const componentName = this.generateComponentName(path.join(this.config.modulesDir, moduleName, 'pages', `${this.config.notFoundFileName}.tsx`), moduleName, 'NotFound');
|
|
613
|
+
if (routes[routes.length - 1].endsWith(',')) {
|
|
614
|
+
routes[routes.length - 1] = routes[routes.length - 1].slice(0, -1);
|
|
615
|
+
}
|
|
616
|
+
let moduleRoutePath = `/${moduleName}`;
|
|
617
|
+
if (this.modulePathOverrides.has(moduleName)) {
|
|
618
|
+
const overridePath = this.modulePathOverrides.get(moduleName);
|
|
619
|
+
if (overridePath) {
|
|
620
|
+
moduleRoutePath = `/${overridePath}`;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
routes.push(`,`);
|
|
624
|
+
routes.push(` {`);
|
|
625
|
+
routes.push(` name: '${moduleName}NotFound',`);
|
|
626
|
+
routes.push(` component: ${componentName},`);
|
|
627
|
+
routes.push(` path: '${moduleRoutePath}/*',`);
|
|
628
|
+
routes.push(` options: { headerShown: false }`);
|
|
629
|
+
routes.push(` }`);
|
|
630
|
+
});
|
|
631
|
+
if (components.has('NotFound')) {
|
|
632
|
+
if (routes[routes.length - 1].endsWith(',')) {
|
|
633
|
+
routes[routes.length - 1] = routes[routes.length - 1].slice(0, -1);
|
|
634
|
+
}
|
|
635
|
+
routes.push(`,`);
|
|
636
|
+
routes.push(` {`);
|
|
637
|
+
routes.push(` name: 'NotFound',`);
|
|
638
|
+
routes.push(` component: NotFound,`);
|
|
639
|
+
routes.push(` path: '*',`);
|
|
640
|
+
routes.push(` options: { headerShown: false }`);
|
|
641
|
+
routes.push(` }`);
|
|
642
|
+
}
|
|
643
|
+
routes.push('];');
|
|
644
|
+
routes.push('');
|
|
645
|
+
routes.push(this.generateLinkingConfig());
|
|
646
|
+
routes.push('');
|
|
647
|
+
routes.push('export default routes;');
|
|
648
|
+
return routes.join('\n');
|
|
649
|
+
}
|
|
650
|
+
generateScreenName(route) {
|
|
651
|
+
let screenName = '';
|
|
652
|
+
if (route.moduleName) {
|
|
653
|
+
screenName += this.toPascalCase(route.moduleName);
|
|
654
|
+
}
|
|
655
|
+
const pathParts = route.routePath.split('/').filter(part => part && part !== '');
|
|
656
|
+
pathParts.forEach(part => {
|
|
657
|
+
if (part.startsWith(':')) {
|
|
658
|
+
screenName += this.toPascalCase(part.substring(1));
|
|
659
|
+
}
|
|
660
|
+
else {
|
|
661
|
+
screenName += this.toPascalCase(part);
|
|
662
|
+
}
|
|
663
|
+
});
|
|
664
|
+
if (!screenName) {
|
|
665
|
+
screenName = 'Index';
|
|
666
|
+
}
|
|
667
|
+
return screenName;
|
|
668
|
+
}
|
|
669
|
+
generateLinkingConfig() {
|
|
670
|
+
return `
|
|
671
|
+
export const linking = {
|
|
672
|
+
prefixes: ['/'],
|
|
673
|
+
config: {
|
|
674
|
+
screens: routes.reduce((config, route) => {
|
|
675
|
+
if (route.path) {
|
|
676
|
+
const path = route.path.replace(/:(\w+)/g, ':$1');
|
|
677
|
+
config[route.name] = path;
|
|
678
|
+
}
|
|
679
|
+
return config;
|
|
680
|
+
}, {} as Record<string, string>),
|
|
681
|
+
},
|
|
682
|
+
};`;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
async function NativeRouteInitiator(dirname = __dirname) {
|
|
686
|
+
const config = {};
|
|
687
|
+
const generator = new NativeRouteGenerator(config, dirname);
|
|
688
|
+
try {
|
|
689
|
+
await generator.generate();
|
|
690
|
+
console.log('🎉 Native route generation completed successfully!');
|
|
691
|
+
}
|
|
692
|
+
catch (err) {
|
|
693
|
+
console.error('❌ Failed to generate native routes: ', err);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
export default NativeRouteInitiator;
|