From 470a5e5a2d47a66b683b1e2a2284910d648771b6 Mon Sep 17 00:00:00 2001
From: Vlad Rindevich <vladrin@vaadin.com>
Date: Wed, 8 Sep 2021 13:33:16 +0300
Subject: [PATCH] chore(frontend): make publishing more correct (#98)

---
 .github/workflows/frontend-release.yml           |  4 +++-
 frontend/scripts/bump/update-package-versions.js | 15 ++++++++++-----
 2 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/.github/workflows/frontend-release.yml b/.github/workflows/frontend-release.yml
index 05c9b17d5c..500173dce2 100644
--- a/.github/workflows/frontend-release.yml
+++ b/.github/workflows/frontend-release.yml
@@ -41,9 +41,11 @@ jobs:
           GIT_RELEASE_TOKEN: ${{ secrets.GIT_RELEASE_TOKEN }}
           REPO: ${{ github.repository }}
           VERSION_TAG: ${{ github.event.release.tag_name }}
+      - name: Re-build New Version
+        run: npm run build
       - name: Publish
         run: |
-          npx lerna publish --dist-tag \
+          npx lerna publish from-package --dist-tag \
             "$([[ ${{ github.event.release.tag_name }} =~ alpha|beta|rc ]] && echo "next" || echo "latest")"
         env:
           NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
diff --git a/frontend/scripts/bump/update-package-versions.js b/frontend/scripts/bump/update-package-versions.js
index 2b3a8201a0..482f09aeae 100644
--- a/frontend/scripts/bump/update-package-versions.js
+++ b/frontend/scripts/bump/update-package-versions.js
@@ -37,11 +37,16 @@ async function updatePackageRegistrations() {
   await Promise.all(
     packages.map(async (_package) => {
       const indexFile = resolve(packagesRoot, _package, 'src/index.ts');
-      const content = await readFile(indexFile, 'utf8');
-      const updated = content.replace(versionPattern, `version: /* updated-by-script */ '${version}',`);
-      await writeFile(indexFile, updated, 'utf8');
-
-      log(`@vaadin/${_package} registration updated`);
+      const packageFile = resolve(packagesRoot, _package, 'package.json');
+      const [indexContent, packageContent] = await Promise.all([
+        readFile(indexFile, 'utf8'),
+        readFile(packageFile, 'utf8'),
+      ]);
+      const indexUpdated = indexContent.replace(versionPattern, `version: /* updated-by-script */ '${version}',`);
+      const packageUpdated = JSON.stringify({ ...JSON.parse(packageContent), version }, null, 2);
+      await Promise.all([writeFile(indexFile, indexUpdated, 'utf8'), writeFile(packageFile, packageUpdated, 'utf8')]);
+
+      log(`@vaadin/${_package} version and registration updated`);
     })
   );
 }