coffeeinabit 0.0.46 → 0.0.47
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/helpers/version_check.js +172 -0
- package/package.json +1 -1
- package/server.js +6 -0
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { exec } from 'child_process';
|
|
3
|
+
import { promisify } from 'util';
|
|
4
|
+
import { readFileSync } from 'fs';
|
|
5
|
+
import { fileURLToPath } from 'url';
|
|
6
|
+
import { dirname, join } from 'path';
|
|
7
|
+
|
|
8
|
+
const execAsync = promisify(exec);
|
|
9
|
+
|
|
10
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
11
|
+
const __dirname = dirname(__filename);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Version Check Helper
|
|
15
|
+
*
|
|
16
|
+
* Checks npm registry for newer version and automatically installs it if available.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Get current version from package.json
|
|
21
|
+
* @returns {string} Current version
|
|
22
|
+
*/
|
|
23
|
+
function getCurrentVersion() {
|
|
24
|
+
try {
|
|
25
|
+
const packagePath = join(__dirname, '..', 'package.json');
|
|
26
|
+
const packageJson = JSON.parse(readFileSync(packagePath, 'utf8'));
|
|
27
|
+
return packageJson.version;
|
|
28
|
+
} catch (error) {
|
|
29
|
+
console.error('[VersionCheck] Error reading package.json:', error.message);
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Query npm registry for latest version
|
|
36
|
+
* @param {string} packageName - Package name to check
|
|
37
|
+
* @returns {Promise<string|null>} Latest version or null if error
|
|
38
|
+
*/
|
|
39
|
+
async function getLatestVersion(packageName) {
|
|
40
|
+
try {
|
|
41
|
+
const registryUrl = `https://registry.npmjs.org/${packageName}/latest`;
|
|
42
|
+
const response = await axios.get(registryUrl, {
|
|
43
|
+
timeout: 5000,
|
|
44
|
+
headers: {
|
|
45
|
+
'Accept': 'application/json'
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
return response.data.version;
|
|
49
|
+
} catch (error) {
|
|
50
|
+
console.error('[VersionCheck] Error querying npm registry:', error.message);
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Compare two semantic versions
|
|
57
|
+
* @param {string} current - Current version
|
|
58
|
+
* @param {string} latest - Latest version
|
|
59
|
+
* @returns {boolean} True if latest is newer than current
|
|
60
|
+
*/
|
|
61
|
+
function isNewerVersion(current, latest) {
|
|
62
|
+
if (!current || !latest) return false;
|
|
63
|
+
|
|
64
|
+
const currentParts = current.split('.').map(Number);
|
|
65
|
+
const latestParts = latest.split('.').map(Number);
|
|
66
|
+
|
|
67
|
+
for (let i = 0; i < Math.max(currentParts.length, latestParts.length); i++) {
|
|
68
|
+
const currentPart = currentParts[i] || 0;
|
|
69
|
+
const latestPart = latestParts[i] || 0;
|
|
70
|
+
|
|
71
|
+
if (latestPart > currentPart) return true;
|
|
72
|
+
if (latestPart < currentPart) return false;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Install the latest version globally
|
|
80
|
+
* @param {string} packageName - Package name to install
|
|
81
|
+
* @returns {Promise<boolean>} True if installation succeeded
|
|
82
|
+
*/
|
|
83
|
+
async function installLatestVersion(packageName) {
|
|
84
|
+
try {
|
|
85
|
+
console.log(`[VersionCheck] Installing ${packageName}@latest...`);
|
|
86
|
+
const { stdout, stderr } = await execAsync(`npm install -g ${packageName}@latest`, {
|
|
87
|
+
timeout: 120000 // 2 minutes timeout
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
if (stderr && !stderr.includes('npm WARN')) {
|
|
91
|
+
console.error('[VersionCheck] Installation warnings:', stderr);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
console.log('[VersionCheck] ✅ Successfully installed latest version');
|
|
95
|
+
console.log('[VersionCheck] Please restart the application to use the new version');
|
|
96
|
+
return true;
|
|
97
|
+
} catch (error) {
|
|
98
|
+
console.error('[VersionCheck] ❌ Failed to install latest version:', error.message);
|
|
99
|
+
if (error.stdout) console.log('[VersionCheck] stdout:', error.stdout);
|
|
100
|
+
if (error.stderr) console.error('[VersionCheck] stderr:', error.stderr);
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Check for updates and install if newer version is available
|
|
107
|
+
* @param {string} packageName - Package name to check (default: 'coffeeinabit')
|
|
108
|
+
* @param {boolean} autoInstall - Whether to automatically install (default: true)
|
|
109
|
+
* @returns {Promise<object>} Update check result
|
|
110
|
+
*/
|
|
111
|
+
export async function checkAndUpdateVersion(packageName = 'coffeeinabit', autoInstall = true) {
|
|
112
|
+
try {
|
|
113
|
+
console.log('[VersionCheck] Checking for updates...');
|
|
114
|
+
|
|
115
|
+
const currentVersion = getCurrentVersion();
|
|
116
|
+
if (!currentVersion) {
|
|
117
|
+
return {
|
|
118
|
+
success: false,
|
|
119
|
+
error: 'Could not read current version'
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
console.log(`[VersionCheck] Current version: ${currentVersion}`);
|
|
124
|
+
|
|
125
|
+
const latestVersion = await getLatestVersion(packageName);
|
|
126
|
+
if (!latestVersion) {
|
|
127
|
+
return {
|
|
128
|
+
success: false,
|
|
129
|
+
error: 'Could not query npm registry'
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
console.log(`[VersionCheck] Latest version: ${latestVersion}`);
|
|
134
|
+
|
|
135
|
+
if (!isNewerVersion(currentVersion, latestVersion)) {
|
|
136
|
+
console.log('[VersionCheck] ✅ You are running the latest version');
|
|
137
|
+
return {
|
|
138
|
+
success: true,
|
|
139
|
+
currentVersion,
|
|
140
|
+
latestVersion,
|
|
141
|
+
updateAvailable: false
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
console.log(`[VersionCheck] ⚠️ New version available: ${latestVersion} (current: ${currentVersion})`);
|
|
146
|
+
|
|
147
|
+
if (autoInstall) {
|
|
148
|
+
const installed = await installLatestVersion(packageName);
|
|
149
|
+
return {
|
|
150
|
+
success: installed,
|
|
151
|
+
currentVersion,
|
|
152
|
+
latestVersion,
|
|
153
|
+
updateAvailable: true,
|
|
154
|
+
installed
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
success: true,
|
|
160
|
+
currentVersion,
|
|
161
|
+
latestVersion,
|
|
162
|
+
updateAvailable: true,
|
|
163
|
+
installed: false
|
|
164
|
+
};
|
|
165
|
+
} catch (error) {
|
|
166
|
+
console.error('[VersionCheck] Error during version check:', error.message);
|
|
167
|
+
return {
|
|
168
|
+
success: false,
|
|
169
|
+
error: error.message
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
}
|
package/package.json
CHANGED
package/server.js
CHANGED
|
@@ -17,6 +17,7 @@ import { createAuthRoutes } from './routes/auth.js';
|
|
|
17
17
|
import { createAutomationRoutes, createStateRoutes } from './routes/automation.js';
|
|
18
18
|
import { setupSocketHandlers } from './socket/handlers.js';
|
|
19
19
|
import { tryAutoStartAutomation } from './helpers/auto_start.js';
|
|
20
|
+
import { checkAndUpdateVersion } from './helpers/version_check.js';
|
|
20
21
|
|
|
21
22
|
dotenv.config();
|
|
22
23
|
|
|
@@ -104,6 +105,11 @@ server.listen(PORT, async () => {
|
|
|
104
105
|
console.log(' Press Ctrl+C to stop');
|
|
105
106
|
console.log('');
|
|
106
107
|
|
|
108
|
+
// Check for updates on startup (non-blocking)
|
|
109
|
+
checkAndUpdateVersion('coffeeinabit', true).catch(err => {
|
|
110
|
+
console.error('[Server] Version check failed:', err.message);
|
|
111
|
+
});
|
|
112
|
+
|
|
107
113
|
// Wait a bit for everything to initialize, then try to auto-start automation
|
|
108
114
|
setTimeout(() => {
|
|
109
115
|
tryAutoStartAutomation(sessionStore, contextDir, cloudAuth, linkedinAutomation);
|