See #390. Use global php-cs-fixer on Windows without running within php
By using [node-which](https://github.com/isaacs/node-which) I believe I am able to get rid of the `php` command running `php-cs-fixer` as an argument. `which` will find the `php-cs-fixer` program by looking through the `PATH` environment variable listed directories. This will resolve `php-cs-fixer` to an absolute path to the executable and that will be used to run the command. This dramatically cleans up the `PHP-CS-Fixer beautifier` code and specs, too! Doing some Windows tests now.
This commit is contained in:
parent
5094d76930
commit
590dbc805e
|
@ -123,7 +123,7 @@ describe "PHP-CS-Fixer Beautifier", ->
|
||||||
p.then(cb, cb)
|
p.then(cb, cb)
|
||||||
return p
|
return p
|
||||||
|
|
||||||
failWhichProgram('php')
|
# failWhichProgram('php')
|
||||||
failWhichProgram('php-cs-fixer')
|
failWhichProgram('php-cs-fixer')
|
||||||
|
|
||||||
unless isWindows
|
unless isWindows
|
||||||
|
|
|
@ -149,9 +149,13 @@ module.exports = class Beautifier
|
||||||
which: (exe, options = {}) ->
|
which: (exe, options = {}) ->
|
||||||
# Get PATH and other environment variables
|
# Get PATH and other environment variables
|
||||||
@getShellEnvironment()
|
@getShellEnvironment()
|
||||||
.then((env) ->
|
.then((env) =>
|
||||||
new Promise((resolve, reject) ->
|
new Promise((resolve, reject) =>
|
||||||
options.path ?= env.PATH
|
options.path ?= env.PATH
|
||||||
|
if @isWindows
|
||||||
|
# Trick node-which into including files
|
||||||
|
# with no extension as executables
|
||||||
|
options.pathExt ?= ";#{process.env.PATHEXT ? '.EXE'}"
|
||||||
which(exe, options, (err, path) ->
|
which(exe, options, (err, path) ->
|
||||||
resolve(exe) if err
|
resolve(exe) if err
|
||||||
resolve(path)
|
resolve(path)
|
||||||
|
|
|
@ -15,24 +15,7 @@ module.exports = class PHPCSFixer extends Beautifier
|
||||||
beautify: (text, language, options) ->
|
beautify: (text, language, options) ->
|
||||||
@debug('php-cs-fixer', options)
|
@debug('php-cs-fixer', options)
|
||||||
|
|
||||||
isWin = @isWindows
|
@run(options.cs_fixer_path or "php-cs-fixer", [
|
||||||
if isWin
|
|
||||||
# Find php-cs-fixer.phar script
|
|
||||||
@Promise.all([
|
|
||||||
@which(options.cs_fixer_path) if options.cs_fixer_path
|
|
||||||
@which('php-cs-fixer')
|
|
||||||
]).then((paths) =>
|
|
||||||
@debug('php-cs-fixer paths', paths)
|
|
||||||
_ = require('lodash')
|
|
||||||
# Get first valid, absolute path
|
|
||||||
phpCSFixerPath = _.find(paths, (p) -> p and p.charAt(0) is '/' )
|
|
||||||
@verbose('phpCSFixerPath', phpCSFixerPath)
|
|
||||||
@debug('phpCSFixerPath', phpCSFixerPath, paths)
|
|
||||||
# Check if PHP-CS-Fixer path was found
|
|
||||||
if phpCSFixerPath?
|
|
||||||
# Found PHP-CS-Fixer path
|
|
||||||
@run("php", [
|
|
||||||
phpCSFixerPath
|
|
||||||
"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
|
||||||
|
@ -40,34 +23,9 @@ module.exports = class PHPCSFixer extends Beautifier
|
||||||
], {
|
], {
|
||||||
ignoreReturnCode: true
|
ignoreReturnCode: true
|
||||||
help: {
|
help: {
|
||||||
link: "http://php.net/manual/en/install.php"
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.then(=>
|
|
||||||
@readFile(tempFile)
|
|
||||||
)
|
|
||||||
else
|
|
||||||
@verbose('php-cs-fixer not found!')
|
|
||||||
# Could not find PHP-CS-Fixer path
|
|
||||||
@Promise.reject(@commandNotFoundError(
|
|
||||||
'php-cs-fixer'
|
|
||||||
{
|
|
||||||
link: "https://github.com/FriendsOfPHP/PHP-CS-Fixer"
|
link: "https://github.com/FriendsOfPHP/PHP-CS-Fixer"
|
||||||
program: "php-cs-fixer.phar"
|
program: "php-cs-fixer"
|
||||||
pathOption: "PHP - CS Fixer Path"
|
pathOption: "PHP - CS Fixer Path"
|
||||||
})
|
|
||||||
)
|
|
||||||
)
|
|
||||||
else
|
|
||||||
@run("php-cs-fixer", [
|
|
||||||
"fix"
|
|
||||||
"--level=#{options.level}" if options.level
|
|
||||||
"--fixers=#{options.fixers}" if options.fixers
|
|
||||||
tempFile = @tempFile("temp", text)
|
|
||||||
], {
|
|
||||||
ignoreReturnCode: true
|
|
||||||
help: {
|
|
||||||
link: "https://github.com/FriendsOfPHP/PHP-CS-Fixer"
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(=>
|
.then(=>
|
||||||
|
|
Loading…
Reference in New Issue