2019-04-09 08:00:56 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const Promise = require('bluebird');
|
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-04-27 16:18:04 +03:00
|
|
|
const i18n = require('../../lib/common/i18n');
|
2020-05-22 21:22:20 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2019-04-09 08:00:56 +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
|
|
|
|
2019-04-22 18:31:56 +03:00
|
|
|
const check = function check(theme, isZip) {
|
|
|
|
// gscan can slow down boot time if we require on boot, for now nest the require.
|
|
|
|
const gscan = require('gscan');
|
|
|
|
let checkPromise;
|
2017-02-28 01:30:49 +03:00
|
|
|
|
2017-03-13 14:44:44 +03:00
|
|
|
if (isZip) {
|
2017-05-31 19:42:42 +03:00
|
|
|
checkPromise = gscan.checkZip(theme, {
|
2019-09-16 19:22:49 +03:00
|
|
|
keepExtractedDir: true,
|
|
|
|
checkVersion: 'canary'
|
2017-05-31 19:42:42 +03:00
|
|
|
});
|
2017-03-13 14:44:44 +03:00
|
|
|
} else {
|
2019-09-16 19:22:49 +03:00
|
|
|
checkPromise = gscan.check(theme.path, {
|
|
|
|
checkVersion: 'canary'
|
|
|
|
});
|
2017-02-28 01:30:49 +03:00
|
|
|
}
|
2017-03-13 14:44:44 +03:00
|
|
|
|
2018-05-11 08:12:15 +03:00
|
|
|
return checkPromise
|
|
|
|
.then(function resultHandler(checkedTheme) {
|
|
|
|
checkedTheme = gscan.format(checkedTheme, {
|
2019-09-16 19:22:49 +03:00
|
|
|
onlyFatalErrors: config.get('env') === 'production',
|
|
|
|
checkVersion: 'canary'
|
2018-05-11 08:12:15 +03:00
|
|
|
});
|
2017-03-13 14:44:44 +03:00
|
|
|
|
2019-04-22 18:31:56 +03:00
|
|
|
return checkedTheme;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const checkSafe = function checkSafe(theme, isZip) {
|
|
|
|
return check(theme, isZip)
|
|
|
|
.then((checkedTheme) => {
|
|
|
|
if (canActivate(checkedTheme)) {
|
2018-05-11 08:12:15 +03:00
|
|
|
return checkedTheme;
|
|
|
|
}
|
2017-03-13 14:44:44 +03:00
|
|
|
|
2019-05-02 18:59:29 +03:00
|
|
|
// NOTE: When theme cannot be activated and gscan explicitly keeps extracted files (after
|
|
|
|
// being called with `keepExtractedDir: true`), this is the closes 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);
|
|
|
|
}
|
|
|
|
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.ThemeValidationError({
|
|
|
|
message: i18n.t('errors.api.themes.invalidTheme'),
|
2019-04-09 08:00:56 +03:00
|
|
|
errorDetails: Object.assign(
|
|
|
|
_.pick(checkedTheme, ['checkedVersion', 'name', 'path', 'version']), {
|
|
|
|
errors: checkedTheme.results.error
|
|
|
|
}
|
2019-04-22 18:31:56 +03:00
|
|
|
)
|
2018-05-11 08:12:15 +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;
|