Skip to content

Commit

Permalink
Add dynamic command;
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Magné committed Nov 15, 2019
1 parent c9c0a8f commit c322574
Show file tree
Hide file tree
Showing 34 changed files with 148 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# HearthJS

### v2.0.0
- Add dynamic command

### v1.3.0
- Update commander version from 3.0.1 to 4.0.1
- Execute addons before middleware
Expand Down
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
* [Translations](/translations/#translations)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
1 change: 1 addition & 0 deletions docs/addons/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
* [Translations](/translations/#translations)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
1 change: 1 addition & 0 deletions docs/api/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
* [Translations](/translations/#translations)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
33 changes: 33 additions & 0 deletions docs/cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## CLI

To simplify tasks, you can add you own command line to the hearthjs binary.
To do it, add an `index.js` file in the `server/commands/` folder. This file must export the list of your command.

```js
// server/commands/index.js
const commands = [{
name: '', // Your command name
description: '', // Your command description
path: '' // The absolute path to your executable file
}]

module.exports = commands
```

To work, `hearthjs` use `commander` to exeucte commands. The path of the command is an executable file which contains your command details. It should looks like this:

```js
#!/usr/bin/env node
const program = require('commander')

program
.arguments('<arg>')
.action((arg) => {
// Execute your logic here
})
.parse(process.argv)
```

You can customize your command as you want. For more details, check the `commander` [documentation](https://www.npmjs.com/package/commander).

*Note: The file must be an executable file.*
14 changes: 14 additions & 0 deletions docs/cli/_sidebar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
* [Getting started](/#hearthjs)
* [Server](/server/#server)
* [Api](/api/#api)
* [Converter](/converter/#converter)
* [Data validation](/validation/#data-validation)
* [Templating](/templating/#templating)
* [Addons](/addons/#addons)
* [Extern API](/extern-api/#extern-api)
* [Translations](/translations/#translations)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
1 change: 1 addition & 0 deletions docs/converter/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
* [Translations](/translations/#translations)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
1 change: 1 addition & 0 deletions docs/cron/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
* [Add a cron](/cron/#add-a-cron)
* [Manage crons](/cron/#manages-cron)
* [Logger](/logger/#logger)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
1 change: 1 addition & 0 deletions docs/database/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [Translations](/translations/#translations)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Connection](/database/#connection)
* [Timeout](/database/#timeout)
Expand Down
1 change: 1 addition & 0 deletions docs/extern-api/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
* [Translations](/translations/#translations)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
1 change: 1 addition & 0 deletions docs/logger/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
* [Logger](/logger/#logger)
* [Log message](/logger/#log-message)
* [Debug message](/logger/#debug-message)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
1 change: 1 addition & 0 deletions docs/server/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
* [Translations](/translations/#translations)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
1 change: 1 addition & 0 deletions docs/templating/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
* [Translations](/translations/#translations)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
14 changes: 14 additions & 0 deletions docs/tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ _user.login('/new-login-route', (err, response, body) => {})
_user.logout('/new-logout-route', (err, response, body) => {})
```

#### Headers

You can add a default header which will be added automatically to requests.

```js
describe('Test', () => {
let _user = new hearthjs.httpClient('[email protected]', 'password')

_user.headers = {
// Write here the header that must be in all request
}
})
```

#### Execute a request

You can execute simple HTTP request with the client. If you are connected, the client add the cookie to the request to execute an authenticated test.
Expand Down
1 change: 1 addition & 0 deletions docs/tests/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [Translations](/translations/#translations)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
* [Run](/tests/#run)
Expand Down
1 change: 1 addition & 0 deletions docs/translations/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
* [CLI](/translations/#cli)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
1 change: 1 addition & 0 deletions docs/validation/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
* [Translations](/translations/#translations)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
27 changes: 27 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,33 @@ const commands = ['debug', 'migrate', 'start', 'help', 'test', 'translate', 'ini

process.env.HEARTH_SERVER_PATH = process.env.HEARTH_SERVER_PATH || path.join(process.cwd(), 'server')

const projectCommand = require(path.join(process.env.HEARTH_SERVER_PATH, 'commands'))

for (let i = 0; i < projectCommand.length; i++) {
if (projectCommand[i].name === undefined) {
console.error('You must add a name to your command')
process.exit(1)
}

if (projectCommand[i].description === undefined) {
console.error('You must add a description to your command')
process.exit(1)
}

if (projectCommand[i].path === undefined) {
console.error('You must add an executable path to your command')
process.exit(1)
}

if (commands.indexOf(projectCommand[i].name) !== -1) {
console.error(`A command names ${projectCommand[i].name} already exists, please choose another name`)
process.exit(1)
}

program.command(projectCommand[i].name, projectCommand[i].description, { executableFile: projectCommand[i].path })
commands.push(projectCommand[i].name)
}

program
.command('test', 'Run all application tests', { executableFile: path.join(__dirname, 'cli/test') })
.command('debug', 'Launch application with debug', { executableFile: path.join(__dirname, 'cli/debug') })
Expand Down
1 change: 1 addition & 0 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const helper = {
this._createDirectoryIfNotExists(path.join(process.env.HEARTH_SERVER_PATH, 'migration'))
this._createDirectoryIfNotExists(path.join(process.env.HEARTH_SERVER_PATH, 'config'))
this._createDirectoryIfNotExists(path.join(process.env.HEARTH_SERVER_PATH, 'api'))
this._createDirectoryIfNotExists(path.join(process.env.HEARTH_SERVER_PATH, 'commands'))

// Create base for config file
this.createDefaultConfigFile('test', (err) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hearthjs",
"version": "1.3.0",
"version": "2.0.0",
"description": "A NodeJS server framework to build fast Rest API",
"main": "lib/index.js",
"bin": "bin/hearthjs",
Expand Down
3 changes: 3 additions & 0 deletions test/datasets/addonApp/server/commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const cmd = []

module.exports = cmd
3 changes: 3 additions & 0 deletions test/datasets/configApp/server/commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const cmd = []

module.exports = cmd
3 changes: 3 additions & 0 deletions test/datasets/cronApp/server/commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const cmd = []

module.exports = cmd
3 changes: 3 additions & 0 deletions test/datasets/datasetsApp/server/commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const cmd = []

module.exports = cmd
3 changes: 3 additions & 0 deletions test/datasets/errorApp/server/commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const cmd = []

module.exports = cmd
3 changes: 3 additions & 0 deletions test/datasets/errorConfigApp/server/commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const cmd = []

module.exports = cmd
3 changes: 3 additions & 0 deletions test/datasets/externApiApp/server/commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const cmd = []

module.exports = cmd
3 changes: 3 additions & 0 deletions test/datasets/migrationApp/server/commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const cmd = []

module.exports = cmd
3 changes: 3 additions & 0 deletions test/datasets/migrationApp2/server/commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const cmd = []

module.exports = cmd
3 changes: 3 additions & 0 deletions test/datasets/myApp/server/commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const cmd = []

module.exports = cmd
3 changes: 3 additions & 0 deletions test/datasets/mySQLApp/server/commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const cmd = []

module.exports = cmd
3 changes: 3 additions & 0 deletions test/datasets/prefixApp/server/commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const cmd = []

module.exports = cmd
3 changes: 3 additions & 0 deletions test/datasets/testClientApp/server/commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const cmd = []

module.exports = cmd
3 changes: 3 additions & 0 deletions test/datasets/translationApp/server/commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const cmd = []

module.exports = cmd

0 comments on commit c322574

Please sign in to comment.