See #213. Add warning to beautify-directory

- and do not save beautified output if it is empty string
This commit is contained in:
Glavin Wiechert 2015-06-10 11:25:37 -03:00
parent 94553b0a23
commit 5506b28e8a
2 changed files with 21 additions and 25 deletions

View File

@ -7,9 +7,9 @@
'.tree-view .file > .name': [
{label: 'Beautify File', command: 'atom-beautify:beautify-file'}
]
# '.tree-view .directory > .header > .name': [
# {label: 'Beautify Directory', command: 'atom-beautify:beautify-directory'}
# ]
'.tree-view .directory > .header > .name': [
{label: 'Beautify Directory', command: 'atom-beautify:beautify-directory'}
]
'menu': [
{

View File

@ -55,6 +55,15 @@ beautifier.on('beautify::start', ->
beautifier.on('beautify::end', ->
loadingView?.hide()
)
# Show error
showError = (error) =>
if not atom.config.get("atom-beautify.muteAllErrors")
# console.log(e)
stack = error.stack
detail = error.description or error.message
atom.notifications?.addError(error.message, {
stack, detail, dismissable : true})
beautify = ({onSave}) ->
# Deprecation warning for beautify on save
@ -77,17 +86,6 @@ beautify = ({onSave}) ->
path ?= require("path")
forceEntireFile = onSave and atom.config.get("atom-beautify.beautifyEntireFileOnSave")
# Show error
showError = (error) =>
if not atom.config.get("atom-beautify.muteAllErrors")
# console.log(e)
stack = error.stack
detail = error.description or error.message
atom.notifications?.addError(error.message, {
stack, detail, dismissable : true})
# Get the path to the config file
# All of the options
# Listed in order from default (base) to the one with the highest priority
@ -199,7 +197,6 @@ beautifyFilePath = (filePath, callback) ->
$el = $(".icon-file-text[data-path=\"#{filePath}\"]")
$el.addClass('beautifying')
# Cleanup and return callback function
cb = (err, result) ->
$el = $(".icon-file-text[data-path=\"#{filePath}\"]")
@ -214,16 +211,17 @@ beautifyFilePath = (filePath, callback) ->
grammar = atom.grammars.selectGrammar(filePath, input)
grammarName = grammar.name
# Get the options
allOptions = beautifier.getOptionsForPath(filePath)
# Beautify File
completionFun = (output) ->
if output instanceof Error
return cb(output, null ) # output == Error
else if typeof output is "string"
# do not allow empty string
return cb(null, output) if output is ''
# save to file
fs.writeFile(filePath, output, (err) ->
return cb(err) if err
return cb( null , output)
@ -241,8 +239,7 @@ beautifyFile = ({target}) ->
filePath = target.dataset.path
return unless filePath
beautifyFilePath(filePath, (err, result) ->
return console.error('beautifyFile error', err, result) if err
return showError(err) if err
# console.log("Beautify File
)
return
@ -250,26 +247,25 @@ beautifyDirectory = ({target}) ->
dirPath = target.dataset.path
return unless dirPath
return if atom?.confirm(message: "This will beautify all of the files found recursively in this directory, '#{dirPath}'. Do you want to continue?") isnt true
# Show in progress indicate on directory's tree-view entry
$ ?= require("atom-space-pen-views").$
$el = $(".icon-file-directory[data-path=\"#{dirPath}\"]")
$el.addClass('beautifying')
# Process Directory
dir ?= require "node-dir"
async ?= require "async"
dir.files(dirPath, (err, files) ->
return console.error('beautifyDirectory error', err) if err
async.each(files, (filePath, callback) ->
return showError(err) if err
async.each(files, (filePath, callback) ->
# Ignore errors
beautifyFilePath(filePath, -> callback())
, (err) ->
$el = $(".icon-file-directory[data-path=\"#{dirPath}\"]")
$el.removeClass('beautifying')
# console.log('Completed beautifying directory!', dirPath)
)
)