mwalajs 1.0.6 → 1.0.7
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/config/createdatabase.mjs +243 -38
- package/package.json +2 -1
- package/views/index.ejs +499 -494
- package/ujasi/README.md +0 -542
- package/ujasi/app.mjs +0 -33
- package/ujasi/bin/backupnewclean.js +0 -162
- package/ujasi/bin/mwala.mjs +0 -176
- package/ujasi/config/createTablesetdb.mjs +0 -38
- package/ujasi/config/createdatabase.mjs +0 -156
- package/ujasi/config/serverConfig.mjs +0 -1
- package/ujasi/controllers/fileController.mjs +0 -15
- package/ujasi/controllers/homeController.mjs +0 -28
- package/ujasi/models/exampleModel.mjs +0 -5
- package/ujasi/mwalajs/index.js +0 -109
- package/ujasi/mwalajs/index.mjs +0 -121
- package/ujasi/mwalajs/package.json +0 -16
- package/ujasi/package.json +0 -58
- package/ujasi/public/styles.css +0 -115
- package/ujasi/routes/homeRoutes.mjs +0 -12
- package/ujasi/runMigrations.mjs +0 -137
- package/ujasi/setupMwalajs.mjs +0 -58
- package/ujasi/views/about.ejs +0 -159
- package/ujasi/views/index.ejs +0 -227
- package/ujasi/views/sitemap.xml +0 -1
- package/ujasi/views/steps.ejs +0 -514
- package/ujasi/views/welcome.ejs +0 -257
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import { execSync } from 'child_process';
|
|
3
|
-
import fs from 'fs';
|
|
4
|
-
import path from 'path';
|
|
5
|
-
import { fileURLToPath, pathToFileURL } from 'url';
|
|
6
|
-
|
|
7
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
-
const __dirname = path.dirname(__filename);
|
|
9
|
-
|
|
10
|
-
// Dynamically import modules using relative paths
|
|
11
|
-
const { getDbConnection } = await import(pathToFileURL(path.join(__dirname, '../config/createdatabase.mjs')).href);
|
|
12
|
-
const { createTable, dropTable, migrateAll, rollbackLastMigration } = await import(pathToFileURL(path.join(__dirname, '../runMigrations.mjs')).href);
|
|
13
|
-
const { setupMwalajs } = await import(pathToFileURL(path.join(__dirname, '../setupMwalajs.mjs')).href);
|
|
14
|
-
|
|
15
|
-
const args = process.argv.slice(2);
|
|
16
|
-
const command = args[0];
|
|
17
|
-
|
|
18
|
-
if (!command || command === 'help' || command === 'h') {
|
|
19
|
-
console.log(`
|
|
20
|
-
MwalaJS CLI - List of Commands:
|
|
21
|
-
|
|
22
|
-
General:
|
|
23
|
-
- mwala -v | --version → Show the version.
|
|
24
|
-
- mwala help | h → Show this help.
|
|
25
|
-
|
|
26
|
-
Project Management:
|
|
27
|
-
- mwala create-project → Create a new MwalaJS project.
|
|
28
|
-
- mwala init → Initialize MwalaJS.
|
|
29
|
-
|
|
30
|
-
Running App:
|
|
31
|
-
- mwala serve | app.mjs → Start the MwalaJS application.
|
|
32
|
-
|
|
33
|
-
Database:
|
|
34
|
-
- mwala create-db → Create the database.
|
|
35
|
-
- mwala create-table <name> → Create a table.
|
|
36
|
-
- mwala drop-table <name> → Drop a table.
|
|
37
|
-
- mwala migrate all → Run all migrations.
|
|
38
|
-
- mwala rollback all → Roll back last migration.
|
|
39
|
-
|
|
40
|
-
Code Generation:
|
|
41
|
-
- mwala generate model <name>
|
|
42
|
-
- mwala generate controller <name>
|
|
43
|
-
- mwala generate route <name>
|
|
44
|
-
- mwala generate view <name>
|
|
45
|
-
- mwala generate midware <name>
|
|
46
|
-
`);
|
|
47
|
-
process.exit(0);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
switch (command) {
|
|
51
|
-
case 'version':
|
|
52
|
-
case '-v':
|
|
53
|
-
case '--version':
|
|
54
|
-
console.log('MwalaJS Version: 1.0.0');
|
|
55
|
-
process.exit(0);
|
|
56
|
-
|
|
57
|
-
case 'create-project':
|
|
58
|
-
console.log('Project creation logic goes here.');
|
|
59
|
-
// await createProject(); // Uncomment when implemented
|
|
60
|
-
break;
|
|
61
|
-
|
|
62
|
-
case 'serve':
|
|
63
|
-
case 'app.mjs':
|
|
64
|
-
try {
|
|
65
|
-
execSync('node app.mjs', { stdio: 'inherit' });
|
|
66
|
-
} catch (err) {
|
|
67
|
-
console.error(`Failed to start app: ${err.message}`);
|
|
68
|
-
process.exit(1);
|
|
69
|
-
}
|
|
70
|
-
break;
|
|
71
|
-
|
|
72
|
-
case 'init':
|
|
73
|
-
setupMwalajs();
|
|
74
|
-
break;
|
|
75
|
-
|
|
76
|
-
case 'generate': {
|
|
77
|
-
const subCommand = args[1]?.toLowerCase();
|
|
78
|
-
const name = args[2];
|
|
79
|
-
|
|
80
|
-
if (!subCommand || !name) {
|
|
81
|
-
console.error('Specify both type and name: mwala generate <type> <name>');
|
|
82
|
-
process.exit(1);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const map = {
|
|
86
|
-
model: 'models',
|
|
87
|
-
controller: 'controllers',
|
|
88
|
-
route: 'routes',
|
|
89
|
-
view: 'views',
|
|
90
|
-
midware: 'middlewares'
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
if (!map[subCommand]) {
|
|
94
|
-
console.error(`Unknown type '${subCommand}'. Use: ${Object.keys(map).join(', ')}`);
|
|
95
|
-
process.exit(1);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const filePath = path.join(process.cwd(), map[subCommand], `${name}.mjs`);
|
|
99
|
-
if (fs.existsSync(filePath)) {
|
|
100
|
-
console.log(`${subCommand} "${name}" already exists.`);
|
|
101
|
-
process.exit(1);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const templates = {
|
|
105
|
-
model: `export const ${name}Model = {};`,
|
|
106
|
-
controller: `export const ${name}Controller = { get${name}Page: (req, res) => res.render('${name}', { title: '${name} Page' }) };`,
|
|
107
|
-
route: `import mwalajs from 'mwalajs';\nimport { ${name}Controller } from '../controllers/${name}Controller.mjs';\nconst router = mwalajs.Router();\nrouter.get('/', ${name}Controller.get${name}Page);\nexport { router as ${name}Route };`,
|
|
108
|
-
view: `<!DOCTYPE html>\n<html lang="en">\n<head><meta charset="UTF-8"><title>${name} Page</title></head>\n<body>\n<h1>${name} View Page</h1>\n</body>\n</html>`,
|
|
109
|
-
midware: `export const ${name} = (req, res, next) => { next(); };`
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
113
|
-
fs.writeFileSync(filePath, templates[subCommand]);
|
|
114
|
-
console.log(`${subCommand} "${name}" created at ${filePath}`);
|
|
115
|
-
break;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
case 'create-db':
|
|
119
|
-
getDbConnection().then(() => console.log('Database created.')).catch(err => {
|
|
120
|
-
console.error(`Database error: ${err.message}`);
|
|
121
|
-
process.exit(1);
|
|
122
|
-
});
|
|
123
|
-
break;
|
|
124
|
-
|
|
125
|
-
case 'create-table':
|
|
126
|
-
if (!args[1]) {
|
|
127
|
-
console.error('Specify table name.');
|
|
128
|
-
process.exit(1);
|
|
129
|
-
}
|
|
130
|
-
createTable(args[1]);
|
|
131
|
-
break;
|
|
132
|
-
|
|
133
|
-
case 'drop-table':
|
|
134
|
-
if (!args[1]) {
|
|
135
|
-
console.error('Specify table name.');
|
|
136
|
-
process.exit(1);
|
|
137
|
-
}
|
|
138
|
-
dropTable(args[1]);
|
|
139
|
-
break;
|
|
140
|
-
|
|
141
|
-
case 'migrate':
|
|
142
|
-
if (args[1] === 'all') {
|
|
143
|
-
migrateAll();
|
|
144
|
-
} else {
|
|
145
|
-
console.error('Use: mwala migrate all');
|
|
146
|
-
process.exit(1);
|
|
147
|
-
}
|
|
148
|
-
break;
|
|
149
|
-
|
|
150
|
-
case 'rollback':
|
|
151
|
-
if (args[1] === 'all') {
|
|
152
|
-
rollbackLastMigration();
|
|
153
|
-
} else {
|
|
154
|
-
console.error('Use: mwala rollback all');
|
|
155
|
-
process.exit(1);
|
|
156
|
-
}
|
|
157
|
-
break;
|
|
158
|
-
|
|
159
|
-
default:
|
|
160
|
-
console.error(`Unknown command: "${command}". Run "mwala help" for options.`);
|
|
161
|
-
process.exit(1);
|
|
162
|
-
}
|
package/ujasi/bin/mwala.mjs
DELETED
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import { execSync } from 'child_process';
|
|
4
|
-
import fs from 'fs';
|
|
5
|
-
import path from 'path';
|
|
6
|
-
import { fileURLToPath, pathToFileURL } from 'url';
|
|
7
|
-
|
|
8
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
9
|
-
const __dirname = path.dirname(__filename);
|
|
10
|
-
|
|
11
|
-
// Dynamically import modules using relative paths
|
|
12
|
-
const { getDbConnection } = await import(pathToFileURL(path.join(__dirname, '../config/createdatabase.mjs')).href);
|
|
13
|
-
const { createTable, dropTable, migrateAll, rollbackLastMigration } = await import(pathToFileURL(path.join(__dirname, '../runMigrations.mjs')).href);
|
|
14
|
-
const { setupMwalajs } = await import(pathToFileURL(path.join(__dirname, '../setupMwalajs.mjs')).href);
|
|
15
|
-
const { createProject } = await import(pathToFileURL(path.join(__dirname, '../createProject.mjs')).href);
|
|
16
|
-
|
|
17
|
-
const args = process.argv.slice(2);
|
|
18
|
-
const command = args[0];
|
|
19
|
-
|
|
20
|
-
if (!command || command === 'help' || command === 'h') {
|
|
21
|
-
console.log(`
|
|
22
|
-
MwalaJS CLI - List of Commands:
|
|
23
|
-
|
|
24
|
-
General Commands:
|
|
25
|
-
- mwala -v | mwala --version → Show the MwalaJS version.
|
|
26
|
-
- mwala help | mwala h → Show this help message.
|
|
27
|
-
|
|
28
|
-
Project Management:
|
|
29
|
-
- mwala create-project → Create a new MwalaJS project.
|
|
30
|
-
- mwala init → Initialize MwalaJS in the current project.
|
|
31
|
-
|
|
32
|
-
Running the Application:
|
|
33
|
-
- mwala serve | mwala app.mjs → Start the MwalaJS application.
|
|
34
|
-
|
|
35
|
-
Database Operations:
|
|
36
|
-
- mwala create-db → Create the database specified in the .env file.
|
|
37
|
-
- mwala create-table <name> → Create a specific database table.
|
|
38
|
-
- mwala drop-table <name> → Drop a specific database table.
|
|
39
|
-
- mwala migrate all → Run all pending migrations.
|
|
40
|
-
|
|
41
|
-
Code Generation:
|
|
42
|
-
- mwala generate model <name> → Create a new model.
|
|
43
|
-
- mwala generate controller <name> → Create a new controller.
|
|
44
|
-
- mwala generate route <name> → Create a new route.
|
|
45
|
-
- mwala generate view <name> → Create a new view file.
|
|
46
|
-
- mwala generate midware <name> → Create a new middleware.
|
|
47
|
-
|
|
48
|
-
Use "mwala <command>" to execute a command.
|
|
49
|
-
`);
|
|
50
|
-
process.exit(0);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
switch (command) {
|
|
54
|
-
case 'version':
|
|
55
|
-
case '-v':
|
|
56
|
-
case '--version':
|
|
57
|
-
console.log('MwalaJS Version: 1.0.1');
|
|
58
|
-
process.exit(0);
|
|
59
|
-
|
|
60
|
-
case 'create-project':
|
|
61
|
-
createProject();
|
|
62
|
-
break;
|
|
63
|
-
|
|
64
|
-
case 'serve':
|
|
65
|
-
case 'app.mjs':
|
|
66
|
-
try {
|
|
67
|
-
execSync('node app.mjs', { stdio: 'inherit' });
|
|
68
|
-
} catch (error) {
|
|
69
|
-
console.error(` Failed to run the app: ${error.message}`);
|
|
70
|
-
process.exit(1);
|
|
71
|
-
}
|
|
72
|
-
break;
|
|
73
|
-
|
|
74
|
-
case 'init':
|
|
75
|
-
setupMwalajs();
|
|
76
|
-
break;
|
|
77
|
-
|
|
78
|
-
case 'generate': {
|
|
79
|
-
const subCommand = args[1]?.toLowerCase();
|
|
80
|
-
const name = args[2];
|
|
81
|
-
|
|
82
|
-
if (!subCommand || !name) {
|
|
83
|
-
console.log(' Please specify both subCommand and name.');
|
|
84
|
-
process.exit(1);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const paths = {
|
|
88
|
-
model: 'models',
|
|
89
|
-
controller: 'controllers',
|
|
90
|
-
route: 'routes',
|
|
91
|
-
view: 'views',
|
|
92
|
-
midware: 'middlewares'
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
if (!paths[subCommand]) {
|
|
96
|
-
console.log(` Invalid subCommand: ${subCommand}. Valid options are: ${Object.keys(paths).join(', ')}`);
|
|
97
|
-
process.exit(1);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const filePath = path.join(process.cwd(), paths[subCommand], `${name}.mjs`);
|
|
101
|
-
|
|
102
|
-
if (fs.existsSync(filePath)) {
|
|
103
|
-
console.log(` ${name} ${subCommand} already exists.`);
|
|
104
|
-
process.exit(1);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
let content = '';
|
|
108
|
-
switch (subCommand) {
|
|
109
|
-
case 'model':
|
|
110
|
-
content = `export const ${name}Model = {};`;
|
|
111
|
-
break;
|
|
112
|
-
case 'controller':
|
|
113
|
-
content = `export const ${name}Controller = { get${name}Page: (req, res) => { res.render('${name}', { title: '${name} Page' }); } };`;
|
|
114
|
-
break;
|
|
115
|
-
case 'route':
|
|
116
|
-
content = `import mwalajs from 'mwalajs';\nimport { ${name}Controller } from '../controllers/${name}Controller.mjs';\nconst router = mwalajs.Router();\nrouter.get('/', ${name}Controller.get${name}Page);\nexport { router as ${name}Route };`;
|
|
117
|
-
break;
|
|
118
|
-
case 'view':
|
|
119
|
-
content = `<!DOCTYPE html>\n<html lang='en'>\n<head>\n <meta charset='UTF-8'>\n <title>${name} Page</title>\n</head>\n<body>\n <h1>${name} View Page</h1>\n</body>\n</html>`;
|
|
120
|
-
break;
|
|
121
|
-
case 'midware':
|
|
122
|
-
content = `export const ${name} = (req, res, next) => { next(); };`;
|
|
123
|
-
break;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
127
|
-
fs.writeFileSync(filePath, content);
|
|
128
|
-
console.log(` ${name} ${subCommand} created successfully in ${paths[subCommand]}/.`);
|
|
129
|
-
break;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
case 'create-db':
|
|
133
|
-
getDbConnection().then(() => console.log('Database created.')).catch(err => {
|
|
134
|
-
console.error(` Failed to create database: ${err.message}`);
|
|
135
|
-
process.exit(1);
|
|
136
|
-
});
|
|
137
|
-
break;
|
|
138
|
-
|
|
139
|
-
case 'create-table':
|
|
140
|
-
if (!args[1]) {
|
|
141
|
-
console.error(' Please specify a table name.');
|
|
142
|
-
process.exit(1);
|
|
143
|
-
}
|
|
144
|
-
createTable(args[1]);
|
|
145
|
-
break;
|
|
146
|
-
|
|
147
|
-
case 'drop-table':
|
|
148
|
-
if (!args[1]) {
|
|
149
|
-
console.error(' Please specify a table name.');
|
|
150
|
-
process.exit(1);
|
|
151
|
-
}
|
|
152
|
-
dropTable(args[1]);
|
|
153
|
-
break;
|
|
154
|
-
|
|
155
|
-
case 'migrate':
|
|
156
|
-
if (args[1] === 'all') {
|
|
157
|
-
migrateAll();
|
|
158
|
-
} else {
|
|
159
|
-
console.error(' Invalid migration command. Use: mwala migrate all');
|
|
160
|
-
process.exit(1);
|
|
161
|
-
}
|
|
162
|
-
break;
|
|
163
|
-
case 'rollback':
|
|
164
|
-
if (args[1] === 'all') {
|
|
165
|
-
|
|
166
|
-
rollbackLastMigration();
|
|
167
|
-
} else {
|
|
168
|
-
console.error(' Invalid migration command. Use: mwala roll-back all');
|
|
169
|
-
process.exit(1);
|
|
170
|
-
}
|
|
171
|
-
break;
|
|
172
|
-
|
|
173
|
-
default:
|
|
174
|
-
console.error(` Unknown command: ${command}. Run "mwala help" to see available commands.`);
|
|
175
|
-
process.exit(1);
|
|
176
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import Sequelize from 'sequelize';
|
|
2
|
-
import mongoose from 'mongoose';
|
|
3
|
-
import dotenv from 'dotenv';
|
|
4
|
-
|
|
5
|
-
dotenv.config();
|
|
6
|
-
|
|
7
|
-
const dbType = process.env.DB_TYPE || 'mysql';
|
|
8
|
-
|
|
9
|
-
let sequelize;
|
|
10
|
-
let mongooseConnection;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* ✅ Fungua connection kulingana na aina ya database
|
|
14
|
-
*/
|
|
15
|
-
if (['mysql', 'postgres', 'sqlite'].includes(dbType)) {
|
|
16
|
-
sequelize = new Sequelize({
|
|
17
|
-
dialect: dbType,
|
|
18
|
-
host: process.env.DB_HOST || 'localhost',
|
|
19
|
-
database: process.env.DB_NAME,
|
|
20
|
-
username: process.env.DB_USER,
|
|
21
|
-
password: process.env.DB_PASSWORD,
|
|
22
|
-
port: process.env.DB_PORT || (dbType === 'mysql' ? 3306 : 5432),
|
|
23
|
-
storage: dbType === 'sqlite' ? process.env.DB_STORAGE || 'database.sqlite' : undefined,
|
|
24
|
-
logging: false
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
} else if (dbType === 'mongodb') {
|
|
28
|
-
const mongoUri = `mongodb://${process.env.DB_HOST || 'localhost'}:${process.env.DB_PORT || 27017}/${process.env.DB_NAME}`;
|
|
29
|
-
mongooseConnection = mongoose.connect(mongoUri, {
|
|
30
|
-
useNewUrlParser: true,
|
|
31
|
-
useUnifiedTopology: true
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
} else {
|
|
35
|
-
throw new Error('Unsupported database type. Check your .env configuration.');
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export { sequelize, mongooseConnection };
|
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import readlineSync from 'readline-sync';
|
|
3
|
-
import mysql from 'mysql2/promise';
|
|
4
|
-
import { MongoClient } from 'mongodb';
|
|
5
|
-
import sqlite3 from 'sqlite3';
|
|
6
|
-
import pkg from 'pg';
|
|
7
|
-
import dotenv from 'dotenv';
|
|
8
|
-
|
|
9
|
-
const { Client } = pkg;
|
|
10
|
-
|
|
11
|
-
// Function to reset the .env file before processing
|
|
12
|
-
const resetEnvFile = () => {
|
|
13
|
-
try {
|
|
14
|
-
fs.writeFileSync('.env', '', 'utf8'); // Empty the .env file
|
|
15
|
-
console.log(' Cleared .env file.');
|
|
16
|
-
} catch (error) {
|
|
17
|
-
console.error(' Failed to clear .env file:', error.message);
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
// Function to write data to the .env file
|
|
22
|
-
const writeToEnv = (data) => {
|
|
23
|
-
const envContent = Object.keys(data)
|
|
24
|
-
.map(key => `${key}=${data[key]}`)
|
|
25
|
-
.join('\n');
|
|
26
|
-
|
|
27
|
-
fs.writeFileSync('.env', envContent, 'utf8');
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
// Function to create the database connection
|
|
31
|
-
export const getDbConnection = async () => {
|
|
32
|
-
resetEnvFile(); // Clear .env file before proceeding
|
|
33
|
-
|
|
34
|
-
dotenv.config(); // Reload the (now empty) .env file
|
|
35
|
-
|
|
36
|
-
// Supported database types
|
|
37
|
-
const supportedDbTypes = {
|
|
38
|
-
mysql: 'mysql',
|
|
39
|
-
my: 'mysql',
|
|
40
|
-
postgresql: 'postgresql',
|
|
41
|
-
pg: 'postgresql',
|
|
42
|
-
mongodb: 'mongodb',
|
|
43
|
-
mn: 'mongodb',
|
|
44
|
-
sqlite: 'sqlite',
|
|
45
|
-
sq: 'sqlite'
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
let dbType;
|
|
49
|
-
while (true) {
|
|
50
|
-
dbType = readlineSync.question('Enter the database type (mysql/my, postgresql/pg, mongodb/mn, sqlite/sq): ').toLowerCase();
|
|
51
|
-
if (supportedDbTypes[dbType]) {
|
|
52
|
-
dbType = supportedDbTypes[dbType]; // Normalize input
|
|
53
|
-
break;
|
|
54
|
-
} else {
|
|
55
|
-
console.log('❌ Invalid database type. Please enter a valid option.');
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// Prompt for database details
|
|
60
|
-
const dbName = readlineSync.question('Enter the database name: ').trim();
|
|
61
|
-
if (!dbName) {
|
|
62
|
-
console.log(' Database name cannot be empty.');
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
let dbHost = 'localhost';
|
|
67
|
-
let dbUser = '';
|
|
68
|
-
let dbPassword = '';
|
|
69
|
-
|
|
70
|
-
if (dbType !== 'sqlite') {
|
|
71
|
-
dbHost = readlineSync.question('Enter the database host (default: localhost): ') || 'localhost';
|
|
72
|
-
dbUser = readlineSync.question('Enter the database user: ').trim();
|
|
73
|
-
dbPassword = readlineSync.question('Enter the database password: ', { hideEchoBack: true }).trim();
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// Save valid details to .env
|
|
77
|
-
const envData = {
|
|
78
|
-
DB_TYPE: dbType,
|
|
79
|
-
DB_NAME: dbName,
|
|
80
|
-
DB_HOST: dbHost,
|
|
81
|
-
DB_USER: dbUser,
|
|
82
|
-
DB_PASSWORD: dbPassword,
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
writeToEnv(envData);
|
|
86
|
-
console.log(' Database credentials saved to .env file.');
|
|
87
|
-
|
|
88
|
-
let connection;
|
|
89
|
-
|
|
90
|
-
try {
|
|
91
|
-
if (dbType === 'mysql') {
|
|
92
|
-
const tempConnection = await mysql.createConnection({
|
|
93
|
-
host: dbHost,
|
|
94
|
-
user: dbUser,
|
|
95
|
-
password: dbPassword,
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
const [rows] = await tempConnection.query(`SHOW DATABASES LIKE '${dbName}'`);
|
|
99
|
-
if (rows.length === 0) {
|
|
100
|
-
await tempConnection.query(`CREATE DATABASE \`${dbName}\``);
|
|
101
|
-
console.log(` MySQL Database "${dbName}" created successfully.`);
|
|
102
|
-
} else {
|
|
103
|
-
console.log(` MySQL Database "${dbName}" already exists.`);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
connection = await mysql.createConnection({
|
|
107
|
-
host: dbHost,
|
|
108
|
-
user: dbUser,
|
|
109
|
-
password: dbPassword,
|
|
110
|
-
database: dbName,
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
await tempConnection.end();
|
|
114
|
-
} else if (dbType === 'postgresql') {
|
|
115
|
-
const tempClient = new Client({
|
|
116
|
-
host: dbHost,
|
|
117
|
-
user: dbUser,
|
|
118
|
-
password: dbPassword,
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
await tempClient.connect();
|
|
122
|
-
|
|
123
|
-
const checkDb = await tempClient.query(`SELECT datname FROM pg_database WHERE datname = '${dbName}'`);
|
|
124
|
-
if (checkDb.rows.length === 0) {
|
|
125
|
-
await tempClient.query(`CREATE DATABASE ${dbName}`);
|
|
126
|
-
console.log(` PostgreSQL Database "${dbName}" created successfully.`);
|
|
127
|
-
} else {
|
|
128
|
-
console.log(` PostgreSQL Database "${dbName}" already exists.`);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
await tempClient.end();
|
|
132
|
-
|
|
133
|
-
connection = new Client({
|
|
134
|
-
host: dbHost,
|
|
135
|
-
user: dbUser,
|
|
136
|
-
password: dbPassword,
|
|
137
|
-
database: dbName,
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
await connection.connect();
|
|
141
|
-
} else if (dbType === 'mongodb') {
|
|
142
|
-
connection = await MongoClient.connect(`mongodb://${dbHost}:27017`);
|
|
143
|
-
console.log(` MongoDB connection to "${dbName}" established.`);
|
|
144
|
-
} else if (dbType === 'sqlite') {
|
|
145
|
-
connection = new sqlite3.Database(`./${dbName}.sqlite`);
|
|
146
|
-
console.log(` SQLite Database "${dbName}.sqlite" is ready.`);
|
|
147
|
-
} else {
|
|
148
|
-
throw new Error(` Unsupported DB type: ${dbType}`);
|
|
149
|
-
}
|
|
150
|
-
} catch (error) {
|
|
151
|
-
console.error(` Failed to create database: ${error.message}`);
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return connection;
|
|
156
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
|
|
4
|
-
export const fileController = {
|
|
5
|
-
uploadFile: (req, res) => {
|
|
6
|
-
const file = req.file;
|
|
7
|
-
if (file) {
|
|
8
|
-
const uploadPath = path.join(__dirname, '../public/uploads/', file.originalname);
|
|
9
|
-
fs.writeFileSync(uploadPath, file.buffer);
|
|
10
|
-
res.send('File uploaded successfully');
|
|
11
|
-
} else {
|
|
12
|
-
res.status(400).send('No file uploaded');
|
|
13
|
-
}
|
|
14
|
-
},
|
|
15
|
-
};
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
//homeController.mjs
|
|
2
|
-
export const homeController = {
|
|
3
|
-
getHomePage: (req, res) => {
|
|
4
|
-
res.render('index', { title: 'Welcome to MwalaJS MVC' });
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export const Steps = {
|
|
11
|
-
getSteps: (req, res) => {
|
|
12
|
-
res.render('steps', { title: 'Welcome to MwalaJS MVC' });
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export const welcome = {
|
|
17
|
-
getwelcome: (req, res) => {
|
|
18
|
-
res.render('welcome', { title: 'Welcome to MwalaJS MVC' });
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
export const about = {
|
|
24
|
-
getabout: (req, res) => {
|
|
25
|
-
res.render('about', { title: 'Welcome to MwalaJS MVC' });
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
|
package/ujasi/mwalajs/index.js
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import express from 'express';
|
|
2
|
-
|
|
3
|
-
class Mwala {
|
|
4
|
-
constructor() {
|
|
5
|
-
this.app = express();
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
// Set application settings
|
|
9
|
-
set(setting, value) {
|
|
10
|
-
this.app.set(setting, value);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// Accept multiple middlewares or (path, middleware)
|
|
14
|
-
use(...args) {
|
|
15
|
-
this.app.use(...args);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
// Serve static files
|
|
19
|
-
useStatic(dirPath) {
|
|
20
|
-
this.app.use(express.static(dirPath));
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// Route methods
|
|
24
|
-
get(...args) {
|
|
25
|
-
this.app.get(...args);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
post(...args) {
|
|
29
|
-
this.app.post(...args);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
put(...args) {
|
|
33
|
-
this.app.put(...args);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
delete(...args) {
|
|
37
|
-
this.app.delete(...args);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
listen(port, callback) {
|
|
41
|
-
this.app.listen(port, callback);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
Router() {
|
|
45
|
-
return express.Router();
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// Built-in body parsers
|
|
49
|
-
json() {
|
|
50
|
-
return express.json();
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
urlencoded(options = { extended: true }) {
|
|
54
|
-
return express.urlencoded(options);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// Async external middlewares
|
|
58
|
-
async session(options) {
|
|
59
|
-
const { default: session } = await import('express-session');
|
|
60
|
-
return session(options);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
async cookieParser(secret) {
|
|
64
|
-
const { default: cookieParser } = await import('cookie-parser');
|
|
65
|
-
return cookieParser(secret);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
async helmet(options) {
|
|
69
|
-
const { default: helmet } = await import('helmet');
|
|
70
|
-
return helmet(options);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async compress(options) {
|
|
74
|
-
const { default: compression } = await import('compression');
|
|
75
|
-
return compression(options);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
async morgan(format) {
|
|
79
|
-
const { default: morgan } = await import('morgan');
|
|
80
|
-
return morgan(format);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
async override(method) {
|
|
84
|
-
const { default: methodOverride } = await import('method-override');
|
|
85
|
-
return methodOverride(method);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
async cors(options) {
|
|
89
|
-
const { default: cors } = await import('cors');
|
|
90
|
-
return cors(options);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
async rateLimit(options) {
|
|
94
|
-
const { default: rateLimit } = await import('express-rate-limit');
|
|
95
|
-
return rateLimit(options);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
async bodyParserJson() {
|
|
99
|
-
const { default: bodyParser } = await import('body-parser');
|
|
100
|
-
return bodyParser.json();
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
async bodyParserUrlencoded(options = { extended: true }) {
|
|
104
|
-
const { default: bodyParser } = await import('body-parser');
|
|
105
|
-
return bodyParser.urlencoded(options);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export default new Mwala();
|