UNPKG

3.69 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _cliEngineCommand = require('cli-engine-command');
8
9var _cliEngineCommand2 = _interopRequireDefault(_cliEngineCommand);
10
11var _updater = require('../updater');
12
13var _update = require('./plugins/update');
14
15var _update2 = _interopRequireDefault(_update);
16
17var _hooks = require('../hooks');
18
19var _cliUx = require('cli-ux');
20
21var _cliUx2 = _interopRequireDefault(_cliUx);
22
23var _path = require('path');
24
25var _path2 = _interopRequireDefault(_path);
26
27function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
28
29const debug = require('debug')('cli-engine:update');
30
31function brew(...args) {
32 const cp = require('child_process');
33 debug('brew %o', args);
34 return cp.spawnSync('brew', args, { stdio: 'inherit' });
35}
36
37const cliBin = global.config ? global.config.bin : 'heroku';
38
39class Update extends _cliEngineCommand2.default {
40
41 async run() {
42 // on manual run, also log to file
43 if (!this.flags.autoupdate) {
44 _cliUx2.default.config.errlog = _path2.default.join(this.config.cacheDir, 'autoupdate');
45 }
46 this.updater = new _updater.Updater(this.config, this.cli);
47 if (this.config.updateDisabled === 'Update CLI with `brew upgrade heroku`') {
48 this.migrateBrew();
49 } else if (this.config.updateDisabled) {
50 this.out.warn(this.config.updateDisabled);
51 } else {
52 this.out.action.start(`${this.config.name}: Updating CLI`);
53 let channel = this.argv[0] || this.config.channel;
54 let manifest = await this.updater.fetchManifest(channel);
55 if (this.config.version === manifest.version && channel === this.config.channel) {
56 if (!process.env.CLI_ENGINE_HIDE_UPDATED_MESSAGE) {
57 this.out.action.stop(`already on latest version: ${this.config.version}`);
58 }
59 } else {
60 let { yellow, green } = this.out.color;
61 this.out.action.start(`${this.config.name}: Updating CLI from ${green(this.config.version)} to ${green(manifest.version)}${channel === 'stable' ? '' : ' (' + yellow(channel) + ')'}`);
62 await this.updater.update(manifest);
63 this.out.action.stop();
64 try {
65 await this.updater.autoupdate(true);
66 this.out.exit(0);
67 } catch (err) {
68 this.out.warn(err, 'post-install autoupdate failed');
69 }
70 }
71 }
72 debug('fetch version');
73 await this.updater.fetchVersion(true);
74 debug('plugins update');
75 await _update2.default.run({ ...this.config, argv: [] });
76 debug('log chop');
77 await this.logChop();
78 debug('autocomplete');
79 const hooks = new _hooks.Hooks({ config: this.config });
80 await hooks.run('update');
81 debug('done');
82 this.cli.action.stop();
83 }
84
85 async logChop() {
86 try {
87 const logChopper = require('log-chopper').default;
88 await logChopper.chop(this.config.errlog);
89 } catch (e) {
90 debug(e.message);
91 }
92 }
93
94 migrateBrew() {
95 try {
96 debug('migrating from brew');
97 const fs = require('fs-extra');
98 let p = fs.realpathSync('/usr/local/bin/heroku');
99 if (p.match(/\/usr\/local\/Cellar\/heroku\/\d+\.\d+\.\d+\//)) {
100 // not on private tap, move to it
101 this.out.action.start('Upgrading homebrew formula');
102 brew('tap', 'heroku/brew');
103 brew('upgrade', 'heroku/brew/heroku');
104 this.out.action.stop();
105 }
106 } catch (err) {
107 debug(err);
108 }
109 }
110}
111exports.default = Update;
112Update.topic = 'update';
113Update.description = `update the ${cliBin} CLI`;
114Update.args = [{ name: 'channel', optional: true }];
115Update.flags = {
116 autoupdate: _cliEngineCommand.flags.boolean({ hidden: true })
117};
\No newline at end of file