Repository: create-react-app
Issue: Failed 'create-react-app .' deletes current directory
Labels: tag: enhancement

<!--
    Please note that your issue will be fixed much faster if you spend about
    half an hour preparing it, including the exact reproduction steps and a demo.

    If you're in a hurry or don't feel confident, it's fine to report bugs with
    less details, but this makes it less likely they'll get fixed soon.

    In either case, please use this template and fill in as many fields below as you can.

    Note that we don't provide help for webpack questions after ejecting.
    You can find webpack docs at https://webpack.js.org/.
-->

### Describe the bug

When CRA fails, it deletes the created directory. However, when calling `create-react-app .`, that directory is the _current_ directory.

I see this is done intentionally, and CRA purposefully [moves the node process out of the directory](https://github.com/facebook/create-react-app/blob/master/packages/create-react-app/createReactApp.js#L514-L520) in order to delete it, but the shell process is still left in that directory.

#### Why is this a problem?

My user base works on a custom `npm` registry - which can cause failures with module/template/etc resolution when misconfigured - and `npm` chokes when run from a directory which does not exist. **While `npm`'s failure is not an issue in CRA,** `npm` is just an example in this case. _Many_ tools break when trying to run from a directory which does not exist, and users may attempt to use those tools to troubleshoot why CRA failed.

#### Duplicating

```bash
# create a temp space for easy cleanup
mkdir tmp-cra
cd tmp-cra

# make an incomplete template that will fail
mkdir cra-template-broken
cd cra-template-broken
echo '{"name": "cra-template-broken"}' > package.json
cd ..

# run CRA against that template
mkdir cra-working-directory
cd cra-working-directory
npx create-react-app@latest . --template file:../cra-template-broken

# assert on the current directory existing
pwd
ls $PWD
# npm will break when running inside of a directory which does not exist.
# When CRA fails due to registry or other npm configuration issues,
# you may not realize you're in a non-existent directory before attempting to troubleshoot the registry issue.
npm config get registry

# clean up
cd ../..
rm -r tmp-cra
```

<details>
<summary>Sample Output (click to expand)</summary>

![image](https://user-images.githubusercontent.com/14095644/84908796-8c072a80-b07a-11ea-9582-031efccd83c2.png)

</details>

Going back to that [defensive code](https://github.com/facebook/create-react-app/blob/master/packages/create-react-app/createReactApp.js#L514-L520) I mentioned earlier, it seems that `process.chdir(...)` is designed to [change the working directory of the current process, but does not change the directory of the overarching shell process](https://stackoverflow.com/a/32871358), meaning that CRA is successfully navigating up and out of this directory, but the shell is left behind. Apparently that's tough to do (I have no evidence to back that up), so my suggestion is to dodge it entirely by only removing the directory when the process is not in that directory.

My suggestion is to [detect if `root === process.cwd()` and avoid `fs.removeSync`ing the directory](https://github.com/facebook/create-react-app/blob/master/packages/create-react-app/createReactApp.js#L512) (see below), however I also see that this code is in `createReactApp.js`, and as such is the [untouchable stuff of legends](https://github.com/facebook/create-react-app/blob/master/packages/create-react-app/createReactApp.js#L9).

```diff
-        if (!remainingFiles.length) {
+        if (!remainingFiles.length && root !== process.cwd()) {
```

> ⬆️  May require additional `path.resolve(root)`?

The node interaction can be further validated with this script:
```bash
echo "shell pwd before: $PWD"
node -e "
const path = require('path')
console.log('script cwd before', process.cwd())
process.chdir(path.resolve(process.cwd(), '..'))
console.log('script cwd after', process.cwd())
"
echo "shell pwd after: $PWD"
```

<details>
<summary>Sample Output (click to expand)</summary>

![image](https://user-images.githubusercontent.com/14095644/84911474-cfaf6380-b07d-11ea-9ba5-374408912382.png)

</details>

### Did you try recovering your dependencies?
N/A

### Which terms did you search for in User Guide?


```
dir(ectory)
remove
clean
fail
error
```

### Environment

```
Environment Info:

  current version of create-react-app: 3.4.1
  running from /Users/z001gk0/.nvm/versions/node/v12.14.1/lib/node_modules/create-react-app

  System:
    OS: macOS Mojave 10.14.6
    CPU: (8) x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
  Binaries:
    Node: 12.14.1 - ~/.nvm/versions/node/v12.14.1/bin/node
    Yarn: 1.22.4 - ~/.nvm/versions/node/v12.14.1/bin/yarn
    npm: 6.13.7 - ~/.nvm/versions/node/v12.14.1/bin/npm
  Browsers:
    Chrome: 83.0.4103.106
    Firefox: Not Found
    Safari: 13.1.1
  npmPackages:
    react: Not Found
    react-dom: Not Found
    react-scripts: Not Found
  npmGlobalPackages:
    create-react-app: 3.4.1
```

### Steps to reproduce

```bash
# create a temp space for easy cleanup
mkdir tmp-cra
cd tmp-cra

# make an incomplete template that will fail
mkdir cra-template-broken
cd cra-template-broken
echo '{"name": "cra-template-broken"}' > package.json
cd ..

# run CRA against that template
mkdir cra-working-directory
cd cra-working-directory
npx create-react-app@latest . --template file:../cra-template-broken

# assert on the current directory existing
pwd
ls $PWD
# npm will break when running inside of a directory which does not exist.
# When CRA fails due to registry or other npm configuration issues,
# you may not realize you're in a non-existent directory before attempting to troubleshoot the registry issue.
npm config get registry

# clean up
cd ../..
rm -r tmp-cra
```

To re-iterate, the `npm` issue is beside the point and is only placed there as an example of one of many tools which will break when the current directory does not exist.

### Expected behavior

Either:
* CRA should move the shell process out of the working directory when deleting it
* CRA should not delete the working directory when it is the same as the current directory

### Actual behavior

CRA deletes the working directory (when it is the same as the current directory).

### Reproducible demo

It's not really a runtime issue as much as an issue on generation, so `Steps to reproduce` may have to suffice.