@indiccoder/mentis-cli 1.0.5 → 1.0.9

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,83 @@
1
+ import { exec } from 'child_process';
2
+ import { promisify } from 'util';
3
+ import path from 'path';
4
+ import fs from 'fs';
5
+ import chalk from 'chalk';
6
+ import ora from 'ora';
7
+ import inquirer from 'inquirer';
8
+
9
+ const execAsync = promisify(exec);
10
+
11
+ export class UpdateManager {
12
+ private packageName: string;
13
+ private currentVersion: string;
14
+
15
+ constructor() {
16
+ const packageJsonPath = path.join(__dirname, '../../package.json');
17
+ try {
18
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
19
+ this.packageName = packageJson.name;
20
+ this.currentVersion = packageJson.version;
21
+ } catch (e) {
22
+ // Fallback if running from a context where package.json isn't found easily (e.g. global install oddities)
23
+ // But usually this works relative to dist/utils/
24
+ this.packageName = '@indiccoder/mentis-cli';
25
+ this.currentVersion = '0.0.0';
26
+ }
27
+ }
28
+
29
+ public async checkAndPerformUpdate(interactive: boolean = true) {
30
+ const spinner = ora('Checking for updates...').start();
31
+
32
+ try {
33
+ // Check latest version from NPM registry
34
+ const { stdout } = await execAsync(`npm view ${this.packageName} version`);
35
+ const latestVersion = stdout.trim();
36
+
37
+ if (latestVersion === this.currentVersion) {
38
+ spinner.succeed(chalk.green(`You are on the latest version (${this.currentVersion}).`));
39
+ return;
40
+ }
41
+
42
+ spinner.info(chalk.blue(`Update available: ${this.currentVersion} -> ${chalk.bold(latestVersion)}`));
43
+
44
+ if (!interactive) {
45
+ // If running in non-interactive mode (e.g. auto-check prompt), maybe just log it.
46
+ // But for explicit 'update' command, we usually assume interactive or force.
47
+ console.log(chalk.yellow(`Run 'mentis update' or '/update' inside the tool to upgrade.`));
48
+ return;
49
+ }
50
+
51
+ const { confirm } = await inquirer.prompt([{
52
+ type: 'confirm',
53
+ name: 'confirm',
54
+ message: `Do you want to install v${latestVersion} now?`,
55
+ default: true
56
+ }]);
57
+
58
+ if (confirm) {
59
+ await this.installUpdate(latestVersion);
60
+ } else {
61
+ console.log(chalk.yellow('Update skipped.'));
62
+ }
63
+
64
+ } catch (error: any) {
65
+ spinner.fail(chalk.red('Failed to check for updates.'));
66
+ if (process.env.DEBUG) console.error(error);
67
+ }
68
+ }
69
+
70
+ private async installUpdate(version: string) {
71
+ const spinner = ora(`Installing ${this.packageName}@${version}...`).start();
72
+ try {
73
+ await execAsync(`npm install -g ${this.packageName}@latest`);
74
+ spinner.succeed(chalk.green('Update completed successfully!'));
75
+ console.log(chalk.cyan('Please restart Mentis to use the new version.'));
76
+ process.exit(0);
77
+ } catch (error: any) {
78
+ spinner.fail(chalk.red('Update failed.'));
79
+ console.error(chalk.red('Error details:'), error.message);
80
+ console.log(chalk.yellow(`Try running: npm install -g ${this.packageName}@latest`));
81
+ }
82
+ }
83
+ }