2021-07-06 11:02:37 +01:00
|
|
|
const debug = require('@tryghost/debug')('themes');
|
2019-04-09 13:00:56 +08:00
|
|
|
const _ = require('lodash');
|
2019-05-02 17:59:29 +02:00
|
|
|
const fs = require('fs-extra');
|
2020-05-27 12:47:53 -05:00
|
|
|
const config = require('../../../shared/config');
|
2021-07-06 15:38:28 +01:00
|
|
|
const tpl = require('@tryghost/tpl');
|
2020-05-22 13:22:20 -05:00
|
|
|
const errors = require('@tryghost/errors');
|
2019-04-09 13:00:56 +08:00
|
|
|
|
2021-07-06 15:38:28 +01:00
|
|
|
const messages = {
|
2021-07-07 13:40:10 +01:00
|
|
|
themeHasErrors: 'Theme {name} is not compatible or contains errors.',
|
|
|
|
activeThemeHasFatalErrors: 'The currently active theme "{theme}" has fatal errors.',
|
|
|
|
activeThemeHasErrors: 'The currently active theme "{theme}" has errors, but will still work.'
|
2021-07-06 15:38:28 +01:00
|
|
|
};
|
|
|
|
|
2019-04-22 17:31:56 +02:00
|
|
|
const canActivate = function canActivate(checkedTheme) {
|
|
|
|
// CASE: production and no fatal errors
|
|
|
|
// CASE: development returns fatal and none fatal errors, theme is only invalid if fatal errors
|
|
|
|
return !checkedTheme.results.error.length || (config.get('env') === 'development') && !checkedTheme.results.hasFatalErrors;
|
|
|
|
};
|
2017-02-27 22:30:49 +00:00
|
|
|
|
2021-07-07 11:51:04 +01:00
|
|
|
const check = async function check(theme, isZip) {
|
2021-05-18 15:22:02 +01:00
|
|
|
debug('Begin: Check');
|
2019-04-22 17:31:56 +02:00
|
|
|
// gscan can slow down boot time if we require on boot, for now nest the require.
|
|
|
|
const gscan = require('gscan');
|
2021-07-07 11:51:04 +01:00
|
|
|
let checkedTheme;
|
2017-02-27 22:30:49 +00:00
|
|
|
|
2017-03-13 11:44:44 +00:00
|
|
|
if (isZip) {
|
2021-05-18 15:22:02 +01:00
|
|
|
debug('zip mode');
|
2021-07-07 11:51:04 +01:00
|
|
|
checkedTheme = await gscan.checkZip(theme, {
|
2019-09-16 18:22:49 +02:00
|
|
|
keepExtractedDir: true,
|
|
|
|
checkVersion: 'canary'
|
2017-05-31 18:42:42 +02:00
|
|
|
});
|
2017-03-13 11:44:44 +00:00
|
|
|
} else {
|
2021-05-18 15:22:02 +01:00
|
|
|
debug('non-zip mode');
|
2021-07-07 11:51:04 +01:00
|
|
|
checkedTheme = await gscan.check(theme.path, {
|
2019-09-16 18:22:49 +02:00
|
|
|
checkVersion: 'canary'
|
|
|
|
});
|
2017-02-27 22:30:49 +00:00
|
|
|
}
|
2017-03-13 11:44:44 +00:00
|
|
|
|
2021-07-07 11:51:04 +01:00
|
|
|
checkedTheme = gscan.format(checkedTheme, {
|
|
|
|
onlyFatalErrors: config.get('env') === 'production',
|
|
|
|
checkVersion: 'canary'
|
|
|
|
});
|
2017-03-13 11:44:44 +00:00
|
|
|
|
2021-07-07 11:51:04 +01:00
|
|
|
debug('End: Check');
|
|
|
|
return checkedTheme;
|
2019-04-22 17:31:56 +02:00
|
|
|
};
|
|
|
|
|
2021-07-07 13:40:10 +01:00
|
|
|
const checkSafe = async function checkSafe(themeName, theme, isZip) {
|
2021-07-07 11:51:04 +01:00
|
|
|
const checkedTheme = await check(theme, isZip);
|
2017-03-13 11:44:44 +00:00
|
|
|
|
2021-07-07 11:51:04 +01:00
|
|
|
if (canActivate(checkedTheme)) {
|
|
|
|
return checkedTheme;
|
|
|
|
}
|
2019-05-02 17:59:29 +02:00
|
|
|
|
2021-07-07 11:51:04 +01:00
|
|
|
// NOTE: When theme cannot be activated and gscan explicitly keeps extracted files (after
|
|
|
|
// being called with `keepExtractedDir: true`), this is the best place for a cleanup.
|
|
|
|
// TODO: The `keepExtractedDir` flag is the cause of confusion for when and where the cleanup
|
|
|
|
// should be done. It's probably best if gscan is called directly with path to the extracted
|
|
|
|
// directory, this would allow keeping gscan to do just one thing - validate the theme, and
|
|
|
|
// file manipulations could be left to another module/library
|
|
|
|
if (isZip) {
|
|
|
|
fs.remove(checkedTheme.path);
|
|
|
|
}
|
|
|
|
|
2021-07-07 13:49:40 +01:00
|
|
|
throw getThemeValidationError('themeHasErrors', themeName, checkedTheme);
|
2021-07-07 13:40:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const getThemeValidationError = (message, themeName, checkedTheme) => {
|
|
|
|
return new errors.ThemeValidationError({
|
|
|
|
message: tpl(messages[message], {theme: themeName}),
|
2021-07-07 11:51:04 +01:00
|
|
|
errorDetails: Object.assign(
|
|
|
|
_.pick(checkedTheme, ['checkedVersion', 'name', 'path', 'version']), {
|
|
|
|
errors: checkedTheme.results.error
|
|
|
|
}
|
|
|
|
)
|
2021-07-07 13:40:10 +01:00
|
|
|
});
|
2017-02-27 22:30:49 +00:00
|
|
|
};
|
|
|
|
|
2019-04-22 17:31:56 +02:00
|
|
|
module.exports.check = check;
|
|
|
|
module.exports.checkSafe = checkSafe;
|
|
|
|
module.exports.canActivate = canActivate;
|
2021-07-07 13:40:10 +01:00
|
|
|
module.exports.getThemeValidationError = getThemeValidationError;
|