Skip to content

Commit e7e689d

Browse files
committed
Update package version to 1.1.0 and enhance project setup in index.js for default package installation and router configuration
1 parent 32d66c9 commit e7e689d

File tree

2 files changed

+60
-42
lines changed

2 files changed

+60
-42
lines changed

index.js

Lines changed: 59 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
#!/usr/bin/env node
22

3-
43
import inquirer from "inquirer";
54
import { execSync } from "child_process";
65
import path from "path";
76
import fs from "fs";
8-
// const { execSync } = require("child_process");
9-
// const path = require("path");
10-
// const fs = require("fs");
11-
// const inquirer = require("inquirer");
12-
137

148
const run = (cmd, cwd = process.cwd()) => {
159
console.log(`\n📦 Running: ${cmd}`);
@@ -100,9 +94,11 @@ const run = (cmd, cwd = process.cwd()) => {
10094
fs.writeFileSync(mainPath, mainContent);
10195
}
10296

103-
// 6. Install optional packages
104-
if (packages.length > 0) {
105-
run(`npm install ${packages.join(" ")}`, projectPath);
97+
// 6. Install default + optional packages
98+
const defaultPackages = ["react-router-dom"];
99+
const allPackages = [...defaultPackages, ...packages];
100+
if (allPackages.length > 0) {
101+
run(`npm install ${allPackages.join(" ")}`, projectPath);
106102
}
107103

108104
// 7. Create folder structure
@@ -115,49 +111,49 @@ const run = (cmd, cwd = process.cwd()) => {
115111
if (packages.includes("axios")) {
116112
const axiosContent = `import axios from "axios";
117113
118-
export const api = axios.create({
119-
baseURL: import.meta.env.VITE_API_URL || "http://localhost:5000",
120-
headers: { "Content-Type": "application/json" },
121-
timeout: 10000
122-
});
123-
124-
// ✅ Request Interceptor
125-
api.interceptors.request.use(
126-
(config) => {
127-
// Example: Add token if available
128-
const token = localStorage.getItem("token");
129-
if (token) {
114+
export const api = axios.create({
115+
baseURL: import.meta.env.VITE_API_URL || "http://localhost:5000",
116+
headers: { "Content-Type": "application/json" },
117+
timeout: 10000
118+
});
119+
120+
// ✅ Request Interceptor
121+
api.interceptors.request.use(
122+
(config) => {
123+
// Example: Add token if available
124+
const token = localStorage.getItem("token");
125+
if (token) {
130126
config.headers.Authorization = \`Bearer \${token}\`;
131-
}
132-
return config;
133-
},
134-
(error) => {
127+
}
128+
return config;
129+
},
130+
(error) => {
135131
return Promise.reject(error);
136132
}
137-
);
133+
);
138134
139-
// ✅ Response Interceptor
140-
api.interceptors.response.use(
141-
(response) => {
135+
// ✅ Response Interceptor
136+
api.interceptors.response.use(
137+
(response) => {
142138
return response.data; // Return only data for convenience
143139
},
144-
(error) => {
145-
if (error.response) {
140+
(error) => {
141+
if (error.response) {
146142
console.error("API Error:", error.response.data?.message || error.message);
147143
// Example: Handle unauthorized
148144
if (error.response.status === 401) {
149-
// Optionally redirect to login
145+
// Optionally redirect to login
150146
window.location.href = "/login";
151147
}
152-
} else if (error.request) {
148+
} else if (error.request) {
153149
console.error("No response received from server.");
154-
} else {
150+
} else {
155151
console.error("Request setup error:", error.message);
156-
}
157-
return Promise.reject(error);
158152
}
159-
);
160-
`;
153+
return Promise.reject(error);
154+
}
155+
);
156+
`;
161157

162158
fs.writeFileSync(path.join(projectPath, "src", "utils", "axiosInstance.js"), axiosContent);
163159
}
@@ -174,7 +170,7 @@ const run = (cmd, cwd = process.cwd()) => {
174170
appContent = appContent.replace(/import\s+['"]\.\/App\.css['"];?/g, ""); // remove App.css import
175171
appContent = `export default function App() {
176172
return (
177-
<div
173+
<div
178174
style={{
179175
display: "flex",
180176
flexDirection: "column",
@@ -202,10 +198,32 @@ const run = (cmd, cwd = process.cwd()) => {
202198
</p>
203199
</div>
204200
);
205-
}
206-
`;
201+
}`;
207202
fs.writeFileSync(appFile, appContent);
208203

204+
// 10. Default Router setup in main.jsx
205+
const mainFile = fs.existsSync(path.join(projectPath, "src/main.jsx"))
206+
? "src/main.jsx"
207+
: "src/main.tsx";
208+
const mainPath = path.join(projectPath, mainFile);
209+
210+
const routerSetup = `import React from 'react';
211+
import ReactDOM from 'react-dom/client';
212+
import { BrowserRouter, Routes, Route } from 'react-router-dom';
213+
import App from './App';
214+
215+
ReactDOM.createRoot(document.getElementById('root')).render(
216+
<React.StrictMode>
217+
<BrowserRouter>
218+
<Routes>
219+
<Route path="/" element={<App />} />
220+
</Routes>
221+
</BrowserRouter>
222+
</React.StrictMode>
223+
);`;
224+
225+
fs.writeFileSync(mainPath, routerSetup);
226+
209227
console.log("\n✅ Setup complete!");
210228
console.log(`\nNext steps:\n cd ${projectName}\n npm run dev`);
211229
})();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "quickstart-react",
3-
"version": "1.0.0",
3+
"version": "1.1.1",
44
"description": "A CLI tool to quickly scaffold a React + Vite project with optional CSS frameworks and useful packages, ready to use out of the box.",
55
"main": "index.js",
66
"type": "module",

0 commit comments

Comments
 (0)