creepercli 1.0.0 → 1.1.0

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/src/update.js CHANGED
@@ -1,87 +1,73 @@
1
- /*
2
- * Copyright 2026 DemonZDevelopment
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
-
17
- 'use strict';
18
-
19
- const fs = require('fs');
20
- const path = require('path');
21
- const https = require('https');
22
- const { DIR } = require('./session');
23
- const pkg = require('../package.json');
24
-
25
- const UPDATE_FILE = path.join(DIR, 'update.json');
26
- const CHECK_INTERVAL_MS = 24 * 60 * 60 * 1000; // 24 hours
27
-
28
- function checkUpdate() {
29
- try {
30
- const now = Date.now();
31
- let cache = {};
32
- try {
33
- cache = JSON.parse(fs.readFileSync(UPDATE_FILE, 'utf8'));
34
- } catch {
35
- }
36
-
37
- if (cache.lastCheck && now - cache.lastCheck < CHECK_INTERVAL_MS) {
38
- if (cache.latest && isNewerVersion(pkg.version, cache.latest)) {
39
- console.log(`[Notice] A new version of creepercli (v${cache.latest}) is available! Run "npm install -g creeper-cli" to update.\n`);
40
- }
41
- return;
42
- }
43
-
44
- const req = https.get('https://registry.npmjs.org/creeper-cli/latest', {
45
- timeout: 2500,
46
- headers: { 'User-Agent': `creeper-cli/${pkg.version}` }
47
- }, (res) => {
48
- if (res.statusCode !== 200) return;
49
- let body = '';
50
- res.on('data', (chunk) => body += chunk);
51
- res.on('end', () => {
52
- try {
53
- const data = JSON.parse(body);
54
- if (data && data.version) {
55
- const latest = data.version;
56
- fs.mkdirSync(DIR, { recursive: true, mode: 0o700 });
57
- fs.writeFileSync(UPDATE_FILE, JSON.stringify({ lastCheck: now, latest }), { mode: 0o600 });
58
- if (isNewerVersion(pkg.version, latest)) {
59
- console.log(`[Notice] A new version of creepercli (v${latest}) is available! Run "npm install -g creeper-cli" to update.\n`);
60
- }
61
- }
62
- } catch {
63
- }
64
- });
65
- });
66
-
1
+
2
+
3
+ 'use strict';
4
+
5
+ const fs = require('fs');
6
+ const path = require('path');
7
+ const https = require('https');
8
+ const { DIR } = require('./session');
9
+ const pkg = require('../package.json');
10
+
11
+ const UPDATE_FILE = path.join(DIR, 'update.json');
12
+ const CHECK_INTERVAL_MS = 24 * 60 * 60 * 1000;
13
+
14
+ function checkUpdate() {
15
+ try {
16
+ const now = Date.now();
17
+ let cache = {};
18
+ try {
19
+ cache = JSON.parse(fs.readFileSync(UPDATE_FILE, 'utf8'));
20
+ } catch {
21
+ }
22
+
23
+ if (cache.lastCheck && now - cache.lastCheck < CHECK_INTERVAL_MS) {
24
+ if (cache.latest && isNewerVersion(pkg.version, cache.latest)) {
25
+ console.log(`[Notice] A new version of creepercli (v${cache.latest}) is available! Run "npm install -g creepercli" to update.\n`);
26
+ }
27
+ return;
28
+ }
29
+
30
+ const req = https.get('https://registry.npmjs.org/creepercli/latest', {
31
+ timeout: 2500,
32
+ headers: { 'User-Agent': `creepercli/${pkg.version}` }
33
+ }, (res) => {
34
+ if (res.statusCode !== 200) return;
35
+ let body = '';
36
+ res.on('data', (chunk) => body += chunk);
37
+ res.on('end', () => {
38
+ try {
39
+ const data = JSON.parse(body);
40
+ if (data && data.version) {
41
+ const latest = data.version;
42
+ fs.mkdirSync(DIR, { recursive: true, mode: 0o700 });
43
+ fs.writeFileSync(UPDATE_FILE, JSON.stringify({ lastCheck: now, latest }), { mode: 0o600 });
44
+ if (isNewerVersion(pkg.version, latest)) {
45
+ console.log(`[Notice] A new version of creepercli (v${latest}) is available! Run "npm install -g creepercli" to update.\n`);
46
+ }
47
+ }
48
+ } catch {
49
+ }
50
+ });
51
+ });
52
+
67
53
  req.on('error', () => {});
68
- req.end();
54
+ req.on('socket', (socket) => socket.unref());
69
55
  } catch {
70
- }
71
- }
72
-
73
- function isNewerVersion(current, latest) {
74
- if (!current || !latest) return false;
75
- const c = current.split('-')[0].split('.').map((x) => parseInt(x, 10) || 0);
76
- const l = latest.split('-')[0].split('.').map((x) => parseInt(x, 10) || 0);
77
- const max = Math.max(c.length, l.length);
78
- for (let i = 0; i < max; i++) {
79
- const cv = c[i] || 0;
80
- const lv = l[i] || 0;
81
- if (lv > cv) return true;
82
- if (lv < cv) return false;
83
- }
84
- return false;
85
- }
86
-
87
- module.exports = { checkUpdate };
56
+ }
57
+ }
58
+
59
+ function isNewerVersion(current, latest) {
60
+ if (!current || !latest) return false;
61
+ const c = current.split('-')[0].split('.').map((x) => parseInt(x, 10) || 0);
62
+ const l = latest.split('-')[0].split('.').map((x) => parseInt(x, 10) || 0);
63
+ const max = Math.max(c.length, l.length);
64
+ for (let i = 0; i < max; i++) {
65
+ const cv = c[i] || 0;
66
+ const lv = l[i] || 0;
67
+ if (lv > cv) return true;
68
+ if (lv < cv) return false;
69
+ }
70
+ return false;
71
+ }
72
+
73
+ module.exports = { checkUpdate };