How to migrate existing Drupal 8 site into a Composer managed structure?

If you set up your site using a downloadable tarball or zip file your composer.json file isn't structured to do Drupal core updates so you need to convert it to a composer manager structure, how do you do it is very simple.

Run the following command inside your docroot and it will create a new composer.json file with all the dependencies including Drupal core.

ls -1 modules/contrib/ | xargs -L 1 -I {} composer require drupal/{}

How to update Drupal core.

You can run the following command to update the Drupal core.

composer update drupal/core --with-dependencies