Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add import order sorting via prettier #1392

Merged
merged 26 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ jobs:
tailwind: ["true", "false"]
nextAuth: ["true", "false"]
prisma: ["true", "false"]
eslint: ["true", "false"]
prettier: ["true", "false"]

name: "Build and Start T3 App ${{ matrix.trpc }}-${{ matrix.tailwind }}-${{ matrix.nextAuth }}-${{ matrix.prisma }}"
steps:
Expand Down
10 changes: 6 additions & 4 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ const main = async () => {
}

// Rename _eslintrc.json to .eslintrc.json - we use _eslintrc.json to avoid conflicts with the monorepos linter
fs.renameSync(
path.join(projectDir, "_eslintrc.cjs"),
path.join(projectDir, ".eslintrc.cjs"),
);
if (fs.existsSync(path.join(projectDir, "_eslintrc.cjs"))) {
fs.renameSync(
path.join(projectDir, "_eslintrc.cjs"),
path.join(projectDir, ".eslintrc.cjs"),
);
}

if (!noGit) {
await initializeGit(projectDir);
Expand Down
12 changes: 9 additions & 3 deletions cli/src/installers/dependencyVersionMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ export const dependencyVersionMap = {
tailwindcss: "^3.3.0",
autoprefixer: "^10.4.14",
postcss: "^8.4.21",
prettier: "^2.8.6",
"prettier-plugin-tailwindcss": "^0.2.6",
"@types/prettier": "^2.7.2",

// tRPC
"@trpc/client": "^10.18.0",
Expand All @@ -26,5 +23,14 @@ export const dependencyVersionMap = {
"@trpc/next": "^10.18.0",
"@tanstack/react-query": "^4.28.0",
superjson: "1.12.2",

// Prettier
prettier: "^2.8.6",
"prettier-plugin-tailwindcss": "^0.2.6",
"@types/prettier": "^2.7.2",
"@ianvs/prettier-plugin-sort-imports": "^3.7.2",

// Eslint
"eslint-config-next": "^13.4.1",
} as const;
export type AvailableDependencies = keyof typeof dependencyVersionMap;
23 changes: 23 additions & 0 deletions cli/src/installers/eslint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { type Installer } from "./index.js";
import fs from "fs-extra";
import path from "path";
import { PKG_ROOT } from "~/consts.js";
import { addPackageDependency } from "~/utils/addPackageDependency.js";

export const eslint: Installer = ({ projectDir }) => {
addPackageDependency({
dependencies: ["eslint-config-next"],
devMode: true,
projectDir,
});

const configDir = path.join(PKG_ROOT, "template/extras/config");

// Remove the default config
fs.rmSync(path.join(projectDir, "_eslintrc.cjs"));

fs.copySync(
path.join(configDir, "_eslintrc.cjs"),
path.join(projectDir, ".eslintrc.cjs"),
);
};
12 changes: 12 additions & 0 deletions cli/src/installers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { eslint } from "./eslint.js";
import { prettier } from "./prettier.js";
import { envVariablesInstaller } from "~/installers/envVars.js";
import { nextAuthInstaller } from "~/installers/nextAuth.js";
import { prismaInstaller } from "~/installers/prisma.js";
Expand All @@ -13,6 +15,8 @@ export const availablePackages = [
"tailwind",
"trpc",
"envVariables",
"eslint",
"prettier",
] as const;
export type AvailablePackages = (typeof availablePackages)[number];

Expand Down Expand Up @@ -56,4 +60,12 @@ export const buildPkgInstallerMap = (
inUse: true,
installer: envVariablesInstaller,
},
prettier: {
inUse: packages.includes("prettier"),
installer: prettier,
},
eslint: {
inUse: packages.includes("eslint"),
installer: eslint,
},
});
39 changes: 39 additions & 0 deletions cli/src/installers/prettier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { type Installer } from "../installers/index.js";
import { type AvailableDependencies } from "./dependencyVersionMap.js";
import fs from "fs-extra";
import path from "path";
import { PKG_ROOT } from "~/consts.js";
import { addPackageDependency } from "~/utils/addPackageDependency.js";

export const prettier: Installer = ({ projectDir, packages }) => {
const packeagesToInstall: AvailableDependencies[] = [
"prettier",
"@types/prettier",
"@ianvs/prettier-plugin-sort-imports",
];

if (packages?.tailwind.inUse) {
packeagesToInstall.push("prettier-plugin-tailwindcss");
}

addPackageDependency({
projectDir,
dependencies: packeagesToInstall,
devMode: true,
});

if (packages?.tailwind.inUse) {
fs.copySync(
path.join(
PKG_ROOT,
"template/extras/config/prettier-with-tailwind.config.cjs",
),
path.join(projectDir, "prettier.config.cjs"),
);
} else {
fs.copySync(
path.join(PKG_ROOT, "template/extras/config/_prettier.config.cjs"),
path.join(projectDir, "prettier.config.cjs"),
);
}
};
13 changes: 1 addition & 12 deletions cli/src/installers/tailwind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@ import { addPackageDependency } from "~/utils/addPackageDependency.js";
export const tailwindInstaller: Installer = ({ projectDir }) => {
addPackageDependency({
projectDir,
dependencies: [
"tailwindcss",
"postcss",
"autoprefixer",
"prettier",
"prettier-plugin-tailwindcss",
"@types/prettier",
],
dependencies: ["tailwindcss", "postcss", "autoprefixer"],
devMode: true,
});

Expand All @@ -26,16 +19,12 @@ export const tailwindInstaller: Installer = ({ projectDir }) => {
const postcssCfgSrc = path.join(extrasDir, "config/postcss.config.cjs");
const postcssCfgDest = path.join(projectDir, "postcss.config.cjs");

const prettierSrc = path.join(extrasDir, "config/prettier.config.cjs");
const prettierDest = path.join(projectDir, "prettier.config.cjs");

const cssSrc = path.join(extrasDir, "src/styles/globals.css");
const cssDest = path.join(projectDir, "src/styles/globals.css");

fs.copySync(twCfgSrc, twCfgDest);
fs.copySync(postcssCfgSrc, postcssCfgDest);
fs.copySync(cssSrc, cssDest);
fs.copySync(prettierSrc, prettierDest);

// Remove vanilla css file
const indexModuleCss = path.join(projectDir, "src/pages/index.module.css");
Expand Down
22 changes: 0 additions & 22 deletions cli/template/base/_eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,11 @@ const path = require("path");

/** @type {import("eslint").Linter.Config} */
const config = {
overrides: [
{
extends: [
"plugin:@typescript-eslint/recommended-requiring-type-checking",
],
files: ["*.ts", "*.tsx"],
parserOptions: {
project: path.join(__dirname, "tsconfig.json"),
},
},
],
parser: "@typescript-eslint/parser",
parserOptions: {
project: path.join(__dirname, "tsconfig.json"),
},
plugins: ["@typescript-eslint"],
extends: ["next/core-web-vitals", "plugin:@typescript-eslint/recommended"],
rules: {
"@typescript-eslint/consistent-type-imports": [
"warn",
{
prefer: "type-imports",
fixStyle: "inline-type-imports",
},
],
"@typescript-eslint/no-unused-vars": ["warn", { argsIgnorePattern: "^_" }],
},
};

module.exports = config;
5 changes: 3 additions & 2 deletions cli/template/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
"lint": "next lint",
"format": "pnpm format:check --write",
FinnDore marked this conversation as resolved.
Show resolved Hide resolved
"format:check": "pnpm prettier --check --plugin-search-dir=. **/*.{cjs,mjs,ts,tsx,md,json} --ignore-path ../.gitignore --ignore-unknown --no-error-on-unmatched-pattern"
Copy link
Member

Choose a reason for hiding this comment

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

same for these scripts

},
"dependencies": {
"@t3-oss/env-nextjs": "^0.2.1",
Expand All @@ -23,7 +25,6 @@
"@typescript-eslint/eslint-plugin": "^5.56.0",
"@typescript-eslint/parser": "^5.56.0",
"eslint": "^8.36.0",
"eslint-config-next": "^13.4.1",
"typescript": "^5.0.2"
}
}
43 changes: 43 additions & 0 deletions cli/template/extras/config/_eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require("path");

/** @type {import("eslint").Linter.Config} */
const config = {
overrides: [
{
extends: [
"plugin:@typescript-eslint/recommended-requiring-type-checking",
],
files: ["*.ts", "*.tsx"],
parserOptions: {
project: path.join(__dirname, "tsconfig.json"),
},
},
],
parser: "@typescript-eslint/parser",
parserOptions: {
project: path.join(__dirname, "tsconfig.json"),
},
plugins: ["@typescript-eslint"],
extends: ["next/core-web-vitals", "plugin:@typescript-eslint/recommended"],
rules: {
"@typescript-eslint/consistent-type-imports": [
"warn",
{
prefer: "type-imports",
fixStyle: "inline-type-imports",
},
],
"@typescript-eslint/no-unused-vars": ["warn", { argsIgnorePattern: "^_" }],
"@typescript-eslint/no-misused-promises": [
2,
{
checksVoidReturn: {
attributes: false,
},
},
],
},
};

module.exports = config;
6 changes: 6 additions & 0 deletions cli/template/extras/config/_prettier.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import("prettier").Config} */
const config = {
plugins: [require.resolve("@ianvs/prettier-plugin-sort-import")],
juliusmarminge marked this conversation as resolved.
Show resolved Hide resolved
juliusmarminge marked this conversation as resolved.
Show resolved Hide resolved
};

module.exports = config;
1 change: 1 addition & 0 deletions www/.prettierrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const config = {
plugins: [
...baseConfig.plugins,
require.resolve("prettier-plugin-astro"),
require.resolve("@ianvs/prettier-plugin-sort-imports"),
require.resolve("prettier-plugin-tailwindcss"), // MUST come last
],
pluginSearchDirs: false,
Expand Down