codelibrium-cli 1.1.2 → 1.1.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/index.js +98 -3
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -23,6 +23,99 @@ const TOOL_TO_FILENAME = {
|
|
|
23
23
|
};
|
|
24
24
|
|
|
25
25
|
const VALID_TOOLS = Object.keys(TOOL_TO_FILENAME);
|
|
26
|
+
const NPM_PACKAGE = 'codelibrium-cli';
|
|
27
|
+
|
|
28
|
+
function readCliVersion() {
|
|
29
|
+
try {
|
|
30
|
+
const pkgPath = path.join(__dirname, 'package.json');
|
|
31
|
+
const j = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
32
|
+
return typeof j.version === 'string' ? j.version : null;
|
|
33
|
+
} catch {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function versionParts(ver) {
|
|
39
|
+
return String(ver)
|
|
40
|
+
.split('.')
|
|
41
|
+
.map((part) => {
|
|
42
|
+
const m = /^(\d+)/.exec(part);
|
|
43
|
+
return m ? parseInt(m[1], 10) : 0;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** True if `latest` is greater than `current` (simple numeric semver segments). */
|
|
48
|
+
function isNewerAvailable(latest, current) {
|
|
49
|
+
const la = versionParts(latest);
|
|
50
|
+
const cu = versionParts(current);
|
|
51
|
+
const len = Math.max(la.length, cu.length);
|
|
52
|
+
for (let i = 0; i < len; i++) {
|
|
53
|
+
const L = la[i] ?? 0;
|
|
54
|
+
const C = cu[i] ?? 0;
|
|
55
|
+
if (L > C) return true;
|
|
56
|
+
if (L < C) return false;
|
|
57
|
+
}
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function fetchLatestVersionFromNpm() {
|
|
62
|
+
return new Promise((resolve) => {
|
|
63
|
+
const req = https.request(
|
|
64
|
+
{
|
|
65
|
+
hostname: 'registry.npmjs.org',
|
|
66
|
+
path: `/${NPM_PACKAGE}/latest`,
|
|
67
|
+
method: 'GET',
|
|
68
|
+
headers: { Accept: 'application/json', 'User-Agent': 'codelibrium-cli' },
|
|
69
|
+
},
|
|
70
|
+
(res) => {
|
|
71
|
+
let body = '';
|
|
72
|
+
res.setEncoding('utf8');
|
|
73
|
+
res.on('data', (chunk) => {
|
|
74
|
+
body += chunk;
|
|
75
|
+
});
|
|
76
|
+
res.on('end', () => {
|
|
77
|
+
if (res.statusCode < 200 || res.statusCode >= 300) {
|
|
78
|
+
resolve(null);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
const j = JSON.parse(body);
|
|
83
|
+
resolve(typeof j.version === 'string' ? j.version : null);
|
|
84
|
+
} catch {
|
|
85
|
+
resolve(null);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
},
|
|
89
|
+
);
|
|
90
|
+
req.setTimeout(4500, () => {
|
|
91
|
+
req.destroy();
|
|
92
|
+
resolve(null);
|
|
93
|
+
});
|
|
94
|
+
req.on('error', () => resolve(null));
|
|
95
|
+
req.end();
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function promptUpdateIfAvailable() {
|
|
100
|
+
const current = readCliVersion();
|
|
101
|
+
if (!current) return;
|
|
102
|
+
|
|
103
|
+
const latest = await fetchLatestVersionFromNpm();
|
|
104
|
+
if (!latest || !isNewerAvailable(latest, current)) return;
|
|
105
|
+
|
|
106
|
+
console.log('');
|
|
107
|
+
console.log('────────────────────────────────────────────────────────────');
|
|
108
|
+
console.log(`A new version of codelibrium-cli is available (${current} → ${latest}).`);
|
|
109
|
+
console.log('Update with:');
|
|
110
|
+
console.log('');
|
|
111
|
+
console.log(' npm i -g codelibrium-cli');
|
|
112
|
+
console.log('');
|
|
113
|
+
console.log('Or run the latest CLI once with:');
|
|
114
|
+
console.log('');
|
|
115
|
+
console.log(' npx codelibrium-cli@latest login');
|
|
116
|
+
console.log('────────────────────────────────────────────────────────────');
|
|
117
|
+
console.log('');
|
|
118
|
+
}
|
|
26
119
|
|
|
27
120
|
function configDirPath() {
|
|
28
121
|
return path.join(os.homedir(), '.codelibrium');
|
|
@@ -180,9 +273,11 @@ async function cmdWhoami() {
|
|
|
180
273
|
console.log(`${name} <${email}>`);
|
|
181
274
|
}
|
|
182
275
|
|
|
183
|
-
function cmdLogin() {
|
|
276
|
+
async function cmdLogin() {
|
|
277
|
+
await promptUpdateIfAvailable();
|
|
278
|
+
|
|
184
279
|
const state = crypto.randomBytes(16).toString('hex');
|
|
185
|
-
const authUrl = `
|
|
280
|
+
const authUrl = `http://localhost:5173/cli-auth?state=${encodeURIComponent(state)}`;
|
|
186
281
|
|
|
187
282
|
const server = http.createServer((req, res) => {
|
|
188
283
|
let u;
|
|
@@ -310,7 +405,7 @@ async function main() {
|
|
|
310
405
|
return;
|
|
311
406
|
}
|
|
312
407
|
if (cmd === 'login') {
|
|
313
|
-
cmdLogin();
|
|
408
|
+
await cmdLogin();
|
|
314
409
|
return;
|
|
315
410
|
}
|
|
316
411
|
if (cmd === 'logout') {
|