Add .php_cs file support to php-cs-fixer beautifier

This commit is contained in:
Sina Sharifzade 2016-06-09 17:55:41 +04:30 committed by Sina Sharifzade
parent aae09ea463
commit aa00043818
2 changed files with 30 additions and 8 deletions

View File

@ -1,10 +1,11 @@
Promise = require("bluebird")
Promise = require('bluebird')
_ = require('lodash')
fs = require("fs")
temp = require("temp").track()
fs = require('fs')
temp = require('temp').track()
readFile = Promise.promisify(fs.readFile)
which = require('which')
spawn = require('child_process').spawn
path = require('path')
module.exports = class Beautifier
@ -27,7 +28,6 @@ module.exports = class Beautifier
- <string:language>:<string:option_key>:<string:rename>
- <string:language>:<string:option_key>:<function:transform>
- <string:language>:<string:option_key>:<array:mapper>
###
options: {}
@ -79,6 +79,24 @@ module.exports = class Beautifier
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
###

View File

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