@moamc/rn-cli 1.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.
@@ -0,0 +1,116 @@
1
+ const fs = require('fs-extra');
2
+ const path = require('path');
3
+ const chalk = require('chalk');
4
+
5
+ // Find project root by looking for package.json
6
+ const findProjectRoot = () => {
7
+ let currentDir = process.cwd();
8
+
9
+ while (currentDir !== path.parse(currentDir).root) {
10
+ if (fs.existsSync(path.join(currentDir, 'package.json'))) {
11
+ return currentDir;
12
+ }
13
+ currentDir = path.dirname(currentDir);
14
+ }
15
+
16
+ // If not found, use current working directory
17
+ return process.cwd();
18
+ };
19
+
20
+ const PROJECT_ROOT = findProjectRoot();
21
+
22
+ const ensureDirectoryExists = (dirPath) => {
23
+ if (!fs.existsSync(dirPath)) {
24
+ fs.mkdirSync(dirPath, { recursive: true });
25
+ }
26
+ };
27
+
28
+ const fileExists = (filePath) => {
29
+ return fs.existsSync(filePath);
30
+ };
31
+
32
+ const writeFile = (filePath, content) => {
33
+ ensureDirectoryExists(path.dirname(filePath));
34
+ fs.writeFileSync(filePath, content, 'utf8');
35
+ };
36
+
37
+ const readFile = (filePath) => {
38
+ return fs.readFileSync(filePath, 'utf8');
39
+ };
40
+
41
+ const appendToFile = (filePath, content) => {
42
+ const existing = readFile(filePath);
43
+ writeFile(filePath, existing + '\n' + content);
44
+ };
45
+
46
+ const toPascalCase = (str) => {
47
+ return str
48
+ .split(/[-_\s]/)
49
+ .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
50
+ .join('');
51
+ };
52
+
53
+ const toCamelCase = (str) => {
54
+ const pascal = toPascalCase(str);
55
+ return pascal.charAt(0).toLowerCase() + pascal.slice(1);
56
+ };
57
+
58
+ const toKebabCase = (str) => {
59
+ return str
60
+ .replace(/([a-z])([A-Z])/g, '$1-$2')
61
+ .replace(/[\s_]+/g, '-')
62
+ .toLowerCase();
63
+ };
64
+
65
+ const getProjectPath = (...segments) => {
66
+ return path.join(PROJECT_ROOT, ...segments);
67
+ };
68
+
69
+ const logSuccess = (message) => {
70
+ console.log(chalk.green('✓'), message);
71
+ };
72
+
73
+ const logInfo = (message) => {
74
+ console.log(chalk.blue('ℹ'), message);
75
+ };
76
+
77
+ const logWarning = (message) => {
78
+ console.log(chalk.yellow('⚠'), message);
79
+ };
80
+
81
+ const logError = (message) => {
82
+ console.log(chalk.red('✗'), message);
83
+ };
84
+
85
+ const confirmOverwrite = async (inquirer, filePath) => {
86
+ if (fileExists(filePath)) {
87
+ const { overwrite } = await inquirer.prompt([
88
+ {
89
+ type: 'confirm',
90
+ name: 'overwrite',
91
+ message: `File ${path.basename(filePath)} already exists. Overwrite?`,
92
+ default: false,
93
+ },
94
+ ]);
95
+ return overwrite;
96
+ }
97
+ return true;
98
+ };
99
+
100
+ module.exports = {
101
+ PROJECT_ROOT,
102
+ ensureDirectoryExists,
103
+ fileExists,
104
+ writeFile,
105
+ readFile,
106
+ appendToFile,
107
+ toPascalCase,
108
+ toCamelCase,
109
+ toKebabCase,
110
+ getProjectPath,
111
+ logSuccess,
112
+ logInfo,
113
+ logWarning,
114
+ logError,
115
+ confirmOverwrite,
116
+ };
@@ -0,0 +1,74 @@
1
+ const { readFile, writeFile, logSuccess, logWarning } = require('./fileUtils');
2
+ const path = require('path');
3
+
4
+ const addScreenToNavigation = (screenName, featureName, navigationPath) => {
5
+ try {
6
+ let content = readFile(navigationPath);
7
+
8
+ // Check if screen already exists
9
+ if (content.includes(`import ${screenName} from`) || content.includes(`name="${screenName}"`)) {
10
+ logWarning(`${screenName} already exists in Navigation.js`);
11
+ return false;
12
+ }
13
+
14
+ // Add import statement after the last import from screens
15
+ const importPath = `../screens/${featureName}/${screenName}/${screenName}`;
16
+ const newImport = `import ${screenName} from '${importPath}';`;
17
+
18
+ // Find the last import statement from screens directory
19
+ const lastScreenImportRegex = /import\s+\w+\s+from\s+['"]\.\.\/screens\/[^'"]+['"];/g;
20
+ const matches = content.match(lastScreenImportRegex);
21
+
22
+ if (matches && matches.length > 0) {
23
+ const lastImport = matches[matches.length - 1];
24
+ content = content.replace(lastImport, `${lastImport}\n${newImport}`);
25
+ } else {
26
+ // Fallback: add after the screens index import
27
+ const screensImportRegex = /(} from '\.\.\/screens';)/;
28
+ if (content.match(screensImportRegex)) {
29
+ content = content.replace(screensImportRegex, `$1\n${newImport}`);
30
+ }
31
+ }
32
+
33
+ // Add Stack.Screen before </Stack.Navigator>
34
+ const stackNavigatorEndRegex = /(<\/Stack\.Navigator>)/;
35
+ if (content.match(stackNavigatorEndRegex)) {
36
+ const newRoute = `\t\t\t\t<Stack.Screen name="${screenName}" component={${screenName}} />\n\t\t\t\t`;
37
+ content = content.replace(stackNavigatorEndRegex, `${newRoute}$1`);
38
+ }
39
+
40
+ writeFile(navigationPath, content);
41
+ logSuccess(`Added ${screenName} to Navigation.js`);
42
+ return true;
43
+ } catch (error) {
44
+ logWarning(`Failed to update Navigation.js: ${error.message}`);
45
+ return false;
46
+ }
47
+ };
48
+
49
+ const addScreenToIndexExport = (screenName, featureName, indexPath) => {
50
+ try {
51
+ let content = readFile(indexPath);
52
+
53
+ // Check if screen already exists
54
+ if (content.includes(`export { default as ${screenName} }`)) {
55
+ return false;
56
+ }
57
+
58
+ // Add export at the end
59
+ const newExport = `export { default as ${screenName} } from './${featureName}/${screenName}/${screenName}';\n`;
60
+ content = content.trimEnd() + '\n' + newExport;
61
+
62
+ writeFile(indexPath, content);
63
+ logSuccess(`Added ${screenName} to screens/index.js`);
64
+ return true;
65
+ } catch (error) {
66
+ logWarning(`Failed to update screens/index.js: ${error.message}`);
67
+ return false;
68
+ }
69
+ };
70
+
71
+ module.exports = {
72
+ addScreenToNavigation,
73
+ addScreenToIndexExport,
74
+ };