@cto.ai/ops
Version:
š» CTO.ai - The CLI built for Teams š
76 lines (75 loc) ⢠3.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const cli_sdk_1 = require("@cto.ai/cli-sdk");
const util = tslib_1.__importStar(require("util"));
const child_process_1 = tslib_1.__importDefault(require("child_process"));
const exec = util.promisify(child_process_1.default.exec);
const base_1 = tslib_1.__importStar(require("./../base"));
const get_latest_version_1 = require("./../utils/get-latest-version");
const CustomErrors_1 = require("./../errors/CustomErrors");
class Update extends base_1.default {
async run() {
this.parse(Update);
try {
const latestVersion = await (0, get_latest_version_1.getLatestVersion)();
if (this.config.version !== latestVersion) {
await this.logUpdateMessage(latestVersion);
await this.askQuestion();
await this.updateVersion();
await this.trackAnalytics(latestVersion);
}
else {
await this.logUptoDateMessage(latestVersion);
}
}
catch (err) {
this.debug('%O', err);
this.config.runHook('error', { err, accessToken: this.accessToken });
}
}
async logUpdateMessage(latestVersion) {
this.log(`${cli_sdk_1.ux.colors.white(`\nš¦ ${cli_sdk_1.ux.colors.actionBlue('INSTALL UPDATE?')} - You're currently running ${cli_sdk_1.ux.colors.callOutCyan(`Ops CLI ${this.config.version}`)}. A newer version is available (${cli_sdk_1.ux.colors.callOutCyan(`Ops CLI ${latestVersion}`)}) ${cli_sdk_1.ux.colors.reset.green('ā')}`)}`);
}
async logUptoDateMessage(latestVersion) {
this.log(`\nYou're already on the latest Ops CLI (${cli_sdk_1.ux.colors.callOutCyan(`${latestVersion}`)})!`);
}
async trackAnalytics(newVersion) {
if (this.user) {
this.services.analytics.track('Ops CLI Update', {
username: this.user.username,
oldVersion: this.config.version,
newVersion,
}, this.state.config);
}
}
async askQuestion() {
const { install } = await cli_sdk_1.ux.prompt({
type: 'confirm',
name: 'install',
message: `\n${cli_sdk_1.ux.colors.white('Install update?')}`,
});
this.log('');
if (!install) {
this.log(`${cli_sdk_1.ux.colors.errorRed('ā Update cancelled')} ${cli_sdk_1.ux.colors.white("- Let us know when you're ready for some sweet, sweet, updates.")}`);
process.exit();
}
}
async updateVersion() {
try {
cli_sdk_1.ux.spinner.start('Updating version');
await exec('npm install -g @cto.ai/ops');
cli_sdk_1.ux.spinner.stop('Done!');
}
catch (err) {
this.debug('%O', err);
cli_sdk_1.ux.spinner.stop('Failed');
throw new CustomErrors_1.PermissionsError(err);
}
}
}
exports.default = Update;
Update.description = 'Update The Ops CLI.';
Update.flags = {
help: base_1.flags.help({ char: 'h' }),
};