Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Commit

Permalink
feat(commands): add make:ehandler command
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 28, 2017
1 parent e0c0f7f commit 8703159
Show file tree
Hide file tree
Showing 19 changed files with 236 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![Appveyor][appveyor-image]][appveyor-url]
[![Coveralls][coveralls-image]][coveralls-url]

Adonis cli is built on top of [Adonis ace](https://github.com/adonisjs/ace) and helps you scaffold new projects.
Adonis cli is built on top of [Adonis ace](https://github.com/adonisjs/ace) and helps you scaffold new Adonisjs projects.

Also it can proxy all the ace commands for a project, so that you can run them using the global `adonis` command.

Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ try {
require(path.join(process.cwd(), 'ace'))
}
} catch (error) {
if (error.code !== 'ENOENT') {
throw error
}
ace.wireUpWithCommander()
ace.invoke()
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@adonisjs/cli",
"version": "3.0.2",
"description": "Scaffold new adonis application",
"description": "Command line tool for Adonisjs",
"keywords": [
"adonis",
"adonisjs",
Expand All @@ -13,7 +13,7 @@
"Romain Lanz <romain.lanz@slynova.ch>"
],
"license": "MIT",
"main": "src/index.js",
"main": "src/Commands/index.js",
"bin": {
"adonis": "index.js"
},
Expand Down
14 changes: 14 additions & 0 deletions src/Commands/KeyGenerate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ class KeyGenerate extends Command {
return 'Generate secret key for the app'
}

/**
* Load related dependencies
*
* @method loadVendor
*
* @return {void}
*/
loadVendor () {
this.randomString = require('randomstring')
this.dotEnv = require('dotenv')
Expand All @@ -67,6 +74,13 @@ class KeyGenerate extends Command {
* @return {void}
*/
async handle (args, { force = false, echo = false, size, env }) {
/**
* Asking for user conscious
*/
if (process.env.NODE_ENV === 'production' && !force) {
this.error('Cannot generate APP_KEY in production. Pass --force flag to generate')
}

size = size || 32
env = env || '.env'

Expand Down
1 change: 0 additions & 1 deletion src/Commands/Make/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const options = {
hooks: 'Models/Hooks',
listeners: 'Listeners',
exceptions: 'Exceptions',
exceptionHandlers: 'Exceptions/Handlers',
middleware: 'Middleware',
commands: 'Commands',
views: 'resources/views',
Expand Down
10 changes: 8 additions & 2 deletions src/Commands/Make/Command.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

const BaseCommand = require('./Base')

/**
* Creates a new ace command
*
* @class MakeCommand
* @constructor
*/
class MakeCommand extends BaseCommand {
/**
* The command signature
Expand Down Expand Up @@ -55,8 +61,8 @@ class MakeCommand extends BaseCommand {
const lines = [
'Register command as follows',
'',
`1. Open ${this.chalk.yellow('start/app.js')}`,
`2. Add ${this.chalk.yellow(namespace)} to commands array`
`1. Open ${this.chalk.cyan('start/app.js')}`,
`2. Add ${this.chalk.cyan(namespace)} to commands array`
]

this.printInstructions(lines)
Expand Down
20 changes: 13 additions & 7 deletions src/Commands/Make/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
const BaseCommand = require('./Base')

/**
* Generate unique application key
* Make a new HTTP or Ws controller
*
* @class KeyGenerate
* @class MakeController
* @constructor
*/
class MakeController extends BaseCommand {
Expand Down Expand Up @@ -42,7 +42,7 @@ class MakeController extends BaseCommand {
* @return {String}
*/
static get description () {
return 'Make a new HTTP or Ws controller'
return 'Make a new HTTP or Websocket channel controller'
}

/**
Expand All @@ -60,10 +60,16 @@ class MakeController extends BaseCommand {
if (!type || ['ws', 'http'].indexOf(type) <= -1) {
type = await this
.on('validate', (value) => !!value)
.choice('Select controller type', {
http: 'For HTTP requests',
ws: 'For Websocket channel'
})
.choice('Select controller type', [
{
value: 'http',
name: 'For HTTP requests'
},
{
value: 'ws',
name: 'For Websocket channel'
}
])
}

return type === 'ws' ? 'wsController' : 'httpController'
Expand Down
60 changes: 60 additions & 0 deletions src/Commands/Make/ExceptionHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict'

/*
* adonis-cli
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const BaseCommand = require('./Base')

/**
* Make a new global exception handler
*
* @class MakeExceptionHandler
* @constructor
*/
class MakeExceptionHandler extends BaseCommand {
/**
* The command signature
*
* @method signature
*
* @return {String}
*/
static get signature () {
return 'make:ehandler'
}

/**
* The command description
*
* @method description
*
* @return {String}
*/
static get description () {
return 'Make a new global exception handler'
}

/**
* Handle method executed by ace
*
* @method handle
*
* @return {void}
*/
async handle () {
try {
await this.ensureInProjectRoot()
await this.generateBlueprint('exceptionHandler', '', {})
} catch ({ message }) {
this.error(message)
}
}
}

module.exports = MakeExceptionHandler
6 changes: 6 additions & 0 deletions src/Commands/Make/Hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

const BaseCommand = require('./Base')

/**
* Make a new lucid model hook
*
* @class MakeModelHook
* @constructor
*/
class MakeModelHook extends BaseCommand {
/**
* The command signature
Expand Down
6 changes: 6 additions & 0 deletions src/Commands/Make/Listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

const BaseCommand = require('./Base')

/**
* Make a new redis or event listener
*
* @class MakeListener
* @constructor
*/
class MakeListener extends BaseCommand {
/**
* The command signature
Expand Down
14 changes: 10 additions & 4 deletions src/Commands/Make/Middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

const BaseCommand = require('./Base')

/**
* Make a new HTTP or Ws middleware
*
* @class MakeMiddleware
* @constructor
*/
class MakeMiddleware extends BaseCommand {
/**
* The command signature
Expand Down Expand Up @@ -85,8 +91,8 @@ class MakeMiddleware extends BaseCommand {
* @private
*/
_pushHttpInstructions (lines, namespace) {
lines.push(`${lines.length - 1}. Open ${this.chalk.yellow('start/kernel.js')} file`)
lines.push(`${lines.length - 1}. Register ${this.chalk.yellow(namespace)} under global or named middleware`)
lines.push(`${lines.length - 1}. Open ${this.chalk.cyan('start/kernel.js')} file`)
lines.push(`${lines.length - 1}. Register ${this.chalk.cyan(namespace)} under global or named middleware`)
}

/**
Expand All @@ -102,8 +108,8 @@ class MakeMiddleware extends BaseCommand {
* @private
*/
_pushWsInstructions (lines, namespace) {
lines.push(`${lines.length - 1}. Open ${this.chalk.yellow('start/ws.js')} file`)
lines.push(`${lines.length - 1}. Register ${this.chalk.yellow(namespace)} under global or named middleware`)
lines.push(`${lines.length - 1}. Open ${this.chalk.cyan('start/ws.js')} file`)
lines.push(`${lines.length - 1}. Register ${this.chalk.cyan(namespace)} under global or named middleware`)
}

/**
Expand Down
10 changes: 8 additions & 2 deletions src/Commands/Make/Migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@

const BaseCommand = require('./Base')

class MigrationMake extends BaseCommand {
/**
* Make a new migration file
*
* @class MakeMigration
* @constructor
*/
class MakeMigration extends BaseCommand {
/**
* Command signature required by ace
*
Expand Down Expand Up @@ -86,4 +92,4 @@ class MigrationMake extends BaseCommand {
}
}

module.exports = MigrationMake
module.exports = MakeMigration
6 changes: 6 additions & 0 deletions src/Commands/Make/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

const BaseCommand = require('./Base')

/**
* Make a new lucid model
*
* @class MakeModel
* @constructor
*/
class MakeModel extends BaseCommand {
/**
* The command signature
Expand Down
6 changes: 6 additions & 0 deletions src/Commands/Make/View.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

const BaseCommand = require('./Base')

/**
* Make a new edge view
*
* @class MakeView
* @constructor
*/
class MakeView extends BaseCommand {
/**
* The command signature
Expand Down
16 changes: 10 additions & 6 deletions src/Commands/Repl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const { Command } = require('../../../lib/ace')
const historyFile = path.join(os.homedir(), '/.adonis_repl_history')

/**
* Serve the application using forever
* Start the repl server session
*
* @class Serve
* @class Repl
* @constructor
*/
class Repl extends Command {
Expand Down Expand Up @@ -50,8 +50,10 @@ class Repl extends Command {
* Reads the history file
*
* @param {Object} repl
*
* @private
*/
readHistoryFile (repl) {
_readHistoryFile (repl) {
try {
fs.statSync(historyFile)
repl.rli.history = fs.readFileSync(historyFile, 'utf-8').split('\n').reverse()
Expand All @@ -66,8 +68,10 @@ class Repl extends Command {
* Save the history to the history file.
*
* @param {Object} repl
*
* @private
*/
addHistorySaveListener (repl) {
_addHistorySaveListener (repl) {
const fd = fs.openSync(historyFile, 'a')
repl.rli.addListener('line', (code) => {
if (code && code !== '.history') {
Expand Down Expand Up @@ -101,8 +105,8 @@ class Repl extends Command {
server.context.make = global.make
}

this.readHistoryFile(server)
this.addHistorySaveListener(server)
this._readHistoryFile(server)
this._addHistorySaveListener(server)
awaitOutside.addAwaitOutsideToReplServer(server)
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/Commands/Serve/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ class Serve extends Command {
watch: dev,
watchIgnorePatterns: [
'*.edge',
'*.md',
'*.adoc',
'*.asciidoc',
'resources',
'database',
'README.md',
'public',
'package.json',
'package-lock.json',
'.gitignore',
Expand All @@ -89,7 +93,7 @@ class Serve extends Command {
console.log('')

child.on('watch:restart', (info) => {
console.log(`${this.chalk.magenta(info.file)} ${info.stat.replace(process.cwd(), '')}`)
console.log(`${this.chalk.magenta(info.file)} ${info.stat.replace(process.cwd(), '').replace(path.sep, '')}`)
})
child.start()
}
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ module.exports = {
'make:hook': require('./Make/Hook'),
'make:migration': require('./Make/Migration'),
'make:listener': require('./Make/Listener'),
'repl': require('./Repl')
'repl': require('./Repl'),
'make:ehandler': require('./Make/ExceptionHandler')
}
Loading

0 comments on commit 8703159

Please sign in to comment.