Skip to content

Commit

Permalink
more flags
Browse files Browse the repository at this point in the history
  • Loading branch information
OrJDev committed Nov 13, 2022
1 parent 709d50e commit 47d3596
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 45 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-jd-app",
"version": "1.0.78",
"version": "1.0.79",
"main": "dist/index.js",
"scripts": {
"test": "ts-node -r tsconfig-paths/register src",
Expand Down
58 changes: 31 additions & 27 deletions src/helpers/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ export const installPkgs = async (
}
};

export async function getCtxWithInstallers(ctx: IAppCtx): Promise<ICtx> {
export async function getCtxWithInstallers(
ctx: IAppCtx,
curr: string[]
): Promise<ICtx> {
let installers: string[] = [];
let pkgs: string[] = [];
const curr = process.argv
.slice(2)
.filter((arg) => arg.startsWith("--"))
.map((arg) => arg.slice(2));
const skip = curr.includes("skip");
try {
installers = await fs.readdir(path.join(__dirname, "../installers"));
} catch {}
Expand All @@ -163,30 +163,34 @@ export async function getCtxWithInstallers(ctx: IAppCtx): Promise<ICtx> {
.join(", ")}`
);
}
let optInstallers = installers.filter(
(pkg) => !validInstallers.includes(pkg)
);
const opts = ["TailwindCSS", "UnoCSS"];
for (const opt of opts) {
if (validInstallers.includes(opt)) {
optInstallers = optInstallers.filter((pkg) => !opts.includes(pkg));
if (!skip) {
let optInstallers = installers.filter(
(pkg) => !validInstallers.includes(pkg)
);
const opts = ["TailwindCSS", "UnoCSS"];
for (const opt of opts) {
if (validInstallers.includes(opt)) {
optInstallers = optInstallers.filter((pkg) => !opts.includes(pkg));
}
}
const newPkgs = (
await inquirer.prompt<{ pkgs: string[] }>({
name: "pkgs",
type: "checkbox",
message: "What should we use for this app?",
choices: optInstallers,
validate: (ans: string[]) => {
if (ans.includes("TailwindCSS") && ans.includes("UnoCSS")) {
return "You can't use both TailwindCSS and UnoCSS at the same time";
}
return true;
},
})
).pkgs;
pkgs = [...validInstallers, ...newPkgs];
} else {
pkgs = validInstallers;
}
const newPkgs = (
await inquirer.prompt<{ pkgs: string[] }>({
name: "pkgs",
type: "checkbox",
message: "What should we use for this app?",
choices: optInstallers,
validate: (ans: string[]) => {
if (ans.includes("TailwindCSS") && ans.includes("UnoCSS")) {
return "You can't use both TailwindCSS and UnoCSS at the same time";
}
return true;
},
})
).pkgs;
pkgs = [...validInstallers, ...newPkgs];
}
if (pkgs.includes("Prisma") && !ctx.vercel) {
console.log(
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import chalk from "chalk";
import runInstallers, { getCtxWithInstallers } from "./helpers/installer";

async function main() {
const args = process.argv
.slice(2)
.filter((arg) => arg.startsWith("--"))
.map((arg) => arg.slice(2).toLowerCase());
const pkgManager = getUserPackageManager();
const appCtx = await project.initApp();
const appCtx = await project.initApp(args);
await project.copyTemplate(appCtx);
const ctx = await getCtxWithInstallers(appCtx);
const ctx = await getCtxWithInstallers(appCtx, args);
const [scripts, deps, env, commands] = await runInstallers(ctx);
await project.modifyProject(ctx, scripts, env);
await project.installDeps(pkgManager, ctx.userDir, ctx.installers.length > 0);
Expand Down
43 changes: 28 additions & 15 deletions src/utils/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,26 @@ import { installPkgs } from "~helpers/installer";
import { updateEnv } from "~helpers/env";
import { modifyConfigIfNeeded } from "~helpers/vite";

export async function initApp(): Promise<IAppCtx> {
export async function initApp(args: string[]): Promise<IAppCtx> {
console.log();
const appName = (
await inquirer.prompt<{ appName: string }>({
name: "appName",
type: "input",
message: "What is the name of the app?",
validate: validateName,
default: "my-app",
})
).appName;
let pName = args
.find((o) => o.startsWith("pname="))
?.split("pname=")
.pop();
if (pName && !validateName(pName)) {
pName = undefined;
}
const appName =
pName ||
(
await inquirer.prompt<{ appName: string }>({
name: "appName",
type: "input",
message: "What is the name of the app?",
validate: validateName,
default: "my-app",
})
).appName;
const userDir = path.resolve(process.cwd(), appName);
let exists = await existsOrCreate(userDir);
if (exists) {
Expand All @@ -46,11 +55,15 @@ export async function initApp(): Promise<IAppCtx> {
process.exit(1);
}
}
const { vercel } = await inquirer.prompt<{ vercel: boolean }>({
name: "vercel",
type: "confirm",
message: "Will you deploy this project to vercel?",
});
let vercel =
args.includes("vercel") ||
(
await inquirer.prompt<{ vercel: boolean }>({
name: "vercel",
type: "confirm",
message: "Will you deploy this project to vercel?",
})
).vercel;
return {
appName,
userDir,
Expand Down

0 comments on commit 47d3596

Please sign in to comment.