fix(cli.js): use cargo search on crate latest version detection (#1563)

This commit is contained in:
Lucas Fernandes Nogueira 2021-04-21 12:43:46 -03:00 committed by GitHub
parent 1f089fb4f9
commit 07eb6cec8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 91 deletions

View File

@ -0,0 +1,5 @@
---
"cli.js": patch
---
The `tauri deps` command now properly detects `beta-rc` crate updates.

View File

@ -1,67 +0,0 @@
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
import { ManagementType, Result } from './types'
import { getCrateLatestVersion, semverLt } from './util'
import getScriptVersion from '../../helpers/get-script-version'
import logger from '../../helpers/logger'
import { sync as spawnSync } from 'cross-spawn'
import inquirer from 'inquirer'
const log = logger('dependency:cargo-commands')
const dependencies = ['tauri-bundler']
async function manageDependencies(
managementType: ManagementType
): Promise<Result> {
const installedDeps = []
const updatedDeps = []
for (const dependency of dependencies) {
const currentVersion = getScriptVersion('cargo', [dependency])
if (currentVersion === null) {
log(`Installing ${dependency}...`)
spawnSync('cargo', ['install', dependency])
installedDeps.push(dependency)
} else if (managementType === ManagementType.Update) {
const latestVersion = await getCrateLatestVersion(dependency)
if (semverLt(currentVersion, latestVersion)) {
const inquired = (await inquirer.prompt([
{
type: 'confirm',
name: 'answer',
message: `[CARGO COMMANDS] "${dependency}" latest version is ${latestVersion}. Do you want to update?`,
default: false
}
])) as { answer: boolean }
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (inquired.answer) {
spawnSync('cargo', ['install', dependency, '--force'])
updatedDeps.push(dependency)
}
} else {
log(`"${dependency}" is up to date`)
}
} else {
log(`"${dependency}" is already installed`)
}
}
const result: Result = new Map<ManagementType, string[]>()
result.set(ManagementType.Install, installedDeps)
result.set(ManagementType.Update, updatedDeps)
return result
}
async function install(): Promise<Result> {
return await manageDependencies(ManagementType.Install)
}
async function update(): Promise<Result> {
return await manageDependencies(ManagementType.Update)
}
export { install, update }

View File

@ -66,13 +66,16 @@ async function manageDependencies(
: manifestDep?.version
if (currentVersion === undefined) {
log(`Installing ${dependency}...`)
const latestVersion = await getCrateLatestVersion(dependency)
// eslint-disable-next-line security/detect-object-injection
manifest.dependencies[dependency] = dependencyDefinition(latestVersion)
const latestVersion = getCrateLatestVersion(dependency)
if (latestVersion !== null) {
// eslint-disable-next-line security/detect-object-injection
manifest.dependencies[dependency] = dependencyDefinition(latestVersion)
}
installedDeps.push(dependency)
} else if (managementType === ManagementType.Update) {
const latestVersion = await getCrateLatestVersion(dependency)
if (semverLt(currentVersion, latestVersion)) {
const latestVersion = getCrateLatestVersion(dependency)
console.log(dependency, currentVersion, latestVersion)
if (latestVersion !== null && semverLt(currentVersion, latestVersion)) {
const inquired = (await inquirer.prompt([
{
type: 'confirm',

View File

@ -4,7 +4,6 @@
import logger from '../../helpers/logger'
import * as rust from './rust'
import * as cargoCommands from './cargo-commands'
import * as cargoCrates from './cargo-crates'
import * as npmPackages from './npm-packages'
@ -14,14 +13,12 @@ module.exports = {
async installDependencies() {
log('Installing missing dependencies...')
rust.install()
await cargoCommands.install()
await cargoCrates.install()
await npmPackages.install()
},
async updateDependencies() {
log('Updating dependencies...')
rust.update()
await cargoCommands.update()
await cargoCrates.update()
await npmPackages.update()
}

View File

@ -2,16 +2,12 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
import https from 'https'
import { IncomingMessage } from 'http'
import { spawnSync } from '../../helpers/spawn'
import { sync as crossSpawnSync } from 'cross-spawn'
import { appDir, resolve as appResolve } from '../../helpers/app-paths'
import { existsSync } from 'fs'
import semver from 'semver'
const BASE_URL = 'https://docs.rs/crate/'
async function useYarn(): Promise<boolean> {
const hasYarnLockfile = existsSync(appResolve.app('yarn.lock'))
if (hasYarnLockfile) {
@ -24,18 +20,16 @@ async function useYarn(): Promise<boolean> {
}
}
async function getCrateLatestVersion(crateName: string): Promise<string> {
return await new Promise((resolve, reject) => {
const url = `${BASE_URL}${crateName}`
https.get(url, (res: IncomingMessage) => {
if (res.statusCode !== 302 || !res.headers.location) {
reject(res)
} else {
const version = res.headers.location.replace(url + '/', '')
resolve(version)
}
})
})
function getCrateLatestVersion(crateName: string): string | null {
const child = crossSpawnSync('cargo', ['search', crateName, '--limit', '1'])
const output = String(child.output[1])
// eslint-disable-next-line security/detect-non-literal-regexp
const matches = new RegExp(crateName + ' = "(\\S+)"', 'g').exec(output)
if (matches?.[1]) {
return matches[1]
} else {
return null
}
}
async function getNpmLatestVersion(packageName: string): Promise<string> {