2019-04-09 13:00:56 +08:00
|
|
|
const _ = require('lodash');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const config = require('../../config');
|
|
|
|
const common = require('../../lib/common');
|
|
|
|
|
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
|
|
|
|
2019-04-22 17:31:56 +02: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-27 22:30:49 +00:00
|
|
|
|
2017-03-13 11:44:44 +00:00
|
|
|
if (isZip) {
|
2017-05-31 18:42:42 +02:00
|
|
|
checkPromise = gscan.checkZip(theme, {
|
|
|
|
keepExtractedDir: true
|
|
|
|
});
|
2017-03-13 11:44:44 +00:00
|
|
|
} else {
|
|
|
|
checkPromise = gscan.check(theme.path);
|
2017-02-27 22:30:49 +00:00
|
|
|
}
|
2017-03-13 11:44:44 +00:00
|
|
|
|
2018-05-11 13:12:15 +08:00
|
|
|
return checkPromise
|
|
|
|
.then(function resultHandler(checkedTheme) {
|
|
|
|
checkedTheme = gscan.format(checkedTheme, {
|
|
|
|
onlyFatalErrors: config.get('env') === 'production'
|
|
|
|
});
|
2017-03-13 11:44:44 +00:00
|
|
|
|
2019-04-22 17:31:56 +02:00
|
|
|
return checkedTheme;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const checkSafe = function checkSafe(theme, isZip) {
|
|
|
|
return check(theme, isZip)
|
|
|
|
.then((checkedTheme) => {
|
|
|
|
if (canActivate(checkedTheme)) {
|
2018-05-11 13:12:15 +08:00
|
|
|
return checkedTheme;
|
|
|
|
}
|
2017-03-13 11:44:44 +00:00
|
|
|
|
2018-05-11 13:12:15 +08:00
|
|
|
return Promise.reject(new common.errors.ThemeValidationError({
|
|
|
|
message: common.i18n.t('errors.api.themes.invalidTheme'),
|
2019-04-09 13:00:56 +08:00
|
|
|
errorDetails: Object.assign(
|
|
|
|
_.pick(checkedTheme, ['checkedVersion', 'name', 'path', 'version']), {
|
|
|
|
errors: checkedTheme.results.error
|
|
|
|
}
|
2019-04-22 17:31:56 +02:00
|
|
|
)
|
2018-05-11 13:12:15 +08: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;
|