Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Fallback to global instance of prettier before falling back to a bundled one #397

Merged
merged 2 commits into from
Mar 24, 2018
Merged

Fallback to global instance of prettier before falling back to a bundled one #397

merged 2 commits into from
Mar 24, 2018

Conversation

kachkaev
Copy link
Member

@kachkaev kachkaev commented Mar 22, 2018

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

@kachkaev kachkaev mentioned this pull request Mar 22, 2018
@codecov-io
Copy link

codecov-io commented Mar 22, 2018

Codecov Report

Merging #397 into master will increase coverage by 0.47%.
The diff coverage is 100%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
src/helpers/getPrettierInstance.js 100% <100%> (ø) ⬆️
src/helpers/getPrettierPath.js 100% <100%> (ø)
src/displayDebugInfo/index.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 08035ff...d5de657. Read the comment docs.

@kachkaev
Copy link
Member Author

kachkaev commented Mar 22, 2018

I also added printing git diff to the CI script, which should only run when dirty files are found. Hope this helps future contributors!

Had to re-commit four times because checkForDistChanges was failing and I could not figure out why. Tried not to exclude dist and even tweaked this folder manually before simply realising that the problem was with yarn.lock not being up-to-date 😅 My mistake was that I did not notice yarn.lock initially and thought you were using npm. Not a surprise that a new package in package.json made yarn.lock out of sync and was thus making this file dirty in CI.

Copy link
Collaborator

@robwise robwise left a 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);
Copy link
Collaborator

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 };
Copy link
Collaborator

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

@@ -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');
Copy link
Collaborator

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');
Copy link
Collaborator

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?

@olsonpm
Copy link
Contributor

olsonpm commented Mar 22, 2018

mind adding a line in displayDebugInfo/index.js so we have this available when helping users with bugs?

@kachkaev
Copy link
Member Author

kachkaev commented Mar 22, 2018

@olsonpm which line?

UPD: I think I got it. 5 mins.

@olsonpm
Copy link
Contributor

olsonpm commented Mar 22, 2018

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>"

@kachkaev
Copy link
Member Author

kachkaev commented Mar 22, 2018

@olsonpm I'm printing prettier's path in debug info now. Could you please help me with passing editor and thus filePath in there? It'd be great if you could commit a PR on top of mine – I'll be away for some time now.

@olsonpm
Copy link
Contributor

olsonpm commented Mar 22, 2018

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

@kachkaev
Copy link
Member Author

Got you. Now finally going to sleep 😅

@kachkaev
Copy link
Member Author

kachkaev commented Mar 23, 2018

@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 🎉

@robwise
Copy link
Collaborator

robwise commented Mar 24, 2018

LGTM!

@krugar
Copy link

krugar commented Aug 22, 2018

This does not find a globally installed prettier under linux when node is installed via nvm. Might be related to jonschlinkert/global-prefix#19, but I didn't really care to dig, as this does not hamper use of prettier-atom in any way (it falls back gracefully to the bundled prettier).

I just wanted to give you folks a heads up.

@robwise
Copy link
Collaborator

robwise commented Aug 23, 2018

Thanks for reporting, I will make an issue anyway just in case someone is looking for this info: #448

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants