Skip to content

Commit

Permalink
Fix #2460
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyo930021 committed Dec 8, 2020
1 parent e46f29c commit 6f17207
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
35 changes: 28 additions & 7 deletions server/src/services/dependencyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,24 @@ export interface DependencyService {
workspacePath: string;
init(workspacePath: string, useWorkspaceDependencies: boolean, tsSDKPath?: string): Promise<void>;
get<L extends keyof RuntimeLibrary>(lib: L, filePath?: string): Dependency<RuntimeLibrary[L]>;
getBundled<L extends keyof RuntimeLibrary>(lib: L): Dependency<RuntimeLibrary[L]>;
}

export const createDependencyService = () => {
let useWorkspaceDeps: boolean;
let rootPath: string;
let loaded: { [K in keyof RuntimeLibrary]: Dependency<RuntimeLibrary[K]>[] };

const bundledModules = {
typescript: ts,
prettier: prettier,
'@starptech/prettyhtml': prettyHTML,
'prettier-eslint': prettierEslint,
'prettier-tslint': prettierTslint,
'stylus-supremacy': stylusSupremacy,
'@prettier/plugin-pug': prettierPluginPug
};

async function init(workspacePath: string, useWorkspaceDependencies: boolean, tsSDKPath?: string) {
const nodeModulesPaths = useWorkspaceDependencies ? await createNodeModulesPaths(workspacePath) : [];

Expand Down Expand Up @@ -200,12 +211,12 @@ export const createDependencyService = () => {
rootPath = workspacePath;
loaded = {
typescript: await loadTypeScript(),
prettier: await loadCommonDep('prettier', prettier),
'@starptech/prettyhtml': await loadCommonDep('@starptech/prettyhtml', prettyHTML),
'prettier-eslint': await loadCommonDep('prettier-eslint', prettierEslint),
'prettier-tslint': await loadCommonDep('prettier-tslint', prettierTslint),
'stylus-supremacy': await loadCommonDep('stylus-supremacy', stylusSupremacy),
'@prettier/plugin-pug': await loadCommonDep('@prettier/plugin-pug', prettierPluginPug)
prettier: await loadCommonDep('prettier', bundledModules['prettier']),
'@starptech/prettyhtml': await loadCommonDep('@starptech/prettyhtml', bundledModules['@starptech/prettyhtml']),
'prettier-eslint': await loadCommonDep('prettier-eslint', bundledModules['prettier-eslint']),
'prettier-tslint': await loadCommonDep('prettier-tslint', bundledModules['prettier-tslint']),
'stylus-supremacy': await loadCommonDep('stylus-supremacy', bundledModules['stylus-supremacy']),
'@prettier/plugin-pug': await loadCommonDep('@prettier/plugin-pug', bundledModules['@prettier/plugin-pug'])
};
}

Expand Down Expand Up @@ -235,6 +246,15 @@ export const createDependencyService = () => {
return result ?? deps[0];
};

const getBundled = <L extends keyof RuntimeLibrary>(lib: L): Dependency<RuntimeLibrary[L]> => {
return {
dir: '',
version: '',
bundled: true,
module: bundledModules[lib]
};
};

return {
get useWorkspaceDependencies() {
return useWorkspaceDeps;
Expand All @@ -243,6 +263,7 @@ export const createDependencyService = () => {
return rootPath;
},
init,
get
get,
getBundled
};
};
5 changes: 4 additions & 1 deletion server/src/utils/prettier/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ export function prettierPluginPugify(
initialIndent: boolean
): TextEdit[] {
try {
const prettier = dependencyService.get('prettier', fileFsPath).module;
let prettier = dependencyService.get('prettier', fileFsPath).module;
if (prettier.version.startsWith('1')) {
prettier = dependencyService.getBundled('prettier').module;
}
const prettierPluginPug = dependencyService.get('@prettier/plugin-pug', fileFsPath).module;
const prettierOptions = getPrettierOptions(dependencyService, prettier, fileFsPath, parser, vlsFormatConfig);
prettierOptions.pluginSearchDirs = [];
Expand Down

1 comment on commit 6f17207

@fisker
Copy link

@fisker fisker commented on 6f17207 Dec 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a Prettier maintainer, happened to see this when checking Prettier related issues.

Why are we doing this? I think this may lead to unexpected result, for example we changed trailingComma to es5 in v2, you have a project using Prettier v1, then you got the format result from v2. This will be very confusing, and hard to debug. Why don't we just say this Prettier version is not supportted?

Please sign in to comment.