Merge pull request #1010 from sharifzadesina/master

Add .php_cs file support to php-cs-fixer beautifier
This commit is contained in:
Glavin Wiechert 2016-06-09 13:12:17 -03:00
commit 66be231b95
3 changed files with 34 additions and 12 deletions

View File

@ -1,10 +1,11 @@
Promise = require("bluebird") Promise = require('bluebird')
_ = require('lodash') _ = require('lodash')
fs = require("fs") fs = require('fs')
temp = require("temp").track() temp = require('temp').track()
readFile = Promise.promisify(fs.readFile) readFile = Promise.promisify(fs.readFile)
which = require('which') which = require('which')
spawn = require('child_process').spawn spawn = require('child_process').spawn
path = require('path')
module.exports = class Beautifier module.exports = class Beautifier
@ -27,7 +28,6 @@ module.exports = class Beautifier
- <string:language>:<string:option_key>:<string:rename> - <string:language>:<string:option_key>:<string:rename>
- <string:language>:<string:option_key>:<function:transform> - <string:language>:<string:option_key>:<function:transform>
- <string:language>:<string:option_key>:<array:mapper> - <string:language>:<string:option_key>:<array:mapper>
### ###
options: {} options: {}
@ -79,6 +79,24 @@ module.exports = class Beautifier
return readFile(filePath, "utf8") return readFile(filePath, "utf8")
) )
###
Find file
###
findFile: (startDir, fileNames) ->
throw new Error "Specify file names to find." unless arguments.length
unless fileNames instanceof Array
fileNames = [fileNames]
startDir = startDir.split(path.sep)
while startDir.length
currentDir = startDir.join(path.sep)
for fileName in fileNames
filePath = path.join(currentDir, fileName)
try
fs.accessSync(filePath, fs.R_OK)
return filePath
startDir.pop()
return null
### ###
If platform is Windows If platform is Windows
### ###

View File

@ -2,6 +2,7 @@
Beautifier = require('./beautifier') Beautifier = require('./beautifier')
module.exports = class MarkoBeautifier extends Beautifier module.exports = class MarkoBeautifier extends Beautifier
name: 'Marko Beautifier' name: 'Marko Beautifier'
options: options:
@ -22,7 +23,7 @@ module.exports = class MarkoBeautifier extends Beautifier
prettyprintOptions = prettyprintOptions =
syntax : options.syntax syntax : options.syntax
filename: if context.filePath then context.filePath else require.resolve('marko-prettyprint') filename: if context? and context.filePath? then context.filePath else require.resolve('marko-prettyprint')
indent: indent indent: indent
try try

View File

@ -4,19 +4,21 @@ Requires https://github.com/FriendsOfPHP/PHP-CS-Fixer
"use strict" "use strict"
Beautifier = require('./beautifier') Beautifier = require('./beautifier')
path = require('path')
module.exports = class PHPCSFixer extends Beautifier module.exports = class PHPCSFixer extends Beautifier
name: "PHP-CS-Fixer"
options: { name: 'PHP-CS-Fixer'
options:
PHP: true PHP: true
}
beautify: (text, language, options) -> beautify: (text, language, options, context) ->
@debug('php-cs-fixer', options) @debug('php-cs-fixer', options)
isWin = @isWindows configFile = if context? and context.filePath? then @findFile(path.dirname(context.filePath), '.php_cs')
if isWin
if @isWindows
# Find php-cs-fixer.phar script # Find php-cs-fixer.phar script
@Promise.all([ @Promise.all([
@which(options.cs_fixer_path) if options.cs_fixer_path @which(options.cs_fixer_path) if options.cs_fixer_path
@ -24,7 +26,6 @@ module.exports = class PHPCSFixer extends Beautifier
]).then((paths) => ]).then((paths) =>
@debug('php-cs-fixer paths', paths) @debug('php-cs-fixer paths', paths)
_ = require 'lodash' _ = require 'lodash'
path = require 'path'
# Get first valid, absolute path # Get first valid, absolute path
phpCSFixerPath = _.find(paths, (p) -> p and path.isAbsolute(p) ) phpCSFixerPath = _.find(paths, (p) -> p and path.isAbsolute(p) )
@verbose('phpCSFixerPath', phpCSFixerPath) @verbose('phpCSFixerPath', phpCSFixerPath)
@ -37,6 +38,7 @@ module.exports = class PHPCSFixer extends Beautifier
"fix" "fix"
"--level=#{options.level}" if options.level "--level=#{options.level}" if options.level
"--fixers=#{options.fixers}" if options.fixers "--fixers=#{options.fixers}" if options.fixers
"--config-file=#{configFile}" if configFile
tempFile = @tempFile("temp", text) tempFile = @tempFile("temp", text)
], { ], {
ignoreReturnCode: true ignoreReturnCode: true
@ -64,6 +66,7 @@ module.exports = class PHPCSFixer extends Beautifier
"fix" "fix"
"--level=#{options.level}" if options.level "--level=#{options.level}" if options.level
"--fixers=#{options.fixers}" if options.fixers "--fixers=#{options.fixers}" if options.fixers
"--config-file=#{configFile}" if configFile
tempFile = @tempFile("temp", text) tempFile = @tempFile("temp", text)
], { ], {
ignoreReturnCode: true ignoreReturnCode: true