@jsarc/initiator 0.0.0

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/js/routing.js ADDED
@@ -0,0 +1,473 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { fileURLToPath } from 'url';
4
+ import { Pajo } from '@jsarc/pajo';
5
+ const __filename = fileURLToPath(import.meta.url);
6
+ const __dirname = path.dirname(__filename);
7
+ export class RouteGenerator {
8
+ config;
9
+ routeFiles = [];
10
+ modules = new Set();
11
+ constructor(config = {}, dirname = __dirname) {
12
+ this.config = {
13
+ srcDir: config.srcDir || Pajo.join(dirname, 'src') || '',
14
+ modulesDir: config.modulesDir || Pajo.join(dirname, 'src\\modules') || '',
15
+ pagesDir: config.pagesDir || Pajo.join(dirname, 'src\\pages') || '',
16
+ outputFile: config.outputFile || Pajo.join(dirname, 'src\\auto-routes.tsx') || '',
17
+ layoutFileName: config.layoutFileName || '_layout',
18
+ errorFileName: config.errorFileName || '_error',
19
+ notFoundFileName: config.notFoundFileName || '_404'
20
+ };
21
+ console.log(`--> RouteGenerator config:: `, this.config);
22
+ }
23
+ async generate() {
24
+ console.log('🔍 Scanning for route files...');
25
+ await this.findRouteFiles();
26
+ await this.generateAutoRoutesFile();
27
+ console.log(`✅ Generated ${this.config.outputFile} with ${this.routeFiles.length} routes`);
28
+ console.log(`📦 Found modules with routes: ${Array.from(this.modules).join(', ')}`);
29
+ }
30
+ async findRouteFiles() {
31
+ this.routeFiles = [];
32
+ await this.scanDirectoryForRoutes(this.config.pagesDir);
33
+ if (fs.existsSync(this.config.modulesDir)) {
34
+ const moduleDirs = fs.readdirSync(this.config.modulesDir, { withFileTypes: true })
35
+ .filter(dirent => dirent.isDirectory())
36
+ .map(dirent => dirent.name);
37
+ for (const moduleName of moduleDirs) {
38
+ const modulePagesDir = path.join(this.config.modulesDir, moduleName, 'pages');
39
+ if (fs.existsSync(modulePagesDir)) {
40
+ this.modules.add(moduleName);
41
+ await this.scanDirectoryForRoutes(modulePagesDir, moduleName);
42
+ }
43
+ }
44
+ }
45
+ }
46
+ async scanDirectoryForRoutes(dir, moduleName) {
47
+ try {
48
+ const items = fs.readdirSync(dir, { withFileTypes: true });
49
+ for (const item of items) {
50
+ const fullPath = path.join(dir, item.name);
51
+ if (item.name === 'node_modules' ||
52
+ item.name === 'dist' ||
53
+ item.name === 'build' ||
54
+ item.name.startsWith('.')) {
55
+ continue;
56
+ }
57
+ if (item.isDirectory()) {
58
+ await this.scanDirectoryForRoutes(fullPath, moduleName);
59
+ }
60
+ else if (item.isFile()) {
61
+ const ext = path.extname(item.name).toLowerCase();
62
+ if (['.tsx', '.jsx'].includes(ext)) {
63
+ const fileName = path.basename(item.name, ext);
64
+ if (fileName === this.config.layoutFileName ||
65
+ fileName === this.config.errorFileName ||
66
+ fileName === this.config.notFoundFileName) {
67
+ continue;
68
+ }
69
+ await this.processRouteFile(fullPath, moduleName);
70
+ }
71
+ }
72
+ }
73
+ }
74
+ catch (error) {
75
+ console.error(`Error scanning directory ${dir}:`, error);
76
+ }
77
+ }
78
+ async processRouteFile(filePath, moduleName) {
79
+ try {
80
+ let routePath = this.convertFilePathToRoutePath(filePath, moduleName);
81
+ const componentName = this.extractComponentName(filePath, moduleName);
82
+ const layoutComponent = this.findLayoutComponent(filePath, moduleName);
83
+ const errorComponent = this.findErrorComponent(filePath, moduleName);
84
+ const notFoundComponent = this.findNotFoundComponent(filePath, moduleName);
85
+ const routeFile = {
86
+ filePath,
87
+ relativePath: path.relative(this.config.srcDir, filePath),
88
+ routePath,
89
+ componentName,
90
+ layoutComponent,
91
+ errorComponent,
92
+ notFoundComponent,
93
+ moduleName
94
+ };
95
+ this.routeFiles.push(routeFile);
96
+ }
97
+ catch (error) {
98
+ console.error(`Error processing route file ${filePath}:`, error);
99
+ }
100
+ }
101
+ convertFilePathToRoutePath(filePath, moduleName) {
102
+ let routePath = filePath;
103
+ routePath = routePath.replace(this.config.srcDir, '');
104
+ if (moduleName) {
105
+ const modulePagesPrefix = `modules${path.sep}${moduleName}${path.sep}pages`;
106
+ if (routePath.includes(modulePagesPrefix)) {
107
+ routePath = routePath.substring(routePath.indexOf(modulePagesPrefix) + modulePagesPrefix.length);
108
+ routePath = `/${moduleName}${routePath}`;
109
+ }
110
+ }
111
+ else {
112
+ const pagesPrefix = 'pages';
113
+ if (routePath.includes(pagesPrefix)) {
114
+ routePath = routePath.substring(routePath.indexOf(pagesPrefix) + pagesPrefix.length);
115
+ }
116
+ }
117
+ routePath = routePath.replace(/\.(tsx|jsx)$/, '');
118
+ routePath = routePath.replace(/\\/g, '/');
119
+ if (routePath.endsWith('/index')) {
120
+ routePath = routePath.replace(/\/index$/, '');
121
+ }
122
+ routePath = routePath.replace(/\[([^\]]+)\]/g, '{$1}');
123
+ if (!routePath.startsWith('/')) {
124
+ routePath = '/' + routePath;
125
+ }
126
+ if (routePath === '/' || routePath === '//') {
127
+ return '/';
128
+ }
129
+ return routePath;
130
+ }
131
+ extractComponentName(filePath, moduleName) {
132
+ const fileName = path.basename(filePath, path.extname(filePath));
133
+ const dirName = path.dirname(filePath);
134
+ const parts = dirName.split(path.sep);
135
+ let startIndex = 0;
136
+ if (moduleName) {
137
+ const modulePagesPath = path.sep + 'modules' + path.sep + moduleName + path.sep + 'pages';
138
+ startIndex = parts.findIndex((part, index, arr) => {
139
+ const pathSoFar = arr.slice(0, index + 1).join(path.sep);
140
+ return pathSoFar.endsWith(modulePagesPath);
141
+ }) + 1;
142
+ }
143
+ else {
144
+ const pagesPath = path.sep + 'pages';
145
+ startIndex = parts.findIndex((part, index, arr) => {
146
+ const pathSoFar = arr.slice(0, index + 1).join(path.sep);
147
+ return pathSoFar.endsWith(pagesPath);
148
+ }) + 1;
149
+ }
150
+ if (startIndex < 0)
151
+ startIndex = 0;
152
+ const relevantParts = parts.slice(startIndex);
153
+ let componentName = moduleName ? this.toPascalCase(moduleName) : '';
154
+ relevantParts.forEach(part => {
155
+ if (part && part !== 'pages') {
156
+ componentName += this.toPascalCase(part);
157
+ }
158
+ });
159
+ let fileNamePart = fileName;
160
+ if (fileName.startsWith('{') && fileName.endsWith('}')) {
161
+ const paramName = fileName.substring(1, fileName.length - 1);
162
+ fileNamePart = this.toPascalCase(paramName) + 'Params';
163
+ }
164
+ else {
165
+ fileNamePart = this.toPascalCase(fileName);
166
+ }
167
+ if (!fileNamePart.endsWith('Page')) {
168
+ fileNamePart += 'Page';
169
+ }
170
+ const reservedNames = ['Layout', 'ErrorBoundary', 'NotFound'];
171
+ if (reservedNames.includes(fileNamePart)) {
172
+ fileNamePart += 'Component';
173
+ }
174
+ return componentName + fileNamePart;
175
+ }
176
+ toPascalCase(str) {
177
+ if (!str)
178
+ return '';
179
+ return str
180
+ .split(/[-_]/)
181
+ .map(part => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase())
182
+ .join('');
183
+ }
184
+ findLayoutComponent(filePath, moduleName) {
185
+ const dir = path.dirname(filePath);
186
+ let currentDir = dir;
187
+ while (currentDir !== this.config.srcDir && currentDir !== path.dirname(this.config.srcDir)) {
188
+ const layoutPath = path.join(currentDir, `${this.config.layoutFileName}.tsx`);
189
+ if (fs.existsSync(layoutPath)) {
190
+ return this.generateComponentName(layoutPath, moduleName, 'Layout');
191
+ }
192
+ if (moduleName) {
193
+ const modulePath = path.join(this.config.modulesDir, moduleName);
194
+ if (currentDir === modulePath) {
195
+ break;
196
+ }
197
+ }
198
+ currentDir = path.dirname(currentDir);
199
+ }
200
+ const globalLayoutPath = path.join(this.config.pagesDir, `${this.config.layoutFileName}.tsx`);
201
+ if (fs.existsSync(globalLayoutPath)) {
202
+ return 'Layout';
203
+ }
204
+ return undefined;
205
+ }
206
+ findErrorComponent(filePath, moduleName) {
207
+ const dir = path.dirname(filePath);
208
+ let currentDir = dir;
209
+ while (currentDir !== this.config.srcDir && currentDir !== path.dirname(this.config.srcDir)) {
210
+ const errorPath = path.join(currentDir, `${this.config.errorFileName}.tsx`);
211
+ if (fs.existsSync(errorPath)) {
212
+ return this.generateComponentName(errorPath, moduleName, 'ErrorBoundary');
213
+ }
214
+ if (moduleName) {
215
+ const modulePath = path.join(this.config.modulesDir, moduleName);
216
+ if (currentDir === modulePath) {
217
+ break;
218
+ }
219
+ }
220
+ currentDir = path.dirname(currentDir);
221
+ }
222
+ const globalErrorPath = path.join(this.config.pagesDir, `${this.config.errorFileName}.tsx`);
223
+ if (fs.existsSync(globalErrorPath)) {
224
+ return 'ErrorBoundary';
225
+ }
226
+ return undefined;
227
+ }
228
+ findNotFoundComponent(filePath, moduleName) {
229
+ if (!moduleName)
230
+ return undefined;
231
+ const modulePagesDir = path.join(this.config.modulesDir, moduleName, 'pages');
232
+ const notFoundPath = path.join(modulePagesDir, `${this.config.notFoundFileName}.tsx`);
233
+ if (fs.existsSync(notFoundPath)) {
234
+ return this.generateComponentName(notFoundPath, moduleName, 'NotFound');
235
+ }
236
+ return undefined;
237
+ }
238
+ generateComponentName(filePath, moduleName, suffix) {
239
+ if (!moduleName) {
240
+ return suffix;
241
+ }
242
+ const fileName = path.basename(filePath, path.extname(filePath));
243
+ const dirName = path.dirname(filePath);
244
+ const modulePagesPath = path.join(this.config.modulesDir, moduleName, 'pages');
245
+ const relativePath = path.relative(modulePagesPath, dirName);
246
+ let componentName = this.toPascalCase(moduleName);
247
+ if (relativePath && relativePath !== '.') {
248
+ const dirParts = relativePath.split(path.sep).filter(part => part && part !== '.');
249
+ dirParts.forEach(part => {
250
+ componentName += this.toPascalCase(part);
251
+ });
252
+ }
253
+ if (fileName !== this.config.layoutFileName &&
254
+ fileName !== this.config.errorFileName &&
255
+ fileName !== this.config.notFoundFileName) {
256
+ componentName += this.toPascalCase(fileName);
257
+ }
258
+ return componentName + suffix;
259
+ }
260
+ async generateAutoRoutesFile() {
261
+ const outputDir = path.dirname(this.config.outputFile);
262
+ if (!fs.existsSync(outputDir)) {
263
+ fs.mkdirSync(outputDir, { recursive: true });
264
+ }
265
+ const content = this.generateFileContent();
266
+ fs.writeFileSync(this.config.outputFile, content, 'utf-8');
267
+ }
268
+ getRelativeImportPath(targetPath) {
269
+ const outputDir = path.dirname(this.config.outputFile);
270
+ const relative = path.relative(outputDir, targetPath);
271
+ let importPath = relative.replace(/\\/g, '/');
272
+ if (!importPath.startsWith('.') && !importPath.startsWith('/')) {
273
+ importPath = './' + importPath;
274
+ }
275
+ importPath = importPath.replace(/\.(tsx|jsx)$/, '');
276
+ return importPath;
277
+ }
278
+ generateFileContent() {
279
+ const components = new Set();
280
+ const imports = [
281
+ `import { lazy } from 'react';`,
282
+ `import type { RouteObject } from 'react-router-dom';`,
283
+ ``
284
+ ];
285
+ const globalLayoutPath = path.join(this.config.pagesDir, `${this.config.layoutFileName}.tsx`);
286
+ const globalErrorPath = path.join(this.config.pagesDir, `${this.config.errorFileName}.tsx`);
287
+ const global404Path = path.join(this.config.pagesDir, `${this.config.notFoundFileName}.tsx`);
288
+ if (fs.existsSync(globalLayoutPath)) {
289
+ const importPath = this.getRelativeImportPath(globalLayoutPath);
290
+ imports.push(`const Layout = lazy(() => import('${importPath}'));`);
291
+ components.add('Layout');
292
+ }
293
+ if (fs.existsSync(globalErrorPath)) {
294
+ const importPath = this.getRelativeImportPath(globalErrorPath);
295
+ imports.push(`const ErrorBoundary = lazy(() => import('${importPath}'));`);
296
+ components.add('ErrorBoundary');
297
+ }
298
+ if (fs.existsSync(global404Path)) {
299
+ const importPath = this.getRelativeImportPath(global404Path);
300
+ imports.push(`const NotFound = lazy(() => import('${importPath}'));`);
301
+ components.add('NotFound');
302
+ }
303
+ imports.push(``);
304
+ const specialComponents = new Map();
305
+ const findSpecialFiles = (baseDir, isModule = false, moduleName) => {
306
+ if (!fs.existsSync(baseDir))
307
+ return;
308
+ const walkDir = (dir) => {
309
+ try {
310
+ const items = fs.readdirSync(dir, { withFileTypes: true });
311
+ for (const item of items) {
312
+ const fullPath = path.join(dir, item.name);
313
+ if (item.isDirectory()) {
314
+ walkDir(fullPath);
315
+ }
316
+ else if (item.isFile()) {
317
+ const ext = path.extname(item.name).toLowerCase();
318
+ if (['.tsx', '.jsx'].includes(ext)) {
319
+ const fileName = path.basename(item.name, ext);
320
+ if (fileName === this.config.layoutFileName ||
321
+ fileName === this.config.errorFileName ||
322
+ fileName === this.config.notFoundFileName) {
323
+ const componentName = this.generateComponentName(fullPath, moduleName, fileName === this.config.layoutFileName ? 'Layout' :
324
+ fileName === this.config.errorFileName ? 'ErrorBoundary' : 'NotFound');
325
+ if (!components.has(componentName)) {
326
+ specialComponents.set(fullPath, componentName);
327
+ components.add(componentName);
328
+ }
329
+ }
330
+ }
331
+ }
332
+ }
333
+ }
334
+ catch (error) {
335
+ console.error(`Error walking directory ${dir}:`, error);
336
+ }
337
+ };
338
+ walkDir(baseDir);
339
+ };
340
+ findSpecialFiles(this.config.pagesDir, false);
341
+ this.modules.forEach(moduleName => {
342
+ const modulePagesDir = path.join(this.config.modulesDir, moduleName, 'pages');
343
+ if (fs.existsSync(modulePagesDir)) {
344
+ findSpecialFiles(modulePagesDir, true, moduleName);
345
+ }
346
+ });
347
+ specialComponents.forEach((componentName, filePath) => {
348
+ const importPath = this.getRelativeImportPath(filePath);
349
+ imports.push(`const ${componentName} = lazy(() => import('${importPath}'));`);
350
+ });
351
+ if (specialComponents.size > 0) {
352
+ imports.push(``);
353
+ }
354
+ this.routeFiles.forEach(route => {
355
+ const importPath = this.getRelativeImportPath(route.filePath);
356
+ imports.push(`const ${route.componentName} = lazy(() => import('${importPath}'));`);
357
+ components.add(route.componentName);
358
+ });
359
+ imports.push(``);
360
+ const routes = ['export const routes: RouteObject[] = ['];
361
+ const sortedRoutes = this.routeFiles.sort((a, b) => {
362
+ if (a.routePath === '/')
363
+ return -1;
364
+ if (b.routePath === '/')
365
+ return 1;
366
+ if (!a.moduleName && b.moduleName)
367
+ return -1;
368
+ if (a.moduleName && !b.moduleName)
369
+ return 1;
370
+ if (a.moduleName === b.moduleName) {
371
+ return a.routePath.localeCompare(b.routePath);
372
+ }
373
+ return 0;
374
+ });
375
+ let lastModuleName = undefined;
376
+ sortedRoutes.forEach((route, index) => {
377
+ if (route.moduleName !== lastModuleName) {
378
+ if (lastModuleName !== undefined) {
379
+ routes.push(``);
380
+ }
381
+ if (route.moduleName) {
382
+ routes.push(` // ${route.moduleName} module routes`);
383
+ }
384
+ lastModuleName = route.moduleName;
385
+ }
386
+ const routeLines = [' {'];
387
+ routeLines.push(` path: '${route.routePath.replace(/\{([^}]+)\}/g, ':$1')}',`);
388
+ let layoutComponent = route.layoutComponent;
389
+ if (!layoutComponent && route.moduleName) {
390
+ const moduleLayoutPath = path.join(this.config.modulesDir, route.moduleName, 'pages', `${this.config.layoutFileName}.tsx`);
391
+ if (fs.existsSync(moduleLayoutPath)) {
392
+ layoutComponent = this.generateComponentName(moduleLayoutPath, route.moduleName, 'Layout');
393
+ }
394
+ }
395
+ if (layoutComponent) {
396
+ routeLines.push(` element: <${layoutComponent}><${route.componentName} /></${layoutComponent}>,`);
397
+ }
398
+ else {
399
+ routeLines.push(` element: <${route.componentName} />,`);
400
+ }
401
+ let errorComponent = route.errorComponent;
402
+ if (!errorComponent && route.moduleName) {
403
+ const moduleErrorPath = path.join(this.config.modulesDir, route.moduleName, 'pages', `${this.config.errorFileName}.tsx`);
404
+ if (fs.existsSync(moduleErrorPath)) {
405
+ errorComponent = this.generateComponentName(moduleErrorPath, route.moduleName, 'ErrorBoundary');
406
+ }
407
+ }
408
+ if (errorComponent) {
409
+ routeLines.push(` errorElement: <${errorComponent} />`);
410
+ }
411
+ if (routeLines[routeLines.length - 1].endsWith(',')) {
412
+ routeLines[routeLines.length - 1] = routeLines[routeLines.length - 1].slice(0, -1);
413
+ }
414
+ routeLines.push(' }');
415
+ if (index < sortedRoutes.length - 1) {
416
+ routeLines[routeLines.length - 1] += ',';
417
+ }
418
+ routes.push(routeLines.join('\n'));
419
+ });
420
+ const modulesWith404 = new Set();
421
+ specialComponents.forEach((componentName, filePath) => {
422
+ const fileName = path.basename(filePath, path.extname(filePath));
423
+ if (fileName === this.config.notFoundFileName) {
424
+ const pathParts = filePath.split(path.sep);
425
+ const modulesIndex = pathParts.indexOf('modules');
426
+ if (modulesIndex !== -1 && modulesIndex + 1 < pathParts.length) {
427
+ const moduleName = pathParts[modulesIndex + 1];
428
+ if (!modulesWith404.has(moduleName)) {
429
+ modulesWith404.add(moduleName);
430
+ if (routes[routes.length - 1].endsWith(',')) {
431
+ routes[routes.length - 1] = routes[routes.length - 1].slice(0, -1);
432
+ }
433
+ routes.push(`,`);
434
+ routes.push(` {`);
435
+ routes.push(` path: '/${moduleName}/*',`.replace(/\{([^}]+)\}/g, ':$1'));
436
+ routes.push(` element: <${componentName} />`);
437
+ routes.push(` }`);
438
+ }
439
+ }
440
+ }
441
+ });
442
+ if (components.has('NotFound')) {
443
+ if (routes[routes.length - 1].endsWith(',')) {
444
+ routes[routes.length - 1] = routes[routes.length - 1].slice(0, -1);
445
+ }
446
+ routes.push(`,`);
447
+ routes.push(` {`);
448
+ routes.push(` path: '*',`.replace(/\{([^}]+)\}/g, ':$1'));
449
+ routes.push(` element: <NotFound />`);
450
+ routes.push(` }`);
451
+ }
452
+ routes.push('];');
453
+ routes.push('');
454
+ routes.push('export default routes;');
455
+ return [
456
+ ...imports,
457
+ '',
458
+ ...routes
459
+ ].join('\n');
460
+ }
461
+ }
462
+ async function RouteInitiator(dirname = __dirname) {
463
+ const config = {};
464
+ const generator = new RouteGenerator(config, dirname);
465
+ try {
466
+ await generator.generate();
467
+ console.log('🎉 Route generation completed successfully!');
468
+ }
469
+ catch (err) {
470
+ console.error('❌ Failed to generate routes: ', err);
471
+ }
472
+ }
473
+ export default RouteInitiator;
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@jsarc/initiator",
3
+ "publishConfig": {
4
+ "access": "public"
5
+ },
6
+ "version": "0.0.0",
7
+ "description": "INITIATOR est un plugin d'initialisation intelligent pour les applications React avec TypeScript/Javascript. Il génère automatiquement les fichiers de configuration, de routage et d'internationalisation basés sur la structure de votre projet.",
8
+ "main": "index.ts",
9
+ "keywords": [],
10
+ "author": "INICODE <contact.inicode@gmail.com>",
11
+ "license": "MIT",
12
+ "scripts": {
13
+ "init": "npm init --scope=@jsarc/initiator",
14
+ "login": "npm login"
15
+ },
16
+ "devDependencies": {},
17
+ "dependencies": {
18
+ "@jsarc/pajo": "^0.0.1-beta.0.1"
19
+ }
20
+ }