Skip to content

Commit 8d05354

Browse files
authored
Merge pull request #26 from bangajs/develop
Develop
2 parents e7748a2 + 542fbb4 commit 8d05354

31 files changed

+7601
-518
lines changed

Diff for: .gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules/
2-
test-app/
2+
test-app/*
3+
!test-app/tests*
34
to-do

Diff for: bin/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ process.title = "bangajs-cli";
88
process.on("unhandledRejection", (err) => console.log( chalk.red(err.message)));
99

1010
try {
11-
require("../lib/bootstrap").parse()
11+
require("./../lib/bootstrap").parse()
1212
if (process.ARGS) require("./../lib")()
1313
} catch (error) {
1414
console.log(chalk.red(error.message))

Diff for: lib/cmd/docs.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ const opn = require('opn');
22
const { docs } = require("../config")
33

44
module.exports = () => {
5-
// opens the url in the default browser
6-
opn(`${docs}`);
5+
// opens the url in the default browser
6+
opn(`${docs}`);
77
}

Diff for: lib/cmd/new.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const chalk = require('chalk');
1+
const chalk = require("chalk");
22
const ejs = require("ejs");
33
const path = require("path");
44
const spawnSync = require("child_process").spawnSync;
@@ -37,7 +37,7 @@ const projectFiles = [
3737
{ path: "src/routes/user.route.js", template: "src-route-user", options: ["auth"] },
3838

3939
{ path: "src/services/auth.service.js", template: "src-service-auth", options: ["auth"] },
40-
{ path: "src/services/mail.service.js", template: "src-service-mail", options: [] },
40+
{ path: "src/services/mail.service.js", template: "src-service-mail", options: ["auth"] },
4141
{ path: "src/services/user.service.js", template: "src-service-user", options: ["auth"] },
4242

4343
{ path: "src/utils/custom-error.js", template: "src-utils-custom-error", options: [] },
@@ -49,14 +49,15 @@ const projectFiles = [
4949
{ path: "public/", template: null, options: [] },
5050
{ path: "tests/", template: null, options: [] },
5151
{ path: "uploads/.gitignore", template: ".gitignore", options: [] },
52-
{ path: "views/", template: null, options: [] },
52+
{ path: "uploads/default.jpeg", file: "assets/default.jpeg", options: ["auth"] },
53+
{ path: "src/views/", template: null, options: [] },
5354
]
5455

5556
function execShellCommand(command = "banga", args = []) {
5657
const processDefaults = {
5758
cwd: process.cwd() + "/" + ARGS.$project_name,
5859
shell: true,
59-
stdio: 'inherit'
60+
stdio: "inherit"
6061
};
6162

6263
const ls = spawnSync(command, args, processDefaults)
@@ -71,13 +72,14 @@ function execShellCommand(command = "banga", args = []) {
7172

7273
module.exports = async () => {
7374
try {
74-
// Validate project name
75+
// Check if project name is valid
7576
const parsedName = app.parseName(ARGS._[1])
76-
if (!parsedName) throw new Error(`banga: '${ARGS._[2]}' is an invalid name`)
77+
if (!parsedName) throw new Error(`banga: "${ARGS._[2]}" is an invalid name`)
7778

7879
ARGS.$project_name = FormatString(parsedName.name)
7980
ARGS.root = true
8081

82+
8183
// Create project files
8284
console.log(chalk.bold("\n⏳ Creating Project files...\n"))
8385
for (let file of projectFiles) {
@@ -96,22 +98,27 @@ module.exports = async () => {
9698
}
9799
console.log(chalk.cyan("\n✅ Created project files."))
98100

101+
99102
// Install dependencies - npm install
100103
if (ARGS.dep) {
101104
console.log(chalk.bold("\n\n\n⏳ Installing packages...\n"))
102105
execShellCommand("npm", ["install"])
103-
console.log(chalk.cyan("✅ Packages installed."))
106+
console.log(chalk.cyan("\n✅ Packages installed."))
104107
}
105108

109+
106110
// Initialize git - git init
107111
if (ARGS.git) {
108112
console.log(chalk.bold("\n\n\n⏳ Initialising git...\n"))
109113
execShellCommand("git", ["init"])
114+
execShellCommand("git", ["branch", "-M", "main"])
110115
console.log(chalk.cyan("\n✅ Git initialised."))
111116
}
112117

118+
113119
// All set
114-
console.log(chalk.cyan("\n\n\n✅ All set! You're good to go! \n\n"))
120+
console.log("\n\n\n================================================\n")
121+
console.log(chalk.bold.cyan(" 🏁 All set! Your 'sauce' is ready!\n"))
115122

116123
} catch (error) {
117124
console.log(chalk.red(error.message))

Diff for: lib/cmd/version.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ const chalk = require("chalk")
22
const package = require("../../package.json")
33

44
module.exports = () => {
5-
console.log(`${chalk.yellowBright("\nBàngáJS")} v${package.version}\n`)
5+
console.log(`${chalk.yellowBright("\nBàngáJS")} v${package.version}\n`)
66
}

Diff for: lib/templates/asset/controller.ejs

+35-31
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,78 @@
11
<% if (canRender(["service"])) { -%>
22
const <%= $name.toPascalCase() %>Serv = require("./../services/<%= $name.toKebabCase() %>.service");
3-
<% } -%>
3+
<% } -%>
44
const response = require("./../utils/response");
55

66

77
class <%= $name.toPascalCase() %>Contoller {
8-
<%# :::::::::::::::::::: CREATE :::::::::::::::::::: -%>
8+
<%# ::::::::::::::::::::::::::::: CREATE ::::::::::::::::::::::::::::: -%>
99
<% if (canRender(["crud", "create"])) { -%>
1010
11-
async create(req, res) {
11+
async create(req, res) {
1212
<% if (canRender(["service"])) { -%>
13-
const result = await <%= $name.toPascalCase() %>Serv.create(req.body);
14-
res.status(201).send(response("<%= $name.toSentenceCase() %> created", result));
13+
const result = await <%= $name.toPascalCase() %>Serv.create(req.body);
14+
res.status(201).send(response("<%= $name.toSentenceCase() %> created", result));
1515
<% } -%>
1616
<% if (!canRender(["service"])) { -%>
17-
// create
18-
res.status(201).send(response("<%= $name.toSentenceCase() %> created", null));
17+
// Write your code here
18+
19+
res.status(201).send(response("<%= $name.toSentenceCase() %> created", null));
1920
<% } -%>
2021
}
2122
<% } -%>
22-
<%# :::::::::::::::::::: GET ALL :::::::::::::::::::: -%>
23+
<%# ::::::::::::::::::::::::::::: FIND ALL ::::::::::::::::::::::::::::: -%>
2324
<% if (canRender(["crud", "read"])) { -%>
2425
25-
async getAll(req, res) {
26+
async findAll(req, res) {
2627
<% if (canRender(["service"])) { -%>
27-
const result = await <%= $name.toPascalCase() %>Serv.getAll();
28-
res.status(200).send(response("All <%= $name.toSentenceCase() %>", result));
28+
const result = await <%= $name.toPascalCase() %>Serv.findAll(req.body);
29+
res.status(200).send(response("all <%= $name.toSentenceCase() %>", result));
2930
<% } -%>
3031
<% if (!canRender(["service"])) { -%>
31-
// get all
32-
res.status(200).send(response("All <%= $name.toSentenceCase() %>", null));
32+
// Write your code here
33+
res.status(200).send(response("all <%= $name.toSentenceCase() %>", null));
3334
<% } -%>
3435
}
35-
<%# :::::::::::::::::::: GET ONE :::::::::::::::::::: %>
36-
async getOne(req, res) {
36+
<%# ::::::::::::::::::::::::::::: FIND ONE ::::::::::::::::::::::::::::: %>
37+
async findOne(req, res) {
3738
<% if (canRender(["service"])) { -%>
38-
const result = await <%= $name.toPascalCase() %>Serv.getOne(req.params.<%= $name.toCamelCase() %>Id);
39-
res.status(200).send(response("<%= $name.toSentenceCase() %> data", result));
39+
const result = await <%= $name.toPascalCase() %>Serv.findOne(req.body);
40+
res.status(200).send(response("<%= $name.toSentenceCase() %> data", result));
4041
<% } -%>
4142
<% if (!canRender(["service"])) { -%>
42-
// get one
43-
res.status(200).send(response("<%= $name.toSentenceCase() %> data", null));
43+
// Write your code here
44+
45+
res.status(200).send(response("<%= $name.toSentenceCase() %> data", null));
4446
<% } -%>
4547
}
4648
<% } -%>
47-
<%# :::::::::::::::::::: UPDATE :::::::::::::::::::: -%>
49+
<%# ::::::::::::::::::::::::::::: UPDATE ONE ::::::::::::::::::::::::::::: -%>
4850
<% if (canRender(["crud", "update"])) { -%>
4951
50-
async update(req, res) {
52+
async updateOne(req, res) {
5153
<% if (canRender(["service"])) { -%>
52-
const result = await <%= $name.toPascalCase() %>Serv.update(req.params.<%= $name.toCamelCase() %>Id, req.body);
53-
res.status(200).send(response("<%= $name.toSentenceCase() %> updated", result));
54+
const result = await <%= $name.toPascalCase() %>Serv.updateOne(req.body);
55+
res.status(200).send(response("<%= $name.toSentenceCase() %> updated", result));
5456
<% } -%>
5557
<% if (!canRender(["service"])) { -%>
56-
// update
57-
res.status(200).send(response("<%= $name.toSentenceCase() %> updated", null));
58+
// Write your code here
59+
60+
res.status(200).send(response("<%= $name.toSentenceCase() %> updated", null));
5861
<% } -%>
5962
}
6063
<% } -%>
61-
<%# :::::::::::::::::::: DELETE :::::::::::::::::::: -%>
64+
<%# ::::::::::::::::::::::::::::: DELETE ONE ::::::::::::::::::::::::::::: -%>
6265
<% if (canRender(["crud", "delete"])) { -%>
6366
64-
async delete(req, res) {
67+
async deleteOne(req, res) {
6568
<% if (canRender(["service"])) { -%>
66-
const result = await <%= $name.toPascalCase() %>Serv.delete(req.params.<%= $name.toCamelCase() %>Id);
67-
res.status(200).send(response("<%= $name.toSentenceCase() %> deleted", result));
69+
const result = await <%= $name.toPascalCase() %>Serv.deleteOne(req.body);
70+
res.status(200).send(response("<%= $name.toSentenceCase() %> deleted", result));
6871
<% } -%>
6972
<% if (!canRender(["service"])) { -%>
70-
// delete
71-
res.status(200).send(response("<%= $name.toSentenceCase() %> deleted", null));
73+
// Write your code here
74+
75+
res.status(200).send(response("<%= $name.toSentenceCase() %> deleted", null));
7276
<% } -%>
7377
}
7478
<% } -%>

Diff for: lib/templates/asset/route.ejs

+15-15
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,42 @@ router.post("/", <% if (canRender(["auth"])) { -%>auth(role.USER), <% } -%>
1818
(req, res) => null);
1919
<% } -%>
2020
<% } -%>
21-
<%# :::::::::::::::::::: GET ALL :::::::::::::::::::: -%>
21+
<%# :::::::::::::::::::: FIND ALL :::::::::::::::::::: -%>
2222
<% if (canRender(["crud", "read"])) { -%>
2323
router.get("/", <% if (canRender(["auth"])) { -%>auth(role.USER), <% } -%>
2424
<% if (canRender(["controller"])) { -%>
25-
<%= $name.toPascalCase() %>Ctrl.getAll);
25+
<%= $name.toPascalCase() %>Ctrl.findAll);
2626
<% } -%>
2727
<% if (!canRender(["controller"])) { -%>
28-
(req, res) => null);
28+
(req, res) => {});
2929
<% } -%>
30-
<%# :::::::::::::::::::: GET ONE :::::::::::::::::::: -%>
31-
router.get("/:<%= $name.toCamelCase() %>Id" ,<% if (canRender(["auth"])) { -%>auth(role.USER), <% } -%>
30+
<%# :::::::::::::::::::: FIND ONE :::::::::::::::::::: -%>
31+
router.get("/:<%= $name.toSnakeCase() %>_id", <% if (canRender(["auth"])) { -%>auth(role.USER), <% } -%>
3232
<% if (canRender(["controller"])) { -%>
33-
<%= $name.toPascalCase() %>Ctrl.getOne);
33+
<%= $name.toPascalCase() %>Ctrl.findOne);
3434
<% } -%>
3535
<% if (!canRender(["controller"])) { -%>
36-
(req, res) => null);
36+
(req, res) => {});
3737
<% } -%>
3838
<% } -%>
39-
<%# :::::::::::::::::::: UPDATE :::::::::::::::::::: -%>
39+
<%# :::::::::::::::::::: UPDATE ONE :::::::::::::::::::: -%>
4040
<% if (canRender(["crud", "update"])) { -%>
41-
router.put("/:<%= $name.toCamelCase() %>Id" ,<% if (canRender(["auth"])) { -%>auth(role.USER), <% } -%>
41+
router.put("/:<%= $name.toSnakeCase() %>_id", <% if (canRender(["auth"])) { -%>auth(role.USER), <% } -%>
4242
<% if (canRender(["controller"])) { -%>
43-
<%= $name.toPascalCase() %>Ctrl.update);
43+
<%= $name.toPascalCase() %>Ctrl.updateOne);
4444
<% } -%>
4545
<% if (!canRender(["controller"])) { -%>
46-
(req, res) => null);
46+
(req, res) => {});
4747
<% } -%>
4848
<% } -%>
49-
<%# :::::::::::::::::::: DELETE :::::::::::::::::::: -%>
49+
<%# :::::::::::::::::::: DELETE ONE :::::::::::::::::::: -%>
5050
<% if (canRender(["crud", "delete"])) { -%>
51-
router.delete("/:<%= $name.toCamelCase() %>Id" ,<% if (canRender(["auth"])) { -%>auth(role.USER), <% } -%>
51+
router.delete("/:<%= $name.toSnakeCase() %>_id", <% if (canRender(["auth"])) { -%>auth(role.USER), <% } -%>
5252
<% if (canRender(["controller"])) { -%>
53-
<%= $name.toPascalCase() %>Ctrl.delete);
53+
<%= $name.toPascalCase() %>Ctrl.deleteOne);
5454
<% } -%>
5555
<% if (!canRender(["controller"])) { -%>
56-
(req, res) => null);
56+
(req, res) => {});
5757
<% } -%>
5858
<% } -%>
5959

Diff for: lib/templates/asset/service.ejs

+43-28
Original file line numberDiff line numberDiff line change
@@ -10,73 +10,88 @@ class <%= $name.toPascalCase() %>Service {
1010
1111
async create(data) {
1212
<% if (canRender(["model"])) { -%>
13-
return await new <%= $name.toPascalCase() %>(data).save();
13+
const <%= $name.toCamelCase() %> = await new <%= $name.toPascalCase() %>(data).save();
14+
15+
return <%= $name.toCamelCase() %>
1416
<% } -%>
1517
<% if (!canRender(["model"])) { -%>
16-
// create
17-
return null
18+
// Write your code here
19+
20+
return
1821
<% } -%>
1922
}
2023
<% } -%>
21-
<%# :::::::::::::::::::: GET ALL :::::::::::::::::::: -%>
24+
<%# :::::::::::::::::::: FIND ALL :::::::::::::::::::: -%>
2225
<% if (canRender(["crud", "read"])) { -%>
2326
24-
async getAll() {
27+
async findAll(data) {
2528
<% if (canRender(["model"])) { -%>
26-
return await <%= $name.toPascalCase() %>.find({});
29+
const <%= $name.toCamelCase() %>s = await <%= $name.toPascalCase() %>.find();
30+
31+
return <%= $name.toCamelCase() %>s
2732
<% } -%>
2833
<% if (!canRender(["model"])) { -%>
29-
// get all
30-
return null
34+
// Write your code here
35+
36+
return
3137
<% } -%>
3238
}
33-
<%# :::::::::::::::::::: GET ONE :::::::::::::::::::: %>
34-
async getOne(<%= $name.toCamelCase() %>Id) {
39+
<%# :::::::::::::::::::: FIND ONE :::::::::::::::::::: %>
40+
async findOne(data) {
41+
const { <%= $name.toSnakeCase() %>_id } = data
42+
3543
<% if (canRender(["model"])) { -%>
36-
const <%= $name.toCamelCase() %> = await <%= $name.toPascalCase() %>.findOne({ _id: <%= $name.toCamelCase() %>Id });
37-
if (!<%= $name.toCamelCase() %>) throw new CustomError("<%= $name.toPascalCase() %> does not exists");
44+
const <%= $name.toCamelCase() %> = await <%= $name.toPascalCase() %>.findOne({ _id: <%= $name.toSnakeCase() %>_id });
45+
if (!<%= $name.toCamelCase() %>) throw new CustomError("<%= $name.toSentenceCase() %> does not exists");
3846
3947
return <%= $name.toCamelCase() %>
4048
<% } -%>
4149
<% if (!canRender(["model"])) { -%>
42-
// get one
43-
return null
50+
// Write your code here
51+
52+
return
4453
<% } -%>
4554
}
4655
<% } -%>
47-
<%# :::::::::::::::::::: UPDATE :::::::::::::::::::: -%>
56+
<%# :::::::::::::::::::: UPDATE ONE :::::::::::::::::::: -%>
4857
<% if (canRender(["crud", "update"])) { -%>
4958
50-
async update(<%= $name.toCamelCase() %>Id, data) {
59+
async updateOne(data) {
60+
const { <%= $name.toSnakeCase() %>_id } = data
61+
5162
<% if (canRender(["model"])) { -%>
5263
const <%= $name.toCamelCase() %> = await <%= $name.toPascalCase() %>.findByIdAndUpdate(
53-
{ _id: <%= $name.toCamelCase() %>Id },
54-
{ $set: data },
55-
{ new: true }
64+
{ _id: <%= $name.toSnakeCase() %>_id },
65+
{ $set: data }
5666
);
57-
58-
if (!<%= $name.toCamelCase() %>) throw new CustomError("<%= $name.toPascalCase() %> dosen't exist", 404);
67+
if (!<%= $name.toCamelCase() %>) throw new CustomError("<%= $name.toSentenceCase() %> dosen't exist", 404);
5968
6069
return <%= $name.toCamelCase() %>;
6170
<% } -%>
6271
<% if (!canRender(["model"])) { -%>
63-
// update
64-
return null
72+
// Write your code here
73+
74+
return
6575
<% } -%>
6676
}
6777
<% } -%>
68-
<%# :::::::::::::::::::: DELETE :::::::::::::::::::: -%>
78+
<%# :::::::::::::::::::: DELETE ONE :::::::::::::::::::: -%>
6979
<% if (canRender(["crud", "delete"])) { -%>
7080
71-
async delete(<%= $name.toCamelCase() %>Id) {
81+
async deleteOne(data) {
82+
const { <%= $name.toSnakeCase() %>_id } = data
83+
7284
<% if (canRender(["model"])) { -%>
73-
const <%= $name.toCamelCase() %> = await <%= $name.toPascalCase() %>.findOne({ _id: <%= $name.toCamelCase() %>Id });
85+
const <%= $name.toCamelCase() %> = await <%= $name.toPascalCase() %>.findOne({ _id: <%= $name.toSnakeCase() %>_id });
86+
if (!<%= $name.toCamelCase() %>) throw new CustomError("<%= $name.toSentenceCase() %> dosen't exist", 404);
7487
<%= $name.toCamelCase() %>.remove()
88+
7589
return <%= $name.toCamelCase() %>
7690
<% } -%>
7791
<% if (!canRender(["model"])) { -%>
78-
// delete
79-
return null
92+
// Write your code here
93+
94+
return
8095
<% } -%>
8196
}
8297
<% } -%>

0 commit comments

Comments
 (0)