Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

reset-project.js

112 lines3.5 KB

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​
9const fs = require('fs')
10const path = require('path')
11const readline = require('readline')
12​
13const root = process.cwd()
14const oldDirs = ['app', 'components', 'hooks', 'constants', 'scripts']
15const exampleDir = 'app-example'
16const newAppDir = 'app'
17const exampleDirPath = path.join(root, exampleDir)
18​
19const indexContent = `import { Text, View } from "react-native";
20​
21export 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 );
35​
36const layoutContent = `import { Stack } from "expo-router";
37​
38export default function RootLayout() {
39 return <Stack />;
42​
43const rl = readline.createInterface({
44 input: process.stdin,
45 output: process.stdout,
46})
47​
48const 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 }
100​
101rl.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 },