From aa00043818b094f43e31cc2db5e81bcf0623cd5e Mon Sep 17 00:00:00 2001 From: Sina Sharifzade Date: Thu, 9 Jun 2016 17:55:41 +0430 Subject: [PATCH] Add .php_cs file support to php-cs-fixer beautifier --- src/beautifiers/beautifier.coffee | 26 ++++++++++++++++++++++---- src/beautifiers/php-cs-fixer.coffee | 12 ++++++++---- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/beautifiers/beautifier.coffee b/src/beautifiers/beautifier.coffee index b0fd696..be01137 100644 --- a/src/beautifiers/beautifier.coffee +++ b/src/beautifiers/beautifier.coffee @@ -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 - :: - :: - :: - ### 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 ### diff --git a/src/beautifiers/php-cs-fixer.coffee b/src/beautifiers/php-cs-fixer.coffee index 3161337..435c537 100644 --- a/src/beautifiers/php-cs-fixer.coffee +++ b/src/beautifiers/php-cs-fixer.coffee @@ -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