mwalajs 1.1.16 → 1.1.17
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/createProject.mjs +178 -51
- package/package.json +1 -1
package/createProject.mjs
CHANGED
|
@@ -1,44 +1,67 @@
|
|
|
1
|
-
// createProject.mjs
|
|
1
|
+
// createProject.mjs (ULTIMATE CROSS-PLATFORM VERSION)
|
|
2
|
+
|
|
2
3
|
import fs from "fs-extra";
|
|
3
4
|
import path from "path";
|
|
4
5
|
import readline from "readline";
|
|
5
6
|
import os from "os";
|
|
7
|
+
import { fileURLToPath } from "url";
|
|
8
|
+
|
|
9
|
+
/* =========================
|
|
10
|
+
SAFE PATH HELPERS
|
|
11
|
+
========================= */
|
|
12
|
+
|
|
13
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
14
|
+
const __dirname = path.dirname(__filename);
|
|
6
15
|
|
|
7
16
|
/**
|
|
8
|
-
*
|
|
17
|
+
* Detect best possible MwalaJS template path
|
|
18
|
+
* Works on ALL OS
|
|
9
19
|
*/
|
|
10
20
|
function getMwalajsPath() {
|
|
11
21
|
const envPath = process.env.MWALAJSPATH;
|
|
12
22
|
|
|
13
|
-
const
|
|
23
|
+
const possiblePaths = [
|
|
14
24
|
envPath,
|
|
25
|
+
path.join(process.cwd(), "template"),
|
|
26
|
+
path.join(__dirname, "template"),
|
|
27
|
+
path.join(__dirname, "../template"),
|
|
28
|
+
path.join(__dirname, "../../template"),
|
|
15
29
|
"C:\\Program Files\\mwalajs",
|
|
30
|
+
"C:\\mwalajs",
|
|
16
31
|
"/usr/local/lib/mwalajs",
|
|
32
|
+
"/usr/lib/mwalajs",
|
|
17
33
|
"/var/www/mwalajs",
|
|
18
|
-
|
|
34
|
+
"/opt/mwalajs"
|
|
19
35
|
];
|
|
20
36
|
|
|
21
|
-
for (const p of
|
|
22
|
-
if (p && fs.existsSync(p))
|
|
37
|
+
for (const p of possiblePaths) {
|
|
38
|
+
if (p && fs.existsSync(p)) {
|
|
39
|
+
return fs.realpathSync(p);
|
|
40
|
+
}
|
|
23
41
|
}
|
|
24
42
|
|
|
25
|
-
return null;
|
|
43
|
+
return null;
|
|
26
44
|
}
|
|
27
45
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
|
|
46
|
+
/* =========================
|
|
47
|
+
CLI INPUT
|
|
48
|
+
========================= */
|
|
49
|
+
|
|
50
|
+
function ask(rl, question) {
|
|
51
|
+
return new Promise((resolve) => {
|
|
52
|
+
rl.question(question, (ans) => resolve(ans.trim()));
|
|
53
|
+
});
|
|
33
54
|
}
|
|
34
55
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
56
|
+
/* =========================
|
|
57
|
+
MANUAL TEMPLATE GENERATOR
|
|
58
|
+
(ALWAYS WORKS - FALLBACK)
|
|
59
|
+
========================= */
|
|
60
|
+
|
|
38
61
|
function createManualTemplate(target) {
|
|
39
|
-
console.log("⚠ No template found.
|
|
62
|
+
console.log("\n⚠ No template found. Generating FULL MwalaJS scaffold...\n");
|
|
40
63
|
|
|
41
|
-
const
|
|
64
|
+
const dirs = [
|
|
42
65
|
"controllers",
|
|
43
66
|
"routes",
|
|
44
67
|
"models",
|
|
@@ -47,16 +70,23 @@ function createManualTemplate(target) {
|
|
|
47
70
|
"views/pages",
|
|
48
71
|
"middlewares",
|
|
49
72
|
"migrations",
|
|
73
|
+
"config",
|
|
74
|
+
"public",
|
|
50
75
|
"public/css",
|
|
51
76
|
"public/js",
|
|
52
|
-
"public/images"
|
|
77
|
+
"public/images",
|
|
78
|
+
"storage",
|
|
79
|
+
"logs"
|
|
53
80
|
];
|
|
54
81
|
|
|
55
|
-
|
|
82
|
+
dirs.forEach((dir) => {
|
|
56
83
|
fs.mkdirSync(path.join(target, dir), { recursive: true });
|
|
57
84
|
});
|
|
58
85
|
|
|
59
|
-
|
|
86
|
+
/* =========================
|
|
87
|
+
APP ENTRY
|
|
88
|
+
========================= */
|
|
89
|
+
|
|
60
90
|
fs.writeFileSync(
|
|
61
91
|
path.join(target, "app.mjs"),
|
|
62
92
|
`import mwalajs from 'mwalajs';
|
|
@@ -68,30 +98,43 @@ const __dirname = path.dirname(__filename);
|
|
|
68
98
|
|
|
69
99
|
mwalajs.set('view engine', 'ejs');
|
|
70
100
|
mwalajs.set('views', path.join(__dirname, 'views'));
|
|
101
|
+
|
|
71
102
|
mwalajs.useStatic(path.join(__dirname, 'public'));
|
|
72
103
|
|
|
104
|
+
/* ROUTES */
|
|
73
105
|
mwalajs.get('/', (req, res) => {
|
|
74
|
-
res.render('pages/index', {
|
|
106
|
+
res.render('pages/index', {
|
|
107
|
+
title: '🚀 MwalaJS Application Running'
|
|
108
|
+
});
|
|
75
109
|
});
|
|
76
110
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
111
|
+
/* SERVER */
|
|
112
|
+
const PORT = process.env.PORT || 3000;
|
|
113
|
+
mwalajs.listen(PORT, () => {
|
|
114
|
+
console.log(\`🚀 Server running on http://localhost:\${PORT}\`);
|
|
80
115
|
});
|
|
81
116
|
`
|
|
82
117
|
);
|
|
83
118
|
|
|
84
|
-
|
|
119
|
+
/* =========================
|
|
120
|
+
CONTROLLER
|
|
121
|
+
========================= */
|
|
122
|
+
|
|
85
123
|
fs.writeFileSync(
|
|
86
124
|
path.join(target, "controllers/homeController.mjs"),
|
|
87
125
|
`export const homeController = {
|
|
88
|
-
|
|
89
|
-
res.render('pages/index', {
|
|
126
|
+
index: (req, res) => {
|
|
127
|
+
res.render('pages/index', {
|
|
128
|
+
title: 'Home Page'
|
|
129
|
+
});
|
|
90
130
|
}
|
|
91
131
|
};`
|
|
92
132
|
);
|
|
93
133
|
|
|
94
|
-
|
|
134
|
+
/* =========================
|
|
135
|
+
ROUTES
|
|
136
|
+
========================= */
|
|
137
|
+
|
|
95
138
|
fs.writeFileSync(
|
|
96
139
|
path.join(target, "routes/homeRoutes.mjs"),
|
|
97
140
|
`import mwalajs from 'mwalajs';
|
|
@@ -99,70 +142,127 @@ import { homeController } from '../controllers/homeController.mjs';
|
|
|
99
142
|
|
|
100
143
|
const router = mwalajs.Router();
|
|
101
144
|
|
|
102
|
-
router.get('/', homeController.
|
|
145
|
+
router.get('/', homeController.index);
|
|
103
146
|
|
|
104
147
|
export { router as homeRoutes };
|
|
105
148
|
`
|
|
106
149
|
);
|
|
107
150
|
|
|
108
|
-
|
|
151
|
+
/* =========================
|
|
152
|
+
MODEL EXAMPLE
|
|
153
|
+
========================= */
|
|
154
|
+
|
|
155
|
+
fs.writeFileSync(
|
|
156
|
+
path.join(target, "models/User.mjs"),
|
|
157
|
+
`export class User {
|
|
158
|
+
constructor() {
|
|
159
|
+
this.table = "users";
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Add model logic here
|
|
163
|
+
}
|
|
164
|
+
`
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
/* =========================
|
|
168
|
+
VIEW
|
|
169
|
+
========================= */
|
|
170
|
+
|
|
109
171
|
fs.writeFileSync(
|
|
110
172
|
path.join(target, "views/pages/index.ejs"),
|
|
111
173
|
`<!DOCTYPE html>
|
|
112
174
|
<html>
|
|
113
175
|
<head>
|
|
176
|
+
<meta charset="UTF-8" />
|
|
114
177
|
<title><%= title %></title>
|
|
115
178
|
<style>
|
|
116
|
-
body {
|
|
117
|
-
|
|
179
|
+
body {
|
|
180
|
+
font-family: Arial;
|
|
181
|
+
background: linear-gradient(135deg,#0f172a,#1e293b);
|
|
182
|
+
color: white;
|
|
183
|
+
text-align: center;
|
|
184
|
+
padding: 60px;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
.box {
|
|
188
|
+
background: rgba(255,255,255,0.05);
|
|
189
|
+
padding: 30px;
|
|
190
|
+
border-radius: 16px;
|
|
191
|
+
display: inline-block;
|
|
192
|
+
box-shadow: 0 10px 30px rgba(0,0,0,0.3);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
h1 {
|
|
196
|
+
font-size: 40px;
|
|
197
|
+
}
|
|
118
198
|
</style>
|
|
119
199
|
</head>
|
|
120
200
|
<body>
|
|
121
|
-
<div class="
|
|
122
|
-
<h1>🚀
|
|
201
|
+
<div class="box">
|
|
202
|
+
<h1>🚀 MwalaJS Ready</h1>
|
|
123
203
|
<p><%= title %></p>
|
|
124
204
|
</div>
|
|
125
205
|
</body>
|
|
126
206
|
</html>`
|
|
127
207
|
);
|
|
128
208
|
|
|
129
|
-
|
|
209
|
+
/* =========================
|
|
210
|
+
PACKAGE JSON
|
|
211
|
+
========================= */
|
|
212
|
+
|
|
130
213
|
fs.writeFileSync(
|
|
131
214
|
path.join(target, "package.json"),
|
|
132
215
|
`{
|
|
133
216
|
"name": "mwalajs-app",
|
|
217
|
+
"version": "1.0.0",
|
|
134
218
|
"type": "module",
|
|
135
219
|
"scripts": {
|
|
136
220
|
"start": "node app.mjs"
|
|
137
221
|
},
|
|
138
222
|
"dependencies": {
|
|
139
|
-
"mwalajs": "*"
|
|
223
|
+
"mwalajs": "*",
|
|
224
|
+
"ejs": "*"
|
|
140
225
|
}
|
|
141
226
|
}`
|
|
142
227
|
);
|
|
143
228
|
|
|
229
|
+
/* =========================
|
|
230
|
+
README
|
|
231
|
+
========================= */
|
|
232
|
+
|
|
144
233
|
fs.writeFileSync(
|
|
145
234
|
path.join(target, "README.md"),
|
|
146
|
-
`# MwalaJS
|
|
235
|
+
`# 🚀 MwalaJS Project
|
|
236
|
+
|
|
237
|
+
## Run project
|
|
147
238
|
|
|
148
|
-
Run:
|
|
149
239
|
npm install
|
|
150
240
|
npm start
|
|
241
|
+
|
|
242
|
+
## Structure
|
|
243
|
+
- MVC architecture
|
|
244
|
+
- Controllers
|
|
245
|
+
- Routes
|
|
246
|
+
- Models
|
|
247
|
+
- Views
|
|
151
248
|
`
|
|
152
249
|
);
|
|
153
250
|
|
|
154
|
-
console.log("✅
|
|
251
|
+
console.log("✅ FULL manual scaffold created successfully!");
|
|
155
252
|
}
|
|
156
253
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
*/
|
|
254
|
+
/* =========================
|
|
255
|
+
MAIN PROJECT CREATOR
|
|
256
|
+
========================= */
|
|
257
|
+
|
|
160
258
|
export async function createProject(projectArg) {
|
|
161
259
|
const rl = readline.createInterface({
|
|
162
260
|
input: process.stdin,
|
|
163
261
|
output: process.stdout
|
|
164
262
|
});
|
|
165
263
|
|
|
264
|
+
let createdMode = "unknown";
|
|
265
|
+
|
|
166
266
|
try {
|
|
167
267
|
let projectName = projectArg?.trim();
|
|
168
268
|
|
|
@@ -171,7 +271,7 @@ export async function createProject(projectArg) {
|
|
|
171
271
|
}
|
|
172
272
|
|
|
173
273
|
if (!projectName) {
|
|
174
|
-
console.log("❌ Project name required");
|
|
274
|
+
console.log("❌ Project name is required");
|
|
175
275
|
return;
|
|
176
276
|
}
|
|
177
277
|
|
|
@@ -186,10 +286,12 @@ export async function createProject(projectArg) {
|
|
|
186
286
|
|
|
187
287
|
const templatePath = getMwalajsPath();
|
|
188
288
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
289
|
+
/* =========================
|
|
290
|
+
TEMPLATE MODE
|
|
291
|
+
========================= */
|
|
292
|
+
|
|
293
|
+
if (templatePath) {
|
|
294
|
+
console.log("📦 Template found:", templatePath);
|
|
193
295
|
|
|
194
296
|
const items = [
|
|
195
297
|
"controllers",
|
|
@@ -203,6 +305,8 @@ export async function createProject(projectArg) {
|
|
|
203
305
|
"README.md"
|
|
204
306
|
];
|
|
205
307
|
|
|
308
|
+
let copied = 0;
|
|
309
|
+
|
|
206
310
|
for (const item of items) {
|
|
207
311
|
const src = path.join(templatePath, item);
|
|
208
312
|
const dest = path.join(target, item);
|
|
@@ -210,19 +314,39 @@ export async function createProject(projectArg) {
|
|
|
210
314
|
if (fs.existsSync(src)) {
|
|
211
315
|
fs.copySync(src, dest);
|
|
212
316
|
console.log("✔ copied:", item);
|
|
317
|
+
copied++;
|
|
213
318
|
} else {
|
|
214
319
|
console.log("⚠ missing:", item);
|
|
215
320
|
}
|
|
216
321
|
}
|
|
217
322
|
|
|
218
|
-
|
|
219
|
-
|
|
323
|
+
if (copied === 0) {
|
|
324
|
+
console.log("⚠ Template empty → switching to manual mode...");
|
|
220
325
|
createManualTemplate(target);
|
|
326
|
+
createdMode = "manual";
|
|
327
|
+
} else {
|
|
328
|
+
createdMode = "template";
|
|
221
329
|
}
|
|
330
|
+
|
|
331
|
+
} else {
|
|
332
|
+
/* =========================
|
|
333
|
+
MANUAL MODE
|
|
334
|
+
========================= */
|
|
335
|
+
|
|
336
|
+
createManualTemplate(target);
|
|
337
|
+
createdMode = "manual";
|
|
222
338
|
}
|
|
223
339
|
|
|
224
|
-
|
|
340
|
+
/* =========================
|
|
341
|
+
FINAL OUTPUT (NO FAKE SUCCESS)
|
|
342
|
+
========================= */
|
|
343
|
+
|
|
344
|
+
console.log("\n============================");
|
|
345
|
+
console.log("🎉 PROJECT CREATED SUCCESSFULLY");
|
|
346
|
+
console.log("============================");
|
|
225
347
|
console.log("📁 Path:", target);
|
|
348
|
+
console.log("⚙ Mode:", createdMode.toUpperCase());
|
|
349
|
+
console.log("============================\n");
|
|
226
350
|
|
|
227
351
|
} catch (err) {
|
|
228
352
|
console.error("❌ Create project failed:", err.message);
|
|
@@ -231,7 +355,10 @@ export async function createProject(projectArg) {
|
|
|
231
355
|
}
|
|
232
356
|
}
|
|
233
357
|
|
|
234
|
-
|
|
358
|
+
/* =========================
|
|
359
|
+
CLI EXECUTION
|
|
360
|
+
========================= */
|
|
361
|
+
|
|
235
362
|
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
236
363
|
createProject(process.argv[2]);
|
|
237
364
|
}
|