|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +module.exports = function ui(ctx) { |
| 4 | + const chalk = require('chalk'); |
| 5 | + const logSymbols = require('log-symbols'); |
| 6 | + const each = require('lodash/each'); |
| 7 | + const errors = require('../../errors'); |
| 8 | + const getData = require('./data'); |
| 9 | + let gscanReport; |
| 10 | + let demoPost; |
| 11 | + |
| 12 | + return getData({ |
| 13 | + dir: ctx.instance.dir, |
| 14 | + database: ctx.instance.config.get('database'), |
| 15 | + version: `versions/${ctx.version}` |
| 16 | + }).then((response) => { |
| 17 | + gscanReport = response.gscanReport; |
| 18 | + demoPost = response.demoPost; |
| 19 | + |
| 20 | + ctx.ui.log(chalk.bold.underline.white(`\n\nChecking theme compatibility for Ghost ${ctx.version}\n`)); |
| 21 | + if (!gscanReport.results.error.all.length && !gscanReport.results.warning.all.length) { |
| 22 | + ctx.ui.log(`${logSymbols.success} Your theme is compatible.\n`); |
| 23 | + |
| 24 | + if (demoPost && demoPost.uuid) { |
| 25 | + const demoLink = `${ctx.instance.config.get('url')}p/${demoPost.uuid}/`; |
| 26 | + ctx.ui.log(`Visit the demo post at ${chalk.cyan(demoLink)} to see how your theme looks like in Ghost 2.0`); |
| 27 | + } |
| 28 | + |
| 29 | + ctx.ui.log(`You can also check theme compatibility at ${chalk.cyan('https://gscan.ghost.org')}\n`); |
| 30 | + } else { |
| 31 | + let message = ''; |
| 32 | + |
| 33 | + if (gscanReport.results.warning.all.length && !gscanReport.results.error.all.length) { |
| 34 | + message += `${chalk.yellow('⚠')} Your theme has `; |
| 35 | + |
| 36 | + let text = 'warning'; |
| 37 | + |
| 38 | + if (gscanReport.results.warning.all.length > 1) { |
| 39 | + text = 'warnings'; |
| 40 | + } |
| 41 | + |
| 42 | + message += chalk.bold.yellow(`${gscanReport.results.warning.all.length} ${text}`); |
| 43 | + } else if (!gscanReport.results.warning.all.length && gscanReport.results.error.all.length) { |
| 44 | + message += `${chalk.red('⚠')} Your theme has `; |
| 45 | + |
| 46 | + let text = 'error'; |
| 47 | + |
| 48 | + if (gscanReport.results.error.all.length > 1) { |
| 49 | + text = 'errors'; |
| 50 | + } |
| 51 | + |
| 52 | + message += chalk.bold.red(`${gscanReport.results.error.all.length} ${text}`); |
| 53 | + } else if (gscanReport.results.warning.all.length && gscanReport.results.error.all.length) { |
| 54 | + message += `${chalk.red('⚠')} Your theme has `; |
| 55 | + |
| 56 | + let text1 = 'error'; |
| 57 | + let text2 = 'warning'; |
| 58 | + |
| 59 | + if (gscanReport.results.error.all.length > 1) { |
| 60 | + text1 = 'errors'; |
| 61 | + } |
| 62 | + |
| 63 | + if (gscanReport.results.warning.all.length > 1) { |
| 64 | + text2 = 'warnings'; |
| 65 | + } |
| 66 | + |
| 67 | + message += chalk.bold.red(`${gscanReport.results.error.all.length} ${text1}`); |
| 68 | + message += ' and '; |
| 69 | + message += chalk.bold.yellow(`${gscanReport.results.warning.all.length} ${text2}`); |
| 70 | + } |
| 71 | + |
| 72 | + message += '\n'; |
| 73 | + ctx.ui.log(message); |
| 74 | + |
| 75 | + return ctx.ui.confirm('View error and warning details?', null, {prefix: chalk.cyan('?')}); |
| 76 | + } |
| 77 | + }).then((answer) => { |
| 78 | + if (answer) { |
| 79 | + const spaces = ' '; |
| 80 | + |
| 81 | + if (gscanReport.results.error.all.length) { |
| 82 | + ctx.ui.log(chalk.bold.red('\nErrors')); |
| 83 | + |
| 84 | + each(gscanReport.results.error.byFiles, (errors, fileName) => { |
| 85 | + if (!errors.length) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + let message = chalk.bold.white(`${spaces}File: `); |
| 90 | + message += chalk.white(`${fileName}`); |
| 91 | + message += '\n'; |
| 92 | + |
| 93 | + errors.forEach((error, index) => { |
| 94 | + if (error.fatal) { |
| 95 | + message += `${spaces}- ${chalk.bold.red('Fatal error:')} ${error.rule.replace(/(<([^>]+)>)/ig, '')}`; |
| 96 | + } else { |
| 97 | + message += `${spaces}- ${error.rule.replace(/(<([^>]+)>)/ig, '')}`; |
| 98 | + } |
| 99 | + |
| 100 | + if (index < (errors.length - 1)) { |
| 101 | + message += '\n'; |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + message += '\n'; |
| 106 | + ctx.ui.log(message); |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + if (gscanReport.results.warning.all.length) { |
| 111 | + ctx.ui.log(chalk.bold.yellow('\nWarnings')); |
| 112 | + |
| 113 | + each(gscanReport.results.warning.byFiles, (warnings, fileName) => { |
| 114 | + if (!warnings.length) { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + let message = chalk.bold.white(`${spaces}File: `); |
| 119 | + message += chalk.white(`${fileName}`); |
| 120 | + message += '\n'; |
| 121 | + |
| 122 | + warnings.forEach((warning, index) => { |
| 123 | + message += `${spaces}- ${warning.rule.replace(/(<([^>]+)>)/ig, '')}`; |
| 124 | + |
| 125 | + if (index < (warnings.length - 1)) { |
| 126 | + message += '\n'; |
| 127 | + } |
| 128 | + }); |
| 129 | + |
| 130 | + message += '\n'; |
| 131 | + ctx.ui.log(message); |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + if (demoPost && demoPost.uuid) { |
| 136 | + const demoLink = `${ctx.instance.config.get('url')}p/${demoPost.uuid}/`; |
| 137 | + ctx.ui.log(`Visit the demo post at ${chalk.cyan(demoLink)} to see how your theme looks like in Ghost 2.0`); |
| 138 | + } |
| 139 | + |
| 140 | + ctx.ui.log(`You can also check theme compatibility at ${chalk.cyan('https://gscan.ghost.org')}\n`); |
| 141 | + } |
| 142 | + |
| 143 | + if (gscanReport.results.hasFatalErrors) { |
| 144 | + return Promise.reject(new errors.CliError({ |
| 145 | + message: 'Migration failed. Your theme has fatal errors.\n For additional theme help visit https://themes.ghost.org/docs/changelog', |
| 146 | + logMessageOnly: true |
| 147 | + })); |
| 148 | + } |
| 149 | + |
| 150 | + return ctx.ui.confirm(`Are you sure you want to proceed with migrating to Ghost ${ctx.version}?`, null, {prefix: chalk.cyan('?')}) |
| 151 | + .then((answer) => { |
| 152 | + if (!answer) { |
| 153 | + return Promise.reject(new errors.CliError({ |
| 154 | + message: `Update aborted. Your blog is still on ${ctx.activeVersion}.`, |
| 155 | + logMessageOnly: true |
| 156 | + })); |
| 157 | + } |
| 158 | + }); |
| 159 | + }); |
| 160 | +}; |
0 commit comments