Repository: remix-run/remix
Pull request #3573 (2022-06-25 11:40:10)

feat(remix-dev/cli/init): add `--no-delete` flag
Follow-up of #3571

kentcdodds: Rats. I took too long reviewing. Could you handle the merge conflict?

Patch:
diff --git a/packages/remix-dev/cli/commands.ts b/packages/remix-dev/cli/commands.ts
index e05ebc7ab..84552b7f2 100644
--- a/packages/remix-dev/cli/commands.ts
+++ b/packages/remix-dev/cli/commands.ts
@@ -56,7 +56,13 @@ export async function create({
   spinner.clear();
 }
 
-export async function init(projectDir: string) {
+type InitFlags = {
+  deleteScript?: boolean;
+};
+export async function init(
+  projectDir: string,
+  { deleteScript = true }: InitFlags = {}
+) {
   let initScriptDir = path.join(projectDir, "remix.init");
   let initScript = path.resolve(initScriptDir, "index.js");
 
@@ -79,7 +85,9 @@ export async function init(projectDir: string) {
   try {
     await initFn({ isTypeScript, packageManager, rootDirectory: projectDir });
 
-    await fse.remove(initScriptDir);
+    if (deleteScript) {
+      await fse.remove(initScriptDir);
+    }
   } catch (error) {
     if (error instanceof Error) {
       error.message = `${colors.error("🚨 Oops, remix.init failed")}\n\n${
diff --git a/packages/remix-dev/cli/run.ts b/packages/remix-dev/cli/run.ts
index d2752bad8..d9c4a24e3 100644
--- a/packages/remix-dev/cli/run.ts
+++ b/packages/remix-dev/cli/run.ts
@@ -43,6 +43,8 @@ ${colors.logoBlue("R")} ${colors.logoGreen("E")} ${colors.logoYellow(
   \`dev\` Options:
     --debug             Attach Node.js inspector
     --port, -p          Choose the port from which to run your app
+  \`init\` Options:
+    --no-delete         Skip deleting the \`remix.init\` script
   \`routes\` Options:
     --json              Print the routes as JSON
   \`migrate\` Options:
@@ -151,6 +153,7 @@ export async function run(argv: string[] = process.argv.slice(2)) {
   let args = arg(
     {
       "--debug": Boolean,
+      "--no-delete": Boolean,
       "--dry": Boolean,
       "--force": Boolean,
       "--help": Boolean,
@@ -192,6 +195,9 @@ export async function run(argv: string[] = process.argv.slice(2)) {
     return;
   }
 
+  if (args["--no-delete"]) {
+    flags.delete = false;
+  }
   if (args["--no-install"]) {
     flags.install = false;
   }
@@ -408,7 +414,7 @@ export async function run(argv: string[] = process.argv.slice(2)) {
       if (hasInitScript) {
         if (installDeps) {
           console.log("💿 Running remix.init script");
-          await commands.init(projectDir);
+          await commands.init(projectDir, { deleteScript: true });
         } else {
           console.log();
           console.log(
@@ -442,7 +448,9 @@ export async function run(argv: string[] = process.argv.slice(2)) {
       break;
     }
     case "init":
-      await commands.init(input[1] || process.env.REMIX_ROOT || process.cwd());
+      await commands.init(input[1] || process.env.REMIX_ROOT || process.cwd(), {
+        deleteScript: flags.delete,
+      });
       break;
     case "routes":
       await commands.routes(input[1], flags.json ? "json" : "jsx");


Test patch:
diff --git a/packages/remix-dev/__tests__/cli-test.ts b/packages/remix-dev/__tests__/cli-test.ts
index 1473f9b68..3fafd540f 100644
--- a/packages/remix-dev/__tests__/cli-test.ts
+++ b/packages/remix-dev/__tests__/cli-test.ts
@@ -111,6 +111,8 @@ describe("remix CLI", () => {
           \`dev\` Options:
             --debug             Attach Node.js inspector
             --port, -p          Choose the port from which to run your app
+          \`init\` Options:
+            --no-delete         Skip deleting the \`remix.init\` script
           \`routes\` Options:
             --json              Print the routes as JSON
           \`migrate\` Options:
diff --git a/packages/remix-dev/__tests__/create-test.ts b/packages/remix-dev/__tests__/create-test.ts
index 9e82fddfd..eb8897242 100644
--- a/packages/remix-dev/__tests__/create-test.ts
+++ b/packages/remix-dev/__tests__/create-test.ts
@@ -479,6 +479,30 @@ describe("the create command", () => {
     expect(fse.existsSync(path.join(projectDir, "remix.init"))).toBeFalsy();
   });
 
+  it("It keeps the `remix.init` script when using the `--no-delete` flag", async () => {
+    let projectDir = await getProjectDir("remix-init-manual");
+    await run([
+      "create",
+      projectDir,
+      "--template",
+      path.join(__dirname, "fixtures", "successful-remix-init.tar.gz"),
+      "--no-install",
+      "--typescript",
+    ]);
+    expect(output.trim()).toBe(
+      getOptOutOfInstallMessage() +
+        "\n\n" +
+        getSuccessMessage(path.join("<TEMP_DIR>", "remix-init-manual"))
+    );
+
+    output = "";
+    process.chdir(projectDir);
+    await run(["init", "--no-delete"]);
+
+    expect(output).toBe("");
+    expect(fse.existsSync(path.join(projectDir, "remix.init"))).toBeTruthy();
+  });
+
   it("throws an error when invalid remix.init script when automatically ran", async () => {
     let projectDir = await getProjectDir("invalid-remix-init-manual");
     await expect(