Open burhandodhy opened 7 years ago
I also noticed this watching phpcbf.js, I attempted to delete the argument.. still no luck, running phpcbf with --standard=WordPress path/to/file from the cmd works fine for me.. still couldn't find a solution to run that within atom editor..
Question to you that's unrelated: I am new to the atom editor therefore i would like to ask how did you get that output? where is the console panel that shows you that?
Here is my solution. Removed the 1 argument.
Replace this line let tempFile = tempWrite.sync(editor.getText());
with
let tempFile = atom.workspace.getActiveTextEditor().getPath();
@Vertigoooo Just open View > Developer > Toggle Developer Tools, or press Ctrl + Shift + I
@burhandodhy Hi, thanks for posting your solution.. still no luck for me.. using @Arcesilas tip i was able to debug the code when i get to the line
childProcess.execFile(phpcbf, args, (err, stdOut, stdErr) => {
I get the following error message saying childProcess is undefined (?)
Uncaught ReferenceError: childProcess is not defined
at eval (eval at evaluate (:85:21), <anonymous>:1:1)
at file:///C:/Users/-/.atom/packages/phpcbf/lib/phpcbf.js:55:7
at C:\Users\-\.atom\packages\phpcbf\node_modules\which\which.js:91:20
at FSReqWrap.oncomplete (fs.js:112:15)
here are all the imports at the beginning of the file:
'use babel';
import {CompositeDisposable} from 'atom';
import which from 'which';
import fs from 'fs';
import tempWrite from 'temp-write';
import childProcess from 'child_process';
@burhandodhy I don't understand your "solution". For me, the solution is simply to remove the argument --no-patch
from args list on line 54:
let args = [`--standard=${this.standard}`, tempFile];
@Arcesilas by removing the --no-patch. is it work for you ?
Whenever I run phpcbf it gives the "No error fix" message, even file have tons of standard errors.
I get the same message as you get. However, I think it is another bug, unrelated to this one.
I've successfully tried with spawn instead of execFile to write on stdin on phpcbf: Replace the entire "childProcess.execFile" block with this:
const cbf = childProcess.spawn(phpcbf, [`--standard=${this.standard}`]);
cbf.stderr.on('data', (data) => {
return atom.notifications.addError('phpcbf error: '+data);
});
cbf.stdout.on('data', (data) => {
editor.setText(data);
atom.notifications.addSuccess(`Reformatted to ${this.standard}.`);
});
cbf.stdin.write(editor.getText());
cbf.stdin.end();
I'm completely new to atom package development, so this piece of code may not be perfect, but it works for me.
This is the equivalent to:
cat current_file.php | phpcbf --standard=PSR2 -
The final -
tells phpcbf to read the input on stdin.
The problem does not seem to be from this package. Running this in terminal (bash, not in Atom):
phpcbf --standard=PSR2 /tmp/e0083687-4d33-45f5-a3a5-528f26f216b1
(where /tmp/e0083687-4d33-45f5-a3a5-528f26f216b1
is the tempFile generated by the package)
returns
No fixable errors were found
Time: 30ms; Memory: 4Mb
Whereas running:
cat /tmp/e0083687-4d33-45f5-a3a5-528f26f216b1 | phpcbf --standard=PSR2 -
fixes the CS errors & warnings and displays the fixed source code on the output...
The bug does not come from phpcbf itself, since running it on the original file fixes the errors and warning. The last dependency involved is temp-write node package, which may be buggy or may not correctly write the temporary file (could be an encoding issue?)
Actually, it seems that phpcbf refuses to fix files which do not have .php
extension... And temp files do not have any extension at all...
Alternatively, you could use this:
import crypto from 'crypto';
// ...
let hash = crypto.createHash('sha1').update(editor.buffer.file.path).digest('hex');
let tempFile = tempWrite.sync(editor.getText(), `${hash}.php`);
let args = [`--standard=${this.standard}`, tempFile];
temp-write allows us to specify a filepath. Unfortunately, when passing a filepath, temp-write will reate a temporary directory and create a name with that filepath. Some packages for temporary files manipulation allow to pass a suffix... It's not the case with temp-write and it's not my call to decide to use another dependency. So I've decided to hash the current filepath, to avoid "collisions", even if temporary files should not be manipulated manually. Feel free to no use crypto and not hash the filepath:
let tempFile = tempWrite.sync(editor.getText(), editor.buffer.file.getBaseName());
@Arcesilas Hey thanks for sharing with us.. I got a bit lost with your explanations.. can you please add your full phpcbf.js file so we can view ?
Maybe you should open a fork for this project fixing the issues.. offering a solution that work's since beautifier doesn't seem to work with phpcbf for me..
https://gist.github.com/Arcesilas/28e08aa09031676ed417b42955600de0
There are 3 possible versions:
As I said, the problem is that phpcbf does not work on files without .php extension. Both of the "tempfile solutions" use .php suffixed files and the third one does not use files at all.
I've already forked the repo, I may propose a pull request, but I'd like @jcartledge to give his opinion about the solutions proposed. Or, I may fork he package and propose my own one.
@Arcesilas Hello again, thank you for sharing with me your solutions.. I have went over and copied the three of them into my phpcbf.js file and made sure to reload atom everytime and still no luck.. :)
I started debugging the first two gists your published but then again code exists on the line with the 'which' expression..
tried the last version with the stdin then again you get phpcbf not defind.. changed it to a string (since it should be the name of the file to call), https://nodejs.org/api/child_process.html here you can see that it represents a string.. I used this.executablePath which is the absolute path to the phpcbf.phar file but if you have it in your path probably just phpcbf will be enough..
then i received an error from the stdin : Error: This socket has been ended by the other party
and there i got stuck..
eventually nothing worked.. i attempted to come up with a solution but i lack the knowledge of the atom api and overall developing packages for atom..
My idea is since the cli tool is working properly just with phpcbf --standard=WordPress path/to/file just to save the file and execute the command directly to the path of that file.. since phpcbf doesn't print out the output of the fixed code it changes the file and prints out results for example:
D:\Projects\Websites\-\wp-content\themes\asd-child>phpcbf --standard=WordPress functions.php
PHPCBF RESULT SUMMARY
----------------------------------------------------------------------
FILE FIXED REMAINING
----------------------------------------------------------------------
...-\wp-content\themes\asd-child\functions.php FAILED TO FIX
----------------------------------------------------------------------
A TOTAL OF 10 ERRORS WERE FIXED IN 1 FILE
----------------------------------------------------------------------
PHPCBF FAILED TO FIX 1 FILE
----------------------------------------------------------------------
Time: 498ms; Memory: 12Mb
and that did the job but i wouldn't like to go through all this process in the cmd just to reformat my code..
I am a C# developer and hence my habit is to reformat code with Ctrl+E, Ctrl+D from (visual studio) and so i created a shortcut for my convenience and i find it more practical then running the command in the prompt everytime:
in keymaps file add:
'atom-text-editor':
'ctrl-e ctrl-d': 'phpcbf:reformat'
Hi,
Sorry for the delay, I'm not at my home now and finally have some time to answer.
If you get an error saying that phpcbf is undefined, then it's not related to my solutions (which need phpcnf to be working).
I don't know what the error can be, but I think it's not related to this package.
@Arcesilas Hi, I eventually managed to used beautify to run phpcbf.. this package is too broken and doesn't function..
I recommend switching to atom-beautify here's a link describing my issue with beautify and the solution
Hi,
This package is not compatible with PHPCS 3.0 or greater and gives this error every time.