Skip to content

Commit

Permalink
Merge pull request #876 from maeldur/master
Browse files Browse the repository at this point in the history
prefer file local prettier
  • Loading branch information
octref authored Sep 25, 2018
2 parents 2eb99a4 + ebc39df commit 4e83011
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 2 deletions.
28 changes: 28 additions & 0 deletions ThirdPartyNotices.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,32 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

===

3. https://github.com/prettier/prettier-vscode

===

MIT License

Copyright (c) 2017 Esben Petersen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

===
3 changes: 3 additions & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"parse-gitignore": "^1.0.1",
"prettier": "^1.11.1",
"prettier-eslint": "^8.8.1",
"read-pkg-up": "^4.0.0",
"resolve": "^1.8.1",
"stylus": "^0.54.5",
"stylus-supremacy": "~2.12.0",
"typescript": "^3.0.3",
Expand All @@ -48,6 +50,7 @@
"@types/lodash": "^4.14.91",
"@types/mocha": "^5.2.5",
"@types/node": "^10.11.0",
"@types/resolve": "0.0.8",
"codecov": "^3.0.4",
"glob": "^7.1.2",
"mocha": "^5.2.0",
Expand Down
6 changes: 4 additions & 2 deletions server/src/utils/prettier/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { FormattingOptions, TextEdit, Range } from 'vscode-languageserver-types'
import { ParserOption, Prettier, PrettierConfig, PrettierVSCodeConfig, PrettierEslintFormat } from './prettier';
import { indentSection } from '../strings';

import { requireLocalPkg } from './requirePkg';

export function prettierify(
code: string,
filePath: string,
Expand All @@ -14,7 +16,7 @@ export function prettierify(
parser: ParserOption
): TextEdit[] {
try {
const prettier = require('prettier') as Prettier;
const prettier = requireLocalPkg(filePath, 'prettier') as Prettier;
const prettierOptions = getPrettierOptions(prettierVSCodeConfig, parser, filePath);

const prettierifiedCode = prettier.format(code, prettierOptions);
Expand All @@ -36,7 +38,7 @@ export function prettierEslintify(
parser: ParserOption
): TextEdit[] {
try {
const prettierEslint = require('prettier-eslint') as PrettierEslintFormat;
const prettierEslint = requireLocalPkg(filePath, 'prettier-eslint') as PrettierEslintFormat;
const prettierOptions = getPrettierOptions(prettierVSCodeConfig, parser, filePath);

const prettierifiedCode = prettierEslint({
Expand Down
55 changes: 55 additions & 0 deletions server/src/utils/prettier/requirePkg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Adapted from
* https://github.com/prettier/prettier-vscode/blob/1843acb5d/src/requirePkg.ts
*/
import * as path from 'path';
import * as resolve from 'resolve';
const readPkgUp = require('read-pkg-up');

/**
* Recursively search for a package.json upwards containing given package
* as a dependency or devDependency.
* @param {string} fspath file system path to start searching from
* @param {string} pkgName package's name to search for
* @returns {string} resolved path to prettier
*/
function findPkg(fspath: string, pkgName: string): string | undefined {
const res = readPkgUp.sync({ cwd: fspath, normalize: false });
const { root } = path.parse(fspath);
if (
res.pkg &&
((res.pkg.dependencies && res.pkg.dependencies[pkgName]) ||
(res.pkg.devDependencies && res.pkg.devDependencies[pkgName]))
) {
return resolve.sync(pkgName, { basedir: res.path });
} else if (res.path) {
const parent = path.resolve(path.dirname(res.path), '..');
if (parent !== root) {
return findPkg(parent, pkgName);
}
}
return;
}

/**
* Require package explicitely installed relative to given path.
* Fallback to bundled one if no pacakge was found bottom up.
* @param {string} fspath file system path starting point to resolve package
* @param {string} pkgName package's name to require
* @returns module
*/
function requireLocalPkg(fspath: string, pkgName: string): any {
const modulePath = findPkg(fspath, pkgName);
if (modulePath !== void 0) {
try {
return require(modulePath);
} catch (e) {
console.log(
`Failed to load ${pkgName} from ${modulePath}. Using bundled`
);
}
}

return require(pkgName);
}
export { requireLocalPkg };
16 changes: 16 additions & 0 deletions server/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@
version "10.11.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.11.0.tgz#ddd0d67a3b6c3810dd1a59e36675fa82de5e19ae"

"@types/resolve@0.0.8":
version "0.0.8"
resolved "https://registry.yarnpkg.com/@types/resolve/-/resolve-0.0.8.tgz#f26074d238e02659e323ce1a13d041eee280e194"
dependencies:
"@types/node" "*"

"@types/semver@^5.4.0":
version "5.5.0"
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-5.5.0.tgz#146c2a29ee7d3bae4bf2fcb274636e264c813c45"
Expand Down Expand Up @@ -2473,6 +2479,10 @@ path-key@^2.0.0, path-key@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/path-key/-/path-key-2.0.1.tgz#411cadb574c5a140d3a4b1910d40d80cc9f40b40"

path-parse@^1.0.5:
version "1.0.6"
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.6.tgz#d62dbb5679405d72c4737ec58600e9ddcf06d24c"

path-type@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/path-type/-/path-type-1.1.0.tgz#59c44f7ee491da704da415da5a4070ba4f8fe441"
Expand Down Expand Up @@ -2767,6 +2777,12 @@ resolve-url@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/resolve-url/-/resolve-url-0.2.1.tgz#2c637fe77c893afd2a663fe21aa9080068e2052a"

resolve@^1.8.1:
version "1.8.1"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.8.1.tgz#82f1ec19a423ac1fbd080b0bab06ba36e84a7a26"
dependencies:
path-parse "^1.0.5"

restore-cursor@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/restore-cursor/-/restore-cursor-2.0.0.tgz#9f7ee287f82fd326d4fd162923d62129eee0dfaf"
Expand Down

0 comments on commit 4e83011

Please sign in to comment.