Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the [change log](CHANGELOG.md) for changes and road map.
## Features

- Prettifies JavaScript or TypeScript files
- Uses [prettier](https://github.com/jlongster/prettier) node module
- Uses [prettier](https://github.com/jlongster/prettier) node module (uses local version or falls back to plugin version)
- Reads the standard [prettier configuration file](https://prettier.io/docs/en/configuration.html)

### Prettify
Expand Down
2 changes: 0 additions & 2 deletions src/Commands/CommandRegistration.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Operations;
Expand Down
31 changes: 30 additions & 1 deletion src/NodeProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,15 @@ public async Task<string> ExecuteProcessAsync(string input, Encoding encoding, s
if (!await EnsurePackageInstalledAsync())
return null;

string command = $"/c \"\"{_executable}\" --stdin-filepath \"{filePath}\" --stdin\"";
string executable = FindPrettierExecutable(filePath);
if (executable == null)
{
Logger.Log("No local prettier found. Falling back to plugin version");

executable = _executable;
}

string command = $"/c \"\"{executable}\" --stdin-filepath \"{filePath}\" --stdin\"";

var start = new ProcessStartInfo("cmd", command)
{
Expand Down Expand Up @@ -126,6 +134,27 @@ public async Task<string> ExecuteProcessAsync(string input, Encoding encoding, s
}
}

private string FindPrettierExecutable(string filePath)
{
string currentDir = filePath;

while ((currentDir = Path.GetDirectoryName(currentDir)) != null)
{
if (File.Exists(Path.Combine(currentDir, "package.json")))
{
string executable = Path.Combine(currentDir, @"node_modules\.bin\prettier.cmd");
if (File.Exists(executable))
{
Logger.Log($"Using prettier from {executable}");
return executable;
}
}
// If not, move a level up and try again.
}

return null;
}

private static void ModifyPathVariable(ProcessStartInfo start)
{
string path = start.EnvironmentVariables["PATH"];
Expand Down