1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const sdk_1 = require("@cto.ai/sdk");
|
5 | const util = require('util');
|
6 | const exec = util.promisify(require('child_process').exec);
|
7 | const base_1 = tslib_1.__importStar(require("../base"));
|
8 | const get_latest_version_1 = require("../utils/get-latest-version");
|
9 | const CustomErrors_1 = require("../errors/CustomErrors");
|
10 | class Update extends base_1.default {
|
11 | async run() {
|
12 | this.parse(Update);
|
13 | try {
|
14 | const latestVersion = await get_latest_version_1.getLatestVersion();
|
15 | await this.logUpdateMessage(latestVersion);
|
16 | await this.askQuestion();
|
17 | await this.updateVersion();
|
18 | await this.trackAnalytics(latestVersion);
|
19 | }
|
20 | catch (err) {
|
21 | this.debug('%O', err);
|
22 | this.config.runHook('error', { err, accessToken: this.accessToken });
|
23 | }
|
24 | }
|
25 | async 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 | async trackAnalytics(newVersion) {
|
29 | if (this.user) {
|
30 | await this.services.analytics.track({
|
31 | userId: this.user.email,
|
32 | teamId: this.team.id,
|
33 | cliEvent: 'Ops CLI Update',
|
34 | event: 'Ops CLI Update',
|
35 | properties: {
|
36 | oldVersion: this.config.version,
|
37 | newVersion,
|
38 | },
|
39 | }, this.accessToken);
|
40 | }
|
41 | }
|
42 | async askQuestion() {
|
43 | const { install } = await sdk_1.ux.prompt({
|
44 | type: 'confirm',
|
45 | name: 'install',
|
46 | message: `\n${sdk_1.ux.colors.white('Install update?')}`,
|
47 | });
|
48 | this.log('');
|
49 | if (!install) {
|
50 | 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.")}`);
|
51 | process.exit();
|
52 | }
|
53 | }
|
54 | async updateVersion() {
|
55 | try {
|
56 | sdk_1.ux.spinner.start('Updating version');
|
57 | await exec('npm install -g @cto.ai/ops');
|
58 | sdk_1.ux.spinner.stop('Done!');
|
59 | }
|
60 | catch (err) {
|
61 | this.debug('%O', err);
|
62 | sdk_1.ux.spinner.stop('Failed');
|
63 | throw new CustomErrors_1.PermissionsError(err);
|
64 | }
|
65 | }
|
66 | }
|
67 | exports.default = Update;
|
68 | Update.description = 'Update the Ops CLI.';
|
69 | Update.flags = {
|
70 | help: base_1.flags.help({ char: 'h' }),
|
71 | };
|