Skip to content

Commit 1d0bdfa

Browse files
feat: add tsconfig.build.json to cli
1 parent 9e3d0d1 commit 1d0bdfa

6 files changed

+58
-56
lines changed

src/cli/constants.ts

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import pico from "picocolors";
2-
3-
import type { FrameworkOption, PromItem } from "./types";
4-
51
export const vscodeSettingsString = `
2+
// Enable the ESlint flat config support
3+
"eslint.useFlatConfig": true,
4+
65
// Disable the default formatter, use eslint instead
76
"prettier.enable": false,
87
"editor.formatOnSave": false,
@@ -35,20 +34,11 @@ export const vscodeSettingsString = `
3534
"json",
3635
"jsonc",
3736
"yaml",
38-
"toml",
39-
"luau"
37+
"toml",
38+
"luau"
4039
]
4140
`;
4241

43-
export const frameworkOptions: Array<PromItem<FrameworkOption>> = [
44-
{
45-
label: pico.cyan("React"),
46-
value: "react",
47-
},
48-
];
49-
50-
export const frameworks: Array<FrameworkOption> = frameworkOptions.map(({ value }) => value);
51-
5242
export const dependenciesMap = {
5343
react: ["@eslint-react/eslint-plugin", "eslint-plugin-react-hooks"],
5444
} as const;

src/cli/run.ts

+5-24
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import path from "node:path";
55
import process from "node:process";
66
import pico from "picocolors";
77

8-
import { frameworkOptions, frameworks } from "./constants";
8+
import { addTsconfigBuild } from "./stages/add-tsconfig.build.json";
99
import { updateEslintFiles } from "./stages/update-eslint-files";
1010
import { updatePackageJson } from "./stages/update-package-json";
1111
import { updateVscodeSettings } from "./stages/update-vscode-settings";
12-
import type { FrameworkOption, PromItem, PromptResult } from "./types";
12+
import type { FrameworkOption, PromptResult } from "./types";
1313
import { isGitClean } from "./utils";
1414

1515
export interface CliRunOptions {
@@ -38,26 +38,6 @@ export async function run(options: CliRunOptions = {}): Promise<undefined> {
3838
if (!argumentSkipPrompt) {
3939
result = (await p.group(
4040
{
41-
frameworks: ({ results }) => {
42-
const isArgumentTemplateValid =
43-
typeof argumentTemplate === "string" &&
44-
!!frameworks.includes(<FrameworkOption>argumentTemplate);
45-
46-
if (!results.uncommittedConfirmed || isArgumentTemplateValid) {
47-
return;
48-
}
49-
50-
const message =
51-
!isArgumentTemplateValid && argumentTemplate
52-
? `"${argumentTemplate}" isn't a valid template. Please choose from below: `
53-
: "Select a framework:";
54-
55-
return p.multiselect<Array<PromItem<FrameworkOption>>, FrameworkOption>({
56-
message: pico.reset(message),
57-
options: frameworkOptions,
58-
required: false,
59-
});
60-
},
6141
uncommittedConfirmed: () => {
6242
if (argumentSkipPrompt || isGitClean()) {
6343
return Promise.resolve(true);
@@ -94,9 +74,10 @@ export async function run(options: CliRunOptions = {}): Promise<undefined> {
9474
}
9575
}
9676

97-
await updatePackageJson(result);
98-
await updateEslintFiles(result);
77+
await updatePackageJson();
78+
await updateEslintFiles();
9979
await updateVscodeSettings(result);
80+
await addTsconfigBuild();
10081

10182
p.log.success(pico.green(`Setup completed`));
10283
p.outro(`Now you can update the dependencies and run ${pico.blue("eslint . --fix")}\n`);
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as p from "@clack/prompts";
2+
3+
import fs from "node:fs";
4+
import fsp from "node:fs/promises";
5+
import path from "path";
6+
import pico from "picocolors";
7+
8+
export async function addTsconfigBuild(): Promise<void> {
9+
const cwd = process.cwd();
10+
11+
const pathTsconfigBuild = path.join(cwd, "tsconfig.build.json");
12+
13+
if (fs.existsSync(pathTsconfigBuild)) {
14+
p.log.info(pico.yellow("tsconfig.build.json already exists"));
15+
return;
16+
}
17+
18+
const tsconfigBuildContent = `{
19+
"extends": "./tsconfig.json",
20+
"include": ["src/**/*", "eslint.config.ts"],
21+
"exclude": ["node_modules"]
22+
}
23+
`;
24+
25+
await fsp.writeFile(pathTsconfigBuild, tsconfigBuildContent, "utf-8");
26+
p.log.success(pico.green(`Created tsconfig.build.json`));
27+
p.log.info(
28+
pico.yellow(
29+
`You must add '"exclude": ["./eslint.config.ts"]' to your tsconfig.json. In the future, this will be done automatically.`,
30+
),
31+
);
32+
}

src/cli/stages/update-eslint-files.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import process from "node:process";
99
import parse from "parse-gitignore";
1010
import pico from "picocolors";
1111

12-
import type { PromptResult } from "../types";
1312
import { getEslintConfigContent } from "../utils";
1413

15-
export async function updateEslintFiles(result: PromptResult): Promise<void> {
14+
export async function updateEslintFiles(): Promise<void> {
1615
const cwd = process.cwd();
1716
const pathESLintIgnore = path.join(cwd, ".eslintignore");
1817
const pathPackageJSON = path.join(cwd, "package.json");
@@ -45,9 +44,14 @@ export async function updateEslintFiles(result: PromptResult): Promise<void> {
4544
configLines.push(`ignores: ${JSON.stringify(eslintIgnores)},`);
4645
}
4746

48-
for (const framework of result.frameworks) {
49-
configLines.push(`${framework}: true,`);
50-
}
47+
configLines.push(
48+
`${"react"}: true,`,
49+
`typescript: {
50+
parserOptions: {
51+
project: "tsconfig.build.json",
52+
},
53+
},`,
54+
);
5155

5256
const mainConfig = configLines.map(index => ` ${index}`).join("\n");
5357
const additionalConfig: Array<string> = [];

src/cli/stages/update-package-json.ts

+4-12
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import process from "node:process";
66
import pico from "picocolors";
77

88
import { dependenciesMap, pkgJson as packageJson } from "../constants";
9-
import type { PromptResult } from "../types";
109

11-
export async function updatePackageJson(result: PromptResult): Promise<void> {
10+
export async function updatePackageJson(): Promise<void> {
1211
const cwd = process.cwd();
1312

1413
const pathPackageJSON = path.join(cwd, "package.json");
@@ -26,16 +25,9 @@ export async function updatePackageJson(result: PromptResult): Promise<void> {
2625

2726
const addedPackages: Array<string> = [];
2827

29-
for (const framework of result.frameworks) {
30-
const deps = dependenciesMap[framework];
31-
if (!deps) {
32-
continue;
33-
}
34-
35-
for (const dep of deps) {
36-
package_.devDependencies[dep] = packageJson.devDependencies[dep];
37-
addedPackages.push(dep);
38-
}
28+
for (const dep of dependenciesMap["react"]) {
29+
package_.devDependencies[dep] = packageJson.devDependencies[dep];
30+
addedPackages.push(dep);
3931
}
4032

4133
if (addedPackages.length) {

src/configs/typescript.ts

+3
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,11 @@ export async function typescript(
115115
languageOptions: {
116116
parser: parserTs,
117117
parserOptions: {
118+
ecmaVersion: 2018,
118119
extraFileExtensions: componentExtensions.map(extension => `.${extension}`),
120+
jsx: true,
119121
sourceType: "module",
122+
useJSXTextNode: true,
120123
...(tsconfigPath
121124
? {
122125
project: tsconfigPath,

0 commit comments

Comments
 (0)