simplywatch
Version:
Watches files and upon change executes a command for each file INDIVIDUALLY with file-related params
52 lines (38 loc) • 1.34 kB
text/coffeescript
Promise = require 'bluebird'
execa = require 'execa'
extend = require 'smart-extend'
placeholderRegex = /(?:\#\{|\{\{)([^\/\}]+)(?:\}\}|\})/ig
defaultResult = {stdout:'', stderr:''}
class CommandExecution
constructor: (, , )->
return new CommandExecution(, , ) if isnt CommandExecution
= 'pending'
if typeof is 'string' and
= .replace placeholderRegex, (entire, placeholder)=>
if [placeholder]? then [placeholder] else entire
start: ()->
=
Promise.resolve()
.then (command)=>
= switch typeof command
when 'function'
command(, )
when 'string'
execa.shell command, env:extend({'FORCE_COLOR':'true'}, process.env)
.then (result)-> result ||= defaultResult
.then ()=> = 'success'
.tapCatch (err)-> err.stack = err.message if err.message.includes('Command failed:')
.catch ()=> = 'failure'
.then ()=>
then: (cb)->
.then cb
catch: (cb)->
.catch cb
cancel: ()->
._fulfill(=defaultResult)
if typeof is 'string'
?.kill()
else if .isCancellable?()
.cancel()
= 'cancel'
module.exports = CommandExecution