create-zerra-app 1.0.1 → 1.1.0

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/index.js CHANGED
@@ -3,18 +3,143 @@
3
3
  const { Command } = require("commander");
4
4
  const fs = require("fs-extra");
5
5
  const path = require("path");
6
+ const inquirer = require("inquirer");
6
7
 
7
8
  const program = new Command();
8
9
 
9
10
  program
10
11
  .argument("<project-name>")
11
- .action((projectName) => {
12
+ .action(async (projectName) => {
12
13
  const targetPath = path.join(process.cwd(), projectName);
13
- const templatePath = path.join(__dirname, "templates/js-base");
14
14
 
15
- fs.copySync(templatePath, targetPath);
15
+ // 1. Ask for Database Preference and CLI features
16
+ const answers = await inquirer.prompt([
17
+ {
18
+ type: "list",
19
+ name: "database",
20
+ message: "Which database would you like to use?",
21
+ choices: [
22
+ { name: "None (Simple API)", value: "js-base" },
23
+ { name: "SQL (PostgreSQL/MySQL)", value: "js-sql" },
24
+ { name: "MongoDB", value: "js-mongo" },
25
+ { name: "Supabase", value: "js-supabase" },
26
+ { name: "Firebase", value: "js-firebase" },
27
+ ],
28
+ },
29
+ {
30
+ type: "confirm",
31
+ name: "installDeps",
32
+ message: "Would you like to install dependencies automatically?",
33
+ default: true,
34
+ },
35
+ {
36
+ type: "confirm",
37
+ name: "initGit",
38
+ message: "Would you like to initialize a new git repository?",
39
+ default: true,
40
+ },
41
+ {
42
+ type: "checkbox",
43
+ name: "features",
44
+ message: "Which advanced Zerra features would you like to enable?",
45
+ choices: [
46
+ { name: "Beautiful Request Logging", value: "logging", checked: true },
47
+ { name: "Dynamic Routing ([id].js)", value: "dynamicRouting", checked: true },
48
+ { name: "File-Based Middleware (_middleware.js)", value: "middleware", checked: true },
49
+ { name: "Auto-load Environment Variables (.env)", value: "dotenv", checked: true },
50
+ { name: "Automatic Input Validation (Schema)", value: "validation", checked: true },
51
+ { name: "Multipart File Uploads (req.files)", value: "multipart", checked: true }
52
+ ]
53
+ }
54
+ ]);
16
55
 
17
- console.log(`šŸš€ Zerra project created at ${targetPath}`);
56
+ const baseTemplatePath = path.join(__dirname, "templates", "js-base");
57
+ const dbTemplatePath = path.join(__dirname, "templates", answers.database);
58
+
59
+ console.log(`\nšŸ—ļø Generating Zerra project: ${projectName}...`);
60
+
61
+ try {
62
+ // 1. Copy the Base Template (Foundation)
63
+ if (fs.existsSync(baseTemplatePath)) {
64
+ await fs.copy(baseTemplatePath, targetPath);
65
+ }
66
+
67
+ // 2. Overlay Database-Specific Template (if not base)
68
+ if (answers.database !== "js-base" && fs.existsSync(dbTemplatePath)) {
69
+ // We use a custom copy to handle package.json merging
70
+ await fs.copy(dbTemplatePath, targetPath, {
71
+ overwrite: true,
72
+ filter: (src) => !src.endsWith("package.json"), // Handle package.json separately
73
+ });
74
+
75
+ const dbPkgPath = path.join(dbTemplatePath, "package.json");
76
+ const targetPkgPath = path.join(targetPath, "package.json");
77
+
78
+ if (fs.existsSync(dbPkgPath) && fs.existsSync(targetPkgPath)) {
79
+ const basePkg = await fs.readJson(targetPkgPath);
80
+ const dbPkg = await fs.readJson(dbPkgPath);
81
+
82
+ // Merge dependencies and scripts
83
+ basePkg.dependencies = { ...(basePkg.dependencies || {}), ...(dbPkg.dependencies || {}) };
84
+ basePkg.scripts = { ...(basePkg.scripts || {}), ...(dbPkg.scripts || {}) };
85
+
86
+ await fs.writeJson(targetPkgPath, basePkg, { spaces: 2 });
87
+ }
88
+ }
89
+
90
+ // 3. Customize project name in package.json
91
+ const pkgPath = path.join(targetPath, "package.json");
92
+ if (fs.existsSync(pkgPath)) {
93
+ const pkg = await fs.readJson(pkgPath);
94
+ pkg.name = projectName;
95
+ await fs.writeJson(pkgPath, pkg, { spaces: 2 });
96
+ }
97
+
98
+ // 4. Generate zerra.config.json based on feature selection
99
+ const featureConfig = {
100
+ logging: answers.features.includes('logging'),
101
+ dynamicRouting: answers.features.includes('dynamicRouting'),
102
+ middleware: answers.features.includes('middleware'),
103
+ dotenv: answers.features.includes('dotenv'),
104
+ validation: answers.features.includes('validation'),
105
+ multipart: answers.features.includes('multipart')
106
+ };
107
+
108
+ const configJsonPath = path.join(targetPath, 'zerra.config.json');
109
+ await fs.writeJson(configJsonPath, { features: featureConfig }, { spaces: 2 });
110
+
111
+ const { execSync } = require("child_process");
112
+
113
+ if (answers.installDeps) {
114
+ console.log(`\nšŸ“¦ Installing dependencies...`);
115
+ try {
116
+ execSync("npm install", { cwd: targetPath, stdio: "inherit" });
117
+ } catch (installErr) {
118
+ console.warn(`āš ļø Failed to install dependencies automatically. You may need to run 'npm install' manually.`);
119
+ }
120
+ }
121
+
122
+ if (answers.initGit) {
123
+ try {
124
+ execSync("git init", { cwd: targetPath, stdio: "ignore" });
125
+ execSync("git add .", { cwd: targetPath, stdio: "ignore" });
126
+ execSync('git commit -m "Initial commit from create-zerra-app"', { cwd: targetPath, stdio: "ignore" });
127
+ console.log(`🌱 Initialized a git repository.`);
128
+ } catch (gitErr) {
129
+ // Ignore git errors
130
+ }
131
+ }
132
+
133
+ console.log(`\nšŸš€ Zerra project created successfully at ${targetPath}`);
134
+ console.log(`\nNext steps:`);
135
+ console.log(` cd ${projectName}`);
136
+ if (!answers.installDeps) {
137
+ console.log(` npm install`);
138
+ }
139
+ console.log(` npm run dev\n`);
140
+ } catch (err) {
141
+ console.error("āŒ Error creating project:", err);
142
+ }
18
143
  });
19
144
 
20
145
  program.parse();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-zerra-app",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -15,6 +15,7 @@
15
15
  "type": "commonjs",
16
16
  "dependencies": {
17
17
  "commander": "^14.0.3",
18
- "fs-extra": "^11.3.4"
18
+ "fs-extra": "^11.3.4",
19
+ "inquirer": "^8.2.7"
19
20
  }
20
21
  }
@@ -0,0 +1,10 @@
1
+ const logger = require("../services/logger");
2
+
3
+ module.exports = (req, res) => {
4
+ logger.info("Hello endpoint was hit!");
5
+ res.json({
6
+ message: "Hello from Zerra! šŸš€",
7
+ timestamp: new Date().toISOString(),
8
+ query: req.query
9
+ });
10
+ };
@@ -3,5 +3,8 @@
3
3
  "version": "1.0.0",
4
4
  "scripts": {
5
5
  "dev": "node server.js"
6
+ },
7
+ "dependencies": {
8
+ "zerra-core": "^1.1.0"
6
9
  }
7
10
  }
@@ -1,9 +1,3 @@
1
- const http = require("http");
1
+ const { startServer } = require("zerra-core");
2
2
 
3
- const server = http.createServer((req, res) => {
4
- res.end("Zerra server running šŸš€");
5
- });
6
-
7
- server.listen(3000, () => {
8
- console.log("Server running on http://localhost:3000");
9
- });
3
+ startServer(3000);
@@ -0,0 +1,4 @@
1
+ module.exports = {
2
+ info: (message) => console.log(`[INFO] ${new Date().toISOString()}: ${message}`),
3
+ error: (message) => console.error(`[ERROR] ${new Date().toISOString()}: ${message}`),
4
+ };
@@ -0,0 +1,9 @@
1
+ const firebase = require("../services/firebase");
2
+
3
+ module.exports = async (req, res) => {
4
+ const snapshot = await firebase.firestore().collection("users").get();
5
+ res.json({
6
+ message: "Hello from Zerra! Firebase is ready.",
7
+ userCount: snapshot.docs.length
8
+ });
9
+ };
@@ -0,0 +1,5 @@
1
+ {
2
+ "dependencies": {
3
+ "firebase-admin": "^11.0.0"
4
+ }
5
+ }
@@ -0,0 +1,11 @@
1
+ // This is a stub for your Firebase Admin SDK
2
+ module.exports = {
3
+ firestore: () => ({
4
+ collection: (name) => ({
5
+ get: async () => {
6
+ console.log(`[FIREBASE] Getting collection: ${name}`);
7
+ return { docs: [] };
8
+ }
9
+ })
10
+ })
11
+ };
@@ -0,0 +1,10 @@
1
+ const db = require("../services/db");
2
+
3
+ module.exports = async (req, res) => {
4
+ const users = await db.find("users", {});
5
+ res.json({
6
+ message: "Hello from Zerra! MongoDB is connected.",
7
+ count: users.length,
8
+ users: users
9
+ });
10
+ };
@@ -0,0 +1,14 @@
1
+ // Simple MongoDB connection boilerplate (using mongoose)
2
+ const mongoose = require("mongoose");
3
+
4
+ const connectDB = async () => {
5
+ try {
6
+ const conn = await mongoose.connect(process.env.MONGO_URI || "mongodb://localhost:27017/zerra_db");
7
+ console.log(`🌿 MongoDB Connected: ${conn.connection.host}`);
8
+ } catch (error) {
9
+ console.error(`Error: ${error.message}`);
10
+ process.exit(1);
11
+ }
12
+ };
13
+
14
+ module.exports = connectDB;
@@ -0,0 +1,5 @@
1
+ {
2
+ "dependencies": {
3
+ "mongodb": "^6.0.0"
4
+ }
5
+ }
@@ -0,0 +1,7 @@
1
+ // This is a stub for your MongoDB connection (e.g., using Mongoose or mongodb driver)
2
+ module.exports = {
3
+ find: async (collection, query) => {
4
+ console.log(`[MONGO] Finding in ${collection}:`, query);
5
+ return []; // Return mock results
6
+ }
7
+ };
@@ -0,0 +1,6 @@
1
+ const db = require("../services/db");
2
+
3
+ module.exports = async (req, res) => {
4
+ const users = await db.query("SELECT * FROM users");
5
+ res.end(`Hello from Zerra! Database is connected. Found ${users.length} users.`);
6
+ };
@@ -0,0 +1,6 @@
1
+ {
2
+ "dependencies": {
3
+ "pg": "^8.0.0",
4
+ "mysql2": "^3.0.0"
5
+ }
6
+ }
@@ -0,0 +1,7 @@
1
+ // This is a stub for your SQL database connection (e.g., using Knex, Sequelize, or pg)
2
+ module.exports = {
3
+ query: async (sql, params) => {
4
+ console.log(`[DB QUERY] Executing: ${sql} with params:`, params);
5
+ return []; // Return mock results
6
+ }
7
+ };
@@ -0,0 +1,10 @@
1
+ const supabase = require("../services/supabase");
2
+
3
+ module.exports = async (req, res) => {
4
+ const { data, error } = await supabase.from("profiles").select("*");
5
+ res.json({
6
+ message: "Hello from Zerra! Supabase is ready.",
7
+ profiles: data,
8
+ error: error
9
+ });
10
+ };
@@ -0,0 +1,10 @@
1
+ const { createClient } = require("@supabase/supabase-js");
2
+
3
+ const supabaseUrl = process.env.SUPABASE_URL || "https://your-project.supabase.co";
4
+ const supabaseKey = process.env.SUPABASE_KEY || "your-anon-key";
5
+
6
+ const supabase = createClient(supabaseUrl, supabaseKey);
7
+
8
+ console.log("⚔ Supabase Client Initialized");
9
+
10
+ module.exports = supabase;
@@ -0,0 +1,5 @@
1
+ {
2
+ "dependencies": {
3
+ "@supabase/supabase-js": "^2.0.0"
4
+ }
5
+ }
@@ -0,0 +1,9 @@
1
+ // This is a stub for your Supabase client
2
+ module.exports = {
3
+ from: (table) => ({
4
+ select: () => {
5
+ console.log(`[SUPABASE] Selecting from ${table}`);
6
+ return { data: [], error: null };
7
+ }
8
+ })
9
+ };
@@ -1 +0,0 @@
1
- console.log("Welcome to your new Zerra application!");