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 +129 -4
- package/package.json +3 -2
- package/templates/js-base/api/hello.js +10 -0
- package/templates/js-base/package.json +3 -0
- package/templates/js-base/server.js +2 -8
- package/templates/js-base/services/logger.js +4 -0
- package/templates/js-firebase/api/hello.js +9 -0
- package/templates/js-firebase/package.json +5 -0
- package/templates/js-firebase/services/firebase.js +11 -0
- package/templates/js-mongo/api/hello.js +10 -0
- package/templates/js-mongo/config/db.js +14 -0
- package/templates/js-mongo/package.json +5 -0
- package/templates/js-mongo/services/db.js +7 -0
- package/templates/js-sql/api/hello.js +6 -0
- package/templates/js-sql/package.json +6 -0
- package/templates/js-sql/services/db.js +7 -0
- package/templates/js-supabase/api/hello.js +10 -0
- package/templates/js-supabase/config/supabase.js +10 -0
- package/templates/js-supabase/package.json +5 -0
- package/templates/js-supabase/services/supabase.js +9 -0
- package/templates/js-base/index.js +0 -1
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
}
|
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
const
|
|
1
|
+
const { startServer } = require("zerra-core");
|
|
2
2
|
|
|
3
|
-
|
|
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,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,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,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;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
console.log("Welcome to your new Zerra application!");
|