Skip to content

Commit 0ceb19f

Browse files
committed
fix(cli): enhance eslint support and improve plugin handling
- Added support for `@typescript-eslint/parser` in ESLint plugin template. - Introduced `tsconfigRootDir` in ESLint configuration for better TypeScript integration. - Replaced `GlobalProviders` with `lazyInject` in plugin loader to optimize plugin initialization. - Added logging for dependency alterations in `EslintInitHook`. - Improved Yarn Berry handling with error catch in `set version berry` command.
1 parent e0fffc6 commit 0ceb19f

File tree

4 files changed

+36
-26
lines changed

4 files changed

+36
-26
lines changed

packages/cli-core/src/packageManagers/supports/YarnBerryManager.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export class YarnBerryManager extends BaseManager {
3535
});
3636

3737
// then switch to berry
38-
this.cliExeca.runSync(this.cmd, ["set", "version", "berry"]);
38+
try {
39+
this.cliExeca.runSync(this.cmd, ["set", "version", "berry"]);
40+
} catch (er) {}
3941
}
4042

4143
add(deps: string[], options: ManagerCmdOpts) {

packages/cli-core/src/utils/loadPlugins.ts

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {GlobalProviders, injector, logger} from "@tsed/di";
1+
import {injector, lazyInject, logger} from "@tsed/di";
22
import chalk from "chalk";
33
import figures from "figures";
44

@@ -19,29 +19,32 @@ export async function loadPlugins() {
1919
.filter((mod) => mod.startsWith(`@${name}/cli-plugin`) || mod.includes(`${name}-cli-plugin`))
2020
.map(async (mod) => {
2121
try {
22-
const {default: plugin} = await fs.importModule(mod, rootDir);
23-
24-
if (!$inj.has(plugin)) {
25-
const provider = GlobalProviders.get(plugin)?.clone();
26-
27-
if (provider?.imports.length) {
28-
await all(
29-
provider.imports.map(async (token: any) => {
30-
$inj.add(token, GlobalProviders.get(token)?.clone());
31-
32-
if ($inj.settings.get("loaded")) {
33-
await $inj.invoke(token);
34-
}
35-
})
36-
);
37-
}
38-
39-
$inj.add(plugin, provider);
40-
41-
if ($inj.settings.get("loaded")) {
42-
await $inj.invoke(plugin);
43-
}
22+
if ($inj.settings.get("loaded")) {
23+
logger().info("Try to load ", mod);
24+
await lazyInject(() => fs.importModule(mod, rootDir));
4425
}
26+
27+
// if (!$inj.has(plugin)) {
28+
// const provider = GlobalProviders.get(plugin)?.clone();
29+
//
30+
// if (provider?.imports.length) {
31+
// await all(
32+
// provider.imports.map(async (token: any) => {
33+
// $inj.add(token, GlobalProviders.get(token)?.clone());
34+
//
35+
// if ($inj.settings.get("loaded")) {
36+
// await $inj.invoke(token);
37+
// }
38+
// })
39+
// );
40+
// }
41+
//
42+
// $inj.add(plugin, provider);
43+
//
44+
// if ($inj.settings.get("loaded")) {
45+
// await $inj.invoke(plugin);
46+
// }
47+
// }
4548
logger().info(chalk.green(figures.tick), mod, "module loaded");
4649
} catch (er) {
4750
logger().warn(chalk.red(figures.cross), "Fail to load plugin", mod);

packages/cli-plugin-eslint/src/hooks/EslintInitHook.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {type CliCommandHooks, type InitCmdContext, render, type RenderDataContext} from "@tsed/cli";
22
import {PackageManagersModule, ProjectPackageJson, type Task} from "@tsed/cli-core";
3-
import {inject, injectable} from "@tsed/di";
3+
import {inject, injectable, logger} from "@tsed/di";
4+
import chalk from "chalk";
45

56
import {TEMPLATE_DIR} from "../utils/templateDir.js";
67

@@ -33,6 +34,8 @@ export class EslintInitHook implements CliCommandHooks {
3334
}
3435

3536
$alterPackageJson(packageJson: ProjectPackageJson, data: RenderDataContext): ProjectPackageJson | Promise<ProjectPackageJson> {
37+
logger().info("Alter package.json dependencies by eslint plugin");
38+
3639
packageJson.addScripts({
3740
"test:lint": "eslint",
3841
"test:lint:fix": "eslint --fix"

packages/cli-plugin-eslint/src/templates/eslint.template.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default defineTemplate({
2727
render(_, data: RenderDataContext) {
2828
const imports = [
2929
'import typescriptEslint from "@typescript-eslint/eslint-plugin";',
30+
'import typescriptParser from "@typescript-eslint/parser";',
3031
data.prettier ? 'import pluginPrettierRecommended from "eslint-plugin-prettier/recommended";' : "",
3132
'import pluginSimpleImportSort from "eslint-plugin-simple-import-sort";',
3233
data.vitest ? 'import vitest from "eslint-plugin-vitest";' : "",
@@ -47,7 +48,8 @@ export default [
4748
parser: typescriptParser,
4849
parserOptions: {
4950
ecmaVersion: "latest",
50-
sourceType: "module"
51+
sourceType: "module",
52+
tsconfigRootDir: join(import.meta.dirname, "tsconfig.eslint.json")
5153
},
5254
globals: {
5355
...globals.node

0 commit comments

Comments
 (0)