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: override default config files #1091

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,11 @@ module.exports = {
"error",
{ argsIgnorePattern: "^_", destructuredArrayIgnorePattern: "^_" },
],
"prettier/prettier": [
"error",
{
endOfLine: "auto",
},
],
},
};
15 changes: 15 additions & 0 deletions cli/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ interface CliFlags {
trpc: boolean /** @internal - used in CI */;
prisma: boolean /** @internal - used in CI */;
nextAuth: boolean /** @internal - used in CI */;
twConfig: undefined | string;
tsConfig: undefined | string;
}

interface CliResults {
Expand All @@ -38,6 +40,8 @@ const defaultOptions: CliResults = {
trpc: false,
prisma: false,
nextAuth: false,
twConfig: undefined,
tsConfig: undefined,
},
};

Expand Down Expand Up @@ -69,6 +73,17 @@ export const runCli = async () => {
"Bypass the CLI and use all default options to bootstrap a new t3-app",
false,
)
.option(
"--twConfig [path]",
"Path to a custom tailwind.congif.cjs file",
(value) =>
value.match(/(tailwind\.config)\.(js|cjs)/) ? value : undefined,
)
.option(
"--tsConfig [path]",
"Path to a custom tsconfig.json file",
(value) => (value.includes("tsconfig.json") ? value : undefined),
)
/** START CI-FLAGS */
/**
* @experimental - used for CI E2E tests
Expand Down
13 changes: 13 additions & 0 deletions cli/src/helpers/createProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ import { installPackages } from "~/helpers/installPackages.js";
import { scaffoldProject } from "~/helpers/scaffoldProject.js";
import { selectAppFile, selectIndexFile } from "~/helpers/selectBoilerplate.js";
import { getUserPkgManager } from "~/utils/getUserPkgManager.js";
import { overrideConfigs } from "~/helpers/overrideConfigs.js";

interface CreateProjectOptions {
projectName: string;
packages: PkgInstallerMap;
noInstall: boolean;
twConfig?: string | undefined;
tsConfig?: string | undefined;
}

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

// Overriding default configuration files with the selected boilerplate
overrideConfigs({
tsConfig,
twConfig,
projectDir,
packages,
});

// 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
49 changes: 49 additions & 0 deletions cli/src/helpers/overrideConfigs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import ora from "ora";
import { PkgInstallerMap } from "~/installers/index.js";
import fs from "fs-extra";
import path from "path";

type overrideConfigOptions = {
twConfig?: string | undefined;
tsConfig?: string | undefined;
packages?: PkgInstallerMap;
projectDir: string;
};

export const overrideConfigs = ({
twConfig,
tsConfig,
packages,
projectDir,
}: overrideConfigOptions) => {
if (twConfig !== undefined || tsConfig !== undefined) {
const spinner = ora("Config overrides detected").start();
spinner.info("Config overrides detected");
if (twConfig) {
if (fs.existsSync(twConfig) && packages?.tailwind?.inUse) {
spinner.info(`Overriding default tailwind.config.cjs with ${twConfig}`);
fs.unlinkSync(path.join(projectDir, "tailwind.config.cjs"));
fs.copyFileSync(twConfig, path.join(projectDir, "tailwind.config.cjs"));
spinner.info("successfully copied tailwind config");
} else if (!packages?.tailwind?.inUse) {
spinner.info("Tailwind not in use, skipping override");
} else {
spinner.fail(`Could not find ${twConfig}`);
}
}
if (tsConfig) {
if (fs.existsSync(tsConfig)) {
spinner.info(`Overriding default tsconfig.json with ${tsConfig}`);
fs.unlinkSync(path.join(projectDir, "tsconfig.json"));
fs.copyFileSync(tsConfig, path.join(projectDir, "tsconfig.json"));
spinner.info("successfully copied typescript config");
} else {
spinner.fail(`Could not find ${tsConfig}`);
}
}
spinner.warn(
"WARNING: create-t3-app does not take responsibility for any issues caused by overriding default configs",
);
spinner.succeed("Config overrides complete");
}
};
4 changes: 3 additions & 1 deletion cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const main = async () => {
const {
appName,
packages,
flags: { noGit, noInstall },
flags: { noGit, noInstall, twConfig, tsConfig },
} = await runCli();

const usePackages = buildPkgInstallerMap(packages);
Expand All @@ -43,6 +43,8 @@ const main = async () => {
projectName: appDir,
packages: usePackages,
noInstall,
twConfig,
tsConfig,
});

// Write name to package.json
Expand Down
14 changes: 8 additions & 6 deletions www/src/pages/en/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ After your app has been scaffolded, check out the [first steps](/en/usage/first-

## Advanced usage

| Option/Flag | Description |
| ----------------- | ----------------------------------------------------------------------- |
| `[dir]` | Include a directory argument with a name for the project |
| `--noGit` | Explicitly tell the CLI to not initialize a new git repo in the project |
| `-y`, `--default` | Bypass the CLI and bootstrap a new t3-app with all options selected |
| `--noInstall` | Generate project without installing dependencies |
| Option/Flag | Description |
| ------------------- | ----------------------------------------------------------------------- |
| `[dir]` | Include a directory argument with a name for the project |
| `--noGit` | Explicitly tell the CLI to not initialize a new git repo in the project |
| `-y`, `--default` | Bypass the CLI and bootstrap a new t3-app with all options selected |
| `--noInstall` | Generate project without installing dependencies |
| `--tsConfig [path]` | Override default typescript config file. File path must be absolute. |
| `--twConfig [path]` | Override default tailwind config file. File path must be absolute. |

## Experimental usage

Expand Down