Skip to content

Commit

Permalink
✅ Refactoring & Test
Browse files Browse the repository at this point in the history
  • Loading branch information
59naga committed Jun 4, 2015
1 parent a10b904 commit 32d99be
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 149 deletions.
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "abigail",
"bin" : "abigail",
"description": "the Minimal task runner. Enhance your npm-scripts.",
"version": "0.1.0-alpha.4",
"version": "0.1.0-beta.0",

"scripts": {
"default": "jasminetea test --verbose --timeout 3000",
Expand All @@ -14,6 +14,11 @@
"compile": "coffee --bare --compile index.coffee",
"postcompile": "coffee --output lib --bare --compile src"
},
"config": {
"ghooks": {
"pre-commit": "npm run test"
}
},

"dependencies": {
"bluebird": "^2.9.27",
Expand All @@ -22,6 +27,7 @@
"minimist": "^1.1.0"
},
"devDependencies": {
"ghooks": "^0.3.2",
"jasminetea": "^0.2.0-beta.3",
"object-parser-cli": "0.0.1-alpha.0"
},
Expand Down
56 changes: 34 additions & 22 deletions src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,13 @@ class Abigail extends Utility

try
@scripts= (require path.join process.cwd(),'package').scripts
@log "Use #{@npm('./package.json')}" unless @test
@log "Use #{@json('./package.json')}"

catch
@scripts= {}
@log "Missing #{@npm('./package.json')}" unless @test
@log "Missing #{@json('./package.json')}"

i= 0
@args=
while @_[i]?
names= @_[i++].split ','

scripts=
for name in names
lazy= name[0] is '_'
name= name.slice 1 if lazy

script= new String(if @scripts[name]? then 'npm run '+name else name)
script.lazy= lazy
script.raw= name
script

globs= @_[i++]?.split ','
globs?= []
globs= (glob.replace /^_/,'!' for glob in globs)

{scripts,globs}
@args= @toArgs @_

return if @test

Expand All @@ -53,5 +34,36 @@ class Abigail extends Utility
if singleArgument
@tasks[0].noWatch= on

toArgs: (minimistArgv)->
i= 0

while minimistArgv[i]?
names= minimistArgv[i++].split ','

scripts=
for name in names
lazy= name[0] is '_'
name= name.slice 1 if lazy

script=
if @scripts[name]?
new String 'npm run '+name
else
new String name

script.lazy= lazy
script.raw= name

script

globs= minimistArgv[i++]?.split ','
globs?= []

globs=
for glob in globs
glob.replace /^_/,'!'

{scripts,globs}

module.exports= new Abigail
module.exports.Abigail= Abigail
44 changes: 25 additions & 19 deletions src/task.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,37 @@ Promise= require 'bluebird'
path= require 'path'
spawn= (require 'child_process').spawn

# Public
class Task extends Utility
constructor: (@scripts=[],globs=[],test=false)->
@busy= no
constructor: (@scripts=[],globs=[],@test=false)->
@globs= @toAbsolute globs

@globs=
for glob in globs
blacklist= glob[0] is '!'
glob= glob.slice 1 if blacklist

globAbsolute= path.resolve process.cwd(),glob
globAbsolute= '!'+globAbsolute if blacklist
globAbsolute

return if test

gaze @globs,(error,watcher)=>
gaze @globs,(error,@watcher)=>
throw error if error?

unless @noWatch
if @globs.length
@log "Watch #{@whereabouts(globs)} for #{@strong(@scripts)}."

@execute @scripts unless @scripts[0].lazy
@busy= no
@execute @scripts unless @scripts[0]?.lazy

watcher.on 'all',(event,filepath)=>
@watcher.on 'all',(event,filepath)=>
return if @busy

name= path.relative process.cwd(),filepath
@log 'File',@whereabouts(name),event

@execute @scripts

toAbsolute: (globs)->
for glob in globs
blacklist= glob[0] is '!'
glob= glob.slice 1 if blacklist

globAbsolute= path.resolve process.cwd(),glob
globAbsolute= '!'+globAbsolute if blacklist
globAbsolute

execute: (scripts)->
return if @busy

Expand Down Expand Up @@ -67,16 +67,22 @@ class Task extends Utility
if @noWatch
process.exit ~~(1 in codes)

codes

spawn: (script)->
[bin,args...]= script.split /\s+/
options=
cwd: process.cwd()
stdio:'inherit'
options.stdio= 'ignore' if @test

@log "Run #{@strong(script)}"

new Promise (resolve)=>
child= spawn bin,args,{cwd:process.cwd(),stdio:'inherit'}
child= spawn bin,args,options

child.on 'error',(error)=>
@log ';;',"Failing #{@strong(script)}, Due to #{error}."
@log @sweat+" Failing #{@strong(script)}. Due to #{error}..."
resolve 1

child.on 'exit',(code)=>
Expand Down
139 changes: 77 additions & 62 deletions src/utility.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,87 +4,102 @@ chalk= require 'chalk'
pkg= require '../package'
path= require 'path'

# Public
class Utility
icon: chalk.magenta '@'+chalk.underline(' ')+'@'

_log: Date.now()
log: (args...)->
suffix= ' ms'
diff= Date.now()-@_log ? 0
if diff>1000
diff= ~~(diff/1000)
suffix= 'sec'
if diff>60
diff= ~~(diff/60)
suffix= 'min'
if diff>60
diff= ~~(diff/60)
suffix= ' hr'
console.log ([chalk.gray((' +'+diff+suffix).slice(-8)),@icon].concat args)...
@_log= Date.now()
elapsed= chalk.gray (' +'+@getElapsed()).slice -8
output= [elapsed,@icon].concat args

return output if @test

console.log output...

npm: (arg)->
json: (arg)->
chalk.bgRed arg

strong: (args,conjunctive=', ')->
args= [args] unless args instanceof Array
args= (arg?.raw ? arg for arg in args)

(chalk.underline arg for arg in args).join(conjunctive)
(chalk.underline arg for arg in args).join conjunctive

whereabouts: (args,conjunctive=' and ')->
args= [args] if typeof args is 'string'
args= [args] unless args instanceof Array
args= [chalk.red('undefined')] if args[0] is undefined

(chalk.underline arg for arg in args).join(conjunctive)
(chalk.underline arg for arg in args).join conjunctive

help: ->
log= console.log
text= ""

log ""
log " #{@icon} Abigail v#{pkg.version}"
log " "
log " Usage:"
log " $ abigail #{@strong('scripts')} #{@whereabouts('globs')} [#{@strong('scripts')} #{@whereabouts('globs')}] ..."
log " "
log " Example:"
log " $ #{chalk.inverse('abigail compile "*.coffee"')}"
log " > #{@icon} Watch #{@whereabouts('*.coffee')} for #{@strong('npm run compile')}"
log " > #{@icon} Run #{@strong('compile')}"
log " > ..."
log " "
log " $ #{chalk.inverse('abigail test test/**,src/**')}"
log " > #{@icon} Watch #{@whereabouts(['test/**','src/**'])} for #{@strong('test')}"
log " > #{@icon} Run #{@strong('test')}"
log " > ..."
log " "
log " $ #{chalk.inverse('abigail test,lint test/**,src/**')}"
log " > #{@icon} Watch #{@whereabouts(['test/**','src/**'])} for #{@strong('test')}"
log " > #{@icon} Begin #{@strong(['test','lint'])} ..."
log " > #{@icon} Run #{@strong('test')}"
log " > ..."
log " > #{@icon} Run #{@strong('lint')}"
log " > ..."
log " "
log " $ #{chalk.inverse('abigail \'echo cool\' "*.md"')}"
log " > #{@icon} Watch #{@whereabouts('*.md')} for #{@strong('echo cool')}"
log " > #{@icon} Run #{@strong('echo cool')}"
log " > ..."
log " "
log " $ #{chalk.inverse('abigail _test test/**,src/**')}"
log " > #{@icon} Watch #{@whereabouts(['test/**','src/**'])} for #{@strong('test')}"
log " "
log " $ #{chalk.inverse('abigail _test test/**,_src/**')}"
log " > #{@icon} Watch #{@whereabouts(['test/**','!src/**'])} for #{@strong('test')}"
log ""
text+= "\n #{@icon} Abigail v#{pkg.version}"
text+= "\n "
text+= "\n Usage:"
text+= "\n $ abigail #{@strong('scripts')} #{@whereabouts('globs')} [#{@strong('scripts')} #{@whereabouts('globs')}] ..."
text+= "\n "
text+= "\n Example:"
text+= "\n $ #{chalk.inverse('abigail compile "*.coffee"')}"
text+= "\n > #{@icon} Watch #{@whereabouts('*.coffee')} for #{@strong('npm run compile')}"
text+= "\n > #{@icon} Run #{@strong('compile')}"
text+= "\n > ..."
text+= "\n "
text+= "\n $ #{chalk.inverse('abigail test test/**,src/**')}"
text+= "\n > #{@icon} Watch #{@whereabouts(['test/**','src/**'])} for #{@strong('test')}"
text+= "\n > #{@icon} Run #{@strong('test')}"
text+= "\n > ..."
text+= "\n "
text+= "\n $ #{chalk.inverse('abigail test,lint test/**,src/**')}"
text+= "\n > #{@icon} Watch #{@whereabouts(['test/**','src/**'])} for #{@strong('test')}"
text+= "\n > #{@icon} Begin #{@strong(['test','lint'])} ..."
text+= "\n > #{@icon} Run #{@strong('test')}"
text+= "\n > ..."
text+= "\n > #{@icon} Run #{@strong('lint')}"
text+= "\n > ..."
text+= "\n "
text+= "\n $ #{chalk.inverse('abigail \'echo cool\' "*.md"')}"
text+= "\n > #{@icon} Watch #{@whereabouts('*.md')} for #{@strong('echo cool')}"
text+= "\n > #{@icon} Run #{@strong('echo cool')}"
text+= "\n > ..."
text+= "\n "
text+= "\n $ #{chalk.inverse('abigail _test test/**,src/**')}"
text+= "\n > #{@icon} Watch #{@whereabouts(['test/**','src/**'])} for #{@strong('test')}"
text+= "\n "
text+= "\n $ #{chalk.inverse('abigail _test test/**,_src/**')}"
text+= "\n > #{@icon} Watch #{@whereabouts(['test/**','!src/**'])} for #{@strong('test')}"
text+= "\n"

process.exit 0
@output text

version: ->
log= console.log

log "v#{pkg.version}"

@output "v#{pkg.version}"

output: (text)->
return text if @test

console.log text
process.exit 0

# Private
icon: chalk.magenta '@'+chalk.underline(' ')+'@'
sweat: chalk.magenta ';'

elapsed: Date.now()
getElapsed: ->
suffix= ' ms'

diff= Date.now()-@elapsed ? 0
@elapsed= Date.now()

if diff>1000
diff= ~~(diff/1000)
suffix= 'sec'
if diff>60
diff= ~~(diff/60)
suffix= 'min'
if diff>60
diff= ~~(diff/60)
suffix= ' hr'

diff+suffix

module.exports.Utility= Utility

0 comments on commit 32d99be

Please sign in to comment.