Skip to content

Commit 8d58b94

Browse files
authored
Merge pull request #20 from saucecodee/develop
Develop
2 parents e12573b + 138b5c8 commit 8d58b94

File tree

7 files changed

+67
-54
lines changed

7 files changed

+67
-54
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[![BangaJS Logo](https://i.ibb.co/GC3hqjC/banga-banner.jpg)](https://bangajs.netlify.app/)
22

3-
BàngáJS is a CLI generator for scaffolding [ExpressJS](https://expressjs.com) applications and generating application layer files with speed and efficiency.
3+
BàngáJS is a CLI generator for scaffolding [ExpressJS](https://expressjs.com) applications and generating application layer files for speed and efficiency.
44

55
[![NPM Version][npm-image]][npm-url]
66
[![NPM Downloads][downloads-image]][downloads-url]

lib/cmd/new.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
const chalk = require('chalk');
2-
const ejs = require("ejs")
3-
const path = require("path")
4-
const spawn = require('child_process').spawnSync
2+
const ejs = require("ejs");
3+
const path = require("path");
4+
const spawnSync = require('child_process').spawnSync;
55

66
const app = require('./../utils/app');
7-
const FM = require("./../utils/file-manager")
7+
const FM = require("./../utils/file-manager");
88
const TextFormatter = require("../utils/text-formatter");
9-
const ARGS = process.ARGS
9+
const ARGS = process.ARGS;
1010

1111
const projectFiles = [
1212
{ path: ".env.dev", template: ".env-dev", options: [] },
@@ -50,17 +50,19 @@ const projectFiles = [
5050
{ path: "uploads/", template: null, options: [] },
5151
]
5252

53-
function runCommand(command = "banga", args = []) {
53+
function execShellCommand(command = "banga", args = []) {
5454
const processDefaults = {
5555
cwd: process.cwd() + "/" + ARGS.$projectName,
5656
shell: true,
5757
stdio: 'inherit'
5858
};
5959

60-
const ls = spawn(command, args, processDefaults)
60+
const ls = spawnSync(command, args, processDefaults)
6161

62-
if (ls.error) {
63-
throw ls.error
62+
// Terminate process when error occurs
63+
if (ls.stderr) {
64+
console.error(ls.stderr)
65+
process.exitCode = 1;
6466
}
6567
}
6668

@@ -75,7 +77,7 @@ module.exports = async () => {
7577
ARGS.root = true
7678

7779
// Create project files
78-
console.log(chalk.bold("\nCreating Project files..."))
80+
console.log(chalk.bold("\n> Creating Project files...\n"))
7981
for (let file of projectFiles) {
8082
if (app.canRender(file.options)) {
8183
const filePath = `${ARGS.$projectName}/${file.path}`
@@ -90,26 +92,24 @@ module.exports = async () => {
9092
console.log(`${chalk.green("CREATE")}: ${newFile.full}`)
9193
}
9294
}
93-
console.log(chalk.cyan("Created project files ✅"))
95+
console.log(chalk.cyan("\nCreated project files ✅"))
9496

95-
// Install dependencies
96-
if(ARGS.dep){
97-
// Run npm install
98-
console.log(chalk.bold("\nInstalling packages..."))
99-
runCommand("npm", ["install"])
97+
// Install dependencies - npm install
98+
if (ARGS.dep) {
99+
console.log(chalk.bold("\n\n\n> Installing packages...\n"))
100+
execShellCommand("npm", ["install"])
100101
console.log(chalk.cyan("Packages installed ✅"))
101102
}
102103

103-
// Initialize git
104-
if(ARGS.git){
105-
// Initialize Git
106-
console.log(chalk.bold("\nInitialising git..."))
107-
runCommand("git", ["init"])
108-
console.log(chalk.cyan("Git initialised ✅"))
104+
// Initialize git - git init
105+
if (ARGS.git) {
106+
console.log(chalk.bold("\n\n\n> Initialising git...\n"))
107+
execShellCommand("git", ["init"])
108+
console.log(chalk.cyan("\nGit initialised ✅"))
109109
}
110110

111111
// All set
112-
console.log(chalk.cyan("\n\nYou're good to go! ✅"))
112+
console.log(chalk.cyan("\n\n\nAll set! You're good to go! ✅"))
113113

114114
} catch (error) {
115115
console.log(chalk.red(error.message))

lib/templates/project/config-env-dev.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"USER": "",
1919
"PASSWORD": "",
2020
"PORT": "",
21+
"SECURE": "",
2122
"DOMAIN": ""
2223
}
2324
}

lib/templates/project/config-env-prod.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"USER": "",
1919
"PASSWORD": "",
2020
"PORT": "",
21+
"SECURE": "",
2122
"DOMAIN": ""
2223
}
2324
}

lib/templates/project/src-service-auth.ejs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
const JWT = require("jsonwebtoken")
22
const bcrypt = require("bcrypt")
33
const crypto = require("crypto")
4+
45
const User = require("./../models/user.model");
56
const Token = require("./../models/token.model");
7+
const MailServ = require("./../services/mail.service");
68
const CustomError = require("./../utils/custom-error");
7-
const { JWT_SECRET, BCRYPT_SALT, url } = require("./../config")
9+
const { JWT_SECRET, BCRYPT_SALT, url } = require("./../config");
10+
811

912
class AuthService {
13+
// User sign up
1014
async signup(data) {
1115
let user = await User.findOne({ email: data.email })
1216
if (user) throw new CustomError("Email already exists");
@@ -15,14 +19,19 @@ class AuthService {
1519
const token = JWT.sign({ id: user._id, role: user.role }, JWT_SECRET);
1620
await user.save();
1721

22+
// Request email verification
23+
await this.RequestEmailVerification(user.email)
24+
1825
return data = {
1926
uid: user._id,
2027
email: user.email,
2128
role: user.role,
29+
verified: user.isVerified,
2230
token: token
2331
};
2432
}
2533

34+
// User sign in
2635
async signin(data) {
2736
if (!data.email) throw new CustomError("Email is required");
2837
if (!data.password) throw new CustomError("Password is required");
@@ -46,6 +55,7 @@ class AuthService {
4655
};
4756
}
4857

58+
// Update user password
4959
async updatePassword(userId, data) {
5060
const user = await User.findOne({ _id: userId });
5161
if (!user) throw new CustomError("User dose not exist")
@@ -64,6 +74,7 @@ class AuthService {
6474
return
6575
}
6676

77+
// Sends a verification mail to user email
6778
async RequestEmailVerification(email) {
6879
const user = await User.findOne({ email })
6980
if (!user) throw new CustomError("Email does not exist")
@@ -83,16 +94,19 @@ class AuthService {
8394

8495
const link = `${url.CLIENT_URL}/email-verification?uid=${user._id}&verifyToken=${verifyToken}`
8596

86-
//send Mail
87-
return link
97+
// Send Mail
98+
await new MailServ(user).sendEmailVerificationMail(link)
99+
100+
return
88101
}
89102

103+
// Verify user
90104
async VerifyEmail(data) {
91105
const { userId, verifyToken } = data
92106

93107
const user = await User.findOne({ _id: userId })
94-
if(!user) throw new CustomError("User does not exist")
95-
if(user.isVerified) throw new CustomError("Email is already verified")
108+
if (!user) throw new CustomError("User does not exist")
109+
if (user.isVerified) throw new CustomError("Email is already verified")
96110

97111
let VToken = await Token.findOne({ userId })
98112
if (!VToken) throw new CustomError("Invalid or expired password reset token")
@@ -110,6 +124,7 @@ class AuthService {
110124
return
111125
}
112126

127+
// Sends a reset password mail to user email
113128
async RequestPasswordReset(email) {
114129
const user = await User.findOne({ email })
115130
if (!user) throw new CustomError("Email does not exist")
@@ -128,10 +143,13 @@ class AuthService {
128143

129144
const link = `${url.CLIENT_URL}/reset-password?uid=${user._id}&resetToken=${resetToken}`
130145

131-
//send mail
132-
return link
146+
// Send Mail
147+
await new MailServ(user).sendPasswordResetMail(link)
148+
149+
return
133150
}
134151

152+
// Resets user password
135153
async resetPassword(data) {
136154
const { userId, resetToken, password } = data
137155

@@ -148,10 +166,10 @@ class AuthService {
148166
{ $set: { password: hash } },
149167
{ new: true })
150168

151-
await RToken.deleteOne()
169+
await RToken.deleteOne()
152170

153171
return
154172
}
155173
}
156174

157-
module.exports = new AuthService();
175+
module.exports = new AuthService();

lib/templates/project/src-service-mail.ejs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,16 @@ const nodemailer = require("nodemailer");
22
const CustomError = require("./../utils/custom-error");
33
const { mailer, APP_NAME } = require("./../config");
44

5-
6-
// Custom email response object
7-
const emailResponse = (status, message) => {
8-
return {
9-
status: status,
10-
message: message
11-
}
12-
}
13-
145
class MailService {
156
constructor(user) {
167
this.user = user;
178
}
189

19-
async send(subject, content, to, from) {
10+
async send(subject, content, recipient, from) {
2011
from = from || `${APP_NAME} <no-reply${mailer.DOMAIN}>`
2112
content = content || " "
2213

23-
if (!to || to.length < 1) throw new CustomError("Recipient is required");
14+
if (!recipient || recipient.length < 1) throw new CustomError("Recipient is required");
2415
if (!subject) throw new CustomError("Subject is required");
2516

2617
// Define nodemailer transporter
@@ -34,30 +25,32 @@ class MailService {
3425
}
3526
});
3627

37-
const sendMail = await transporter.sendMail({
28+
const result = await transporter.sendMail({
3829
from,
39-
to: Array.isArray(to) ? to.join() : to,
30+
to: Array.isArray(recipient) ? recipient.join() : recipient,
4031
subject,
4132
text: content
4233
});
4334

44-
return await sendMail
35+
if (!result) throw new CustomError("Unable to send mail")
36+
37+
return result
4538
}
4639

4740
async sendEmailVerificationMail(link) {
4841
const subject = "Email Verification";
49-
const content = `Hey ${this.user.name}, /n Please click on the link to verify your email ${link}`
50-
const email = this.user.email
42+
const content = `Hey ${this.user.name}, Please click on the link to verify your email ${link}`
43+
const recipient = this.user.email
5144

52-
await this.send(subject, content, email)
45+
return await this.send(subject, content, recipient)
5346
}
5447

5548
async sendPasswordResetMail(link) {
5649
const subject = "Reset password";
57-
const content = `Hey ${this.user.name}, /n Please click on the link to reset your password ${link}`
58-
const email = this.user.email
50+
const content = `Hey ${this.user.name}, Please click on the link to reset your password ${link}`
51+
const recipient = this.user.email
5952

60-
await this.send(subject, content, email)
53+
return await this.send(subject, content, recipient)
6154
}
6255
}
6356

lib/templates/project/src-service-user.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class UserService {
1414
{ _id: userId },
1515
{ password: 0, __v: 0 }
1616
);
17-
if (!user) throw new CustomError("User does not exists");
17+
if (!user) throw new CustomError("User does not exist");
1818

1919
return user
2020
}

0 commit comments

Comments
 (0)