werkzeug
Version:
compiles ts, coffee, sass/scss, less/ stylus and packs browser ready bundles
211 lines (152 loc) • 5.53 kB
text/coffeescript
Path = require 'path'
CP = require 'child_process'
IPC = require './utils/ipc'
SW = require './utils/stopwatch'
PH = require './utils/path-helper'
Log = require './utils/log'
CLIENTS = [
'assets',
'styl',
'less',
'sass',
'coffee',
'ts'
]
class CompilerClient
constructor: (, ) ->
= .cfg
= PH.getIn ,
= false
= null
init: () ->
path = Path.join __dirname, 'compiler',
= new IPC(CP.fork(path), )
.send 'init',
prepare: () ->
= []
= []
= []
= false
SW.start 'compiler.' +
null
add: (file) ->
.push file
null
compile: () ->
if .length
if not
.send 'compile',
else
= true
null
exit: () ->
.exit() if
null
class Compiler
constructor: () ->
= .cfg
= false
= []
= []
= []
for type in CLIENTS
if
client = new CompilerClient(type, @)
@[type] = client
.push client
null
isEnabled: (type) ->
[type].enabled
compile: () ->
SW.start 'compiler.all'
= []
= []
= []
client.prepare() for client in
for file in .files
if file.dirty or file.errors
path = file.path
removed = file.removed
used = false
f = path:path, removed:removed, error:false
# add removed files also to update ts file map
# allow d.ts files from everywhere #TODO: maybe change this
if and PH.testTS(path) and (path.indexOf(.root) == 0 or /\.d\.ts/.test path)
.add f
used = true
else if and PH.testCoffee(path) and not removed and path.indexOf(.root) == 0
.add f
used = true
else if and PH.testSass(path) and not removed and path.indexOf(.root) == 0
.add f
used = true
else if and PH.testLess(path) and not removed and path.indexOf(.root) == 0
.add f
used = true
else if and PH.testStyl(path) and not removed and path.indexOf(.root) == 0
.add f
used = true
# ignore removed files in this else -> all removed files will be added separate
else if and path.indexOf(.root) == 0 and not removed
.add f
used = true
# add all removed to assets
if removed
.add f
used = true
if used
.push f
client.compile() for client in
l = .length
if l
console.log() if
#Log.info 'compiler', "starting ... (#{l} #{Log.count l, 'file'})" + JSON.stringify
Log.info 'compiler', "starting ... (#{l} #{Log.count l, 'file'})"
return true
false
compiled: (type, errors) ->
try
client = @[type]
client.compiled = true
errors = errors or []
for error in errors
if error.error
.push error
client.errors.push error
else if error.warning
.push error
client.warnings.push error
error.type = type if not error.type
path = error.path
file = .fileMap[path]
if file and error.error
file.errors = true
#TODO: handle error somehow
else
#console.log 'error in file, but file not in root: ', path
t = SW.stop 'compiler.' + type
Log.info type, 'ready', t, client.errors.length, client.warnings.length
compiled = true
compiled = compiled and client.compiled for client in
if compiled
= true
t = SW.stop 'compiler.all'
l = .length
Log.info 'compiler', 'ready', t, l
.compiled()
catch e
console.log 'compiled error: ', e.toString()
null
logErrors: () ->
base = .base
for client in
if client.warnings.length
Log.warn client.warnings, base
for client in
if client.errors.length
Log.error client.errors, base
null
exit: () ->
client.exit() for client in
null
module.exports = Compiler