unbound-cli 0.1.8 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.js +3 -0
- package/src/update-check.js +79 -0
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -4,6 +4,9 @@ const { Command } = require('commander');
|
|
|
4
4
|
const config = require('./config');
|
|
5
5
|
const output = require('./output');
|
|
6
6
|
const { version } = require('../package.json');
|
|
7
|
+
const { checkForUpdates } = require('./update-check');
|
|
8
|
+
|
|
9
|
+
checkForUpdates();
|
|
7
10
|
|
|
8
11
|
const program = new Command();
|
|
9
12
|
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { execFile } = require('child_process');
|
|
4
|
+
const { CONFIG_DIR } = require('./config');
|
|
5
|
+
|
|
6
|
+
const CACHE_FILE = path.join(CONFIG_DIR, 'update-check.json');
|
|
7
|
+
const CHECK_INTERVAL = 24 * 60 * 60 * 1000; // 24 hours
|
|
8
|
+
const REGISTRY_URL = 'https://registry.npmjs.org/unbound-cli/latest';
|
|
9
|
+
|
|
10
|
+
function newerThan(latest, current) {
|
|
11
|
+
const a = latest.split('.').map(Number);
|
|
12
|
+
const b = current.split('.').map(Number);
|
|
13
|
+
for (let i = 0; i < 3; i++) {
|
|
14
|
+
if ((a[i] || 0) > (b[i] || 0)) return true;
|
|
15
|
+
if ((a[i] || 0) < (b[i] || 0)) return false;
|
|
16
|
+
}
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function checkForUpdates() {
|
|
21
|
+
if (!process.stderr.isTTY || process.env.UNBOUND_NO_UPDATE_CHECK) return;
|
|
22
|
+
|
|
23
|
+
const { version: current } = require('../package.json');
|
|
24
|
+
let cache = null;
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
cache = JSON.parse(fs.readFileSync(CACHE_FILE, 'utf-8'));
|
|
28
|
+
} catch {
|
|
29
|
+
// No cache or corrupted — will trigger a background check
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// If cached result shows a newer version, schedule notice on exit
|
|
33
|
+
if (cache && cache.latest && newerThan(cache.latest, current)) {
|
|
34
|
+
const useColor = !process.env.NO_COLOR;
|
|
35
|
+
const yellow = (s) => useColor ? `\x1b[33m${s}\x1b[0m` : s;
|
|
36
|
+
process.on('exit', () => {
|
|
37
|
+
console.error('');
|
|
38
|
+
console.error(yellow(`A new version of Unbound CLI is available: ${current} → ${cache.latest}`));
|
|
39
|
+
console.error(yellow(`Update using: npm install -g unbound-cli`));
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// If cache is stale or missing, spawn a background check
|
|
44
|
+
const isStale = !cache || !cache.checkedAt || (Date.now() - cache.checkedAt > CHECK_INTERVAL);
|
|
45
|
+
if (isStale) {
|
|
46
|
+
const configDir = CONFIG_DIR.replace(/\\/g, '\\\\');
|
|
47
|
+
const cacheFile = CACHE_FILE.replace(/\\/g, '\\\\');
|
|
48
|
+
const script = `
|
|
49
|
+
const https = require('https');
|
|
50
|
+
const fs = require('fs');
|
|
51
|
+
https.get('${REGISTRY_URL}', { headers: { 'Accept': 'application/json' }, timeout: 10000 }, (res) => {
|
|
52
|
+
let data = '';
|
|
53
|
+
res.on('data', (c) => data += c);
|
|
54
|
+
res.on('end', () => {
|
|
55
|
+
try {
|
|
56
|
+
const latest = JSON.parse(data).version;
|
|
57
|
+
if (latest) {
|
|
58
|
+
fs.mkdirSync('${configDir}', { recursive: true });
|
|
59
|
+
fs.writeFileSync('${cacheFile}', JSON.stringify({ latest, checkedAt: Date.now() }));
|
|
60
|
+
}
|
|
61
|
+
} catch {}
|
|
62
|
+
});
|
|
63
|
+
}).on('error', () => {});
|
|
64
|
+
`;
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
const child = execFile(process.execPath, ['-e', script], {
|
|
68
|
+
detached: true,
|
|
69
|
+
stdio: 'ignore',
|
|
70
|
+
windowsHide: true,
|
|
71
|
+
});
|
|
72
|
+
child.unref();
|
|
73
|
+
} catch {
|
|
74
|
+
// Best-effort — silently ignore failures
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
module.exports = { checkForUpdates };
|