Skip to content

Commit

Permalink
Use user workspace prettier. daidodo/format-imports-vscode#70
Browse files Browse the repository at this point in the history
  • Loading branch information
daidodo committed Apr 6, 2022
1 parent 2120afc commit dfd2865
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
29 changes: 28 additions & 1 deletion src/lib/config/prettier.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pt from 'prettier';
import prettier from 'prettier';

import findUp from '@dozerg/find-up';

import { logger } from '../common';
import { type Configuration } from './types';
Expand All @@ -7,6 +9,7 @@ import { type Configuration } from './types';
export function loadPretConfig(fileName: string): Configuration {
const log = logger('format-imports.loadPretConfig');
log.debug('Loading Prettier/EditorConfig config for fileName:', fileName);
const pt = findPrettier(fileName);
log.debug('Prettier API version:', pt.version);
try {
const config = pt.resolveConfig.sync(fileName, { useCache: false, editorconfig: true });
Expand Down Expand Up @@ -44,3 +47,27 @@ export function loadPretConfig(fileName: string): Configuration {
return {};
}
}

function findPrettier(fromPath: string) {
const log = logger('format-imports.findPrettier');
const userPrettier = findUserPrettier(fromPath);
if (userPrettier) return userPrettier;
log.warn('Cannot find prettier module from path:', fromPath, 'so use pre-packed');
return prettier;
}

declare const __webpack_require__: typeof require;
declare const __non_webpack_require__: typeof require;
const req = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require;

function findUserPrettier(fromPath: string) {
const log = logger('format-imports.findUserPrettier');
const [prettierPath] = findUp.sync('node_modules/prettier', {
cwd: fromPath,
stopAtLimit: 1,
type: 'directory',
});
if (!prettierPath) return undefined;
log.debug('Found prettier in', prettierPath);
return req(prettierPath);
}
10 changes: 5 additions & 5 deletions src/lib/format/config/eslint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,18 @@ async function loadESLintConfig(fileName: string, configFile?: string) {
}
}

declare const __webpack_require__: typeof require;
declare const __non_webpack_require__: typeof require;
const req = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require;

function findESLint(fromPath: string) {
const log = logger('format-imports.findESLint');
const userESLint = findUserESLint(fromPath);
if (userESLint) return userESLint;
log.warn('Cannot find eslint module from path:', fromPath, ', use pre-packed');
log.warn('Cannot find eslint module from path:', fromPath, 'so use pre-packed');
return ESLint;
}

declare const __webpack_require__: typeof require;
declare const __non_webpack_require__: typeof require;
const req = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require;

function findUserESLint(fromPath: string) {
const log = logger('format-imports.findUserESLint');
const [eslintPath] = findUp.sync('node_modules/eslint', {
Expand Down

0 comments on commit dfd2865

Please sign in to comment.