cokit-cli 1.0.3 → 1.0.4
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 +14 -2
- package/src/utils/update-checker.js +126 -0
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,17 +1,26 @@
|
|
|
1
1
|
// CoKit CLI - Make GitHub Copilot smarter in 30 seconds
|
|
2
2
|
import { Command } from 'commander';
|
|
3
|
+
import { readFileSync } from 'fs';
|
|
4
|
+
import { fileURLToPath } from 'url';
|
|
5
|
+
import { dirname, join } from 'path';
|
|
3
6
|
import { initCommand } from './commands/init.js';
|
|
4
7
|
import { addCommand } from './commands/add.js';
|
|
5
8
|
import { listCommand } from './commands/list.js';
|
|
6
9
|
import { doctorCommand } from './commands/doctor.js';
|
|
7
10
|
import { updateCommand } from './commands/update.js';
|
|
11
|
+
import { checkForUpdates } from './utils/update-checker.js';
|
|
12
|
+
|
|
13
|
+
// Get version from package.json
|
|
14
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
15
|
+
const __dirname = dirname(__filename);
|
|
16
|
+
const pkg = JSON.parse(readFileSync(join(__dirname, '..', 'package.json'), 'utf8'));
|
|
8
17
|
|
|
9
18
|
const program = new Command();
|
|
10
19
|
|
|
11
20
|
program
|
|
12
21
|
.name('cokit')
|
|
13
22
|
.description('Make GitHub Copilot smarter in 30 seconds')
|
|
14
|
-
.version(
|
|
23
|
+
.version(pkg.version);
|
|
15
24
|
|
|
16
25
|
// Register commands
|
|
17
26
|
program.addCommand(initCommand);
|
|
@@ -20,4 +29,7 @@ program.addCommand(listCommand);
|
|
|
20
29
|
program.addCommand(doctorCommand);
|
|
21
30
|
program.addCommand(updateCommand);
|
|
22
31
|
|
|
23
|
-
|
|
32
|
+
// Check for updates (async, non-blocking)
|
|
33
|
+
checkForUpdates().then(() => {
|
|
34
|
+
program.parse();
|
|
35
|
+
});
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// Update checker - Notify users when new version is available
|
|
2
|
+
import { homedir } from 'os';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
|
|
5
|
+
import { warn, hint, bold, cyan, dim } from './colors.js';
|
|
6
|
+
import { PACKAGE_ROOT } from './paths.js';
|
|
7
|
+
|
|
8
|
+
const CACHE_DIR = join(homedir(), '.cokit');
|
|
9
|
+
const CACHE_FILE = join(CACHE_DIR, 'update-check.json');
|
|
10
|
+
const CHECK_INTERVAL_MS = 24 * 60 * 60 * 1000; // 24 hours
|
|
11
|
+
const NPM_REGISTRY_URL = 'https://registry.npmjs.org/cokit-cli';
|
|
12
|
+
|
|
13
|
+
// Get current version from package.json
|
|
14
|
+
function getCurrentVersion() {
|
|
15
|
+
try {
|
|
16
|
+
const pkg = JSON.parse(readFileSync(join(PACKAGE_ROOT, 'package.json'), 'utf8'));
|
|
17
|
+
return pkg.version;
|
|
18
|
+
} catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Read cache file
|
|
24
|
+
function readCache() {
|
|
25
|
+
try {
|
|
26
|
+
if (existsSync(CACHE_FILE)) {
|
|
27
|
+
return JSON.parse(readFileSync(CACHE_FILE, 'utf8'));
|
|
28
|
+
}
|
|
29
|
+
} catch { /* ignore */ }
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Write cache file
|
|
34
|
+
function writeCache(data) {
|
|
35
|
+
try {
|
|
36
|
+
if (!existsSync(CACHE_DIR)) {
|
|
37
|
+
mkdirSync(CACHE_DIR, { recursive: true });
|
|
38
|
+
}
|
|
39
|
+
writeFileSync(CACHE_FILE, JSON.stringify(data, null, 2));
|
|
40
|
+
} catch { /* ignore */ }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Fetch latest version from npm registry
|
|
44
|
+
async function fetchLatestVersion() {
|
|
45
|
+
try {
|
|
46
|
+
const controller = new AbortController();
|
|
47
|
+
const timeout = setTimeout(() => controller.abort(), 3000); // 3s timeout
|
|
48
|
+
|
|
49
|
+
const response = await fetch(NPM_REGISTRY_URL, {
|
|
50
|
+
signal: controller.signal,
|
|
51
|
+
headers: { 'Accept': 'application/json' }
|
|
52
|
+
});
|
|
53
|
+
clearTimeout(timeout);
|
|
54
|
+
|
|
55
|
+
if (!response.ok) return null;
|
|
56
|
+
|
|
57
|
+
const data = await response.json();
|
|
58
|
+
return data['dist-tags']?.latest || null;
|
|
59
|
+
} catch {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Compare semver versions (returns 1 if a > b, -1 if a < b, 0 if equal)
|
|
65
|
+
function compareVersions(a, b) {
|
|
66
|
+
const partsA = a.split('.').map(Number);
|
|
67
|
+
const partsB = b.split('.').map(Number);
|
|
68
|
+
|
|
69
|
+
for (let i = 0; i < 3; i++) {
|
|
70
|
+
const numA = partsA[i] || 0;
|
|
71
|
+
const numB = partsB[i] || 0;
|
|
72
|
+
if (numA > numB) return 1;
|
|
73
|
+
if (numA < numB) return -1;
|
|
74
|
+
}
|
|
75
|
+
return 0;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Display update notification
|
|
79
|
+
function showUpdateNotification(currentVersion, latestVersion) {
|
|
80
|
+
console.log();
|
|
81
|
+
console.log(bold(cyan('╭─────────────────────────────────────────╮')));
|
|
82
|
+
console.log(bold(cyan('│')) + ' Update available! ' + dim(currentVersion) + ' → ' + bold(cyan(latestVersion)) + ' ' + bold(cyan('│')));
|
|
83
|
+
console.log(bold(cyan('│')) + ' ' + bold(cyan('│')));
|
|
84
|
+
console.log(bold(cyan('│')) + ' Run: ' + bold('npx cokit-cli@latest init -g') + ' ' + bold(cyan('│')));
|
|
85
|
+
console.log(bold(cyan('╰─────────────────────────────────────────╯')));
|
|
86
|
+
console.log();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Main update check function (non-blocking)
|
|
90
|
+
export async function checkForUpdates() {
|
|
91
|
+
try {
|
|
92
|
+
const currentVersion = getCurrentVersion();
|
|
93
|
+
if (!currentVersion) return;
|
|
94
|
+
|
|
95
|
+
const cache = readCache();
|
|
96
|
+
const now = Date.now();
|
|
97
|
+
|
|
98
|
+
// Check if we have a cached result that's still valid
|
|
99
|
+
if (cache && cache.checkedAt && (now - cache.checkedAt) < CHECK_INTERVAL_MS) {
|
|
100
|
+
// Use cached result
|
|
101
|
+
if (cache.latestVersion && compareVersions(cache.latestVersion, currentVersion) > 0) {
|
|
102
|
+
showUpdateNotification(currentVersion, cache.latestVersion);
|
|
103
|
+
}
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Fetch latest version (non-blocking, don't await in main flow)
|
|
108
|
+
const latestVersion = await fetchLatestVersion();
|
|
109
|
+
|
|
110
|
+
if (latestVersion) {
|
|
111
|
+
// Update cache
|
|
112
|
+
writeCache({
|
|
113
|
+
checkedAt: now,
|
|
114
|
+
latestVersion,
|
|
115
|
+
currentVersion
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// Show notification if update available
|
|
119
|
+
if (compareVersions(latestVersion, currentVersion) > 0) {
|
|
120
|
+
showUpdateNotification(currentVersion, latestVersion);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
} catch {
|
|
124
|
+
// Silently fail - don't disrupt user experience
|
|
125
|
+
}
|
|
126
|
+
}
|