From 47d3596f0d2f712fef065835b4efdad94b7f12ff Mon Sep 17 00:00:00 2001
From: OrJDev <orjdeveloper@gmail.com>
Date: Sun, 13 Nov 2022 18:28:48 +0200
Subject: [PATCH] more flags

---
 package.json             |  2 +-
 src/helpers/installer.ts | 58 +++++++++++++++++++++-------------------
 src/index.ts             |  8 ++++--
 src/utils/project.ts     | 43 ++++++++++++++++++-----------
 4 files changed, 66 insertions(+), 45 deletions(-)

diff --git a/package.json b/package.json
index eb2689b..dd4c533 100644
--- a/package.json
+++ b/package.json
@@ -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",
diff --git a/src/helpers/installer.ts b/src/helpers/installer.ts
index dd26961..4e045de 100644
--- a/src/helpers/installer.ts
+++ b/src/helpers/installer.ts
@@ -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 {}
@@ -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(
diff --git a/src/index.ts b/src/index.ts
index d697c33..3fbb174 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -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);
diff --git a/src/utils/project.ts b/src/utils/project.ts
index f392d6a..8d3554a 100644
--- a/src/utils/project.ts
+++ b/src/utils/project.ts
@@ -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) {
@@ -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,