See #213. Add warning to beautify-directory
- and do not save beautified output if it is empty string
This commit is contained in:
parent
94553b0a23
commit
5506b28e8a
|
@ -7,9 +7,9 @@
|
||||||
'.tree-view .file > .name': [
|
'.tree-view .file > .name': [
|
||||||
{label: 'Beautify File', command: 'atom-beautify:beautify-file'}
|
{label: 'Beautify File', command: 'atom-beautify:beautify-file'}
|
||||||
]
|
]
|
||||||
# '.tree-view .directory > .header > .name': [
|
'.tree-view .directory > .header > .name': [
|
||||||
# {label: 'Beautify Directory', command: 'atom-beautify:beautify-directory'}
|
{label: 'Beautify Directory', command: 'atom-beautify:beautify-directory'}
|
||||||
# ]
|
]
|
||||||
|
|
||||||
'menu': [
|
'menu': [
|
||||||
{
|
{
|
||||||
|
|
|
@ -55,6 +55,15 @@ beautifier.on('beautify::start', ->
|
||||||
beautifier.on('beautify::end', ->
|
beautifier.on('beautify::end', ->
|
||||||
loadingView?.hide()
|
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}) ->
|
beautify = ({onSave}) ->
|
||||||
# Deprecation warning for beautify on save
|
# Deprecation warning for beautify on save
|
||||||
|
@ -77,17 +86,6 @@ beautify = ({onSave}) ->
|
||||||
path ?= require("path")
|
path ?= require("path")
|
||||||
forceEntireFile = onSave and atom.config.get("atom-beautify.beautifyEntireFileOnSave")
|
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
|
# Get the path to the config file
|
||||||
# All of the options
|
# All of the options
|
||||||
# Listed in order from default (base) to the one with the highest priority
|
# 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 = $(".icon-file-text[data-path=\"#{filePath}\"]")
|
||||||
$el.addClass('beautifying')
|
$el.addClass('beautifying')
|
||||||
|
|
||||||
|
|
||||||
# Cleanup and return callback function
|
# Cleanup and return callback function
|
||||||
cb = (err, result) ->
|
cb = (err, result) ->
|
||||||
$el = $(".icon-file-text[data-path=\"#{filePath}\"]")
|
$el = $(".icon-file-text[data-path=\"#{filePath}\"]")
|
||||||
|
@ -214,16 +211,17 @@ beautifyFilePath = (filePath, callback) ->
|
||||||
grammar = atom.grammars.selectGrammar(filePath, input)
|
grammar = atom.grammars.selectGrammar(filePath, input)
|
||||||
grammarName = grammar.name
|
grammarName = grammar.name
|
||||||
|
|
||||||
|
|
||||||
# Get the options
|
# Get the options
|
||||||
allOptions = beautifier.getOptionsForPath(filePath)
|
allOptions = beautifier.getOptionsForPath(filePath)
|
||||||
|
|
||||||
|
|
||||||
# Beautify File
|
# Beautify File
|
||||||
completionFun = (output) ->
|
completionFun = (output) ->
|
||||||
if output instanceof Error
|
if output instanceof Error
|
||||||
return cb(output, null ) # output == Error
|
return cb(output, null ) # output == Error
|
||||||
else if typeof output is "string"
|
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) ->
|
fs.writeFile(filePath, output, (err) ->
|
||||||
return cb(err) if err
|
return cb(err) if err
|
||||||
return cb( null , output)
|
return cb( null , output)
|
||||||
|
@ -241,36 +239,34 @@ beautifyFile = ({target}) ->
|
||||||
filePath = target.dataset.path
|
filePath = target.dataset.path
|
||||||
return unless filePath
|
return unless filePath
|
||||||
beautifyFilePath(filePath, (err, result) ->
|
beautifyFilePath(filePath, (err, result) ->
|
||||||
return console.error('beautifyFile error', err, result) if err
|
return showError(err) if err
|
||||||
|
# console.log("Beautify File
|
||||||
# console.log("Beautify File
|
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
beautifyDirectory = ({target}) ->
|
beautifyDirectory = ({target}) ->
|
||||||
dirPath = target.dataset.path
|
dirPath = target.dataset.path
|
||||||
return unless dirPath
|
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
|
# Show in progress indicate on directory's tree-view entry
|
||||||
$ ?= require("atom-space-pen-views").$
|
$ ?= require("atom-space-pen-views").$
|
||||||
$el = $(".icon-file-directory[data-path=\"#{dirPath}\"]")
|
$el = $(".icon-file-directory[data-path=\"#{dirPath}\"]")
|
||||||
$el.addClass('beautifying')
|
$el.addClass('beautifying')
|
||||||
|
|
||||||
|
|
||||||
# Process Directory
|
# Process Directory
|
||||||
dir ?= require "node-dir"
|
dir ?= require "node-dir"
|
||||||
async ?= require "async"
|
async ?= require "async"
|
||||||
dir.files(dirPath, (err, files) ->
|
dir.files(dirPath, (err, files) ->
|
||||||
return console.error('beautifyDirectory error', err) if err
|
return showError(err) if err
|
||||||
async.each(files, (filePath, callback) ->
|
|
||||||
|
|
||||||
|
async.each(files, (filePath, callback) ->
|
||||||
# Ignore errors
|
# Ignore errors
|
||||||
beautifyFilePath(filePath, -> callback())
|
beautifyFilePath(filePath, -> callback())
|
||||||
, (err) ->
|
, (err) ->
|
||||||
$el = $(".icon-file-directory[data-path=\"#{dirPath}\"]")
|
$el = $(".icon-file-directory[data-path=\"#{dirPath}\"]")
|
||||||
$el.removeClass('beautifying')
|
$el.removeClass('beautifying')
|
||||||
|
# console.log('Completed beautifying directory!', dirPath)
|
||||||
# console.log('Completed beautifying directory!', dirPath)
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue