@codaijs/keel 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__tests__/sail-installer.test.js +25 -25
- package/dist/sail-installer.js +174 -174
- package/dist/scaffold.js +68 -68
- package/package.json +58 -58
- package/sails/_template/addon.json +20 -20
- package/sails/_template/install.ts +402 -402
- package/sails/admin-dashboard/README.md +117 -117
- package/sails/admin-dashboard/addon.json +28 -28
- package/sails/admin-dashboard/files/backend/middleware/admin.ts +34 -34
- package/sails/admin-dashboard/files/backend/routes/admin.ts +243 -243
- package/sails/admin-dashboard/files/frontend/components/admin/StatsCard.tsx +40 -40
- package/sails/admin-dashboard/files/frontend/components/admin/UsersTable.tsx +240 -240
- package/sails/admin-dashboard/files/frontend/hooks/useAdmin.ts +149 -149
- package/sails/admin-dashboard/files/frontend/pages/admin/Dashboard.tsx +173 -173
- package/sails/admin-dashboard/files/frontend/pages/admin/UserDetail.tsx +203 -203
- package/sails/admin-dashboard/install.ts +305 -305
- package/sails/analytics/README.md +178 -178
- package/sails/analytics/addon.json +27 -27
- package/sails/analytics/files/frontend/components/AnalyticsProvider.tsx +58 -58
- package/sails/analytics/files/frontend/hooks/useAnalytics.ts +64 -64
- package/sails/analytics/files/frontend/lib/analytics.ts +103 -103
- package/sails/analytics/install.ts +297 -297
- package/sails/file-uploads/addon.json +30 -30
- package/sails/file-uploads/files/backend/routes/files.ts +198 -198
- package/sails/file-uploads/files/backend/schema/files.ts +36 -36
- package/sails/file-uploads/files/backend/services/file-storage.ts +128 -128
- package/sails/file-uploads/files/frontend/components/FileList.tsx +248 -248
- package/sails/file-uploads/files/frontend/components/FileUploadButton.tsx +147 -147
- package/sails/file-uploads/files/frontend/hooks/useFileUpload.ts +106 -106
- package/sails/file-uploads/files/frontend/hooks/useFiles.ts +118 -118
- package/sails/file-uploads/files/frontend/pages/Files.tsx +37 -37
- package/sails/file-uploads/install.ts +466 -466
- package/sails/gdpr/README.md +174 -174
- package/sails/gdpr/addon.json +27 -27
- package/sails/gdpr/files/backend/routes/gdpr.ts +140 -140
- package/sails/gdpr/files/backend/services/gdpr.ts +293 -293
- package/sails/gdpr/files/frontend/components/auth/ConsentCheckboxes.tsx +97 -97
- package/sails/gdpr/files/frontend/components/gdpr/AccountDeletionRequest.tsx +192 -192
- package/sails/gdpr/files/frontend/components/gdpr/DataExportButton.tsx +75 -75
- package/sails/gdpr/files/frontend/pages/PrivacyPolicy.tsx +186 -186
- package/sails/gdpr/install.ts +756 -756
- package/sails/google-oauth/README.md +121 -121
- package/sails/google-oauth/addon.json +22 -22
- package/sails/google-oauth/files/GoogleButton.tsx +50 -50
- package/sails/google-oauth/install.ts +252 -252
- package/sails/i18n/README.md +193 -193
- package/sails/i18n/addon.json +30 -30
- package/sails/i18n/files/frontend/components/LanguageSwitcher.tsx +108 -108
- package/sails/i18n/files/frontend/hooks/useLanguage.ts +31 -31
- package/sails/i18n/files/frontend/lib/i18n.ts +32 -32
- package/sails/i18n/files/frontend/locales/de/common.json +44 -44
- package/sails/i18n/files/frontend/locales/en/common.json +44 -44
- package/sails/i18n/install.ts +407 -407
- package/sails/push-notifications/README.md +163 -163
- package/sails/push-notifications/addon.json +31 -31
- package/sails/push-notifications/files/backend/routes/notifications.ts +153 -153
- package/sails/push-notifications/files/backend/schema/notifications.ts +31 -31
- package/sails/push-notifications/files/backend/services/notifications.ts +117 -117
- package/sails/push-notifications/files/frontend/components/PushNotificationInit.tsx +12 -12
- package/sails/push-notifications/files/frontend/hooks/usePushNotifications.ts +154 -154
- package/sails/push-notifications/install.ts +384 -384
- package/sails/r2-storage/addon.json +29 -29
- package/sails/r2-storage/files/backend/services/storage.ts +71 -71
- package/sails/r2-storage/files/frontend/components/ProfilePictureUpload.tsx +167 -167
- package/sails/r2-storage/install.ts +412 -412
- package/sails/rate-limiting/addon.json +20 -20
- package/sails/rate-limiting/files/backend/middleware/rate-limit-store.ts +104 -104
- package/sails/rate-limiting/files/backend/middleware/rate-limit.ts +137 -137
- package/sails/rate-limiting/install.ts +300 -300
- package/sails/registry.json +107 -107
- package/sails/stripe/README.md +214 -214
- package/sails/stripe/addon.json +24 -24
- package/sails/stripe/files/backend/routes/stripe.ts +154 -154
- package/sails/stripe/files/backend/schema/stripe.ts +74 -74
- package/sails/stripe/files/backend/services/stripe.ts +224 -224
- package/sails/stripe/files/frontend/components/SubscriptionStatus.tsx +135 -135
- package/sails/stripe/files/frontend/hooks/useSubscription.ts +86 -86
- package/sails/stripe/files/frontend/pages/Checkout.tsx +116 -116
- package/sails/stripe/files/frontend/pages/Pricing.tsx +226 -226
- package/sails/stripe/install.ts +378 -378
|
@@ -1,305 +1,305 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Admin Dashboard Sail Installer
|
|
3
|
-
*
|
|
4
|
-
* Adds an admin dashboard for user management and basic metrics.
|
|
5
|
-
* Includes user listing, user detail, stats cards, and a signup chart.
|
|
6
|
-
*
|
|
7
|
-
* Usage:
|
|
8
|
-
* npx tsx sails/admin-dashboard/install.ts
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
readFileSync,
|
|
13
|
-
writeFileSync,
|
|
14
|
-
copyFileSync,
|
|
15
|
-
existsSync,
|
|
16
|
-
mkdirSync,
|
|
17
|
-
} from "node:fs";
|
|
18
|
-
import { resolve, dirname, join } from "node:path";
|
|
19
|
-
import { execSync } from "node:child_process";
|
|
20
|
-
import { input, confirm } from "@inquirer/prompts";
|
|
21
|
-
|
|
22
|
-
// ---------------------------------------------------------------------------
|
|
23
|
-
// Paths
|
|
24
|
-
// ---------------------------------------------------------------------------
|
|
25
|
-
|
|
26
|
-
const SAIL_DIR = dirname(new URL(import.meta.url).pathname);
|
|
27
|
-
const PROJECT_ROOT = resolve(SAIL_DIR, "../..");
|
|
28
|
-
const BACKEND_ROOT = join(PROJECT_ROOT, "packages/backend");
|
|
29
|
-
const FRONTEND_ROOT = join(PROJECT_ROOT, "packages/frontend");
|
|
30
|
-
|
|
31
|
-
// ---------------------------------------------------------------------------
|
|
32
|
-
// Helpers
|
|
33
|
-
// ---------------------------------------------------------------------------
|
|
34
|
-
|
|
35
|
-
interface SailManifest {
|
|
36
|
-
name: string;
|
|
37
|
-
displayName: string;
|
|
38
|
-
version: string;
|
|
39
|
-
requiredEnvVars: { key: string; description: string }[];
|
|
40
|
-
dependencies: { backend: Record<string, string>; frontend: Record<string, string> };
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function loadManifest(): SailManifest {
|
|
44
|
-
return JSON.parse(readFileSync(join(SAIL_DIR, "addon.json"), "utf-8"));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function insertAtMarker(filePath: string, marker: string, code: string): void {
|
|
48
|
-
if (!existsSync(filePath)) {
|
|
49
|
-
console.warn(` Warning: File not found: ${filePath}`);
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
let content = readFileSync(filePath, "utf-8");
|
|
53
|
-
if (!content.includes(marker)) {
|
|
54
|
-
console.warn(` Warning: Marker "${marker}" not found in ${filePath}`);
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
if (content.includes(code.trim())) {
|
|
58
|
-
console.log(` Skipped (already present) -> ${filePath}`);
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
content = content.replace(marker, `${marker}\n${code}`);
|
|
62
|
-
writeFileSync(filePath, content, "utf-8");
|
|
63
|
-
console.log(` Modified -> ${filePath}`);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function copyFile(src: string, dest: string, label: string): void {
|
|
67
|
-
mkdirSync(dirname(dest), { recursive: true });
|
|
68
|
-
copyFileSync(src, dest);
|
|
69
|
-
console.log(` Copied -> ${label}`);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function installDeps(deps: Record<string, string>, workspace: string): void {
|
|
73
|
-
const entries = Object.entries(deps);
|
|
74
|
-
if (entries.length === 0) return;
|
|
75
|
-
const packages = entries.map(([n, v]) => `${n}@${v}`).join(" ");
|
|
76
|
-
const cmd = `npm install ${packages} --workspace=${workspace}`;
|
|
77
|
-
console.log(` Running: ${cmd}`);
|
|
78
|
-
execSync(cmd, { cwd: PROJECT_ROOT, stdio: "inherit" });
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// ---------------------------------------------------------------------------
|
|
82
|
-
// Email validation helper
|
|
83
|
-
// ---------------------------------------------------------------------------
|
|
84
|
-
|
|
85
|
-
function isValidEmail(email: string): boolean {
|
|
86
|
-
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function validateAdminEmails(value: string): true | string {
|
|
90
|
-
if (!value || value.trim().length === 0) {
|
|
91
|
-
return "At least one admin email is required.";
|
|
92
|
-
}
|
|
93
|
-
const emails = value.split(",").map((e) => e.trim()).filter(Boolean);
|
|
94
|
-
if (emails.length === 0) {
|
|
95
|
-
return "At least one admin email is required.";
|
|
96
|
-
}
|
|
97
|
-
for (const email of emails) {
|
|
98
|
-
if (!isValidEmail(email)) {
|
|
99
|
-
return `Invalid email format: "${email}". Please provide valid email addresses separated by commas.`;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return true;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// ---------------------------------------------------------------------------
|
|
106
|
-
// Main
|
|
107
|
-
// ---------------------------------------------------------------------------
|
|
108
|
-
|
|
109
|
-
async function main(): Promise<void> {
|
|
110
|
-
const manifest = loadManifest();
|
|
111
|
-
|
|
112
|
-
// -- Step 1: Welcome --------------------------------------------------------
|
|
113
|
-
console.log("\n------------------------------------------------------------");
|
|
114
|
-
console.log(` Admin Dashboard Sail Installer (v${manifest.version})`);
|
|
115
|
-
console.log("------------------------------------------------------------");
|
|
116
|
-
console.log();
|
|
117
|
-
console.log(" This sail adds an admin dashboard to your project:");
|
|
118
|
-
console.log(" - Dashboard with stats cards and signup chart");
|
|
119
|
-
console.log(" - User management (list, search, view details)");
|
|
120
|
-
console.log(" - Admin actions (verify email, delete user)");
|
|
121
|
-
console.log(" - Access controlled via ADMIN_EMAILS env var");
|
|
122
|
-
console.log(" - Charts powered by recharts");
|
|
123
|
-
console.log();
|
|
124
|
-
|
|
125
|
-
const pkgPath = join(PROJECT_ROOT, "package.json");
|
|
126
|
-
if (existsSync(pkgPath)) {
|
|
127
|
-
const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
|
|
128
|
-
console.log(` Template version: ${pkg.version ?? "unknown"}`);
|
|
129
|
-
console.log();
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// -- Step 2: Collect admin emails -------------------------------------------
|
|
133
|
-
console.log(" The admin dashboard restricts access based on email addresses.");
|
|
134
|
-
console.log(" Only users whose email is in the ADMIN_EMAILS list can access");
|
|
135
|
-
console.log(" the /admin routes.");
|
|
136
|
-
console.log();
|
|
137
|
-
|
|
138
|
-
const adminEmails = await input({
|
|
139
|
-
message: "Admin email addresses (comma-separated):",
|
|
140
|
-
validate: validateAdminEmails,
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
const emails = adminEmails.split(",").map((e) => e.trim()).filter(Boolean);
|
|
144
|
-
|
|
145
|
-
console.log();
|
|
146
|
-
console.log(` Admin emails: ${emails.join(", ")}`);
|
|
147
|
-
console.log();
|
|
148
|
-
|
|
149
|
-
// -- Step 3: Summary --------------------------------------------------------
|
|
150
|
-
console.log(" Summary of changes:");
|
|
151
|
-
console.log(" -------------------");
|
|
152
|
-
console.log(" Files to create (backend):");
|
|
153
|
-
console.log(" + packages/backend/src/middleware/admin.ts");
|
|
154
|
-
console.log(" + packages/backend/src/routes/admin.ts");
|
|
155
|
-
console.log();
|
|
156
|
-
console.log(" Files to create (frontend):");
|
|
157
|
-
console.log(" + packages/frontend/src/pages/admin/Dashboard.tsx");
|
|
158
|
-
console.log(" + packages/frontend/src/pages/admin/UserDetail.tsx");
|
|
159
|
-
console.log(" + packages/frontend/src/components/admin/StatsCard.tsx");
|
|
160
|
-
console.log(" + packages/frontend/src/components/admin/UsersTable.tsx");
|
|
161
|
-
console.log(" + packages/frontend/src/hooks/useAdmin.ts");
|
|
162
|
-
console.log();
|
|
163
|
-
console.log(" Files to modify:");
|
|
164
|
-
console.log(" ~ packages/backend/src/index.ts (add admin routes)");
|
|
165
|
-
console.log(" ~ packages/backend/src/env.ts (add ADMIN_EMAILS)");
|
|
166
|
-
console.log(" ~ packages/frontend/src/router.tsx (add admin pages)");
|
|
167
|
-
console.log(" ~ .env.example / .env");
|
|
168
|
-
console.log();
|
|
169
|
-
console.log(" Environment variables:");
|
|
170
|
-
console.log(` ADMIN_EMAILS=${emails.join(",")}`);
|
|
171
|
-
console.log();
|
|
172
|
-
|
|
173
|
-
// -- Step 4: Confirm --------------------------------------------------------
|
|
174
|
-
const proceed = await confirm({ message: "Proceed with installation?", default: true });
|
|
175
|
-
if (!proceed) {
|
|
176
|
-
console.log("\n Installation cancelled.\n");
|
|
177
|
-
process.exit(0);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
console.log();
|
|
181
|
-
console.log(" Installing...");
|
|
182
|
-
console.log();
|
|
183
|
-
|
|
184
|
-
// -- Step 5: Copy files -----------------------------------------------------
|
|
185
|
-
console.log(" Copying backend files...");
|
|
186
|
-
const backendFiles = [
|
|
187
|
-
{ src: "backend/middleware/admin.ts", dest: "src/middleware/admin.ts" },
|
|
188
|
-
{ src: "backend/routes/admin.ts", dest: "src/routes/admin.ts" },
|
|
189
|
-
];
|
|
190
|
-
for (const f of backendFiles) {
|
|
191
|
-
copyFile(join(SAIL_DIR, "files", f.src), join(BACKEND_ROOT, f.dest), f.dest);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
console.log();
|
|
195
|
-
console.log(" Copying frontend files...");
|
|
196
|
-
const frontendFiles = [
|
|
197
|
-
{ src: "frontend/pages/admin/Dashboard.tsx", dest: "src/pages/admin/Dashboard.tsx" },
|
|
198
|
-
{ src: "frontend/pages/admin/UserDetail.tsx", dest: "src/pages/admin/UserDetail.tsx" },
|
|
199
|
-
{ src: "frontend/components/admin/StatsCard.tsx", dest: "src/components/admin/StatsCard.tsx" },
|
|
200
|
-
{ src: "frontend/components/admin/UsersTable.tsx", dest: "src/components/admin/UsersTable.tsx" },
|
|
201
|
-
{ src: "frontend/hooks/useAdmin.ts", dest: "src/hooks/useAdmin.ts" },
|
|
202
|
-
];
|
|
203
|
-
for (const f of frontendFiles) {
|
|
204
|
-
copyFile(join(SAIL_DIR, "files", f.src), join(FRONTEND_ROOT, f.dest), f.dest);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
// -- Step 6: Modify existing files ------------------------------------------
|
|
208
|
-
console.log();
|
|
209
|
-
console.log(" Modifying backend files...");
|
|
210
|
-
|
|
211
|
-
insertAtMarker(
|
|
212
|
-
join(BACKEND_ROOT, "src/index.ts"),
|
|
213
|
-
"// [SAIL_IMPORTS]",
|
|
214
|
-
'import adminRoutes from "./routes/admin.js";',
|
|
215
|
-
);
|
|
216
|
-
insertAtMarker(
|
|
217
|
-
join(BACKEND_ROOT, "src/index.ts"),
|
|
218
|
-
"// [SAIL_ROUTES]",
|
|
219
|
-
'app.use("/api/admin", adminRoutes);',
|
|
220
|
-
);
|
|
221
|
-
insertAtMarker(
|
|
222
|
-
join(BACKEND_ROOT, "src/env.ts"),
|
|
223
|
-
"// [SAIL_ENV_VARS]",
|
|
224
|
-
' ADMIN_EMAILS: z.string().min(1, "ADMIN_EMAILS is required (comma-separated admin emails)"),',
|
|
225
|
-
);
|
|
226
|
-
|
|
227
|
-
console.log();
|
|
228
|
-
console.log(" Modifying frontend files...");
|
|
229
|
-
|
|
230
|
-
const routerPath = join(FRONTEND_ROOT, "src/router.tsx");
|
|
231
|
-
if (existsSync(routerPath)) {
|
|
232
|
-
let routerContent = readFileSync(routerPath, "utf-8");
|
|
233
|
-
|
|
234
|
-
// Add imports if not present
|
|
235
|
-
if (!routerContent.includes("AdminDashboard")) {
|
|
236
|
-
routerContent = routerContent.replace(
|
|
237
|
-
"export function AppRouter() {",
|
|
238
|
-
'import AdminDashboard from "./pages/admin/Dashboard";\nimport AdminUserDetail from "./pages/admin/UserDetail";\n\nexport function AppRouter() {',
|
|
239
|
-
);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
// Add admin routes before the SAIL_ROUTES marker
|
|
243
|
-
if (!routerContent.includes('path="/admin"')) {
|
|
244
|
-
routerContent = routerContent.replace(
|
|
245
|
-
"{/* [SAIL_ROUTES] */}",
|
|
246
|
-
`<Route element={<ProtectedRoute />}>
|
|
247
|
-
<Route path="/admin" element={<AdminDashboard />} />
|
|
248
|
-
<Route path="/admin/users/:id" element={<AdminUserDetail />} />
|
|
249
|
-
</Route>
|
|
250
|
-
{/* [SAIL_ROUTES] */}`,
|
|
251
|
-
);
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
writeFileSync(routerPath, routerContent, "utf-8");
|
|
255
|
-
console.log(" Modified -> src/router.tsx");
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
// -- Step 7: Update env files -----------------------------------------------
|
|
259
|
-
console.log();
|
|
260
|
-
console.log(" Updating environment files...");
|
|
261
|
-
|
|
262
|
-
for (const envFile of [".env.example", ".env"]) {
|
|
263
|
-
const envPath = join(PROJECT_ROOT, envFile);
|
|
264
|
-
if (!existsSync(envPath)) continue;
|
|
265
|
-
|
|
266
|
-
let content = readFileSync(envPath, "utf-8");
|
|
267
|
-
if (!content.includes("ADMIN_EMAILS")) {
|
|
268
|
-
content += `\n# Admin Dashboard\nADMIN_EMAILS=${emails.join(",")}\n`;
|
|
269
|
-
writeFileSync(envPath, content, "utf-8");
|
|
270
|
-
console.log(` Updated ${envFile}`);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
// -- Step 8: Install dependencies -------------------------------------------
|
|
275
|
-
console.log();
|
|
276
|
-
console.log(" Installing dependencies...");
|
|
277
|
-
installDeps(manifest.dependencies.backend, "packages/backend");
|
|
278
|
-
installDeps(manifest.dependencies.frontend, "packages/frontend");
|
|
279
|
-
|
|
280
|
-
// -- Step 9: Next steps -----------------------------------------------------
|
|
281
|
-
console.log();
|
|
282
|
-
console.log("------------------------------------------------------------");
|
|
283
|
-
console.log(" Admin Dashboard installed successfully!");
|
|
284
|
-
console.log("------------------------------------------------------------");
|
|
285
|
-
console.log();
|
|
286
|
-
console.log(" Next steps:");
|
|
287
|
-
console.log();
|
|
288
|
-
console.log(" 1. Start your dev server:");
|
|
289
|
-
console.log(" npm run dev");
|
|
290
|
-
console.log();
|
|
291
|
-
console.log(" 2. Navigate to /admin in your browser");
|
|
292
|
-
console.log(" (you must be logged in with an admin email)");
|
|
293
|
-
console.log();
|
|
294
|
-
console.log(" 3. To add/remove admin users, update ADMIN_EMAILS in .env:");
|
|
295
|
-
console.log(` ADMIN_EMAILS=${emails.join(",")}`);
|
|
296
|
-
console.log();
|
|
297
|
-
console.log(" 4. Optionally add an admin link to your Header component");
|
|
298
|
-
console.log(" for users with admin privileges.");
|
|
299
|
-
console.log();
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
main().catch((err) => {
|
|
303
|
-
console.error("Installation failed:", err);
|
|
304
|
-
process.exit(1);
|
|
305
|
-
});
|
|
1
|
+
/**
|
|
2
|
+
* Admin Dashboard Sail Installer
|
|
3
|
+
*
|
|
4
|
+
* Adds an admin dashboard for user management and basic metrics.
|
|
5
|
+
* Includes user listing, user detail, stats cards, and a signup chart.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* npx tsx sails/admin-dashboard/install.ts
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
readFileSync,
|
|
13
|
+
writeFileSync,
|
|
14
|
+
copyFileSync,
|
|
15
|
+
existsSync,
|
|
16
|
+
mkdirSync,
|
|
17
|
+
} from "node:fs";
|
|
18
|
+
import { resolve, dirname, join } from "node:path";
|
|
19
|
+
import { execSync } from "node:child_process";
|
|
20
|
+
import { input, confirm } from "@inquirer/prompts";
|
|
21
|
+
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
// Paths
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
|
|
26
|
+
const SAIL_DIR = dirname(new URL(import.meta.url).pathname);
|
|
27
|
+
const PROJECT_ROOT = resolve(SAIL_DIR, "../..");
|
|
28
|
+
const BACKEND_ROOT = join(PROJECT_ROOT, "packages/backend");
|
|
29
|
+
const FRONTEND_ROOT = join(PROJECT_ROOT, "packages/frontend");
|
|
30
|
+
|
|
31
|
+
// ---------------------------------------------------------------------------
|
|
32
|
+
// Helpers
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
|
|
35
|
+
interface SailManifest {
|
|
36
|
+
name: string;
|
|
37
|
+
displayName: string;
|
|
38
|
+
version: string;
|
|
39
|
+
requiredEnvVars: { key: string; description: string }[];
|
|
40
|
+
dependencies: { backend: Record<string, string>; frontend: Record<string, string> };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function loadManifest(): SailManifest {
|
|
44
|
+
return JSON.parse(readFileSync(join(SAIL_DIR, "addon.json"), "utf-8"));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function insertAtMarker(filePath: string, marker: string, code: string): void {
|
|
48
|
+
if (!existsSync(filePath)) {
|
|
49
|
+
console.warn(` Warning: File not found: ${filePath}`);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
let content = readFileSync(filePath, "utf-8");
|
|
53
|
+
if (!content.includes(marker)) {
|
|
54
|
+
console.warn(` Warning: Marker "${marker}" not found in ${filePath}`);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (content.includes(code.trim())) {
|
|
58
|
+
console.log(` Skipped (already present) -> ${filePath}`);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
content = content.replace(marker, `${marker}\n${code}`);
|
|
62
|
+
writeFileSync(filePath, content, "utf-8");
|
|
63
|
+
console.log(` Modified -> ${filePath}`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function copyFile(src: string, dest: string, label: string): void {
|
|
67
|
+
mkdirSync(dirname(dest), { recursive: true });
|
|
68
|
+
copyFileSync(src, dest);
|
|
69
|
+
console.log(` Copied -> ${label}`);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function installDeps(deps: Record<string, string>, workspace: string): void {
|
|
73
|
+
const entries = Object.entries(deps);
|
|
74
|
+
if (entries.length === 0) return;
|
|
75
|
+
const packages = entries.map(([n, v]) => `${n}@${v}`).join(" ");
|
|
76
|
+
const cmd = `npm install ${packages} --workspace=${workspace}`;
|
|
77
|
+
console.log(` Running: ${cmd}`);
|
|
78
|
+
execSync(cmd, { cwd: PROJECT_ROOT, stdio: "inherit" });
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ---------------------------------------------------------------------------
|
|
82
|
+
// Email validation helper
|
|
83
|
+
// ---------------------------------------------------------------------------
|
|
84
|
+
|
|
85
|
+
function isValidEmail(email: string): boolean {
|
|
86
|
+
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function validateAdminEmails(value: string): true | string {
|
|
90
|
+
if (!value || value.trim().length === 0) {
|
|
91
|
+
return "At least one admin email is required.";
|
|
92
|
+
}
|
|
93
|
+
const emails = value.split(",").map((e) => e.trim()).filter(Boolean);
|
|
94
|
+
if (emails.length === 0) {
|
|
95
|
+
return "At least one admin email is required.";
|
|
96
|
+
}
|
|
97
|
+
for (const email of emails) {
|
|
98
|
+
if (!isValidEmail(email)) {
|
|
99
|
+
return `Invalid email format: "${email}". Please provide valid email addresses separated by commas.`;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// ---------------------------------------------------------------------------
|
|
106
|
+
// Main
|
|
107
|
+
// ---------------------------------------------------------------------------
|
|
108
|
+
|
|
109
|
+
async function main(): Promise<void> {
|
|
110
|
+
const manifest = loadManifest();
|
|
111
|
+
|
|
112
|
+
// -- Step 1: Welcome --------------------------------------------------------
|
|
113
|
+
console.log("\n------------------------------------------------------------");
|
|
114
|
+
console.log(` Admin Dashboard Sail Installer (v${manifest.version})`);
|
|
115
|
+
console.log("------------------------------------------------------------");
|
|
116
|
+
console.log();
|
|
117
|
+
console.log(" This sail adds an admin dashboard to your project:");
|
|
118
|
+
console.log(" - Dashboard with stats cards and signup chart");
|
|
119
|
+
console.log(" - User management (list, search, view details)");
|
|
120
|
+
console.log(" - Admin actions (verify email, delete user)");
|
|
121
|
+
console.log(" - Access controlled via ADMIN_EMAILS env var");
|
|
122
|
+
console.log(" - Charts powered by recharts");
|
|
123
|
+
console.log();
|
|
124
|
+
|
|
125
|
+
const pkgPath = join(PROJECT_ROOT, "package.json");
|
|
126
|
+
if (existsSync(pkgPath)) {
|
|
127
|
+
const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
|
|
128
|
+
console.log(` Template version: ${pkg.version ?? "unknown"}`);
|
|
129
|
+
console.log();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// -- Step 2: Collect admin emails -------------------------------------------
|
|
133
|
+
console.log(" The admin dashboard restricts access based on email addresses.");
|
|
134
|
+
console.log(" Only users whose email is in the ADMIN_EMAILS list can access");
|
|
135
|
+
console.log(" the /admin routes.");
|
|
136
|
+
console.log();
|
|
137
|
+
|
|
138
|
+
const adminEmails = await input({
|
|
139
|
+
message: "Admin email addresses (comma-separated):",
|
|
140
|
+
validate: validateAdminEmails,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
const emails = adminEmails.split(",").map((e) => e.trim()).filter(Boolean);
|
|
144
|
+
|
|
145
|
+
console.log();
|
|
146
|
+
console.log(` Admin emails: ${emails.join(", ")}`);
|
|
147
|
+
console.log();
|
|
148
|
+
|
|
149
|
+
// -- Step 3: Summary --------------------------------------------------------
|
|
150
|
+
console.log(" Summary of changes:");
|
|
151
|
+
console.log(" -------------------");
|
|
152
|
+
console.log(" Files to create (backend):");
|
|
153
|
+
console.log(" + packages/backend/src/middleware/admin.ts");
|
|
154
|
+
console.log(" + packages/backend/src/routes/admin.ts");
|
|
155
|
+
console.log();
|
|
156
|
+
console.log(" Files to create (frontend):");
|
|
157
|
+
console.log(" + packages/frontend/src/pages/admin/Dashboard.tsx");
|
|
158
|
+
console.log(" + packages/frontend/src/pages/admin/UserDetail.tsx");
|
|
159
|
+
console.log(" + packages/frontend/src/components/admin/StatsCard.tsx");
|
|
160
|
+
console.log(" + packages/frontend/src/components/admin/UsersTable.tsx");
|
|
161
|
+
console.log(" + packages/frontend/src/hooks/useAdmin.ts");
|
|
162
|
+
console.log();
|
|
163
|
+
console.log(" Files to modify:");
|
|
164
|
+
console.log(" ~ packages/backend/src/index.ts (add admin routes)");
|
|
165
|
+
console.log(" ~ packages/backend/src/env.ts (add ADMIN_EMAILS)");
|
|
166
|
+
console.log(" ~ packages/frontend/src/router.tsx (add admin pages)");
|
|
167
|
+
console.log(" ~ .env.example / .env");
|
|
168
|
+
console.log();
|
|
169
|
+
console.log(" Environment variables:");
|
|
170
|
+
console.log(` ADMIN_EMAILS=${emails.join(",")}`);
|
|
171
|
+
console.log();
|
|
172
|
+
|
|
173
|
+
// -- Step 4: Confirm --------------------------------------------------------
|
|
174
|
+
const proceed = await confirm({ message: "Proceed with installation?", default: true });
|
|
175
|
+
if (!proceed) {
|
|
176
|
+
console.log("\n Installation cancelled.\n");
|
|
177
|
+
process.exit(0);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
console.log();
|
|
181
|
+
console.log(" Installing...");
|
|
182
|
+
console.log();
|
|
183
|
+
|
|
184
|
+
// -- Step 5: Copy files -----------------------------------------------------
|
|
185
|
+
console.log(" Copying backend files...");
|
|
186
|
+
const backendFiles = [
|
|
187
|
+
{ src: "backend/middleware/admin.ts", dest: "src/middleware/admin.ts" },
|
|
188
|
+
{ src: "backend/routes/admin.ts", dest: "src/routes/admin.ts" },
|
|
189
|
+
];
|
|
190
|
+
for (const f of backendFiles) {
|
|
191
|
+
copyFile(join(SAIL_DIR, "files", f.src), join(BACKEND_ROOT, f.dest), f.dest);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
console.log();
|
|
195
|
+
console.log(" Copying frontend files...");
|
|
196
|
+
const frontendFiles = [
|
|
197
|
+
{ src: "frontend/pages/admin/Dashboard.tsx", dest: "src/pages/admin/Dashboard.tsx" },
|
|
198
|
+
{ src: "frontend/pages/admin/UserDetail.tsx", dest: "src/pages/admin/UserDetail.tsx" },
|
|
199
|
+
{ src: "frontend/components/admin/StatsCard.tsx", dest: "src/components/admin/StatsCard.tsx" },
|
|
200
|
+
{ src: "frontend/components/admin/UsersTable.tsx", dest: "src/components/admin/UsersTable.tsx" },
|
|
201
|
+
{ src: "frontend/hooks/useAdmin.ts", dest: "src/hooks/useAdmin.ts" },
|
|
202
|
+
];
|
|
203
|
+
for (const f of frontendFiles) {
|
|
204
|
+
copyFile(join(SAIL_DIR, "files", f.src), join(FRONTEND_ROOT, f.dest), f.dest);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// -- Step 6: Modify existing files ------------------------------------------
|
|
208
|
+
console.log();
|
|
209
|
+
console.log(" Modifying backend files...");
|
|
210
|
+
|
|
211
|
+
insertAtMarker(
|
|
212
|
+
join(BACKEND_ROOT, "src/index.ts"),
|
|
213
|
+
"// [SAIL_IMPORTS]",
|
|
214
|
+
'import adminRoutes from "./routes/admin.js";',
|
|
215
|
+
);
|
|
216
|
+
insertAtMarker(
|
|
217
|
+
join(BACKEND_ROOT, "src/index.ts"),
|
|
218
|
+
"// [SAIL_ROUTES]",
|
|
219
|
+
'app.use("/api/admin", adminRoutes);',
|
|
220
|
+
);
|
|
221
|
+
insertAtMarker(
|
|
222
|
+
join(BACKEND_ROOT, "src/env.ts"),
|
|
223
|
+
"// [SAIL_ENV_VARS]",
|
|
224
|
+
' ADMIN_EMAILS: z.string().min(1, "ADMIN_EMAILS is required (comma-separated admin emails)"),',
|
|
225
|
+
);
|
|
226
|
+
|
|
227
|
+
console.log();
|
|
228
|
+
console.log(" Modifying frontend files...");
|
|
229
|
+
|
|
230
|
+
const routerPath = join(FRONTEND_ROOT, "src/router.tsx");
|
|
231
|
+
if (existsSync(routerPath)) {
|
|
232
|
+
let routerContent = readFileSync(routerPath, "utf-8");
|
|
233
|
+
|
|
234
|
+
// Add imports if not present
|
|
235
|
+
if (!routerContent.includes("AdminDashboard")) {
|
|
236
|
+
routerContent = routerContent.replace(
|
|
237
|
+
"export function AppRouter() {",
|
|
238
|
+
'import AdminDashboard from "./pages/admin/Dashboard";\nimport AdminUserDetail from "./pages/admin/UserDetail";\n\nexport function AppRouter() {',
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Add admin routes before the SAIL_ROUTES marker
|
|
243
|
+
if (!routerContent.includes('path="/admin"')) {
|
|
244
|
+
routerContent = routerContent.replace(
|
|
245
|
+
"{/* [SAIL_ROUTES] */}",
|
|
246
|
+
`<Route element={<ProtectedRoute />}>
|
|
247
|
+
<Route path="/admin" element={<AdminDashboard />} />
|
|
248
|
+
<Route path="/admin/users/:id" element={<AdminUserDetail />} />
|
|
249
|
+
</Route>
|
|
250
|
+
{/* [SAIL_ROUTES] */}`,
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
writeFileSync(routerPath, routerContent, "utf-8");
|
|
255
|
+
console.log(" Modified -> src/router.tsx");
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// -- Step 7: Update env files -----------------------------------------------
|
|
259
|
+
console.log();
|
|
260
|
+
console.log(" Updating environment files...");
|
|
261
|
+
|
|
262
|
+
for (const envFile of [".env.example", ".env"]) {
|
|
263
|
+
const envPath = join(PROJECT_ROOT, envFile);
|
|
264
|
+
if (!existsSync(envPath)) continue;
|
|
265
|
+
|
|
266
|
+
let content = readFileSync(envPath, "utf-8");
|
|
267
|
+
if (!content.includes("ADMIN_EMAILS")) {
|
|
268
|
+
content += `\n# Admin Dashboard\nADMIN_EMAILS=${emails.join(",")}\n`;
|
|
269
|
+
writeFileSync(envPath, content, "utf-8");
|
|
270
|
+
console.log(` Updated ${envFile}`);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// -- Step 8: Install dependencies -------------------------------------------
|
|
275
|
+
console.log();
|
|
276
|
+
console.log(" Installing dependencies...");
|
|
277
|
+
installDeps(manifest.dependencies.backend, "packages/backend");
|
|
278
|
+
installDeps(manifest.dependencies.frontend, "packages/frontend");
|
|
279
|
+
|
|
280
|
+
// -- Step 9: Next steps -----------------------------------------------------
|
|
281
|
+
console.log();
|
|
282
|
+
console.log("------------------------------------------------------------");
|
|
283
|
+
console.log(" Admin Dashboard installed successfully!");
|
|
284
|
+
console.log("------------------------------------------------------------");
|
|
285
|
+
console.log();
|
|
286
|
+
console.log(" Next steps:");
|
|
287
|
+
console.log();
|
|
288
|
+
console.log(" 1. Start your dev server:");
|
|
289
|
+
console.log(" npm run dev");
|
|
290
|
+
console.log();
|
|
291
|
+
console.log(" 2. Navigate to /admin in your browser");
|
|
292
|
+
console.log(" (you must be logged in with an admin email)");
|
|
293
|
+
console.log();
|
|
294
|
+
console.log(" 3. To add/remove admin users, update ADMIN_EMAILS in .env:");
|
|
295
|
+
console.log(` ADMIN_EMAILS=${emails.join(",")}`);
|
|
296
|
+
console.log();
|
|
297
|
+
console.log(" 4. Optionally add an admin link to your Header component");
|
|
298
|
+
console.log(" for users with admin privileges.");
|
|
299
|
+
console.log();
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
main().catch((err) => {
|
|
303
|
+
console.error("Installation failed:", err);
|
|
304
|
+
process.exit(1);
|
|
305
|
+
});
|