Skip to content

Commit ae1f9ce

Browse files
authored
refactor(update): refactor update command (#1049)
no issue - refactor command/tests to use async/await - remove runCommand usage for start/stop
1 parent be69f38 commit ae1f9ce

File tree

2 files changed

+369
-455
lines changed

2 files changed

+369
-455
lines changed

lib/commands/update.js

+67-89
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
'use strict';
21
const fs = require('fs-extra');
32
const path = require('path');
43

54
// Utils
6-
const errors = require('../errors');
5+
const {GhostError} = require('../errors');
76
const Command = require('../command');
87
const DoctorCommand = require('./doctor');
98

@@ -15,7 +14,7 @@ class UpdateCommand extends Command {
1514
return yargs;
1615
}
1716

18-
run(argv) {
17+
async run(argv) {
1918
const chalk = require('chalk');
2019
const semver = require('semver');
2120

@@ -56,79 +55,74 @@ class UpdateCommand extends Command {
5655
context.installPath = path.join(process.cwd(), 'versions', context.version);
5756
}
5857

59-
return instance.isRunning().then((isRunning) => {
60-
if (isRunning) {
61-
instance.loadRunningEnvironment(true);
58+
const isRunning = await instance.isRunning();
59+
if (isRunning) {
60+
instance.loadRunningEnvironment(true);
6261

63-
// argv.restart will only be undefined if it wasn't passed in
64-
if (!('restart' in argv)) {
65-
argv.restart = true;
66-
}
62+
// argv.restart will only be undefined if it wasn't passed in
63+
if (!('restart' in argv)) {
64+
argv.restart = true; // eslint-disable-line require-atomic-updates
6765
}
66+
}
6867

69-
instance.checkEnvironment();
70-
71-
// TODO: add meaningful update checks after this task
72-
const tasks = [{
73-
title: 'Downloading and updating Ghost',
74-
skip: ({rollback}) => rollback,
75-
task: this.downloadAndUpdate
76-
}, {
77-
title: 'Updating to a major version',
78-
task: majorUpdate,
79-
enabled: () => semver.major(context.version) > semver.major(instance.version)
80-
}, {
81-
title: 'Stopping Ghost',
82-
enabled: () => isRunning,
83-
task: this.stop.bind(this)
84-
}, {
85-
title: 'Rolling back database migrations',
86-
enabled: ({rollback}) => rollback,
87-
task: migrator.rollback
88-
}, {
89-
title: 'Linking latest Ghost and recording versions',
90-
task: this.link
91-
}, {
92-
title: 'Running database migrations',
93-
skip: ({rollback}) => rollback,
94-
task: migrator.migrate,
95-
// CASE: We have moved the execution of knex-migrator into Ghost 2.0.0.
96-
// If you are already on v2 or you update from v1 to v2, then skip the task.
97-
// We compare the major versions, otherwise pre-releases won't match.
98-
enabled: () => semver.major(context.version) < 2
99-
}, {
100-
title: 'Restarting Ghost',
101-
enabled: () => argv.restart,
102-
task: this.restart.bind(this)
103-
}, {
104-
title: 'Removing old Ghost versions',
105-
skip: ({rollback}) => rollback,
106-
task: this.removeOldVersions
107-
}];
108-
109-
return this.runCommand(DoctorCommand, Object.assign(
110-
{quiet: true, categories: ['update']},
111-
argv
112-
)).then(
113-
() => this.runCommand(MigrateCommand, {quiet: true})
114-
).then(
115-
() => this.ui.run(() => this.version(context), 'Checking for latest Ghost version')
116-
).then((result) => {
117-
if (!result) {
118-
this.ui.log('All up to date!', 'cyan');
119-
return;
120-
}
121-
122-
return this.ui.listr(tasks, context)
123-
.catch((error) => {
124-
if (error instanceof errors.GhostError && !context.rollback) {
125-
return this.rollbackFromFail(error, context.version, argv['auto-rollback']);
126-
}
127-
128-
throw error;
129-
});
130-
});
131-
});
68+
instance.checkEnvironment();
69+
70+
// TODO: add meaningful update checks after this task
71+
const tasks = [{
72+
title: 'Downloading and updating Ghost',
73+
skip: ({rollback}) => rollback,
74+
task: this.downloadAndUpdate
75+
}, {
76+
title: 'Updating to a major version',
77+
task: majorUpdate,
78+
enabled: () => semver.major(context.version) > semver.major(instance.version)
79+
}, {
80+
title: 'Stopping Ghost',
81+
enabled: () => isRunning,
82+
task: () => instance.stop()
83+
}, {
84+
title: 'Rolling back database migrations',
85+
enabled: ({rollback}) => rollback,
86+
task: migrator.rollback
87+
}, {
88+
title: 'Linking latest Ghost and recording versions',
89+
task: this.link
90+
}, {
91+
title: 'Running database migrations',
92+
skip: ({rollback}) => rollback,
93+
task: migrator.migrate,
94+
// CASE: We have moved the execution of knex-migrator into Ghost 2.0.0.
95+
// If you are already on v2 or you update from v1 to v2, then skip the task.
96+
// We compare the major versions, otherwise pre-releases won't match.
97+
enabled: () => semver.major(context.version) < 2
98+
}, {
99+
title: 'Restarting Ghost',
100+
enabled: () => argv.restart,
101+
task: () => instance.start()
102+
}, {
103+
title: 'Removing old Ghost versions',
104+
skip: ({rollback}) => rollback,
105+
task: this.removeOldVersions
106+
}];
107+
108+
await this.runCommand(DoctorCommand, {quiet: true, categories: ['update'], ...argv});
109+
await this.runCommand(MigrateCommand, {quiet: true});
110+
const result = await this.ui.run(() => this.version(context), 'Checking for latest Ghost version');
111+
112+
if (!result) {
113+
this.ui.log('All up to date!', 'cyan');
114+
return;
115+
}
116+
117+
try {
118+
await this.ui.listr(tasks, context);
119+
} catch (error) {
120+
if (error instanceof GhostError && !context.rollback) {
121+
return this.rollbackFromFail(error, context.version, argv['auto-rollback']);
122+
}
123+
124+
throw error;
125+
}
132126
}
133127

134128
downloadAndUpdate(ctx, task) {
@@ -147,22 +141,6 @@ class UpdateCommand extends Command {
147141
return yarnInstall(ctx.ui, ctx.zip);
148142
}
149143

150-
stop() {
151-
const StopCommand = require('./stop');
152-
153-
return this.runCommand(StopCommand, {quiet: true}).catch((error) => {
154-
if (!(error instanceof errors.SystemError) || !error.message.match(/No running Ghost instance/)) {
155-
return Promise.reject(error);
156-
}
157-
});
158-
}
159-
160-
restart() {
161-
const StartCommand = require('./start');
162-
163-
return this.runCommand(StartCommand, {quiet: true});
164-
}
165-
166144
removeOldVersions(ctx, task) {
167145
const semver = require('semver');
168146

0 commit comments

Comments
 (0)