Skip to content

Commit 8b1b85e

Browse files
committed
refactor: update all plugins according to the new CLI api
1 parent 80135ba commit 8b1b85e

File tree

188 files changed

+4069
-2178
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

188 files changed

+4069
-2178
lines changed

commitlint.config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
export default {extends: ["@commitlint/config-conventional"]};
1+
export default {
2+
extends: ["@commitlint/config-conventional"],
3+
rules: {
4+
"header-max-length": [2, "always", 200]
5+
}
6+
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"handlebars": "^4.7.8",
6262
"handlebars-helpers": "^0.10.0",
6363
"inquirer": "^9.3.7",
64-
"inquirer-autocomplete-prompt": "^3.0.1",
64+
"inquirer-autocomplete-prompt": "3.0.1",
6565
"js-yaml": "^4.1.0",
6666
"lerna": "^8.1.8",
6767
"listr2": "^8.2.4",
@@ -82,6 +82,7 @@
8282
"@tsed/markdown-it-symbols": "3.20.8",
8383
"@tsed/monorepo-utils": "2.3.12",
8484
"@tsed/ts-doc": "^4.1.0",
85+
"@types/inquirer-autocomplete-prompt": "3.0.3",
8586
"@types/node": "22.7.4",
8687
"@typescript-eslint/eslint-plugin": "8.7.0",
8788
"@typescript-eslint/parser": "8.7.0",
Lines changed: 90 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,53 @@
1-
import {type InitCmdContext, RootRendererService} from "@tsed/cli";
2-
import {inject, Injectable, OnExec, OnPostInstall, PackageManagersModule, ProjectPackageJson} from "@tsed/cli-core";
1+
import {type CliCommandHooks, type InitCmdContext, render, type RenderDataContext, type RenderOptions} from "@tsed/cli";
2+
import {PackageManagersModule, ProjectPackageJson, type Task} from "@tsed/cli-core";
3+
import {inject, injectable} from "@tsed/di";
34

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

6-
@Injectable()
7-
export class EslintInitHook {
8-
protected packageJson = inject(ProjectPackageJson);
9-
protected packageManagers = inject(PackageManagersModule);
10-
protected rootRenderer = inject(RootRendererService);
11-
12-
@OnExec("init")
13-
onExec(ctx: InitCmdContext) {
14-
if (!ctx.eslint) {
15-
return [];
7+
export class EslintInitHook implements CliCommandHooks {
8+
$alterRenderFiles(
9+
files: (string | RenderOptions)[],
10+
data: RenderDataContext
11+
): (string | RenderOptions)[] | Promise<(string | RenderOptions)[]> {
12+
if (!data.eslint) {
13+
return files;
1614
}
1715

1816
return [
19-
{
20-
title: "Generate files for eslint",
21-
task: (ctx: any) => {
22-
return this.rootRenderer.renderAll(
23-
[
24-
"eslint.config.mjs.hbs",
25-
ctx.lintstaged && ".husky/_/.gitignore.hbs",
26-
ctx.lintstaged && ".husky/_/husky.sh.hbs",
27-
ctx.lintstaged && ".husky/.gitignore.hbs",
28-
ctx.lintstaged && ".husky/post-commit.hbs",
29-
ctx.lintstaged && ".husky/pre-commit.hbs",
30-
ctx.lintstaged && ".lintstagedrc.json.hbs",
31-
ctx.prettier && ".prettierignore.hbs",
32-
ctx.prettier && ".prettierrc.hbs"
33-
],
34-
ctx,
35-
{
36-
templateDir: `${TEMPLATE_DIR}/init`
37-
}
38-
);
39-
}
40-
},
41-
{
42-
title: "Add dependencies",
43-
task: () => {
44-
this.addScripts(ctx);
45-
this.addDependencies(ctx);
46-
this.addDevDependencies(ctx);
47-
}
48-
}
49-
];
17+
...files,
18+
...[
19+
data.lintstaged && ".husky/_/.gitignore",
20+
data.lintstaged && ".husky/_/husky.sh",
21+
data.lintstaged && ".husky/.gitignore",
22+
data.lintstaged && ".husky/post-commit",
23+
data.lintstaged && ".husky/pre-commit",
24+
data.lintstaged && ".lintstagedrc.json",
25+
data.prettier && ".prettierignore",
26+
data.prettier && ".prettierrc"
27+
]
28+
.filter(Boolean)
29+
.map((path) => {
30+
return {
31+
path,
32+
templateDir: `${TEMPLATE_DIR}/init`
33+
};
34+
})
35+
] as (string | RenderOptions)[];
5036
}
5137

52-
@OnPostInstall("init")
53-
onPostInstall(ctx: InitCmdContext) {
54-
return [
55-
{
56-
title: "Add husky prepare task",
57-
skip: !ctx.lintstaged,
58-
task: async () => {
59-
this.packageJson
60-
.refresh()
61-
.addScripts({
62-
prepare: "is-ci || husky install"
63-
})
64-
.write();
65-
66-
await this.packageManagers.runScript("prepare");
67-
}
68-
},
69-
{
70-
title: "Run linter",
71-
task: () => {
72-
return this.packageManagers.runScript("test:lint:fix", {
73-
ignoreError: true
74-
});
75-
}
76-
}
77-
];
78-
}
79-
80-
addScripts(ctx: InitCmdContext) {
81-
this.packageJson.addScripts({
38+
$alterPackageJson(packageJson: ProjectPackageJson, data: RenderDataContext): ProjectPackageJson | Promise<ProjectPackageJson> {
39+
packageJson.addScripts({
8240
"test:lint": "eslint",
8341
"test:lint:fix": "eslint --fix"
8442
});
8543

86-
if (ctx.prettier) {
87-
this.packageJson.addScripts({
44+
if (data.prettier) {
45+
packageJson.addScripts({
8846
prettier: "prettier '**/*.{json,md,yml,yaml}' --write"
8947
});
9048
}
91-
}
92-
93-
addDependencies(ctx: InitCmdContext) {
94-
this.packageJson.addDependencies({}, ctx);
95-
}
9649

97-
addDevDependencies(ctx: InitCmdContext) {
98-
this.packageJson.addDevDependencies(
50+
packageJson.addDevDependencies(
9951
{
10052
"@typescript-eslint/parser": "latest",
10153
"@typescript-eslint/eslint-plugin": "latest",
@@ -105,36 +57,82 @@ export class EslintInitHook {
10557
"eslint-plugin-simple-import-sort": "latest",
10658
globals: "latest"
10759
},
108-
ctx
60+
data
10961
);
11062

111-
if (ctx.lintstaged) {
112-
this.packageJson.addDevDependencies(
63+
if (data.lintstaged) {
64+
packageJson.addDevDependencies(
11365
{
11466
"is-ci": "latest",
11567
husky: "latest",
11668
"lint-staged": "latest"
11769
},
118-
ctx
70+
data
11971
);
12072
}
12173

122-
if (ctx.vitest) {
123-
this.packageJson.addDevDependencies(
74+
if (data.vitest) {
75+
packageJson.addDevDependencies(
12476
{
12577
"eslint-plugin-vitest": "latest"
12678
},
127-
ctx
79+
data
12880
);
12981
}
13082

131-
if (ctx.prettier) {
132-
this.packageJson.addDevDependencies(
83+
if (data.prettier) {
84+
packageJson.addDevDependencies(
13385
{
13486
prettier: "latest"
13587
},
136-
ctx
88+
data
13789
);
13890
}
91+
92+
return packageJson;
93+
}
94+
95+
$alterInitPostInstallTasks(tasks: Task[], data: InitCmdContext): Task[] | Promise<Task[]> {
96+
const packageJson = inject(ProjectPackageJson);
97+
const packageManagers = inject(PackageManagersModule);
98+
99+
return [
100+
...tasks,
101+
{
102+
title: "Add eslint configuration",
103+
task: () => {
104+
render("eslint.config", {
105+
...data,
106+
name: "eslint.config"
107+
});
108+
}
109+
},
110+
{
111+
title: "Add husky prepare task",
112+
skip: !data.lintstaged,
113+
task: async () => {
114+
packageJson
115+
.refresh()
116+
.addScripts({
117+
prepare: "is-ci || husky install"
118+
})
119+
.write();
120+
121+
return packageManagers.runScript("prepare", {
122+
ignoreError: true
123+
});
124+
}
125+
},
126+
{
127+
title: "Run linter",
128+
task: () => {
129+
return packageManagers.runScript("test:lint:fix", {
130+
ignoreError: true
131+
});
132+
}
133+
}
134+
];
139135
}
140136
}
137+
138+
injectable(EslintInitHook);
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import {CliPluginEslintModule} from "./CliPluginEslintModule.js";
1+
import "./templates/eslint.template.js";
22

3+
import {CliPluginEslintModule} from "./CliPluginEslintModule.js";
34
export * from "./utils/templateDir.js";
45

56
export default CliPluginEslintModule;

packages/cli-plugin-eslint/templates/init/eslint.config.mjs.hbs renamed to packages/cli-plugin-eslint/src/templates/eslint.template.ts

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,39 @@
1-
import typescriptEslint from "@typescript-eslint/eslint-plugin";
2-
import typescriptParser from "@typescript-eslint/parser";
3-
{{#if prettier}}import pluginPrettierRecommended from "eslint-plugin-prettier/recommended";
4-
{{/if}}import pluginSimpleImportSort from "eslint-plugin-simple-import-sort";
5-
{{#if vitest}}import vitest from "eslint-plugin-vitest";
6-
{{/if}}import globals from "globals";
1+
import {defineTemplate, type RenderDataContext} from "@tsed/cli";
2+
3+
function vitest() {
4+
return `
5+
{
6+
files: ["**/*.spec.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}"], // or any other pattern
7+
plugins: {
8+
vitest
9+
},
10+
rules: {
11+
...vitest.configs.recommended.rules, // you can also use vitest.configs.all.rules to enable all rules
12+
"vitest/consistent-test-it": ["error", {fn: "it", withinDescribe: "it"}],
13+
"vitest/no-alias-methods": "error"
14+
}
15+
},`;
16+
}
17+
18+
export default defineTemplate({
19+
id: "eslint.config",
20+
label: "ESLint configuration",
21+
hidden: true,
22+
fileName: "eslint.config",
23+
outputDir: ".",
24+
preserveCase: true,
25+
ext: "mjs",
26+
27+
render(_, data: RenderDataContext) {
28+
const imports = [
29+
'import typescriptEslint from "@typescript-eslint/eslint-plugin";',
30+
data.prettier ? 'import pluginPrettierRecommended from "eslint-plugin-prettier/recommended";' : "",
31+
'import pluginSimpleImportSort from "eslint-plugin-simple-import-sort";',
32+
data.vitest ? 'import vitest from "eslint-plugin-vitest";' : "",
33+
'import globals from "globals";'
34+
];
35+
36+
return `${imports.filter(Boolean).join("\n")}
737
838
export default [
939
{
@@ -36,18 +66,7 @@ export default [
3666
"@typescript-eslint/no-explicit-any": 0,
3767
"@typescript-eslint/no-non-null-assertion": 0
3868
}
39-
},{{#if vitest}}
40-
{
41-
files: ["**/*.spec.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}"], // or any other pattern
42-
plugins: {
43-
vitest
44-
},
45-
rules: {
46-
...vitest.configs.recommended.rules, // you can also use vitest.configs.all.rules to enable all rules
47-
"vitest/consistent-test-it": ["error", {fn: "it", withinDescribe: "it"}],
48-
"vitest/no-alias-methods": "error"
49-
}
50-
},{{/if}}
69+
},${data.vitest ? vitest() : ""}
5170
{
5271
files: ["**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}"],
5372
languageOptions: {
@@ -60,6 +79,13 @@ export default [
6079
"simple-import-sort/imports": "error",
6180
"simple-import-sort/exports": "error"
6281
}
63-
}{{#if prettier}},
64-
pluginPrettierRecommended{{/if}}
82+
}${
83+
data.prettier
84+
? `,
85+
pluginPrettierRecommended`
86+
: ""
87+
}
6588
];
89+
`;
90+
}
91+
});

packages/cli-plugin-eslint/templates/init/.husky/.gitignore.hbs renamed to packages/cli-plugin-eslint/templates/init/.husky/.gitignore

File renamed without changes.

packages/cli-plugin-eslint/templates/init/.husky/_/.gitignore.hbs renamed to packages/cli-plugin-eslint/templates/init/.husky/_/.gitignore

File renamed without changes.

packages/cli-plugin-eslint/templates/init/.husky/_/husky.sh.hbs renamed to packages/cli-plugin-eslint/templates/init/.husky/_/husky.sh

File renamed without changes.

packages/cli-plugin-eslint/templates/init/.husky/post-commit.hbs renamed to packages/cli-plugin-eslint/templates/init/.husky/post-commit

File renamed without changes.

packages/cli-plugin-eslint/templates/init/.husky/pre-commit.hbs renamed to packages/cli-plugin-eslint/templates/init/.husky/pre-commit

File renamed without changes.

0 commit comments

Comments
 (0)