UNPKG

2.66 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const sdk_1 = require("@cto.ai/sdk");
5const util = require('util');
6const exec = util.promisify(require('child_process').exec);
7const base_1 = tslib_1.__importStar(require("../base"));
8const get_latest_version_1 = tslib_1.__importDefault(require("../utils/get-latest-version"));
9const CustomErrors_1 = require("../errors/CustomErrors");
10class Update extends base_1.default {
11 async run() {
12 this.parse(Update);
13 try {
14 const latestVersion = await get_latest_version_1.default();
15 await this._logUpdateMessage(latestVersion);
16 await this._askQuestion();
17 await this._updateVersion();
18 this._trackAnalytics(latestVersion);
19 }
20 catch (err) {
21 this.debug('%O', err);
22 this.config.runHook('error', { err, accessToken: this.accessToken });
23 }
24 }
25 _logUpdateMessage(latestVersion) {
26 this.log(`${sdk_1.ux.colors.white(`\nšŸ“¦ ${sdk_1.ux.colors.actionBlue('INSTALL UPDATE?')} - You're about to update to ${sdk_1.ux.colors.callOutCyan(`CTO.ai Ops CLI ${latestVersion}`)} ${sdk_1.ux.colors.reset.green('ā†’')}`)}`);
27 }
28 _trackAnalytics(newVersion) {
29 if (this.user) {
30 this.services.analytics.track({
31 userId: this.user.email,
32 teamId: this.team.id,
33 event: 'Ops CLI Update',
34 properties: {
35 oldVersion: this.config.version,
36 newVersion,
37 },
38 }, this.accessToken);
39 }
40 }
41 async _askQuestion() {
42 const { install } = await sdk_1.ux.prompt({
43 type: 'confirm',
44 name: 'install',
45 message: `\n${sdk_1.ux.colors.white('Install update?')}`,
46 });
47 this.log('');
48 if (!install) {
49 this.log(`${sdk_1.ux.colors.errorRed('āŒ Update cancelled')} ${sdk_1.ux.colors.white("- Let us know when you're ready for some sweet, sweet, updates.")}`);
50 process.exit();
51 }
52 }
53 async _updateVersion() {
54 try {
55 sdk_1.ux.spinner.start('Updating version');
56 await exec('npm install -g @cto.ai/ops');
57 sdk_1.ux.spinner.stop('Done!');
58 }
59 catch (err) {
60 this.debug('%O', err);
61 sdk_1.ux.spinner.stop('Failed');
62 throw new CustomErrors_1.PermissionsError(err);
63 }
64 }
65}
66Update.description = 'Update the ops CLI.';
67Update.flags = {
68 help: base_1.flags.help({ char: 'h' }),
69};
70exports.default = Update;