Skip to content

Commit be8bdc5

Browse files
authored
Merge pull request #24 from bangajs/develop
Develop
2 parents d8fc59c + cb4a308 commit be8bdc5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+807
-669
lines changed

Diff for: .DS_Store

6 KB
Binary file not shown.

Diff for: .gitignore

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
node_modules
2-
3-
src
1+
node_modules/
2+
test-app/
43
to-do

Diff for: README.md

+2-2
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 for speed and efficiency.
3+
BàngáJS is a CLI generator tool for scaffolding [ExpressJS](https://expressjs.com) applications and generating application layer files with speed and efficiency ⚡️.
44

55

66
[![NPM Version][npm-image]][npm-url]
@@ -16,7 +16,7 @@ Before installing, make sure you've [downloaded and installed Node.js](https://n
1616
Then install BàngáJS globally:
1717

1818
```bash
19-
$ npm install bangajs -g
19+
$ npm install -g bangajs
2020
```
2121

2222
## Features

Diff for: bin/index.js

100644100755
+9-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
#!/usr/bin/env node
2+
"use strict"
23

3-
const chalk = require('chalk');
4+
const chalk = require("chalk");
45
const help = require("./../lib/cmd/help")
56

7+
process.title = "bangajs-cli";
8+
process.on("unhandledRejection", (err) => console.log( chalk.red(err.message)));
9+
610
try {
7-
require("./../lib/utils/app").parse()
8-
if (process.ARGS) require("./../lib")()
11+
require("../lib/bootstrap").parse()
12+
if (process.ARGS) require("./../lib")()
913
} catch (error) {
10-
console.log(chalk.red(error.message))
11-
help()
14+
console.log(chalk.red(error.message))
15+
help()
1216
}

Diff for: lib/.DS_Store

6 KB
Binary file not shown.

Diff for: lib/bootstrap.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const yargs = require("yargs-parser")
2+
const app = {}
3+
const { command_alias, commands } = require("./config")
4+
const help = require("./cmd/help")
5+
const package = require("../package.json")
6+
7+
8+
/**
9+
* Creates custom process.ARGS object with cli args
10+
*/
11+
app.parse = () => {
12+
const args = yargs(process.argv.splice(2))
13+
const cmd = args._[0] && args._[0].toLowerCase()
14+
15+
if (!cmd) return help()
16+
if (!command_alias[cmd]) throw new Error(`banga: "${cmd}" is not a recognised command or alias`)
17+
18+
args.$cmd = command_alias[cmd]
19+
args.$version = package.version
20+
21+
process.ARGS = { ...commands[args.$cmd], ...args }
22+
}
23+
24+
/**
25+
* Validate and parse file/path names
26+
*
27+
* @param {string} str File path
28+
*/
29+
app.parseName = (str) => {
30+
const strArray = str.split("/")
31+
32+
// Remove "/" at the beginning of the string if there is any
33+
if (str.charAt(0) == "/") strArray.shift()
34+
35+
// Check if name is valid
36+
var patt = new RegExp(/^[\w\/-]{1,}$/);
37+
if (!patt.test(str)) return false
38+
39+
const name = strArray.splice((strArray.length - 1), 1).join()
40+
if (!name) return false
41+
42+
const path = strArray.join("/")
43+
44+
return {
45+
name,
46+
path
47+
}
48+
}
49+
50+
/**
51+
* Checks if a template is renderable with regards to the given options
52+
*
53+
* @param {string[]} options Array of options
54+
*/
55+
app.canRender = (options = []) => {
56+
for (const opt of options)
57+
if (!process.ARGS[opt]) return false
58+
59+
return true
60+
}
61+
62+
module.exports = app

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/generate.js

+62-62
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,88 @@
1-
const chalk = require('chalk');
1+
const chalk = require('chalk')
22
const ejs = require("ejs")
3-
const path = require("path");
3+
const path = require("path")
44

5-
const app = require('./../utils/app');
5+
const app = require("./../bootstrap")
66
const FM = require("./../utils/file-manager")
7-
const TextFormatter = require("./../utils/text-formatter")
7+
const { FormatString } = require("./../utils")
88
const help = require("./help")
99

1010

1111
const ARGS = process.ARGS
1212

1313
const types = ["controller", "model", "route", "service"]
1414
const typeAlias = {
15-
a: "asset",
16-
asset: "asset",
17-
c: "controller",
18-
controller: "controller",
19-
m: "model",
20-
model: "model",
21-
r: "route",
22-
route: "route",
23-
s: "service",
24-
service: "service"
15+
a: "asset",
16+
asset: "asset",
17+
c: "controller",
18+
controller: "controller",
19+
m: "model",
20+
model: "model",
21+
r: "route",
22+
route: "route",
23+
s: "service",
24+
service: "service"
2525
}
2626

2727

2828
// Create a specific file "type"
2929
async function createFile() {
30-
const { $type, $name } = ARGS
31-
const templatePath = path.join(__dirname, `./../templates/asset/${$type}.ejs`)
32-
const data = await ejs.renderFile(templatePath, { canRender: app.canRender, ...ARGS }, {})
30+
const { $type, $name } = ARGS
31+
const templatePath = path.join(__dirname, `./../templates/asset/${$type}.ejs`)
32+
const data = await ejs.renderFile(templatePath, { canRender: app.canRender, ...ARGS }, {})
3333

34-
const newFile = await FM.create(null, data)
34+
const newFile = await FM.create(null, data)
3535

36-
if (newFile.status == "exists")
37-
console.log(`${chalk.yellow("EXISTS")}: ${$name} ${$type} - ${newFile.full}`)
38-
else
39-
console.log(`${chalk.green("CREATE")}: ${$name} ${$type} - ${newFile.full}`)
36+
if (newFile.status == "exists")
37+
console.log(`${chalk.yellow("EXISTS")}: ${$name} ${$type} - ${newFile.full}`)
38+
else
39+
console.log(`${chalk.green("CREATE")}: ${$name} ${$type} - ${newFile.full}`)
4040
}
4141

4242
// Create all all file "types"
4343
async function createAsset() {
44-
for (const type of types) {
45-
if (app.canRender([type])) {
46-
ARGS.$type = type
47-
await createFile()
48-
}
49-
}
44+
for (const type of types) {
45+
if (app.canRender([type])) {
46+
ARGS.$type = type
47+
await createFile()
48+
}
49+
}
5050
}
5151

5252

5353
module.exports = async (type, name) => {
54-
try {
55-
// Check if "name" and "type"
56-
if (!ARGS._[1] && !name) throw new Error(`banga: "<type>" is required`)
57-
if (!ARGS._[2] && !type) throw new Error(`banga: "<name>" is required`)
58-
59-
type = type ? typeAlias[type] : typeAlias[ARGS._[1]]
60-
61-
// Check if "name" is valid
62-
const parseName = app.parseName(ARGS._[2])
63-
if (!parseName) throw new Error(`banga: '${ARGS._[2]}' is an invalid name`)
64-
name = name ? TextFormatter(parseName.name) : TextFormatter(parseName.name)
65-
66-
// Check if "type" is valid
67-
if (!typeAlias[type]) {
68-
throw new Error(`banga: '${ARGS._[1]}' is not a recognised type or alias`)
69-
}
70-
71-
// Add type, name and parsedName.path to process.ARGS
72-
ARGS.$type = type
73-
ARGS.$name = name
74-
ARGS.$path = parseName.path
75-
76-
// Check what "type" of files to create
77-
if (type == "asset") {
78-
await createAsset()
79-
} else {
80-
await createFile()
81-
}
82-
83-
console.log("")
84-
} catch (error) {
85-
console.log(chalk.red(error.message))
86-
help()
87-
}
54+
try {
55+
// Check if "name" and "type"
56+
if (!ARGS._[1] && !name) throw new Error(`banga: "<type>" is required`)
57+
if (!ARGS._[2] && !type) throw new Error(`banga: "<name>" is required`)
58+
59+
type = type ? typeAlias[type] : typeAlias[ARGS._[1]]
60+
61+
// Check if "name" is valid
62+
const parseName = app.parseName(ARGS._[2])
63+
if (!parseName) throw new Error(`banga: '${ARGS._[2]}' is an invalid name`)
64+
name = name ? FormatString(parseName.name) : FormatString(parseName.name)
65+
66+
// Check if "type" is valid
67+
if (!typeAlias[type]) {
68+
throw new Error(`banga: '${ARGS._[1]}' is not a recognised type or alias`)
69+
}
70+
71+
// Add type, name and parsedName.path to process.ARGS
72+
ARGS.$type = type
73+
ARGS.$name = name
74+
ARGS.$path = parseName.path
75+
76+
// Check what "type" of files to create
77+
if (type == "asset") {
78+
await createAsset()
79+
} else {
80+
await createFile()
81+
}
82+
83+
console.log("")
84+
} catch (error) {
85+
console.log(chalk.red(error.message))
86+
help()
87+
}
8888
}

Diff for: lib/cmd/help.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const help = {}
22

33
help.default =
4-
`
4+
`
55
Build ExpressJS applications with speed and efficiency with BàngáJS
66
77
@@ -24,7 +24,7 @@ Run 'banga <command> --help' for more information on a command.
2424

2525

2626
help.generate =
27-
`
27+
`
2828
Generate and/or modify files
2929
3030
@@ -60,7 +60,7 @@ Options:
6060

6161

6262
help.new =
63-
`
63+
`
6464
Creates a new Express app.
6565
6666
Usage:
@@ -78,6 +78,6 @@ Options:
7878

7979

8080
module.exports = () => {
81-
const cmd = process.ARGS && process.ARGS.$cmd
82-
console.log(help[cmd] || help.default)
81+
const cmd = process.ARGS && process.ARGS.$cmd
82+
console.log(help[cmd] || help.default)
8383
}

Diff for: lib/cmd/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
2-
docs: require("./docs"),
3-
generate: require("./generate"),
4-
help: require("./help"),
5-
new: require("./new"),
6-
version: require("./version"),
2+
docs: require("./docs"),
3+
generate: require("./generate"),
4+
help: require("./help"),
5+
new: require("./new"),
6+
version: require("./version"),
77
}

0 commit comments

Comments
 (0)