reset-project.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env node
  2. /**
  3. * This script is used to reset the project to a blank state.
  4. * It deletes or moves the /src and /scripts directories to /example based on user input and creates a new /src/app directory with an index.tsx and _layout.tsx file.
  5. * You can remove the `reset-project` script from package.json and safely delete this file after running it.
  6. */
  7. const fs = require("fs");
  8. const path = require("path");
  9. const readline = require("readline");
  10. const root = process.cwd();
  11. const oldDirs = ["src", "scripts"];
  12. const exampleDir = "example";
  13. const newAppDir = "src/app";
  14. const exampleDirPath = path.join(root, exampleDir);
  15. const indexContent = `import { Text, View, StyleSheet } from "react-native";
  16. export default function Index() {
  17. return (
  18. <View style={styles.container}>
  19. <Text>Edit src/app/index.tsx to edit this screen.</Text>
  20. </View>
  21. );
  22. }
  23. const styles = StyleSheet.create({
  24. container: {
  25. flex: 1,
  26. alignItems: "center",
  27. justifyContent: "center",
  28. },
  29. });
  30. `;
  31. const layoutContent = `import { Stack } from "expo-router";
  32. export default function RootLayout() {
  33. return <Stack />;
  34. }
  35. `;
  36. const rl = readline.createInterface({
  37. input: process.stdin,
  38. output: process.stdout,
  39. });
  40. const moveDirectories = async (userInput) => {
  41. try {
  42. if (userInput === "y") {
  43. // Create the app-example directory
  44. await fs.promises.mkdir(exampleDirPath, { recursive: true });
  45. console.log(`📁 /${exampleDir} directory created.`);
  46. }
  47. // Move old directories to new app-example directory or delete them
  48. for (const dir of oldDirs) {
  49. const oldDirPath = path.join(root, dir);
  50. if (fs.existsSync(oldDirPath)) {
  51. if (userInput === "y") {
  52. const newDirPath = path.join(root, exampleDir, dir);
  53. await fs.promises.rename(oldDirPath, newDirPath);
  54. console.log(`➡️ /${dir} moved to /${exampleDir}/${dir}.`);
  55. } else {
  56. await fs.promises.rm(oldDirPath, { recursive: true, force: true });
  57. console.log(`❌ /${dir} deleted.`);
  58. }
  59. } else {
  60. console.log(`➡️ /${dir} does not exist, skipping.`);
  61. }
  62. }
  63. // Create new /src/app directory
  64. const newAppDirPath = path.join(root, newAppDir);
  65. await fs.promises.mkdir(newAppDirPath, { recursive: true });
  66. console.log("\n📁 New /src/app directory created.");
  67. // Create index.tsx
  68. const indexPath = path.join(newAppDirPath, "index.tsx");
  69. await fs.promises.writeFile(indexPath, indexContent);
  70. console.log("📄 src/app/index.tsx created.");
  71. // Create _layout.tsx
  72. const layoutPath = path.join(newAppDirPath, "_layout.tsx");
  73. await fs.promises.writeFile(layoutPath, layoutContent);
  74. console.log("📄 src/app/_layout.tsx created.");
  75. console.log("\n✅ Project reset complete. Next steps:");
  76. console.log(
  77. `1. Run \`npx expo start\` to start a development server.\n2. Edit src/app/index.tsx to edit the main screen.\n3. Put all your application code in /src, only screens and layout files should be in /src/app.${
  78. userInput === "y"
  79. ? `\n4. Delete the /${exampleDir} directory when you're done referencing it.`
  80. : ""
  81. }`
  82. );
  83. } catch (error) {
  84. console.error(`❌ Error during script execution: ${error.message}`);
  85. }
  86. };
  87. rl.question(
  88. "Do you want to move existing files to /example instead of deleting them? (Y/n): ",
  89. (answer) => {
  90. const userInput = answer.trim().toLowerCase() || "y";
  91. if (userInput === "y" || userInput === "n") {
  92. moveDirectories(userInput).finally(() => rl.close());
  93. } else {
  94. console.log("❌ Invalid input. Please enter 'Y' or 'N'.");
  95. rl.close();
  96. }
  97. }
  98. );