Skip to content

Commit

Permalink
Added support for major blog update
Browse files Browse the repository at this point in the history
refs #759

[ci skip]
  • Loading branch information
kirrg001 committed Aug 2, 2018
1 parent 9808328 commit 1d3f42a
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 3 deletions.
78 changes: 77 additions & 1 deletion lib/commands/update.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const fs = require('fs-extra');
const path = require('path');
const map = require('lodash/map');

// Utils
const errors = require('../errors');
Expand Down Expand Up @@ -60,6 +61,18 @@ class UpdateCommand extends Command {

// TODO: add meaningful update checks after this task
const tasks = [{
title: 'Updating to a major version',
task: this.majorSupport.bind(this),
// CASE: Skip if you are already on ^2 or you update from v1 to v1.
enabled: () => {
if (semver.satisfies(instance.cliConfig.get('active-version'), '^2.0.0') ||
!semver.satisfies(context.version, '^2.0.0')) {
return false;
}

return true;
}
}, {
title: 'Downloading and updating Ghost',
skip: (ctx) => ctx.rollback,
task: this.downloadAndUpdate
Expand All @@ -75,7 +88,7 @@ class UpdateCommand extends Command {
skip: (ctx) => ctx.rollback,
task: migrate,
// CASE: We have moved the execution of knex-migrator into Ghost 2.0.0.
// If you are already on ^2 or you update from ^1 to ^2, then skip the task.
// If you are already on v2 or you update from v1 to v2, then skip the task.
enabled: () => {
if (semver.satisfies(instance.cliConfig.get('active-version'), '^2.0.0') ||
semver.satisfies(context.version, '^2.0.0')) {
Expand Down Expand Up @@ -128,6 +141,69 @@ class UpdateCommand extends Command {
return yarnInstall(ctx.ui, ctx.zip);
}

majorSupport(ctx) {
const majorSupport = require('../utils/major-support');
let gscanReport;
let demoPost;

return majorSupport({
dir: ctx.instance.dir,
database: ctx.instance.config.get('database')
}).then((response) => {
gscanReport = response.gscanReport;
demoPost = response.demoPost;

this.ui.log(`\nYou are about to migrate to Ghost 2.0.0.`, 'green');

this.ui.log(`\n## Theme compatibility`, 'magenta', null, true);

if (!gscanReport.results.error.length && !gscanReport.results.warning.length) {
this.ui.log('\nYour theme is compatible.\n')
} else {
this.ui.log(`\nYour theme has ${gscanReport.results.error.length} errors and ${gscanReport.results.warning.length} warnings.\n`);
this.ui.log('We recommend visiting https://gscan.ghost.org to take a look at the full report.\n');
return this.ui.confirm('Would you like to print the full report anyway?');
}
}).then((answer) => {
if (answer) {
if (gscanReport.results.error.length) {
this.ui.log('\n### Errors\n', 'red');
gscanReport.results.error.forEach((error) => {
this.ui.log(`Rule: ${error.rule}`);
this.ui.log(`File: ${map(error.failures, 'ref').join(',')}`);
});
this.ui.log('\n');
}

if (gscanReport.results.warning.length) {
this.ui.log('\n### Warnings\n', 'yellow');
gscanReport.results.warning.forEach((warning) => {
this.ui.log(`Rule: ${warning.rule}`);
this.ui.log(`File: ${map(warning.failures, 'ref').join(',')}`);
});
this.ui.log('\n');
}
}

return this.ui.confirm('Would you like to look at the demo post?');
}).then((answer) => {
if (answer) {
this.ui.log('\n');
this.ui.log(`${ctx.instance.config.get('url')}p/${demoPost.uuid}/`);
this.ui.log('\n');
}

return this.ui.confirm('Continue with updating to the next major version?')
.then((answer) => {
if (!answer) {
return Promise.reject(new errors.CliError({
message: 'Update aborted. Nothing happens.'
}));
}
});
});
}

stop() {
const StopCommand = require('./stop');

Expand Down
6 changes: 4 additions & 2 deletions lib/ui/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,10 @@ class UI {
* @method log
* @public
*/
log(message, color, stderr) {
if (color) {
log(message, color, stderr, bold) {
if (color && bold) {
message = chalk[color].bold(message);
} else if (color) {
message = chalk[color](message);
}

Expand Down
58 changes: 58 additions & 0 deletions lib/utils/major-support.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const errors = require('../errors');

module.exports = function majorSupport(options = {}) {
const path = require('path');

if (!options.dir) {
return Promise.reject(new errors.CliError({
message: '`dir` is required.'
}))
}

if (!options.database) {
return Promise.reject(new errors.CliError({
message: '`database` is required.'
}))
}

const knexPath = options.knexPath || path.resolve(options.dir, 'current/node_modules/knex');
const gscanPath = options.gscanPath || path.resolve(options.dir, 'current/node_modules/gscan');

const knex = require(knexPath);
const gscan = require(gscanPath);

const connection = knex(Object.assign({useNullAsDefault: true}, options.database));

const themeFolder = options.contentPath || path.resolve(options.dir, 'content', 'themes');
let activeTheme;
let gscanReport;

return connection.raw('SELECT * FROM settings WHERE `key`="active_theme";')
.then((response) => {
activeTheme = response[0].value;

return gscan.check(path.resolve(themeFolder, activeTheme));
})
.then((report) => {
gscanReport = gscan.format(report);

return connection.raw('SELECT uuid FROM posts WHERE slug="v2-demo-post";')
})
.then((demoPost) => {
if (demoPost.length) {
demoPost = demoPost[0];
}

return {
gscanReport: gscanReport,
demoPost: demoPost
}
})
.finally(() => {
return new Promise((resolve) => {
connection.destroy(() => {
resolve();
});
});
});
};

0 comments on commit 1d3f42a

Please sign in to comment.