2021-07-06 13:02:37 +03:00
|
|
|
const debug = require('@tryghost/debug')('themes');
|
2019-04-09 08:00:56 +03:00
|
|
|
const _ = require('lodash');
|
2019-05-02 18:59:29 +03:00
|
|
|
const fs = require('fs-extra');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../../shared/config');
|
2021-09-23 14:44:39 +03:00
|
|
|
const labs = require('../../../shared/labs');
|
2021-07-06 17:38:28 +03:00
|
|
|
const tpl = require('@tryghost/tpl');
|
2020-05-22 21:22:20 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2019-04-09 08:00:56 +03:00
|
|
|
|
2021-07-06 17:38:28 +03:00
|
|
|
const messages = {
|
2021-07-12 20:42:11 +03:00
|
|
|
themeHasErrors: 'Theme "{theme}" is not compatible or contains errors.',
|
2021-07-07 15:40:10 +03:00
|
|
|
activeThemeHasFatalErrors: 'The currently active theme "{theme}" has fatal errors.',
|
|
|
|
activeThemeHasErrors: 'The currently active theme "{theme}" has errors, but will still work.'
|
2021-07-06 17:38:28 +03:00
|
|
|
};
|
|
|
|
|
2019-04-22 18:31:56 +03: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-28 01:30:49 +03:00
|
|
|
|
2021-07-07 13:51:04 +03:00
|
|
|
const check = async function check(theme, isZip) {
|
2021-05-18 17:22:02 +03:00
|
|
|
debug('Begin: Check');
|
2019-04-22 18:31:56 +03:00
|
|
|
// gscan can slow down boot time if we require on boot, for now nest the require.
|
|
|
|
const gscan = require('gscan');
|
2021-07-07 13:51:04 +03:00
|
|
|
let checkedTheme;
|
2017-02-28 01:30:49 +03:00
|
|
|
|
2017-03-13 14:44:44 +03:00
|
|
|
if (isZip) {
|
2021-05-18 17:22:02 +03:00
|
|
|
debug('zip mode');
|
2021-07-07 13:51:04 +03:00
|
|
|
checkedTheme = await gscan.checkZip(theme, {
|
2019-09-16 19:22:49 +03:00
|
|
|
keepExtractedDir: true,
|
2021-09-23 14:44:39 +03:00
|
|
|
checkVersion: 'canary',
|
|
|
|
labs: labs.getAll()
|
2017-05-31 19:42:42 +03:00
|
|
|
});
|
2017-03-13 14:44:44 +03:00
|
|
|
} else {
|
2021-05-18 17:22:02 +03:00
|
|
|
debug('non-zip mode');
|
2021-07-07 13:51:04 +03:00
|
|
|
checkedTheme = await gscan.check(theme.path, {
|
2021-09-23 14:44:39 +03:00
|
|
|
checkVersion: 'canary',
|
|
|
|
labs: labs.getAll()
|
2019-09-16 19:22:49 +03:00
|
|
|
});
|
2017-02-28 01:30:49 +03:00
|
|
|
}
|
2017-03-13 14:44:44 +03:00
|
|
|
|
2021-07-07 13:51:04 +03:00
|
|
|
checkedTheme = gscan.format(checkedTheme, {
|
|
|
|
onlyFatalErrors: config.get('env') === 'production',
|
|
|
|
checkVersion: 'canary'
|
|
|
|
});
|
2017-03-13 14:44:44 +03:00
|
|
|
|
2021-07-07 13:51:04 +03:00
|
|
|
debug('End: Check');
|
|
|
|
return checkedTheme;
|
2019-04-22 18:31:56 +03:00
|
|
|
};
|
|
|
|
|
2021-07-07 15:40:10 +03:00
|
|
|
const checkSafe = async function checkSafe(themeName, theme, isZip) {
|
2021-07-07 13:51:04 +03:00
|
|
|
const checkedTheme = await check(theme, isZip);
|
2017-03-13 14:44:44 +03:00
|
|
|
|
2021-07-07 13:51:04 +03:00
|
|
|
if (canActivate(checkedTheme)) {
|
|
|
|
return checkedTheme;
|
|
|
|
}
|
2019-05-02 18:59:29 +03:00
|
|
|
|
2021-07-07 13:51:04 +03: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 15:49:40 +03:00
|
|
|
throw getThemeValidationError('themeHasErrors', themeName, checkedTheme);
|
2021-07-07 15:40:10 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const getThemeValidationError = (message, themeName, checkedTheme) => {
|
|
|
|
return new errors.ThemeValidationError({
|
|
|
|
message: tpl(messages[message], {theme: themeName}),
|
2021-07-07 13:51:04 +03:00
|
|
|
errorDetails: Object.assign(
|
|
|
|
_.pick(checkedTheme, ['checkedVersion', 'name', 'path', 'version']), {
|
|
|
|
errors: checkedTheme.results.error
|
|
|
|
}
|
|
|
|
)
|
2021-07-07 15:40:10 +03:00
|
|
|
});
|
2017-02-28 01:30:49 +03:00
|
|
|
};
|
|
|
|
|
2019-04-22 18:31:56 +03:00
|
|
|
module.exports.check = check;
|
|
|
|
module.exports.checkSafe = checkSafe;
|
|
|
|
module.exports.canActivate = canActivate;
|
2021-07-07 15:40:10 +03:00
|
|
|
module.exports.getThemeValidationError = getThemeValidationError;
|