Skip to content

Commit

Permalink
Automatically detect packageManager (#1646)
Browse files Browse the repository at this point in the history
* Deprecate the packageManager setting and automatically detect it with
the vscode command.

* Add warning messages for the `packageManager` setting

---------

Co-authored-by: William Wilkinson <will.wilkinson@carebridgehealth.com>
  • Loading branch information
wilkinson4 and William Wilkinson authored May 24, 2023
1 parent 6145416 commit 3b427a0
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion $shared/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,4 @@ export type ConfigurationSettings = {
nodePath: string | null;
workspaceFolder: WorkspaceFolder | undefined;
workingDirectory: ModeItem | DirectoryItem | undefined;
};
};
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ This extension contributes the following variables to the [settings](https://cod
"eslint.lintTask.options": "-c C:/mydirectory/.eslintrc.json --ignore-path C:/mydirectory/.eslintignore ."
}
```
- `eslint.packageManager`: controls the package manager to be used to resolve the ESLint library. This has only an influence if the ESLint library is resolved globally. Valid values are `"npm"` or `"yarn"` or `"pnpm"`.
- The old `eslint.packageManager` setting is now deprecated and can safely be removed. This controlled the package manager to be used to resolve the ESLint library. This has only an influence if the ESLint library is resolved globally. Valid values are `"npm"` or `"yarn"` or `"pnpm"`.
- `eslint.options`: options to configure how ESLint is started using either the [ESLint class API](http://eslint.org/docs/developer-guide/nodejs-api#eslint-class) or the [CLIEngine API](http://eslint.org/docs/developer-guide/nodejs-api#cliengine). The extension uses the ESLint class API if ESLint version 8 or higher is used or if ESLint version 7 is used and the setting `eslint.useESLintCLass` is set to true. In all other cases the CLIEngine API is used.
An example to point to a custom `.eslintrc.json` file using the new ESLint API is:
```json
Expand Down
17 changes: 14 additions & 3 deletions client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,13 @@ export namespace ESLintClient {
return {};
});

client.onRequest(NoESLintLibraryRequest.type, (params) => {
client.onRequest(NoESLintLibraryRequest.type, async (params) => {
const key = 'noESLintMessageShown';
const state = context.globalState.get<NoESLintState>(key, {});

const uri: Uri = Uri.parse(params.source.uri);
const workspaceFolder = Workspace.getWorkspaceFolder(uri);
const packageManager = Workspace.getConfiguration('eslint', uri).get('packageManager', 'npm');
const packageManager = await getPackageManager(uri);
const localInstall = {
npm: 'npm install eslint',
pnpm: 'pnpm install eslint',
Expand Down Expand Up @@ -582,6 +582,17 @@ export namespace ESLintClient {
return clientOptions;
}

async function getPackageManager(uri: Uri) {
const userProvidedPackageManager:PackageManagers = Workspace.getConfiguration('eslint', uri).get('packageManager', 'npm');
const detectedPackageMananger = await commands.executeCommand<PackageManagers>('npm.packageManager');

if (userProvidedPackageManager === detectedPackageMananger) {
return detectedPackageMananger;
}
client.warn(`Detected package manager(${detectedPackageMananger}) differs from the one in the deprecated packageManager setting(${userProvidedPackageManager}). We will honor this setting until it is removed.`, {}, true);
return userProvidedPackageManager;
}

async function readConfiguration(params: ConfigurationParams): Promise<(ConfigurationSettings | null)[]> {
if (params.items === undefined) {
return [];
Expand Down Expand Up @@ -934,4 +945,4 @@ export namespace ESLintClient {
}
}
}
}
}
2 changes: 1 addition & 1 deletion history/settings_1_9_x.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,4 @@ As with JavaScript validating TypeScript in a mono repository requires that you
{ "directory": "./client", "changeProcessCWD": true },
{ "directory": "./server", "changeProcessCWD": true }
]
```
```
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
"pnpm"
],
"default": "npm",
"description": "The package manager you use to install node modules."
"description": "The package manager you use to install node modules.",
"deprecationMessage": "The setting is deprecated. The Package Manager is automatically detected now."
},
"eslint.problems.shortenToSingleLine": {
"type": "boolean",
Expand Down
2 changes: 1 addition & 1 deletion server/src/eslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1368,4 +1368,4 @@ export namespace ESLint {
return Status.error;
}
}
}
}

0 comments on commit 3b427a0

Please sign in to comment.