From c32257466745d51f1d7e932162f540ade4f31ba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20Magne=CC=81?= Date: Fri, 15 Nov 2019 16:49:53 +0100 Subject: [PATCH] Add dynamic command; --- CHANGELOG.md | 3 ++ docs/_sidebar.md | 1 + docs/addons/_sidebar.md | 1 + docs/api/_sidebar.md | 1 + docs/cli/README.md | 33 +++++++++++++++++++ docs/cli/_sidebar.md | 14 ++++++++ docs/converter/_sidebar.md | 1 + docs/cron/_sidebar.md | 1 + docs/database/_sidebar.md | 1 + docs/extern-api/_sidebar.md | 1 + docs/logger/_sidebar.md | 1 + docs/server/_sidebar.md | 1 + docs/templating/_sidebar.md | 1 + docs/tests/README.md | 14 ++++++++ docs/tests/_sidebar.md | 1 + docs/translations/_sidebar.md | 1 + docs/validation/_sidebar.md | 1 + lib/cli.js | 27 +++++++++++++++ lib/helper.js | 1 + package.json | 2 +- .../addonApp/server/commands/index.js | 3 ++ .../configApp/server/commands/index.js | 3 ++ .../datasets/cronApp/server/commands/index.js | 3 ++ .../datasetsApp/server/commands/index.js | 3 ++ .../errorApp/server/commands/index.js | 3 ++ .../errorConfigApp/server/commands/index.js | 3 ++ .../externApiApp/server/commands/index.js | 3 ++ .../migrationApp/server/commands/index.js | 3 ++ .../migrationApp2/server/commands/index.js | 3 ++ test/datasets/myApp/server/commands/index.js | 3 ++ .../mySQLApp/server/commands/index.js | 3 ++ .../prefixApp/server/commands/index.js | 3 ++ .../testClientApp/server/commands/index.js | 3 ++ .../translationApp/server/commands/index.js | 3 ++ 34 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 docs/cli/README.md create mode 100644 docs/cli/_sidebar.md create mode 100644 test/datasets/addonApp/server/commands/index.js create mode 100644 test/datasets/configApp/server/commands/index.js create mode 100644 test/datasets/cronApp/server/commands/index.js create mode 100644 test/datasets/datasetsApp/server/commands/index.js create mode 100644 test/datasets/errorApp/server/commands/index.js create mode 100644 test/datasets/errorConfigApp/server/commands/index.js create mode 100644 test/datasets/externApiApp/server/commands/index.js create mode 100644 test/datasets/migrationApp/server/commands/index.js create mode 100644 test/datasets/migrationApp2/server/commands/index.js create mode 100644 test/datasets/myApp/server/commands/index.js create mode 100644 test/datasets/mySQLApp/server/commands/index.js create mode 100644 test/datasets/prefixApp/server/commands/index.js create mode 100644 test/datasets/testClientApp/server/commands/index.js create mode 100644 test/datasets/translationApp/server/commands/index.js diff --git a/CHANGELOG.md b/CHANGELOG.md index d572a45..9c7f26b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/_sidebar.md b/docs/_sidebar.md index 376cb92..5c8807c 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -9,5 +9,6 @@ * [Translations](/translations/#translations) * [Cron](/cron/#cron) * [Logger](/logger/#logger) +* [Cli](/cli/#cli) * [Database](/database/#database) * [Tests](/tests/#tests) diff --git a/docs/addons/_sidebar.md b/docs/addons/_sidebar.md index 50bb5c3..799f682 100644 --- a/docs/addons/_sidebar.md +++ b/docs/addons/_sidebar.md @@ -11,5 +11,6 @@ * [Translations](/translations/#translations) * [Cron](/cron/#cron) * [Logger](/logger/#logger) +* [Cli](/cli/#cli) * [Database](/database/#database) * [Tests](/tests/#tests) diff --git a/docs/api/_sidebar.md b/docs/api/_sidebar.md index 99ceedc..9823fa4 100644 --- a/docs/api/_sidebar.md +++ b/docs/api/_sidebar.md @@ -12,5 +12,6 @@ * [Translations](/translations/#translations) * [Cron](/cron/#cron) * [Logger](/logger/#logger) +* [Cli](/cli/#cli) * [Database](/database/#database) * [Tests](/tests/#tests) diff --git a/docs/cli/README.md b/docs/cli/README.md new file mode 100644 index 0000000..b5b6345 --- /dev/null +++ b/docs/cli/README.md @@ -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('') + .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.* diff --git a/docs/cli/_sidebar.md b/docs/cli/_sidebar.md new file mode 100644 index 0000000..5c8807c --- /dev/null +++ b/docs/cli/_sidebar.md @@ -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) diff --git a/docs/converter/_sidebar.md b/docs/converter/_sidebar.md index 1e49f6d..68f5dac 100644 --- a/docs/converter/_sidebar.md +++ b/docs/converter/_sidebar.md @@ -10,5 +10,6 @@ * [Translations](/translations/#translations) * [Cron](/cron/#cron) * [Logger](/logger/#logger) +* [Cli](/cli/#cli) * [Database](/database/#database) * [Tests](/tests/#tests) diff --git a/docs/cron/_sidebar.md b/docs/cron/_sidebar.md index 0257c1f..b62f97a 100644 --- a/docs/cron/_sidebar.md +++ b/docs/cron/_sidebar.md @@ -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) diff --git a/docs/database/_sidebar.md b/docs/database/_sidebar.md index 5cdc65e..88e3e0d 100644 --- a/docs/database/_sidebar.md +++ b/docs/database/_sidebar.md @@ -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) diff --git a/docs/extern-api/_sidebar.md b/docs/extern-api/_sidebar.md index 29a7176..44f0721 100644 --- a/docs/extern-api/_sidebar.md +++ b/docs/extern-api/_sidebar.md @@ -11,5 +11,6 @@ * [Translations](/translations/#translations) * [Cron](/cron/#cron) * [Logger](/logger/#logger) +* [Cli](/cli/#cli) * [Database](/database/#database) * [Tests](/tests/#tests) diff --git a/docs/logger/_sidebar.md b/docs/logger/_sidebar.md index 12efed7..e223083 100644 --- a/docs/logger/_sidebar.md +++ b/docs/logger/_sidebar.md @@ -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) diff --git a/docs/server/_sidebar.md b/docs/server/_sidebar.md index 61f0fce..6ab949d 100644 --- a/docs/server/_sidebar.md +++ b/docs/server/_sidebar.md @@ -15,5 +15,6 @@ * [Translations](/translations/#translations) * [Cron](/cron/#cron) * [Logger](/logger/#logger) +* [Cli](/cli/#cli) * [Database](/database/#database) * [Tests](/tests/#tests) diff --git a/docs/templating/_sidebar.md b/docs/templating/_sidebar.md index 135af75..f9b4a8a 100644 --- a/docs/templating/_sidebar.md +++ b/docs/templating/_sidebar.md @@ -15,5 +15,6 @@ * [Translations](/translations/#translations) * [Cron](/cron/#cron) * [Logger](/logger/#logger) +* [Cli](/cli/#cli) * [Database](/database/#database) * [Tests](/tests/#tests) diff --git a/docs/tests/README.md b/docs/tests/README.md index 85ab73a..9ca5bb2 100644 --- a/docs/tests/README.md +++ b/docs/tests/README.md @@ -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@email.com', '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. diff --git a/docs/tests/_sidebar.md b/docs/tests/_sidebar.md index 349653c..011e27d 100644 --- a/docs/tests/_sidebar.md +++ b/docs/tests/_sidebar.md @@ -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) diff --git a/docs/translations/_sidebar.md b/docs/translations/_sidebar.md index 6ed6245..b9c4cbc 100644 --- a/docs/translations/_sidebar.md +++ b/docs/translations/_sidebar.md @@ -12,5 +12,6 @@ * [CLI](/translations/#cli) * [Cron](/cron/#cron) * [Logger](/logger/#logger) +* [Cli](/cli/#cli) * [Database](/database/#database) * [Tests](/tests/#tests) diff --git a/docs/validation/_sidebar.md b/docs/validation/_sidebar.md index 370d715..0a1836c 100644 --- a/docs/validation/_sidebar.md +++ b/docs/validation/_sidebar.md @@ -12,5 +12,6 @@ * [Translations](/translations/#translations) * [Cron](/cron/#cron) * [Logger](/logger/#logger) +* [Cli](/cli/#cli) * [Database](/database/#database) * [Tests](/tests/#tests) diff --git a/lib/cli.js b/lib/cli.js index f0685ed..5884c67 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -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') }) diff --git a/lib/helper.js b/lib/helper.js index 1c2f9cd..2108a2b 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -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) => { diff --git a/package.json b/package.json index 721ec1b..37abfeb 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/datasets/addonApp/server/commands/index.js b/test/datasets/addonApp/server/commands/index.js new file mode 100644 index 0000000..9c7ea9c --- /dev/null +++ b/test/datasets/addonApp/server/commands/index.js @@ -0,0 +1,3 @@ +const cmd = [] + +module.exports = cmd diff --git a/test/datasets/configApp/server/commands/index.js b/test/datasets/configApp/server/commands/index.js new file mode 100644 index 0000000..9c7ea9c --- /dev/null +++ b/test/datasets/configApp/server/commands/index.js @@ -0,0 +1,3 @@ +const cmd = [] + +module.exports = cmd diff --git a/test/datasets/cronApp/server/commands/index.js b/test/datasets/cronApp/server/commands/index.js new file mode 100644 index 0000000..9c7ea9c --- /dev/null +++ b/test/datasets/cronApp/server/commands/index.js @@ -0,0 +1,3 @@ +const cmd = [] + +module.exports = cmd diff --git a/test/datasets/datasetsApp/server/commands/index.js b/test/datasets/datasetsApp/server/commands/index.js new file mode 100644 index 0000000..9c7ea9c --- /dev/null +++ b/test/datasets/datasetsApp/server/commands/index.js @@ -0,0 +1,3 @@ +const cmd = [] + +module.exports = cmd diff --git a/test/datasets/errorApp/server/commands/index.js b/test/datasets/errorApp/server/commands/index.js new file mode 100644 index 0000000..9c7ea9c --- /dev/null +++ b/test/datasets/errorApp/server/commands/index.js @@ -0,0 +1,3 @@ +const cmd = [] + +module.exports = cmd diff --git a/test/datasets/errorConfigApp/server/commands/index.js b/test/datasets/errorConfigApp/server/commands/index.js new file mode 100644 index 0000000..9c7ea9c --- /dev/null +++ b/test/datasets/errorConfigApp/server/commands/index.js @@ -0,0 +1,3 @@ +const cmd = [] + +module.exports = cmd diff --git a/test/datasets/externApiApp/server/commands/index.js b/test/datasets/externApiApp/server/commands/index.js new file mode 100644 index 0000000..9c7ea9c --- /dev/null +++ b/test/datasets/externApiApp/server/commands/index.js @@ -0,0 +1,3 @@ +const cmd = [] + +module.exports = cmd diff --git a/test/datasets/migrationApp/server/commands/index.js b/test/datasets/migrationApp/server/commands/index.js new file mode 100644 index 0000000..9c7ea9c --- /dev/null +++ b/test/datasets/migrationApp/server/commands/index.js @@ -0,0 +1,3 @@ +const cmd = [] + +module.exports = cmd diff --git a/test/datasets/migrationApp2/server/commands/index.js b/test/datasets/migrationApp2/server/commands/index.js new file mode 100644 index 0000000..9c7ea9c --- /dev/null +++ b/test/datasets/migrationApp2/server/commands/index.js @@ -0,0 +1,3 @@ +const cmd = [] + +module.exports = cmd diff --git a/test/datasets/myApp/server/commands/index.js b/test/datasets/myApp/server/commands/index.js new file mode 100644 index 0000000..9c7ea9c --- /dev/null +++ b/test/datasets/myApp/server/commands/index.js @@ -0,0 +1,3 @@ +const cmd = [] + +module.exports = cmd diff --git a/test/datasets/mySQLApp/server/commands/index.js b/test/datasets/mySQLApp/server/commands/index.js new file mode 100644 index 0000000..9c7ea9c --- /dev/null +++ b/test/datasets/mySQLApp/server/commands/index.js @@ -0,0 +1,3 @@ +const cmd = [] + +module.exports = cmd diff --git a/test/datasets/prefixApp/server/commands/index.js b/test/datasets/prefixApp/server/commands/index.js new file mode 100644 index 0000000..9c7ea9c --- /dev/null +++ b/test/datasets/prefixApp/server/commands/index.js @@ -0,0 +1,3 @@ +const cmd = [] + +module.exports = cmd diff --git a/test/datasets/testClientApp/server/commands/index.js b/test/datasets/testClientApp/server/commands/index.js new file mode 100644 index 0000000..9c7ea9c --- /dev/null +++ b/test/datasets/testClientApp/server/commands/index.js @@ -0,0 +1,3 @@ +const cmd = [] + +module.exports = cmd diff --git a/test/datasets/translationApp/server/commands/index.js b/test/datasets/translationApp/server/commands/index.js new file mode 100644 index 0000000..9c7ea9c --- /dev/null +++ b/test/datasets/translationApp/server/commands/index.js @@ -0,0 +1,3 @@ +const cmd = [] + +module.exports = cmd