soderlind / vscode-phpcbf

PHP Code Beautifier and Fixer for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=persoderlind.vscode-phpcbf
GNU General Public License v3.0
28 stars 10 forks source link

Refactor to do process with cwd and env #12

Closed WraithKenny closed 6 years ago

WraithKenny commented 6 years ago

I was experimenting, and looking at the http://github.com/Microsoft/TSLint repo, and found that passing a config object to ('child_process').spawn() or ('child_process').exec() works well:

let folder = vscode.workspace.getWorkspaceFolder(editor.document.uri);
let folderPath = folder.uri.fsPath;
const childprocess: ChildProcess = exec(
  `./vendor/bin/phpcbf -i`,
  {
    cwd: folderPath,
    env: process.env
  },
  (error, stdout, stderr) => {
    if (error) {
      console.error(`exec error: ${error}`);
      vscode.window.showErrorMessage(`exec error: ${error}`);
      return;
    }
    if (stdout) console.log(`stdout: ${stdout}`);
    if (stderr) console.log(`stderr: ${stderr}`);
  }
);
const childprocess2: ChildProcess = spawn( `./vendor/bin/phpcbf`, ['-i'], {
  cwd: folderPath,
  env: process.env
} );

This would allow running the executablePath without normalizing it (as seen above). It might also negate any issues with finding standards that might be easier to discover locally, i.e., installed via composer into the same folder and registered with dealerdirect/phpcodesniffer-composer-installer

soderlind commented 6 years ago

I know, but I read, some place, that for longer running processes one should use .spawn()