瀏覽代碼

fix(cli.js): force version update on Cargo manifest (#2419)

Lucas Fernandes Nogueira 4 年之前
父節點
當前提交
c544cea8c9
共有 2 個文件被更改,包括 45 次插入17 次删除
  1. 5 0
      .changes/cli.js-deps-update-manifest.md
  2. 40 17
      tooling/cli.js/src/api/dependency-manager/cargo-crates.ts

+ 5 - 0
.changes/cli.js-deps-update-manifest.md

@@ -0,0 +1,5 @@
+---
+"cli.js": patch
+---
+
+Force Cargo manifest update when running the `deps update` command and fix the version that is written to the file.

+ 40 - 17
tooling/cli.js/src/api/dependency-manager/cargo-crates.ts

@@ -32,8 +32,14 @@ function readToml<T>(tomlPath: string): T | null {
   return null
 }
 
-function dependencyDefinition(version: string): CargoManifestDependency {
-  return { version: version.substring(0, version.lastIndexOf('.')) }
+function dependencyDefinition(
+  dependency: string | CargoManifestDependency,
+  version: string
+): string | CargoManifestDependency {
+  if (typeof dependency === 'string') {
+    return version
+  }
+  return { ...dependency, version }
 }
 
 async function manageDependencies(
@@ -73,29 +79,46 @@ async function manageDependencies(
       const latestVersion = getCrateLatestVersion(dependency)
       if (latestVersion !== null) {
         // eslint-disable-next-line security/detect-object-injection
-        manifest.dependencies[dependency] = dependencyDefinition(latestVersion)
+        manifest.dependencies[dependency] = dependencyDefinition(
+          // eslint-disable-next-line security/detect-object-injection
+          manifest.dependencies[dependency],
+          latestVersion
+        )
       }
       installedDeps.push(dependency)
     } else if (managementType === ManagementType.Update) {
       const latestVersion = getCrateLatestVersion(dependency)
-      if (latestVersion !== null && semverLt(currentVersion, latestVersion)) {
-        const inquired = (await inquirer.prompt([
-          {
-            type: 'confirm',
-            name: 'answer',
-            message: `[CRATES] "${dependency}" latest version is ${latestVersion}. Do you want to update?`,
-            default: false
+      if (latestVersion !== null) {
+        if (semverLt(currentVersion, latestVersion)) {
+          const inquired = (await inquirer.prompt([
+            {
+              type: 'confirm',
+              name: 'answer',
+              message: `[CRATES] "${dependency}" latest version is ${latestVersion}. Do you want to update?`,
+              default: false
+            }
+          ])) as { answer: boolean }
+          if (inquired.answer) {
+            log(`Updating ${dependency}...`)
+            // eslint-disable-next-line security/detect-object-injection
+            manifest.dependencies[dependency] = dependencyDefinition(
+              // eslint-disable-next-line security/detect-object-injection
+              manifest.dependencies[dependency],
+              latestVersion
+            )
+            updatedDeps.push(dependency)
           }
-        ])) as { answer: boolean }
-        if (inquired.answer) {
-          log(`Updating ${dependency}...`)
+        } else {
+          // force update the manifest to the show the latest version even if the lockfile is up to date
           // eslint-disable-next-line security/detect-object-injection
-          manifest.dependencies[dependency] =
-            dependencyDefinition(latestVersion)
+          manifest.dependencies[dependency] = dependencyDefinition(
+            // eslint-disable-next-line security/detect-object-injection
+            manifest.dependencies[dependency],
+            latestVersion
+          )
           updatedDeps.push(dependency)
+          log(`"${dependency}" is up to date`)
         }
-      } else {
-        log(`"${dependency}" is up to date`)
       }
     } else {
       log(`"${dependency}" is already installed`)