fix(cli): tell user of missing lockfile or manifest during info (#675)

This commit is contained in:
chip 2020-06-14 15:26:38 -07:00 committed by GitHub
parent 6ac410e04d
commit 577a044bfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 64 deletions

View File

@ -0,0 +1,5 @@
---
"tauri.js": patch
---
Handle and communicate missing lockfile and/or manifest while reading versions during `tauri info`

View File

@ -72,12 +72,12 @@ function printInfo(info: Info): void {
)
}
function readTomlFile<T extends CargoLock | CargoManifest>(filepath: string): T {
if (fs.existsSync(filepath)) {
function readTomlFile<T extends CargoLock | CargoManifest>(filepath: string): T | null {
try {
const file = fs.readFileSync(filepath).toString()
return toml.parse(file) as unknown as T
} else {
return false as unknown as T
} catch (_) {
return null
}
}
@ -85,24 +85,24 @@ function printAppInfo(tauriDir: string): void {
printInfo({ key: 'App', section: true })
const lockPath = path.join(tauriDir, 'Cargo.lock')
const lockContents = readTomlFile<CargoLock>(lockPath)
let tauriPackages
let tauriVersion
const lock = readTomlFile<CargoLock>(lockPath)
const lockPackages = lock ? lock.package.filter(pkg => pkg.name === 'tauri') : []
if (lockContents) {
tauriPackages = lockContents.package.filter(pkg => pkg.name === 'tauri')
if (tauriPackages.length <= 0) {
tauriVersion = chalk.red('unknown')
} else if (tauriPackages.length === 1) {
tauriVersion = chalk.green(tauriPackages[0].version)
} else {
// there are multiple `tauri` packages in the lockfile
// load and check the manifest version to display alongside the found versions
const manifestPath = path.join(tauriDir, 'Cargo.toml')
const manifestContent = readTomlFile<CargoManifest>(manifestPath)
const manifest = readTomlFile<CargoManifest>(manifestPath)
let tauriVersion
if (manifest && lock && lockPackages.length === 1) {
// everything looks good
tauriVersion = chalk.green(lockPackages[0].version)
} else if (lock && lockPackages.length === 1) {
// good lockfile, but no manifest - will cause problems building
tauriVersion = `${chalk.green(lockPackages[0].version)} (${chalk.red('no manifest')})`
} else {
// we found multiple/none `tauri` packages in the lockfile, or
// no manifest. in both cases we want more info on the manifest
const manifestVersion = (): string => {
const tauri = manifestContent.dependencies.tauri
const tauri = manifest?.dependencies.tauri
if (tauri) {
if (typeof tauri === 'string') {
return chalk.yellow(tauri)
@ -111,47 +111,28 @@ function printAppInfo(tauriDir: string): void {
} else if (tauri.path) {
const manifestPath = path.resolve(tauriDir, tauri.path, 'Cargo.toml')
const manifestContent = readTomlFile<CargoManifest>(manifestPath)
const pathVersion = chalk.yellow(manifestContent.package.version)
let pathVersion = manifestContent?.package.version
pathVersion = pathVersion ? chalk.yellow(pathVersion) : chalk.red(pathVersion)
return `path:${tauri.path} [${pathVersion}]`
}
} else {
return chalk.red('no manifest')
}
return chalk.red('unknown')
return chalk.red('unknown manifest')
}
const versions = tauriPackages.map(p => p.version).join(', ')
tauriVersion = `${manifestVersion()} (${chalk.yellow(versions)})`
}
let lockVersion
if (lock && lockPackages.length > 0) {
lockVersion = chalk.yellow(lockPackages.map(p => p.version).join(', '))
} else if (lock && lockPackages.length === 0) {
lockVersion = chalk.red('unknown lockfile')
} else {
const tomlPath = path.join(tauriDir, 'Cargo.toml')
const tomlFile = fs.readFileSync(tomlPath).toString()
const tomlContents = toml.parse(tomlFile) as any as CargoManifest
const tauri = tomlContents.dependencies.tauri
if (tauri) {
if (typeof tauri === 'string') {
tauriVersion = chalk.green(tauri)
} else if (tauri.version) {
tauriVersion = chalk.green(tauri.version)
} else if (tauri.path) {
try {
const tauriTomlPath = path.resolve(
tauriDir,
tauri.path,
'Cargo.toml'
)
const tauriTomlFile = fs.readFileSync(tauriTomlPath).toString()
const tauriTomlContents = toml.parse(tauriTomlFile) as any as CargoManifest
tauriVersion = chalk.green(
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`${tauriTomlContents.package.version} (from source)`
)
} catch (_) {
tauriVersion = chalk.red('unknown')
}
} else {
tauriVersion = chalk.red('unknown')
}
lockVersion = chalk.red('no lockfile')
}
tauriVersion = `${manifestVersion()} (${chalk.yellow(lockVersion)})`
}
printInfo({ key: ' tauri.rs', value: tauriVersion })
try {