Skip to content

Commit

Permalink
removed unit test for CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
neocotic committed Nov 1, 2015
1 parent 92a3c01 commit d213205
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 61 deletions.
96 changes: 58 additions & 38 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ const Utils = require('./utils')

const commandSymbol = Symbol('command')
const finalize = Symbol('finalize')
const listSymbol = Symbol('list')
const loadSymbol = Symbol('load')
const moanSymbol = Symbol('moan')

/**
* Manages interaction with this module from the command-line interface.
Expand All @@ -35,17 +34,28 @@ class CommandLineInterface {
/**
* Creates a new instance of {@link CommandLineInterface}.
*
* @param {CommandLineInterfaceOptions} [options] - the options for the {@link CommandLineInterface}
* @public
*/
constructor() {
constructor(options) {
options = Object.assign({}, CommandLineInterface.defaults, options)

/**
* The {@link Moan} instance being used by this {@link CommandLineInterface}.
*
* @private
* @type {Moan}
*/
this[moanSymbol] = options.moan

/**
* The dedicated <code>Command</code> for this {@link CommandLineInterface}.
*
* @private
* @type {Command}
*/
this[commandSymbol] = new Command()
.version(moan.version)
.version(this[moanSymbol].version)
.usage('[options] <task ...>')
.option('-d, --debug', 'enable debug output')
.option('-f, --file [name]', 'specify alternative name for the Moan file')
Expand All @@ -54,18 +64,18 @@ class CommandLineInterface {
.option('--no-color', 'disable color output')
.option('--stack', 'print stack traces for errors')

moan
this[moanSymbol]
.on('start', () => {
moan.log.writeln('Running...')
this[moanSymbol].log.writeln('Running...')
})
.on('done', () => {
moan.log.ok()
this[moanSymbol].log.ok()
})
.on('error', (name, error) => {
if (error.message) {
moan.log.error(error.message)
this[moanSymbol].log.error(error.message)
} else {
moan.log.error(error)
this[moanSymbol].log.error(error)
}

if (error.stack && this[commandSymbol].stack) {
Expand All @@ -74,11 +84,11 @@ class CommandLineInterface {
.splice(1)
.join('\n')

moan.log.write(`${stack}\n`)
this[moanSymbol].log.write(`${stack}\n`)
}

if (!this[commandSymbol].stack) {
moan.log.writeln('Use the --stack option to print stack traces for errors to help debug problems')
this[moanSymbol].log.writeln('Use the --stack option to print stack traces for errors to help debug problems')
}
})
}
Expand All @@ -96,15 +106,15 @@ class CommandLineInterface {

if (error) {
if (!this[commandSymbol].force) {
moan.log.writeln('Use the --force option to continue running tasks even after an error')
this[moanSymbol].log.writeln('Use the --force option to continue running tasks even after an error')
}

if (moan.currentTask) {
moan.log.writeln(chalk.bgRed.bold('Aborted!'))
if (this[moanSymbol].currentTask) {
this[moanSymbol].log.writeln(chalk.bgRed.bold('Aborted!'))
}
}

moan.log
this[moanSymbol].log
.write('\n')
.separator()
.write(chalk.bold(`BUILD ${error ? 'FAILED' : 'SUCCESS'}`))
Expand All @@ -118,17 +128,17 @@ class CommandLineInterface {
/**
* Logs the list of available task names.
*
* @private
* @public
*/
[listSymbol]() {
let tasks = moan.names()
list() {
let tasks = this[moanSymbol].names()

if (tasks.length) {
moan.log.writeln('The following tasks are available:\n')
this[moanSymbol].log.writeln('The following tasks are available:\n')

tasks.forEach((task) => moan.log.writeln(task))
tasks.forEach((task) => this[moanSymbol].log.writeln(task))
} else {
moan.log.writeln('No tasks were found')
this[moanSymbol].log.writeln('No tasks were found')
}
}

Expand All @@ -140,16 +150,15 @@ class CommandLineInterface {
* successfully but may be rejected if the Moan file could not be found or is invalid.
*
* @return {Promise} The <code>Promise</code> for tracking finding and loading the Moan file.
* @private
* @public
*/
[loadSymbol]() {
const fileName = 'Moan.js'
let moanFile = this[commandSymbol].file || findup(fileName, { nocase: true })
if (!moanFile) {
return Promise.reject(`Unable to find ${fileName} file`)
}

load() {
return new Promise((resolve, reject) => {
let moanFile = this[commandSymbol].file || findup('Moan.js', { nocase: true })
if (!moanFile) {
throw new Error(`Unable to find ${path.basename(moanFile)} file`)
}

fs.stat(moanFile, (error, stat) => {
if (error) {
reject(`Unable to find file: ${moanFile}`)
Expand All @@ -159,7 +168,7 @@ class CommandLineInterface {
process.chdir(path.dirname(moanFile))
require(path.resolve(moanFile))

resolve()
resolve(moanFile)
}
})
})
Expand All @@ -178,16 +187,16 @@ class CommandLineInterface {
let command = this[commandSymbol].parse(args)
let start = moment.utc()

moan.color = !command.noColor
moan.debug = command.debug
moan.force = command.force
this[moanSymbol].color = !command.noColor
this[moanSymbol].debug = command.debug
this[moanSymbol].force = command.force

this[loadSymbol]()
this.load()
.then(() => {
if (command.list) {
this[listSymbol]()
this.list()
} else {
return moan.run(command.args)
return this[moanSymbol].run(command.args)
}
})
.then(() => {
Expand All @@ -199,5 +208,16 @@ class CommandLineInterface {
}
}

module.exports = new CommandLineInterface()
exports.CommandLineInterface = CommandLineInterface
/**
* Options for the {@link CommandLineInterface} constructor.
*
* @public
* @typedef {Object} CommandLineInterfaceOptions
* @property {Moan} [moan] - the {@link Moan} instance to be used
*/
CommandLineInterface.defaults = {
moan
}

module.exports = new CommandLineInterface(moan)
module.exports.CommandLineInterface = CommandLineInterface
23 changes: 0 additions & 23 deletions test/cli.spec.js

This file was deleted.

0 comments on commit d213205

Please sign in to comment.