-
-
Notifications
You must be signed in to change notification settings - Fork 96
Fallback to global instance of prettier before falling back to a bundled one #397
Fallback to global instance of prettier before falling back to a bundled one #397
Conversation
Codecov Report
@@ Coverage Diff @@
## master #397 +/- ##
==========================================
+ Coverage 78.6% 79.07% +0.47%
==========================================
Files 27 28 +1
Lines 486 497 +11
Branches 53 55 +2
==========================================
+ Hits 382 393 +11
Misses 88 88
Partials 16 16
Continue to review full report at Codecov.
|
I also added printing Had to re-commit four times because |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Roger that, good idea with the CI script.
I had a question about whether this works with Yarn globally-installed packages?
Also, can you use the conventional-changelog format for your commit message? Without this, the auto-generated changelog won't pick up your changes properly.
@@ -22,6 +23,24 @@ test("returns user's project's local prettier instance if it exists", () => { | |||
); | |||
}); | |||
|
|||
test("returns global prettier if user's project has no local prettier package", () => { | |||
atomLinter.findCached.mockImplementation(() => undefined); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems to be dead code as you're overriding your mock on line 32?
test("returns global prettier if user's project has no local prettier package", () => { | ||
atomLinter.findCached.mockImplementation(() => undefined); | ||
const filePath = path.join(__dirname, 'sourceFile.js'); | ||
const file = { path: filePath, getPath: () => filePath }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we only need getPath
to be defined? Well, doesn't matter I guess
src/helpers/getPrettierInstance.js
Outdated
@@ -4,11 +4,13 @@ const bundledPrettier = require('prettier'); | |||
const { getCurrentFilePath } = require('../editorInterface'); | |||
const { findCachedFromFilePath } = require('./general'); | |||
const path = require('path'); | |||
const { findCached } = require('atom-linter'); | |||
const globalModules = require('global-modules'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work for globally installed packages in Yarn? The repo makes it sound like it only works for npm? We should really support both if possible.
const filePath = path.join(__dirname, 'sourceFile.js'); | ||
const file = { path: filePath, getPath: () => filePath }; | ||
const editor = createMockTextEditor({ buffer: { file } }); | ||
const prettierLib = path.join(__dirname, '..', '..', 'tests', 'fixtures', 'prettier.js'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm worried that this is a bit confusing because you're using a local prettier instance as a fake prettier instance to use when there is no local prettier instance (it's even confusing to say in English). I think if just renamed it a bit it would be more clear, something like fakeGloballyInstalledPrettier
?
mind adding a line in |
@olsonpm which line? UPD: I think I got it. 5 mins. |
just to be clear, I was just hoping for the version of the user's global prettier if found. If not, I would like that to be explicit as well e.g. "global prettier version: <not found>" |
@olsonpm I'm printing prettier's path in debug info now. Could you please help me with passing editor and thus |
If you have prettier's path, then wouldn't you just const packageJsonPath = path.join(pathToGlobalPrettier, 'package.json')
const globalPrettierVersion = require(packageJsonPath).version Not sure what you're asking |
…rn) before falling back to a bundled one
Got you. Now finally going to sleep 😅 |
@robwise if the PR looks OK to you now after refactoring, it'd really help if you could merge it and release a new version. Can't wait to start using prettier-plugin-elm inside inside markdown files with colleagues! 😄 After Atom's package supports global prettier, I'll just need to finish one PR for prettier and that'll tick all the missing boxes for us 🎉 |
LGTM! |
This does not find a globally installed I just wanted to give you folks a heads up. |
Thanks for reporting, I will make an issue anyway just in case someone is looking for this info: #448 |
The PR partially addresses #395
The extension now searches for a globally installed prettier if a local one is not found; it falls back to a a bundled instance only if both attempts have failed. This change makes it possible to use Prettier plugins when someone does not want to have any npm modules locally. Please note that Prettier still has issues with detecting plugins in global installs, but I hope it gets fixed soon. IMO it's OK to merge this PR before that happens and simply
npm i -g prettier
once the upstream fix arrives.cc @olsonpm