Skip to content

Commit

Permalink
Merge pull request #9 from realglobe-Inc/fix_warning
Browse files Browse the repository at this point in the history
存在しないタスクを実行しようとしたら警告を出してほしい realglobe-Inc/pon#13
  • Loading branch information
okunishinishi authored Jun 13, 2017
2 parents 9baec77 + 06c6202 commit 19fc8eb
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
12 changes: 3 additions & 9 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

const co = require('co')
const argx = require('argx')
const ponfile = require('ponfile')
const runnable = require('./runnable')

/** @lends cli */
function cli (name, options) {
Expand All @@ -18,21 +18,15 @@ function cli (name, options) {
list = false
} = options
return co(function * () {
let runner = ponfile(cwd)
let runner = runnable(cwd)
if (list) {
let pattern = typeof list === 'string' ? list : null
return cli.list(runner.tasks, { pattern })
}
if (names && names.length > 0) {
let results = []
for (let name of names) {
let result = yield Promise.resolve(runner.run(name)).then((result) => {
let isEmpty = Object.keys(result).length === 0
if (isEmpty) {
console.warn(`No task found for name: "${name}"`)
}
return result
})
let result = yield Promise.resolve(runner.run(name))
results.push(result)
}
return results
Expand Down
28 changes: 28 additions & 0 deletions lib/runnable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Define CLI Runner
* @function runnable
* @param {string} cwd - Working directory
* @returns {CLIRunner} Runner instance
*/
'use strict'
const { Ponfile } = require('ponfile')

/**
* Runner implementation for CLI
*/
class CLIRunner extends Ponfile {
run (...patterns) {
return Promise.resolve(
super.run(...arguments)
).then((result) => {
let isEmpty = Object.keys(result).length === 0
if (isEmpty) {
let specified = patterns.length > 1 ? patterns : patterns[ 0 ]
console.warn(`No task found for: ${JSON.stringify(specified)}`)
}
return result
})
}
}

module.exports = (cwd) => new CLIRunner(cwd)
5 changes: 4 additions & 1 deletion misc/mock/mock-project-01/Ponfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ module.exports = ponRunner({
},
b () {
console.log('This is B')
}
},

invalid01: [ '__invalid_pointer_01__', () => {} ],
invalid02: [ '__invalid_pointer_02__' ]
}).bind()

10 changes: 10 additions & 0 deletions test/cli_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ describe('cli', function () {
list: true
})
}))

// https://github.com/realglobe-Inc/pon/issues/13
it('Warning with not existing task', () => co(function * () {
yield cli('invalid01', {
cwd: `${__dirname}/../misc/mock/mock-project-01`
})
yield cli('invalid02', {
cwd: `${__dirname}/../misc/mock/mock-project-01`
})
}))
})

/* global describe, before, after, it */

0 comments on commit 19fc8eb

Please sign in to comment.