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 1 commit
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
Prev Previous commit
Next Next commit
done?
  • Loading branch information
FinnDore committed May 5, 2023
commit 760614aa85d368dfb8c257ecfd597a708be05b47
22 changes: 12 additions & 10 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
#!/usr/bin/env node
import { installDependencies } from "./helpers/installDependencies.js";
import { getVersion } from "./utils/getT3Version.js";
import {
getNpmVersion,
renderVersionWarning,
} from "./utils/renderVersionWarning.js";
import fs from "fs-extra";
import path from "path";
import { type PackageJson } from "type-fest";
Expand All @@ -17,6 +11,12 @@ import { buildPkgInstallerMap } from "~/installers/index.js";
import { logger } from "~/utils/logger.js";
import { parseNameAndPath } from "~/utils/parseNameAndPath.js";
import { renderTitle } from "~/utils/renderTitle.js";
import { installDependencies } from "./helpers/installDependencies.js";
import { getVersion } from "./utils/getT3Version.js";
import {
getNpmVersion,
renderVersionWarning,
} from "./utils/renderVersionWarning.js";

type CT3APackageJSON = PackageJson & {
ct3aMetadata?: {
Expand Down 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
3 changes: 3 additions & 0 deletions cli/src/installers/dependencyVersionMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@ export const dependencyVersionMap = {
"@trpc/next": "^10.18.0",
"@tanstack/react-query": "^4.28.0",
superjson: "1.12.2",

// strictEslintAndPrettier
"@ianvs/prettier-plugin-sort-imports": "^3.7.2",
} as const;
export type AvailableDependencies = keyof typeof dependencyVersionMap;
2 changes: 1 addition & 1 deletion cli/src/installers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const buildPkgInstallerMap = (
installer: envVariablesInstaller,
},
strictEslintAndPrettier: {
inUse: true,
inUse: packages.includes("strictEslintAndPrettier"),
installer: strictEsLintAndPrettier,
},
});
6 changes: 3 additions & 3 deletions cli/src/installers/nextAuth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { type Installer } from "~/installers/index.js";
import { type AvailableDependencies } from "~/installers/dependencyVersionMap.js";
import path from "path";
import fs from "fs-extra";
import path from "path";
import { PKG_ROOT } from "~/consts.js";
import { type AvailableDependencies } from "~/installers/dependencyVersionMap.js";
import { type Installer } from "~/installers/index.js";
import { addPackageDependency } from "~/utils/addPackageDependency.js";

export const nextAuthInstaller: Installer = ({ projectDir, packages }) => {
Expand Down
89 changes: 28 additions & 61 deletions cli/src/installers/strictEslintAndPrettier.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,41 @@
import { type Installer } from "../installers/index.js";
import fs from "fs-extra";
import path from "path";
import { PKG_ROOT } from "~/consts";
import { type Installer } from "~/installers/index";

// import defaultESlintConfig from "~/tesmplate/base/_eslintrc.cjs";
// import { addPackageDependency } from "~/utils/addPackageDependency.js";
import { PKG_ROOT } from "~/consts.js";
import { addPackageDependency } from "~/utils/addPackageDependency.js";

export const strictEsLintAndPrettier: Installer = ({
projectDir,
packages,
}) => {
const usingTailwind = packages?.prisma.inUse;
console.log(defaultESlintConfig);
addPackageDependency({
projectDir,
dependencies: ["prettier", "@types/prettier"],
dependencies: [
"prettier",
"@types/prettier",
"@ianvs/prettier-plugin-sort-imports",
],
devMode: true,
});

const eslintExtra = path.join(PKG_ROOT, "template/extras/src/.eslintrc.js");
fs.copySync(eslintExtra, projectDir);
};
const configDir = path.join(PKG_ROOT, "template/extras/config");

// const getEnvContent = (usingAuth: boolean, usingPrisma: boolean) => {
// let content = `
// # When adding additional environment variables, the schema in "/src/env.mjs"
// # should be updated accordingly.
// `
// .trim()
// .concat("\n");
//
// if (usingPrisma)
// content += `
// # Prisma
// # https://www.prisma.io/docs/reference/database-reference/connection-urls#env
// DATABASE_URL="file:./db.sqlite"
// `;
//
// if (usingAuth)
// content += `
// # Next Auth
// # You can generate a new secret on the command line with:
// # openssl rand -base64 32
// # https://next-auth.js.org/configuration/options#secret
// # NEXTAUTH_SECRET=""
// NEXTAUTH_URL="http:https://localhost:3000"
//
// # Next Auth Discord Provider
// DISCORD_CLIENT_ID=""
// DISCORD_CLIENT_SECRET=""
// `;
//
// if (!usingAuth && !usingPrisma)
// content += `
// # Example:
// # SERVERVAR="foo"
// # NEXT_PUBLIC_CLIENTVAR="bar"
// `;
//
// return content;
// };
//
// const exampleEnvContent = `
// # Since the ".env" file is gitignored, you can use the ".env.example" file to
// # build a new ".env" file when you clone the repo. Keep this file up-to-date
// # when you add new variables to \`.env\`.
//
// # This file will be committed to version control, so make sure not to have any
// # secrets in it. If you are cloning this repo, create a copy of this file named
// # ".env" and populate it with your secrets.
// `
// .trim()
//
fs.rmSync(path.join(projectDir, "_eslintrc.cjs"));
fs.copySync(
path.join(configDir, "_strict-eslintrc.cjs"),
path.join(projectDir, "_eslintrc.cjs"),
);
Copy link
Member

Choose a reason for hiding this comment

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

imo we should have all or nothing, so if the installer isn't selected, no linter will be setup?


if (packages?.tailwind.inUse) {
// Include the congig with tailwind class ordering
fs.copySync(
path.join(configDir, "strict-prettier-with-tailwind.config.cjs"),
path.join(projectDir, "prettier.config.cjs"),
);
} else {
fs.copySync(
path.join(configDir, "prettier.config.cjs"),
path.join(projectDir, "prettier.config.cjs"),
);
}
};
13 changes: 10 additions & 3 deletions cli/src/installers/tailwind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { PKG_ROOT } from "~/consts.js";
import { type Installer } from "~/installers/index.js";
import { addPackageDependency } from "~/utils/addPackageDependency.js";

export const tailwindInstaller: Installer = ({ projectDir }) => {
export const tailwindInstaller: Installer = ({ projectDir, packages }) => {
addPackageDependency({
projectDir,
dependencies: [
Expand All @@ -26,7 +26,10 @@ 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 prettierSrc = path.join(
extrasDir,
"config/prettier-with-tailwind.config.cjs",
);
Copy link
Member

Choose a reason for hiding this comment

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

we can then remove prettier from here - keep all the prettier logic in the prettier-eslint installer.

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

const cssSrc = path.join(extrasDir, "src/styles/globals.css");
Expand All @@ -35,7 +38,11 @@ export const tailwindInstaller: Installer = ({ projectDir }) => {
fs.copySync(twCfgSrc, twCfgDest);
fs.copySync(postcssCfgSrc, postcssCfgDest);
fs.copySync(cssSrc, cssDest);
fs.copySync(prettierSrc, prettierDest);

// Let strictEslintAndPrettier install the prettier config
if (!packages?.strictEslintAndPrettier.inUse) {
fs.copySync(prettierSrc, prettierDest);
}

// Remove vanilla css file
const indexModuleCss = path.join(projectDir, "src/pages/index.module.css");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ const config = {
},
],
"@typescript-eslint/no-unused-vars": ["warn", { argsIgnorePattern: "^_" }],
"@typescript-eslint/no-misused-promises": [
2,
{
checksVoidReturn: {
attributes: false,
},
},
],
},
};

Expand Down
6 changes: 6 additions & 0 deletions cli/template/extras/config/prettier-with-tailwind.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("prettier-plugin-tailwindcss")],
};

module.exports = config;
5 changes: 1 addition & 4 deletions cli/template/extras/config/prettier.config.cjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/** @type {import("prettier").Config} */
const config = {
plugins: [
require.resolve("prettier-plugin-tailwindcss"),
require.resolve("@ianvs/prettier-plugin-sort-imports"),
],
plugins: [require.resolve("@ianvs/prettier-plugin-sort-import")],
};

module.exports = config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** @type {import("prettier").Config} */
const config = {
plugins: [
require.resolve("prettier-plugin-tailwindcss"),
require.resolve("@ianvs/prettier-plugin-sort-import"),
],
};

module.exports = config;