screens.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const fs = require('fs');
  2. const startPath = 'src/Screens/Root';
  3. const findNavigator = (render = (route, name) => {}, importRender = (route, name) => {}, path = startPath, route = '') => {
  4. let result = '';
  5. const list = fs.readdirSync(path, { withFileTypes: true })
  6. .filter(dir => dir.isDirectory())
  7. .map(dir => dir.name);
  8. for(let dir of list) {
  9. if (dir=='component') continue;
  10. findNavigator(render, importRender, path + '/' + dir, route + '/' + dir);
  11. }
  12. if(route != '') {
  13. const names = fs.readdirSync(path, { withFileTypes: true })
  14. .filter(file => file.isFile() && file.name.endsWith('.js'))
  15. .map(file => {
  16. let name = file.name.replace('.js', '');
  17. if(name == 'index') name = 'Index';
  18. return {name: name, routePostfix: name == "Index" ? "" : name};
  19. });
  20. if(names.length > 0) {
  21. render(route, names);
  22. importRender(route, names);
  23. }
  24. };
  25. }
  26. /* Generate Screens */
  27. const generateScreens = () => {
  28. const screenRootPath = '~/Screens/Root';
  29. let navigators = '';
  30. let importers = '';
  31. findNavigator((route, names) => {
  32. let navigator = '';
  33. let findIndex = false;
  34. navigator += `\t\t'${route}': createStackNavigator({\n`;
  35. for(name of names) {
  36. if(name.name == 'Index') findIndex = true;
  37. const postfix = name.routePostfix != "" ? "/"+name.routePostfix: "";
  38. const importName = (route.replace(/\//g, '_') + postfix.replace(/\//g, '_')).replace('_','');
  39. navigator += `\t\t\t'${name.name}': ${importName},\n`;
  40. }
  41. navigator += `\t\t}, { ...navigationOptions }),\n`;
  42. if(findIndex) navigators += navigator;
  43. }, (route, names) => {
  44. let importer = '';
  45. let findIndex = false;
  46. for(name of names) {
  47. if(name.name == 'Index') findIndex = true;
  48. const postfix = name.routePostfix != "" ? "/"+name.routePostfix: "";
  49. const importName = (route.replace(/\//g, '_') + postfix.replace(/\//g, '_')).replace('_', '');
  50. importers += `import ${importName} from '${screenRootPath}${route}${postfix}';\n`;
  51. }
  52. if(findIndex) importers += importer;
  53. });
  54. const string = `import { createStackNavigator } from 'react-navigation-stack';
  55. ${importers}
  56. export default ScreensCreator = (navigationOptions) => {
  57. return {
  58. ${navigators}
  59. }
  60. };
  61. `;
  62. fs.writeFileSync('src/Generated/Screens.js', string, 'utf8');
  63. }
  64. generateScreens();
  65. /* Generate Independent */
  66. const generateIndependent = () => {
  67. const popupRootPath = '~/Screens/Independent';
  68. let navigators = '';
  69. let importers = '';
  70. findNavigator((route, names) => {
  71. for(name of names) {
  72. const postfix = name.routePostfix != "" ? "/"+name.routePostfix: "";
  73. const importName = (route.replace(/\//g, '_') + postfix.replace(/\//g, '_')).replace('_','');
  74. navigators += `\t\t'${route}/${name.name}': ${importName},\n`;
  75. }
  76. }, (route, names) => {
  77. for(name of names) {
  78. const postfix = name.routePostfix != "" ? "/"+name.routePostfix: "";
  79. const importName = (route.replace(/\//g, '_') + postfix.replace(/\//g, '_')).replace('_', '');
  80. importers += `import ${importName} from '${popupRootPath}${route}${postfix}';\n`;
  81. }
  82. }, 'src/Screens/Independent');
  83. const string = `
  84. ${importers}
  85. export default IndependentCreator = () => {
  86. return {
  87. ${navigators}
  88. }
  89. };
  90. `;
  91. fs.writeFileSync('src/Generated/Independent.js', string, 'utf8');
  92. }
  93. generateIndependent();