Skip to content

Commit

Permalink
feat: override default config files
Browse files Browse the repository at this point in the history
simple config override options for cli

regex match tailwind file extension

add lint rule to remove invalid end of line linting error
  • Loading branch information
NathanPip committed Jan 16, 2023
1 parent a36a100 commit 9afc402
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 7 deletions.
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

0 comments on commit 9afc402

Please sign in to comment.