Skip to content

Commit f5342df

Browse files
committed
feat: check if update-notifier is installed before using it
1 parent e9ac009 commit f5342df

File tree

4 files changed

+56
-8
lines changed

4 files changed

+56
-8
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
"update-notifier": "^5.1.0"
6464
},
6565
"devDependencies": {
66+
"@types/resolve": "^1.20.2",
67+
"resolve": "^1.22.2",
6668
"@actions/core": "^1.10.0",
6769
"@actions/exec": "^1.1.1",
6870
"@actions/io": "^1.1.3",

pnpm-lock.yaml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/check-updates.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,26 @@ import { warning } from "ci-log"
22
import { promises } from "fs"
33
const { readFile } = promises
44
import { join } from "path"
5+
import { isInstalled } from "./utils/std/resolve"
56

67
// auto self update notifier
78
export async function checkUpdates() {
89
try {
9-
const [un, packageJsonString] = await Promise.all([
10-
import("update-notifier"),
11-
readFile(join(__dirname, "..", "package.json"), "utf8"),
12-
])
10+
if (await isInstalled("update-notifier")) {
11+
const [un, packageJsonString] = await Promise.all([
12+
import("update-notifier"),
13+
readFile(join(__dirname, "..", "package.json"), "utf8"),
14+
])
1315

14-
const packageJson = JSON.parse(packageJsonString)
16+
const packageJson = JSON.parse(packageJsonString)
1517

16-
// the types do not match the actual export
17-
const updateNotifier = un as unknown as (typeof un)["default"]
18+
// the types do not match the actual export
19+
const updateNotifier = un as unknown as (typeof un)["default"]
1820

19-
updateNotifier({ pkg: packageJson }).notify()
21+
updateNotifier({ pkg: packageJson }).notify()
22+
} else {
23+
warning("setup-cpp could not find its dependency update-notifier, skipping check for updates")
24+
}
2025
} catch (err) {
2126
warning(`Failed to check for updates: ${err instanceof Error ? err.message + err.stack : err}`)
2227
}

src/utils/std/resolve.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import resolveCb from "resolve"
2+
3+
interface PackageMeta {
4+
name: string
5+
version: string
6+
[key: string]: any
7+
}
8+
9+
/** Promise wrapper for resolve */
10+
export async function resolve(id: string): Promise<{ resolved: string; pkg?: PackageMeta }> {
11+
return new Promise((resolve, reject) => {
12+
resolveCb(id, (err, resolved, pkg) => {
13+
if (err) {
14+
reject(err)
15+
} else if (resolved === undefined) {
16+
reject(new Error(`Could not resolve ${id}`))
17+
} else {
18+
resolve({ resolved, pkg })
19+
}
20+
})
21+
})
22+
}
23+
24+
export async function isInstalled(id: string) {
25+
try {
26+
await resolve(id)
27+
return true
28+
} catch {
29+
return false
30+
}
31+
}

0 commit comments

Comments
 (0)