<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Guilherme Tod</title>
    <description>The latest articles on DEV Community by Guilherme Tod (@guilhermetod).</description>
    <link>https://dev.to/guilhermetod</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F420357%2F6352b3da-ca47-411f-aea4-d2e90b43a3d2.jpeg</url>
      <title>DEV Community: Guilherme Tod</title>
      <link>https://dev.to/guilhermetod</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/guilhermetod"/>
    <language>en</language>
    <item>
      <title>Migrating a repo to an Nx monorepo while retaining history</title>
      <dc:creator>Guilherme Tod</dc:creator>
      <pubDate>Thu, 12 Jan 2023 20:29:29 +0000</pubDate>
      <link>https://dev.to/guilhermetod/migrating-a-repo-to-an-nx-monorepo-while-retaining-history-42dp</link>
      <guid>https://dev.to/guilhermetod/migrating-a-repo-to-an-nx-monorepo-while-retaining-history-42dp</guid>
      <description>&lt;h2&gt;
  
  
  Why you should migrate and retain history
&lt;/h2&gt;




&lt;p&gt;If you reached this article, it is likely that you are planning to either start using Nx monorepos or to migrate a an existing project into an existing monorepo.&lt;/p&gt;

&lt;p&gt;If you're unfamiliar with Nx monorepos, make sure to &lt;a href="https://nx.dev/more-concepts/why-monorepos#what-are-the-benefits-of-a-monorepo"&gt;check the many advantages&lt;/a&gt; it can bring you, including better code reusability, dependency maintenance and developer experience.&lt;/p&gt;

&lt;p&gt;Now, if you decided that you're going to migrate your application into an existing monorepo, you might be wondering 1) how to do it the best possible way and 2) if it is possible and worth the effort to maintain your history at all.&lt;/p&gt;

&lt;p&gt;The short answer is simply git. Git will allow you to pull your changes automatically instead of requiring you to manually copy and paste all your files within a folder. Also, your history will be almost entirely preserved with a minimal effort, which is an amazing benefit.&lt;/p&gt;

&lt;p&gt;Another benefit of using git is continuity. We’ll get into more detail later, but after you create the first pull request for the migration, your teammates will review it, you may go into a deep QA process and many other things can happen between the PR creation and the actual merge and release.&lt;/p&gt;

&lt;p&gt;Now, what if during this process a teammate needs to push a fix or an important feature on the original repo? If you don’t use git, you’ll probably be left with the single alternative of finding and replicating every change manually and potentially creating another entire process of review.&lt;/p&gt;

&lt;p&gt;However, if you use git for the first step, you’ll see that you can later continuously pull the changes from one repo to another with the traditional &lt;code&gt;git merge&lt;/code&gt; command. That not only speeds you up, but is also much more reliable in terms of getting the new code correctly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting ready for the migration
&lt;/h2&gt;




&lt;p&gt;Migrating an entire app to another repo can be a pretty impactful change. That means that your first deployment from the monorepo will naturally be a very hesitant.&lt;/p&gt;

&lt;p&gt;Before you start the actual migration, there are a few steps recommended in order to have a smoother transition. The first deployment from the monorepo will likely be a very cautious one. After all, big changes like this always come with a bit of hesitance.&lt;/p&gt;

&lt;p&gt;To minimize the changes that will happen during the actual migration, we can anticipate some steps to make sure that you break big changes into several and smaller steps that will allow you to focus into making sure every one of them gets completely ready and to solve potential issues gradually instead of dropping a massive change that can leave you with a pack of problems to solve.&lt;/p&gt;

&lt;h3&gt;
  
  
  Match dependencies within the repos
&lt;/h3&gt;

&lt;p&gt;One of the most time-consuming tasks from the migration will potentially be matching dependencies. That is especially true if you have major version differences in significant packages, like your framework.&lt;/p&gt;

&lt;p&gt;To do that, you can use a &lt;a href="https://www.jsondiff.com/"&gt;JSON comparison&lt;/a&gt; to see the differences between the two &lt;code&gt;package.json&lt;/code&gt; files, specifically at the &lt;code&gt;dependencies&lt;/code&gt; and &lt;code&gt;devDependencies&lt;/code&gt; fields. Simply paste one file at each side and check the mismatched versions by taking semver’s resolution into account. For this step, you will not add any dependencies to either repo, as the goal here is to only make sure that both applications run correctly with the same dependencies.&lt;/p&gt;

&lt;p&gt;After you make sure that all the dependencies are matching, do a deployment for the affected repos and keep an eye out on the next few days for new bugs that might be related to the upgrades while you work on the next steps.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Warning: While it is possible to manage dependencies with multiple versions within a repo, Nx strongly discourages it, so consider making an effort to match dependencies before attempting to keep different versions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  If possible, match all the code style
&lt;/h3&gt;

&lt;p&gt;If you have lint checks running on CI/CD, you’ll eventually need to adapt the to-be-migrated repo to the new code style in order to be able to push the new code on the monorepo.&lt;/p&gt;

&lt;p&gt;While it’s true that you can easily have a custom lint configuration for different projects within a monorepo, you likely want to follow a similar code style to guarantee consistency between your team. So while this is not mandatory, it is a good thing to consider. This step should take into consideration linting, formatting and other project specific settings like TypeScript strict mode.&lt;/p&gt;

&lt;p&gt;Once more, it’s recommended to do this before you start with the repo migration to ensure that the migration is restricted to its purpose and does not include any other side effects.&lt;/p&gt;

&lt;p&gt;This is also helpful for not running into conflicts when you continuously pull changes to the monorepo as you won’t have to deal with code style and formatting differences.&lt;/p&gt;

&lt;h3&gt;
  
  
  Raise all known issues
&lt;/h3&gt;

&lt;p&gt;When you finally approach the moment of deployment, it is natural that your team will keep an eye out for any issues. This however may lead to teammates reporting existing issues as if they were related to the migration, which can lead to some tension by making others think that some bugs were introduced.&lt;/p&gt;

&lt;p&gt;Also, it’s common that every other team member has a few issues and problems tracked only in their mind that didn’t make it to a real ticket that then gets known to the entire team.&lt;/p&gt;

&lt;p&gt;To mitigate this, it is encouraged that you talk to the teammates to document all the small issues they can think of so that they are easily consulted by anyone before they raise their hands to report a bug considered to be caused by the migration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Performing the migration
&lt;/h2&gt;




&lt;h3&gt;
  
  
  Creating the project
&lt;/h3&gt;

&lt;p&gt;Once all the previous steps are done, it is time to work on the actual migration. This step is surprisingly easy since we’re using git to do the work for us.&lt;/p&gt;

&lt;p&gt;We’ll start by adding a Nx project that will later hold the migrated code. Assuming you’ll be migrating an Angular app named &lt;code&gt;single-app-repo&lt;/code&gt;, simply run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nx generate @nrwl/angular:application single-app-repo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note: always check in the docs for the generator to see the options available&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You’ll get a new &lt;code&gt;apps/single-apps-repo&lt;/code&gt; folder that contains a few configuration files at its root and a &lt;code&gt;src&lt;/code&gt; directory, which will hold the implementation and contains an example app.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bringing the code
&lt;/h3&gt;

&lt;p&gt;With the project setup, we can start bringing in the original repo code. Assuming the repo that will be migrated is named inside the org &lt;code&gt;your-company&lt;/code&gt;, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git remote add single-app-repo github.com/your-company/single-app-repo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will add a new origin that points to the existing repo, which you’ll use to perform the initial merge and to further update if that repo happens to have other activity prior to the first merge.&lt;/p&gt;

&lt;p&gt;After this, we’ll merge the code from the original repo. Assuming the latest changes you want to pull are on the &lt;code&gt;main&lt;/code&gt; branch, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git merge single-app-repo/main &lt;span class="nt"&gt;--allow-unrelated-histories&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will bring all your files to the root of the application and naturally result in some conflicts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Solving the conflicts
&lt;/h3&gt;

&lt;p&gt;You probably are facing conflicts in your &lt;code&gt;package.json&lt;/code&gt; and &lt;code&gt;package-lock.json&lt;/code&gt;/&lt;code&gt;yarn.lock&lt;/code&gt; files. For these files specifically, you won’t be able to retain history as they will live at the same path on both repositories, so you can simply accept the current changes and discard the original repo’s changes.&lt;/p&gt;

&lt;p&gt;For any other conflicting files, it’s worth looking at each and handling them separately as you consider whether they should stay at the root of the monorepo or should be sent to the project’s directory. In either case, it’s recommended that you accept the current changes, but if the second one is the answer, you should create the file manually inside the dir after you solve the conflict.&lt;/p&gt;

&lt;p&gt;Now, lastly, you can take all the added files and move them to &lt;code&gt;apps/single-app-repo&lt;/code&gt;. It’s important to adapt your structure to the Nx one, which means keeping the previously on root files inside the &lt;code&gt;apps/single-app-repo&lt;/code&gt; and the actual source files inside &lt;code&gt;apps/single-app-repo/src&lt;/code&gt;. If you already used Angular’s default structure, your work will probably be done by copying over the files to the indicated folder. Also, if you are creating an Angular app, it’s also required to adapt your &lt;code&gt;angular.json&lt;/code&gt; file into the &lt;code&gt;project.json&lt;/code&gt; file previously created by the NX generator.&lt;/p&gt;

&lt;p&gt;After everything is done and moved, do a last check to see if the history is correctly maintained. If you’re on VSCode, you can use &lt;a href="https://marketplace.visualstudio.com/items?itemName=eamodio.gitlens"&gt;GitLens&lt;/a&gt; and &lt;a href="https://marketplace.visualstudio.com/items?itemName=mhutchie.git-graph"&gt;Git Graph&lt;/a&gt; to have a better visualization of it.&lt;/p&gt;

&lt;p&gt;First, stage all your files. Then, go to a file from the new project that you are confident that should have kept history and see if the blame is being correctly displayed. Lastly, open Git Graph and try to find commits from the original repo to ensure they are being carried over too.&lt;/p&gt;

&lt;p&gt;If everything is well, you’re ready to finally merge with  &lt;code&gt;git merge --continue&lt;/code&gt; and start testing your brand new project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;




&lt;h3&gt;
  
  
  Clean up some of the project default files
&lt;/h3&gt;

&lt;p&gt;You probably overridden most of the files generated by Nx initially with your own implementation. Still, it’s possible that some files were left over. They won’t affect your application, but it’s good for housekeeping that you take a last look and try to remove files. Look for &lt;code&gt;nx-welcome&lt;/code&gt; file, a &lt;code&gt;favicon&lt;/code&gt; that you won’t use and even loose and unnecessary &lt;code&gt;.gitkeep&lt;/code&gt; files.&lt;/p&gt;

&lt;h3&gt;
  
  
  Say goodbye to the old repo
&lt;/h3&gt;

&lt;p&gt;Now that you finished migrating the old repo, you can safely remove it from the remote with &lt;code&gt;git remote remove single-app-repo&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Lastly, it might be a good idea to also tag your repository as deprecated and &lt;a href="https://docs.github.com/en/repositories/archiving-a-github-repository/archiving-repositories"&gt;archive&lt;/a&gt; it to ensure no one keeps working on it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Things to watch out for
&lt;/h3&gt;

&lt;p&gt;After you perform the merge, you’ll need to start testing to see if everything is still working as expected. If your application is building and serving plus you performed the steps to avoid side effects, it’s safe to assume that your code will behave as expected since at the end of the day it’s the same code.&lt;/p&gt;

&lt;p&gt;However, there are a few spots where it’s worth taking a careful look since they can get more easily affected by the repository change, so make sure you double check:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Environment settings&lt;/li&gt;
&lt;li&gt;All sort of assets, including images, icons and etc&lt;/li&gt;
&lt;li&gt;Localization files&lt;/li&gt;
&lt;li&gt;Path references for other directories, including &lt;code&gt;node_modules&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If all of those are fine as well as your other tests, congratulations, you just migrated your entire repo to NX and can enjoy &lt;a href="https://nx.dev/more-concepts/why-monorepos#what-are-the-benefits-of-a-monorepo"&gt;all of its benefits&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus tip
&lt;/h2&gt;




&lt;p&gt;If git isn’t correctly detecting a file’s history when you are continuously merging, abort your merge with git &lt;code&gt;merge --abort&lt;/code&gt; and increase the &lt;code&gt;renameLimit&lt;/code&gt; to make sure that git is more flexible for considering a file as renamed rather than moved. This is specially used if you weren’t able to keep the same formatting and linting and are now left with modified files due to these differences.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
