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
Moved over to a seperate prompt
  • Loading branch information
FinnDore committed May 16, 2023
commit 02b64ded995d7f433f0e48df57acd3170a4272fd
33 changes: 31 additions & 2 deletions cli/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import chalk from "chalk";
import { Command } from "commander";
import inquirer from "inquirer";
import { CREATE_T3_APP, DEFAULT_APP_NAME } from "~/consts.js";
import { type AvailablePackages } from "~/installers/index.js";
import { availablePackages } from "~/installers/index.js";
import {
availablePackages,
type AvailablePackages,
} from "~/installers/index.js";
import { getVersion } from "~/utils/getT3Version.js";
import { getUserPkgManager } from "~/utils/getUserPkgManager.js";
import { logger } from "~/utils/logger.js";
Expand All @@ -15,6 +17,7 @@ interface CliFlags {
noInstall: boolean;
default: boolean;
importAlias: string;
prettierAndExtendedEslint: boolean;

/** @internal Used in CI. */
CI: boolean;
Expand Down Expand Up @@ -47,6 +50,7 @@ const defaultOptions: CliResults = {
prisma: false,
nextAuth: false,
importAlias: "~/",
prettierAndExtendedEslint: false,
},
};

Expand Down Expand Up @@ -114,6 +118,12 @@ export const runCli = async () => {
"Explicitly tell the CLI to use a custom import alias",
defaultOptions.flags.importAlias,
)
/** @experimental - Used for CI E2E tests. Used in conjunction with `--CI` to skip prompting. */
.option(
"-p, --prettier-and-extended-eslint",
"Explicitly tell the CLI to use a custom import alias",
defaultOptions.flags.prettierAndExtendedEslint,
)
/** END CI-FLAGS */
.version(getVersion(), "-v, --version", "Display the version number")
.addHelpText(
Expand Down Expand Up @@ -153,6 +163,9 @@ export const runCli = async () => {
if (cliResults.flags.tailwind) cliResults.packages.push("tailwind");
if (cliResults.flags.prisma) cliResults.packages.push("prisma");
if (cliResults.flags.nextAuth) cliResults.packages.push("nextAuth");
if (cliResults.flags.prettierAndExtendedEslint) {
cliResults.flags.prettierAndExtendedEslint = true;
}
}

// Explained below why this is in a try/catch block
Expand Down Expand Up @@ -186,6 +199,9 @@ export const runCli = async () => {
}

cliResults.flags.importAlias = await promptImportAlias();

cliResults.flags.prettierAndExtendedEslint =
await promtPrettierAndExtendedEslint();
}
} catch (err) {
// If the user is not calling create-t3-app from an interactive terminal, inquirer will throw an error with isTTYError = true
Expand Down Expand Up @@ -329,3 +345,16 @@ const promptImportAlias = async (): Promise<string> => {

return importAlias;
};

const promtPrettierAndExtendedEslint = async (): Promise<boolean> => {
const { prettierAndExtendedEslint } = await inquirer.prompt<
Pick<CliFlags, "prettierAndExtendedEslint">
>({
name: "prettierAndExtendedEslint",
type: "confirm",
message: "Would you like to use prettier and extended eslint conifg?",
default: defaultOptions.flags.prettierAndExtendedEslint,
});

return prettierAndExtendedEslint;
};
14 changes: 14 additions & 0 deletions cli/src/helpers/createProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ import path from "path";
import { installPackages } from "~/helpers/installPackages.js";
import { scaffoldProject } from "~/helpers/scaffoldProject.js";
import { selectAppFile, selectIndexFile } from "~/helpers/selectBoilerplate.js";
import { installExtendedEslint } from "~/installers/eslint.js";
import { type PkgInstallerMap } from "~/installers/index.js";
import { installPrettier } from "~/installers/prettier.js";
import { getUserPkgManager } from "~/utils/getUserPkgManager.js";

interface CreateProjectOptions {
projectName: string;
packages: PkgInstallerMap;
noInstall: boolean;
importAlias: string;
prettierAndExtendedEslint: boolean;
}

export const createProject = async ({
projectName,
packages,
noInstall,
prettierAndExtendedEslint,
}: CreateProjectOptions) => {
const pkgManager = getUserPkgManager();
const projectDir = path.resolve(process.cwd(), projectName);
Expand All @@ -36,6 +40,16 @@ export const createProject = async ({
noInstall,
});

if (prettierAndExtendedEslint) {
const installArgs = {
projectDir,
pkgManager,
noInstall,
};
installPrettier(installArgs);
installExtendedEslint(installArgs);
}

// TODO: Look into using handlebars or other templating engine to scaffold without needing to maintain multiple copies of the same file
selectAppFile({ projectDir, packages });
selectIndexFile({ projectDir, packages });
Expand Down
3 changes: 2 additions & 1 deletion cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const main = async () => {
const {
appName,
packages,
flags: { noGit, noInstall, importAlias },
flags: { noGit, noInstall, importAlias, prettierAndExtendedEslint },
} = await runCli();

const usePackages = buildPkgInstallerMap(packages);
Expand All @@ -45,6 +45,7 @@ const main = async () => {
packages: usePackages,
importAlias: importAlias,
noInstall,
prettierAndExtendedEslint,
});

// Write name to package.json
Expand Down
2 changes: 1 addition & 1 deletion cli/src/installers/eslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from "path";
import { PKG_ROOT } from "~/consts.js";
import { addPackageDependency } from "~/utils/addPackageDependency.js";

export const eslint: Installer = ({ projectDir }) => {
export const installExtendedEslint: Installer = ({ projectDir }) => {
addPackageDependency({
dependencies: ["eslint-config-next"],
devMode: true,
Expand Down
12 changes: 0 additions & 12 deletions cli/src/installers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
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 @@ -15,8 +13,6 @@ export const availablePackages = [
"tailwind",
"trpc",
"envVariables",
"eslint",
"prettier",
] as const;
export type AvailablePackages = (typeof availablePackages)[number];

Expand Down Expand Up @@ -60,12 +56,4 @@ export const buildPkgInstallerMap = (
inUse: true,
installer: envVariablesInstaller,
},
prettier: {
inUse: packages.includes("prettier"),
installer: prettier,
},
eslint: {
inUse: packages.includes("eslint"),
installer: eslint,
},
});
2 changes: 1 addition & 1 deletion cli/src/installers/prettier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { PKG_ROOT } from "~/consts.js";
import { addPackageDependency } from "~/utils/addPackageDependency.js";
import { addPackageScript } from "~/utils/addPackageScript.js";

export const prettier: Installer = ({ projectDir, packages }) => {
export const installPrettier: Installer = ({ projectDir, packages }) => {
const packeagesToInstall: AvailableDependencies[] = [
"prettier",
"@types/prettier",
Expand Down