feat: check if update-notifier is installed before using it

This commit is contained in:
Amin Yahyaabadi 2023-05-24 11:18:46 -07:00
parent e9ac0095ac
commit f5342df15b
4 changed files with 56 additions and 8 deletions

View File

@ -63,6 +63,8 @@
"update-notifier": "^5.1.0"
},
"devDependencies": {
"@types/resolve": "^1.20.2",
"resolve": "^1.22.2",
"@actions/core": "^1.10.0",
"@actions/exec": "^1.1.1",
"@actions/io": "^1.1.3",

View File

@ -44,6 +44,9 @@ importers:
'@types/prettier':
specifier: 2.7.2
version: 2.7.2
'@types/resolve':
specifier: ^1.20.2
version: 1.20.2
'@types/semver':
<<<<<<< HEAD
specifier: ^7.5.0
@ -154,6 +157,9 @@ importers:
readme-md-generator:
specifier: ^1.0.0
version: 1.0.0
resolve:
specifier: ^1.22.2
version: 1.22.2
retry-as-promised:
specifier: ^7.0.4
version: 7.0.4
@ -2668,6 +2674,10 @@ packages:
dev: true
optional: true
/@types/resolve@1.20.2:
resolution: {integrity: sha512-60BCwRFOZCQhDncwQdxxeOEEkbc5dIMccYLwbxsS4TUNeVECQ/pBJ0j09mrHOl/JJvpRPGwO9SvE4nR2Nb/a4Q==}
dev: true
/@types/responselike@1.0.0:
resolution: {integrity: sha512-85Y2BjiufFzaMIlvJDvTTB8Fxl2xfLo4HgmHzVBz08w4wDePCTjYw66PdrolO0kzli3yam/YCgRufyo1DdQVTA==}
dependencies:

View File

@ -2,21 +2,26 @@ import { warning } from "ci-log"
import { promises } from "fs"
const { readFile } = promises
import { join } from "path"
import { isInstalled } from "./utils/std/resolve"
// auto self update notifier
export async function checkUpdates() {
try {
const [un, packageJsonString] = await Promise.all([
import("update-notifier"),
readFile(join(__dirname, "..", "package.json"), "utf8"),
])
if (await isInstalled("update-notifier")) {
const [un, packageJsonString] = await Promise.all([
import("update-notifier"),
readFile(join(__dirname, "..", "package.json"), "utf8"),
])
const packageJson = JSON.parse(packageJsonString)
const packageJson = JSON.parse(packageJsonString)
// the types do not match the actual export
const updateNotifier = un as unknown as (typeof un)["default"]
// the types do not match the actual export
const updateNotifier = un as unknown as (typeof un)["default"]
updateNotifier({ pkg: packageJson }).notify()
updateNotifier({ pkg: packageJson }).notify()
} else {
warning("setup-cpp could not find its dependency update-notifier, skipping check for updates")
}
} catch (err) {
warning(`Failed to check for updates: ${err instanceof Error ? err.message + err.stack : err}`)
}

31
src/utils/std/resolve.ts Normal file
View File

@ -0,0 +1,31 @@
import resolveCb from "resolve"
interface PackageMeta {
name: string
version: string
[key: string]: any
}
/** Promise wrapper for resolve */
export async function resolve(id: string): Promise<{ resolved: string; pkg?: PackageMeta }> {
return new Promise((resolve, reject) => {
resolveCb(id, (err, resolved, pkg) => {
if (err) {
reject(err)
} else if (resolved === undefined) {
reject(new Error(`Could not resolve ${id}`))
} else {
resolve({ resolved, pkg })
}
})
})
}
export async function isInstalled(id: string) {
try {
await resolve(id)
return true
} catch {
return false
}
}