ant-go 0.1.22

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.
@@ -0,0 +1,106 @@
1
+ /**
2
+ * update-check.js — Check for newer version on npm registry.
3
+ * Runs in background, prints hint after command completes.
4
+ * Result is cached for 24h to avoid hitting npm on every run.
5
+ */
6
+
7
+ const fs = require('fs');
8
+ const path = require('path');
9
+ const os = require('os');
10
+ const https = require('https');
11
+
12
+ const PKG_NAME = 'ant-go';
13
+ const CACHE_DIR = path.join(os.homedir(), '.ant-go');
14
+ const CACHE_FILE = path.join(CACHE_DIR, 'update-check.json');
15
+ const TTL_MS = 24 * 60 * 60 * 1000; // 24h
16
+
17
+ function getCurrentVersion() {
18
+ return require('../package.json').version;
19
+ }
20
+
21
+ function loadCache() {
22
+ try {
23
+ if (!fs.existsSync(CACHE_FILE)) return null;
24
+ const data = JSON.parse(fs.readFileSync(CACHE_FILE, 'utf8'));
25
+ if (Date.now() - data.checkedAt < TTL_MS) return data;
26
+ return null;
27
+ } catch {
28
+ return null;
29
+ }
30
+ }
31
+
32
+ function saveCache(latestVersion) {
33
+ try {
34
+ fs.mkdirSync(CACHE_DIR, { recursive: true });
35
+ fs.writeFileSync(CACHE_FILE, JSON.stringify({ latestVersion, checkedAt: Date.now() }, null, 2));
36
+ } catch {}
37
+ }
38
+
39
+ function fetchLatestVersion() {
40
+ return new Promise((resolve) => {
41
+ const req = https.get(
42
+ `https://registry.npmjs.org/${PKG_NAME}/latest`,
43
+ { headers: { Accept: 'application/json' }, timeout: 3000 },
44
+ (res) => {
45
+ let body = '';
46
+ res.on('data', chunk => (body += chunk));
47
+ res.on('end', () => {
48
+ try { resolve(JSON.parse(body).version); }
49
+ catch { resolve(null); }
50
+ });
51
+ }
52
+ );
53
+ req.on('error', () => resolve(null));
54
+ req.on('timeout', () => { req.destroy(); resolve(null); });
55
+ });
56
+ }
57
+
58
+ function isNewer(latest, current) {
59
+ const parse = v => v.split('.').map(Number);
60
+ const [lMaj, lMin, lPat] = parse(latest);
61
+ const [cMaj, cMin, cPat] = parse(current);
62
+ if (lMaj !== cMaj) return lMaj > cMaj;
63
+ if (lMin !== cMin) return lMin > cMin;
64
+ return lPat > cPat;
65
+ }
66
+
67
+ /**
68
+ * Call this at the start of a command.
69
+ * Returns a function — call it at the END of the command to print the hint.
70
+ *
71
+ * Usage:
72
+ * const showUpdateHint = startUpdateCheck();
73
+ * // ... do work ...
74
+ * await showUpdateHint();
75
+ */
76
+ function startUpdateCheck() {
77
+ const current = getCurrentVersion();
78
+
79
+ // Kick off check in background (non-blocking)
80
+ const resultPromise = (async () => {
81
+ const cached = loadCache();
82
+ if (cached) return cached.latestVersion;
83
+ const latest = await fetchLatestVersion();
84
+ if (latest) saveCache(latest);
85
+ return latest;
86
+ })();
87
+
88
+ return async function showUpdateHint() {
89
+ try {
90
+ const latest = await resultPromise;
91
+ if (latest && isNewer(latest, current)) {
92
+ const chalk = require('chalk');
93
+ const { t } = require('./i18n');
94
+ console.log('');
95
+ console.log(chalk.yellow(' ┌─────────────────────────────────────────────────────┐'));
96
+ console.log(chalk.yellow(' │') + chalk.bold(t('updateAvailable', current, latest)) + chalk.yellow(' │'));
97
+ console.log(chalk.yellow(' │') + t('updateRun') + chalk.yellow(' │'));
98
+ console.log(chalk.yellow(' └─────────────────────────────────────────────────────┘'));
99
+ console.log('');
100
+ }
101
+ } catch {}
102
+ };
103
+ }
104
+
105
+ module.exports = { startUpdateCheck };
106
+