fix(init): don't delete existing files when copying the template

SH
2d ago
+215-3

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

Description

reflex init deletes files that are already in the folder, then reports success:

$ mkdir shop && cd shop
$ mkdir assets shop && touch assets/logo.png shop/models.py
$ reflex init --template blank
Success: Initialized shop using the blank template. ...
$ ls assets shop
assets:
favicon.ico

shop:
__init__.py  shop.py

Same on 0.11.0. path_ops.cp empties a folder before copying into it, and path_ops.mv deletes whatever is at the target.

Template folders are now merged without replacing existing files, except reflex.lock/, which init writes just before and the template's must replace. If something named after the app already exists, initialize_app stops with an error before writing rxconfig.py, and you can pick another name with --name. I didn't merge into that folder because init rewrites the template's imports in every file under it, which would edit your own modules. Loose top-level template files, like requirements.txt, still overwrite as before.

The new tests fail on main. With the fix, the commands above stop at the name check and leave both files alone. With only assets/ there, init succeeds and adds favicon.ico next to logo.png.