-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathcheck-for-updates.ts
More file actions
96 lines (84 loc) · 2.24 KB
/
check-for-updates.ts
File metadata and controls
96 lines (84 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import child_process from 'node:child_process';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import process from 'node:process';
import {VERSION} from '../version.js';
/**
* Notifies the user if an update is available.
* @param message The message to display in the update notification.
*/
let isChecking = false;
/** @internal Reset flag for tests only. */
export function resetUpdateCheckFlagForTesting() {
isChecking = false;
}
export async function checkForUpdates(message: string) {
if (isChecking || process.env['CHROME_DEVTOOLS_MCP_NO_UPDATE_CHECKS']) {
return;
}
isChecking = true;
const cachePath = path.join(
os.homedir(),
'.cache',
'chrome-devtools-mcp',
'latest.json',
);
let cachedVersion: string | undefined;
let stats: {mtimeMs: number} | undefined;
try {
stats = await fs.stat(cachePath);
const data = await fs.readFile(cachePath, 'utf8');
cachedVersion = JSON.parse(data).version;
} catch {
// Ignore errors reading cache.
}
if (cachedVersion && cachedVersion !== VERSION) {
console.warn(
`\nUpdate available: ${VERSION} -> ${cachedVersion}\n${message}\n`,
);
}
const now = Date.now();
if (stats && now - stats.mtimeMs < 24 * 60 * 60 * 1000) {
return;
}
// Update mtime immediately to prevent multiple subprocesses.
try {
const parentDir = path.dirname(cachePath);
await fs.mkdir(parentDir, {recursive: true});
const nowTime = new Date();
if (stats) {
await fs.utimes(cachePath, nowTime, nowTime);
} else {
await fs.writeFile(cachePath, JSON.stringify({version: VERSION}));
}
} catch {
// Ignore errors.
}
// In a separate process, check the latest available version number
// and update the local snapshot accordingly.
const scriptPath = path.join(
import.meta.dirname,
'..',
'bin',
'check-latest-version.js',
);
try {
const child = child_process.spawn(
process.execPath,
[scriptPath, cachePath],
{
detached: true,
stdio: 'ignore',
},
);
child.unref();
} catch {
// Fail silently in case of any errors.
}
}