eletrotupi / tcc/ frontend/scripts/reset-project.jsff054a2a3d5d5d8afeba52d3876130ef83d323d5
3.5 KB | 112 lines Raw
1
#!/usr/bin/env node
2

3
/**
4
 * This script is used to reset the project to a blank state.
5
 * 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.
6
 * You can remove the `reset-project` script from package.json and safely delete this file after running it.
7
 */
8

9
const fs = require("fs");
10
const path = require("path");
11
const readline = require("readline");
12

13
const root = process.cwd();
14
const oldDirs = ["app", "components", "hooks", "constants", "scripts"];
15
const exampleDir = "app-example";
16
const newAppDir = "app";
17
const exampleDirPath = path.join(root, exampleDir);
18

19
const indexContent = `import { Text, View } from "react-native";
20

21
export default function Index() {
22
  return (
23
    <View
24
      style={{
25
        flex: 1,
26
        justifyContent: "center",
27
        alignItems: "center",
28
      }}
29
    >
30
      <Text>Edit app/index.tsx to edit this screen.</Text>
31
    </View>
32
  );
33
}
34
`;
35

36
const layoutContent = `import { Stack } from "expo-router";
37

38
export default function RootLayout() {
39
  return <Stack />;
40
}
41
`;
42

43
const rl = readline.createInterface({
44
  input: process.stdin,
45
  output: process.stdout,
46
});
47

48
const moveDirectories = async (userInput) => {
49
  try {
50
    if (userInput === "y") {
51
      // Create the app-example directory
52
      await fs.promises.mkdir(exampleDirPath, { recursive: true });
53
      console.log(`šŸ“ /${exampleDir} directory created.`);
54
    }
55

56
    // Move old directories to new app-example directory or delete them
57
    for (const dir of oldDirs) {
58
      const oldDirPath = path.join(root, dir);
59
      if (fs.existsSync(oldDirPath)) {
60
        if (userInput === "y") {
61
          const newDirPath = path.join(root, exampleDir, dir);
62
          await fs.promises.rename(oldDirPath, newDirPath);
63
          console.log(`āž”ļø /${dir} moved to /${exampleDir}/${dir}.`);
64
        } else {
65
          await fs.promises.rm(oldDirPath, { recursive: true, force: true });
66
          console.log(`āŒ /${dir} deleted.`);
67
        }
68
      } else {
69
        console.log(`āž”ļø /${dir} does not exist, skipping.`);
70
      }
71
    }
72

73
    // Create new /app directory
74
    const newAppDirPath = path.join(root, newAppDir);
75
    await fs.promises.mkdir(newAppDirPath, { recursive: true });
76
    console.log("\nšŸ“ New /app directory created.");
77

78
    // Create index.tsx
79
    const indexPath = path.join(newAppDirPath, "index.tsx");
80
    await fs.promises.writeFile(indexPath, indexContent);
81
    console.log("šŸ“„ app/index.tsx created.");
82

83
    // Create _layout.tsx
84
    const layoutPath = path.join(newAppDirPath, "_layout.tsx");
85
    await fs.promises.writeFile(layoutPath, layoutContent);
86
    console.log("šŸ“„ app/_layout.tsx created.");
87

88
    console.log("\nāœ… Project reset complete. Next steps:");
89
    console.log(
90
      `1. Run \`npx expo start\` to start a development server.\n2. Edit app/index.tsx to edit the main screen.${
91
        userInput === "y"
92
          ? `\n3. Delete the /${exampleDir} directory when you're done referencing it.`
93
          : ""
94
      }`
95
    );
96
  } catch (error) {
97
    console.error(`āŒ Error during script execution: ${error.message}`);
98
  }
99
};
100

101
rl.question(
102
  "Do you want to move existing files to /app-example instead of deleting them? (Y/n): ",
103
  (answer) => {
104
    const userInput = answer.trim().toLowerCase() || "y";
105
    if (userInput === "y" || userInput === "n") {
106
      moveDirectories(userInput).finally(() => rl.close());
107
    } else {
108
      console.log("āŒ Invalid input. Please enter 'Y' or 'N'.");
109
      rl.close();
110
    }
111
  }
112
);