Navigation Menu

Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Commit

Permalink
Make all errors late.
Browse files Browse the repository at this point in the history
All errors are now printed on compilation end.

This allows to stop later compilations on errors that were not
fixed.

Closes gh-411.
  • Loading branch information
paulmillr committed Sep 16, 2012
1 parent ad4a293 commit 9a2d91c
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 13 deletions.
14 changes: 13 additions & 1 deletion src/commands/watch.coffee
Expand Up @@ -94,13 +94,25 @@ changeFileList = (compilers, linters, fileList, path, isHelper) ->
#
# Returns nothing.
getCompileFn = (config, joinConfig, fileList, minifiers, watcher, callback) -> (startTime) ->
assetErrors = fileList.getAssetErrors()
if assetErrors?
assetErrors.forEach (error) -> logger.error error
return

fs_utils.write fileList, config, joinConfig, minifiers, startTime, (error, generatedFiles) ->
return logger.error "Write failed: #{error}" if error?
if error?
if Array.isArray error
error.forEach (subError) ->
logger.error subError
else
logger.error error
return
logger.info "compiled in #{Date.now() - startTime}ms"
unless config.persistent
watcher.close()
process.on 'exit', (previousCode) ->
process.exit (if logger.errorHappened then 1 else previousCode)

callback generatedFiles

# Restart brunch watcher.
Expand Down
12 changes: 10 additions & 2 deletions src/fs_utils/asset.coffee
Expand Up @@ -26,7 +26,15 @@ module.exports = class Asset
logger.debug 'asset', "Initializing fs_utils.Asset", {
@path, directory, @relativePath, @destinationPath
}
Object.freeze this
@error = null
Object.seal this

copy: (callback) ->
common.copy @path, @destinationPath, callback
common.copy @path, @destinationPath, (error) =>
if error?
err = new Error error
err.brunchType = 'Copying'
@error = err
else
@error = null
callback @error
2 changes: 1 addition & 1 deletion src/fs_utils/common.coffee
Expand Up @@ -43,7 +43,7 @@ exports.ignoredAlways = ignoredAlways = (path) ->
exports.copy = (source, destination, callback) ->
return callback() if ignored source
copy = (error) ->
return logger.error error if error?
return callback error if error?
input = fs.createReadStream source
output = fs.createWriteStream destination
request = input.pipe output
Expand Down
19 changes: 12 additions & 7 deletions src/fs_utils/file_list.coffee
Expand Up @@ -22,6 +22,14 @@ module.exports = class FileList extends EventEmitter
@compiling = []
@copying = []

getAssetErrors: ->
invalidAssets = @assets.filter((asset) -> asset.error?)
if invalidAssets.length > 0
invalidAssets.map (invalidAsset) ->
helpers.formatError invalidAsset.error, invalidAsset.path
else
null

# Files that are not really app files.
_ignored: (path, test = @config.conventions.ignored) ->
return yes if path in [@config.paths.config, @config.paths.packageConfig]
Expand Down Expand Up @@ -73,21 +81,18 @@ module.exports = class FileList extends EventEmitter
@compiling.push(file)
file.compile (error) =>
@compiling.splice @compiling.indexOf(file), 1
@_resetTimer()
return if error?
logger.debug 'info', "Compiled file '#{file.path}'"
if error?
return logger.error "#{error.brunchType} of '#{file.path}'
failed. #{error.toString().slice(7)}"
@_compileDependentFiles file.path
@_resetTimer()

_copy: (asset) =>
@copying.push asset
asset.copy (error) =>
@copying.splice @copying.indexOf(asset), 1
logger.debug 'info', "Copied asset '#{asset.path}'"
if error?
return logger.error "Copying of '#{asset.path}' failed. #{error}"
@_resetTimer()
return if error?
logger.debug 'info', "Copied asset '#{asset.path}'"

_add: (path, compiler, linters, isHelper) ->
isVendor = @_isVendor path
Expand Down
8 changes: 6 additions & 2 deletions src/fs_utils/source_file.coffee
Expand Up @@ -17,7 +17,9 @@ module.exports = class SourceFile
fileName = "brunch_#{@compilerName}_#{sysPath.basename @path}"
@realPath = @path
@path = sysPath.join 'vendor', 'scripts', fileName
@cache = Object.seal {data: '', dependencies: [], compilationTime: null}
@cache = Object.seal {
data: '', dependencies: [], compilationTime: null, error: null
}
Object.freeze this

_lint: (data, path, callback) ->
Expand Down Expand Up @@ -59,13 +61,14 @@ module.exports = class SourceFile
# Reads file and compiles it with compiler. Data is cached to `this.data`
# in order to do compilation only if the file was changed.
compile: (callback) ->
callbackError = (type, stringOrError) ->
callbackError = (type, stringOrError) =>
string = if stringOrError instanceof Error
stringOrError.toString().slice(7)
else
stringOrError
error = new Error string
error.brunchType = type
@cache.error = error
callback error

realPath = if @isHelper then @realPath else @path
Expand All @@ -81,4 +84,5 @@ module.exports = class SourceFile
@cache.dependencies = dependencies
@cache.data = @_wrap result if result?
@cache.compilationTime = Date.now()
@cache.error = null
callback null, @cache.data
19 changes: 19 additions & 0 deletions src/fs_utils/write.coffee
Expand Up @@ -4,6 +4,7 @@ async = require 'async'
sysPath = require 'path'
inflection = require 'inflection'
GeneratedFile = require './generated_file'
helpers = require '../helpers'

getPaths = (sourceFile, joinConfig) ->
sourceFileJoinConfig = joinConfig[inflection.pluralize sourceFile.type] or {}
Expand All @@ -29,9 +30,27 @@ changedSince = (startTime) -> (generatedFile) ->
generatedFile.sourceFiles.some (sourceFile) ->
sourceFile.cache.compilationTime >= startTime

gatherErrors = (generatedFiles) ->
errors = []
generatedFiles
.forEach (generatedFile) ->
generatedFile.sourceFiles
.filter (sourceFile) ->
sourceFile.cache.error?
.forEach (sourceFile) ->
cache = sourceFile.cache
errors.push helpers.formatError cache.error, sourceFile.path

if errors.length > 0
errors
else
null

module.exports = write = (fileList, config, joinConfig, minifiers, startTime, callback) ->
files = getFiles fileList, config, joinConfig, minifiers
changed = files.filter(changedSince startTime)
error = gatherErrors files
return callback error if error?
async.forEach changed, ((file, next) -> file.write next), (error) ->
return callback error if error?
callback null, changed
4 changes: 4 additions & 0 deletions src/helpers.coffee
Expand Up @@ -55,6 +55,10 @@ exports.deepFreeze = deepFreeze = (object) ->
.forEach(deepFreeze)
object

exports.formatError = (error, path) ->
"#{error.brunchType} of '#{path}'
failed. #{error.toString().slice(7)}"

sortAlphabetically = (a, b) ->
if a < b
-1
Expand Down

0 comments on commit 9a2d91c

Please sign in to comment.