You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

reset-project.js 3.5KB

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