Skip to content

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
gil0mendes committed Feb 13, 2017
2 parents 1e79f13 + 8e2e81a commit 1537b65
Show file tree
Hide file tree
Showing 61 changed files with 2,284 additions and 1,345 deletions.
4 changes: 4 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"node": true
},

"parserOptions": {
"ecmaVersion": 6
},

"parser": "babel-eslint",

"ecmaFeatures": {
Expand Down
113 changes: 103 additions & 10 deletions bin/Command.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,139 @@
'use strict'

// ----------------------------------------------------------------------------- [Imports]

const Engine = require('../dist/engine').default
const path = require('path')
const pkg = require('../package.json')
const spawn = require('child_process').spawn

// ----------------------------------------------------------------------------- [Module]
/**
* All command extends this class in order to initialize Stellar and
* provide a standard way of creating commands.
*/
module.exports = class {

constructor () {
// define the console colors
/**
* Creates a new command instance.
*
* @type {boolean} initialize When true Stellar will be initialized on the
* handler method execution.
*/
constructor (initialize = false) {
// define console colors
this.FgRed = '\x1b[31m'
this.FgGreen = '\x1b[32m'
this.FgYellow = '\x1b[33m'
this.FgBlue = '\x1b[34m'
this.FgWhite = '\x1b[37m'
this.FgDefault = '\x1b[39m'

// create an usage variable
this.usage = ''
// define console font states
this.FontBold = '\x1b[1m'
this.FontNormal = '\x1b[0m'

// store if is to initialize
this.isToInitialize = initialize
this.api = null
this.engine = null

// FIX `this` binding in the `handler` method
this.handler = this.handler.bind(this)
}

/**
* Build the scope to create a new Stellar instance.
*/
_buildScope () {
return {
rootPath: process.cwd(),
stellarPackageJSON: pkg,
args: this.args
}
}

/**
* Initialize a Stellar instance when requested.
*/
_initializeStellar () {
return new Promise((resolve, reject) => {
// build the scope
const scope = this._buildScope()

// create a new engine instance and save it
this.engine = new Engine(scope)

// initialize the engine
this.engine.initialize((error, api) => {
// if an error occurs reject the promise and return
if (error) { return reject(error) }

// otherwise, store the API reference
this.api = api

// resolve the promise
resolve(this.api)
})
})
}

/**
* Print command usage.
* Catch the yargs command call.
*/
printUsage () {
console.log(`\n${this.FgYellow}Usage: ${this.FgGreen}${this.usage}\n`)
handler (args) {
// store the args
this.args = args

// if the user requested to run this as a deamon we must spawn a new process
if (this.args.deamon) {
// create a new set of arguments removing the `--daemon` options
const newArgs = process.argv.splice(2)
for (const i in newArgs) {
if (newArgs[i].indexOf('--daemon') >= 0) { newArgs.splice(i, 1) }
}
newArgs.push('--isDaemon=true')

const command = path.normalize(`${__dirname}/stellar`)
const child = spawn(command, newArgs, { detached: true, cwd: process.cwd(), env: process.env, stdio: 'ignore' })
console.log(`${command} ${newArgs.join(' ')}`)
console.log(`Spawned child process with pid ${child.pid}`)

// finish the current process
process.nextTick(process.exit)
return
}

// check if is to initialize the Engine
if (this.isToInitialize) {
return this._initializeStellar()
.then(_ => { this.run() })
.catch(error => { this.printError(error) })
}

// run the command
this.run()
}

/**
* Print an error message.
*
* @param msg Message to be printed.
*/
printError (msg) { console.log(`\n${this.FgRed}Error: ${msg}\n`) }
printError (msg) { console.log(`${this.FontBold}${this.FgRed}Error: ${msg}${this.FgDefault}${this.FontNormal}`) }

/**
* Print an info message.
*
* @param msg Message to be printed.
*/
printInfo (msg) { console.log(`\n${this.FgBlue}Info: ${msg}\n`) }
printInfo (msg) { console.log(`${this.FgBlue}Info: ${msg}${this.FgDefault}`) }

/**
* Print a success message.
*
* @param msg Message to be printed.
*/
printSuccess (msg) { console.log(`\n${this.FgGreen}Success: ${msg}\n`) }
printSuccess (msg) { console.log(`${this.FgGreen}Success: ${msg}${this.FgDefault}`) }

}
Loading

0 comments on commit 1537b65

Please sign in to comment.