See #296. Add Logging API to Beautifier

This commit is contained in:
Glavin Wiechert 2015-05-03 14:50:33 -03:00
parent 969f307175
commit 88c58a20bb
3 changed files with 63 additions and 18 deletions

View File

@ -73,6 +73,7 @@
"tidy-markdown": "^0.3.2", "tidy-markdown": "^0.3.2",
"typescript-formatter": "~0.1.4", "typescript-formatter": "~0.1.4",
"underscore-plus": "^1.6.6", "underscore-plus": "^1.6.6",
"winston": "^1.0.0",
"yaml-front-matter": "^3.2.3" "yaml-front-matter": "^3.2.3"
}, },
"activationCommands": { "activationCommands": {

View File

@ -38,6 +38,13 @@ module.exports = class Beautifier
### ###
languages: null languages: null
###
Beautify text
Override this method in subclasses
###
beautify: null
### ###
Show deprecation warning to user. Show deprecation warning to user.
### ###
@ -48,14 +55,14 @@ module.exports = class Beautifier
Create temporary file Create temporary file
### ###
tempFile: (name = "atom-beautify-temp", contents = "") -> tempFile: (name = "atom-beautify-temp", contents = "") ->
return new Promise((resolve, reject) -> return new Promise((resolve, reject) =>
# create temp file # create temp file
temp.open(name, (err, info) -> temp.open(name, (err, info) =>
# console.log(name, err, info) @debug('tempFile', name, err, info)
return reject(err) if err return reject(err) if err
fs.write(info.fd, contents, (err) -> fs.write(info.fd, contents, (err) =>
return reject(err) if err return reject(err) if err
fs.close(info.fd, (err) -> fs.close(info.fd, (err) =>
return reject(err) if err return reject(err) if err
resolve(info.path) resolve(info.path)
) )
@ -143,29 +150,29 @@ module.exports = class Beautifier
args = _.without(args, null) args = _.without(args, null)
# Get PATH and other environment variables # Get PATH and other environment variables
@getShellEnvironment() @getShellEnvironment()
.then((env) -> .then((env) =>
# Spawn command # Spawn command
stdout = "" stdout = ""
stderr = "" stderr = ""
options = { options = {
env: env env: env
} }
console.log('spawn', exe, args) @debug('spawn', exe, args)
cmd = spawn(exe, args, options) cmd = spawn(exe, args, options)
# add a 'data' event listener for the spawn instance # add a 'data' event listener for the spawn instance
cmd.stdout.on('data', (data) -> stdout += data ) cmd.stdout.on('data', (data) -> stdout += data )
cmd.stderr.on('data', (data) -> stderr += data ) cmd.stderr.on('data', (data) -> stderr += data )
# when the spawn child process exits, check if there were any errors and close the writeable stream # when the spawn child process exits, check if there were any errors and close the writeable stream
cmd.on('exit', (returnCode) -> cmd.on('exit', (returnCode) =>
console.log('spawn done', returnCode, stderr, stdout) @debug('spawn done', returnCode, stderr, stdout)
# If return code is not 0 then error occured # If return code is not 0 then error occured
if not ignoreReturnCode and returnCode isnt 0 if not ignoreReturnCode and returnCode isnt 0
reject(stderr) reject(stderr)
else else
resolve(stdout) resolve(stdout)
) )
cmd.on('error', (err) -> cmd.on('error', (err) =>
# console.log('error', err) @debug('error', err)
reject(err) reject(err)
) )
) )
@ -174,16 +181,53 @@ module.exports = class Beautifier
) )
### ###
Beautify text Methods to copy over from Winston's Logger
Override this method in subclasses
### ###
beautify: null _loggerMethods: ['silly','debug','verbose','info','warn','error']
###
Logger instance
###
logger: null
###
Initialize and configure Logger
###
setupLogger: ->
winston = require('winston')
# Create Transport with Writable Stream
# See http://stackoverflow.com/a/21583831/2578205
stream = require('stream')
writable = new stream.Writable({
write: (chunk, encoding, next) ->
console.log(chunk.toString())
next()
})
transport = new (winston.transports.File)({
name: @name
level: 'warn'
timestamp: true
prettyPrint: true
colorize: true
stream: writable
json: false
})
# Initialize logger
@logger = new (winston.Logger)({
# Configure transports
transports: [
transport
]
})
# Merge logger methods into beautifier class
for method in @_loggerMethods
@[method] = @logger[method]
@verbose("Beautifier logger has been initialized.")
### ###
Constructor to setup beautifer Constructor to setup beautifer
### ###
constructor: () -> constructor: () ->
# Setup logger
@setupLogger()
# Handle global options # Handle global options
if @options._? if @options._?
globalOptions = @options._ globalOptions = @options._
@ -199,8 +243,8 @@ module.exports = class Beautifier
else if typeof options is "object" else if typeof options is "object"
@options[lang] = _.merge(globalOptions, options) @options[lang] = _.merge(globalOptions, options)
else else
console.warn("Unsupported options type #{typeof options} for language #{lang}: "+ options) @warn("Unsupported options type #{typeof options} for language #{lang}: "+ options)
# console.log("Options for #{@name}:",@options) @verbose("Options for #{@name}:", @options)
# Set supported languages # Set supported languages
@languages = _.keys(@options) @languages = _.keys(@options)

View File

@ -13,7 +13,7 @@ module.exports = class PHPCSFixer extends Beautifier
} }
beautify: (text, language, options) -> beautify: (text, language, options) ->
console.log('php-cs-fixer', options) @debug('php-cs-fixer', options)
@run("php-cs-fixer", [ @run("php-cs-fixer", [
"fix" "fix"
"--level=#{options.level}" if options.level "--level=#{options.level}" if options.level