2017-02-28 01:30:49 +03:00
|
|
|
var Promise = require('bluebird'),
|
2017-12-14 05:01:23 +03:00
|
|
|
config = require('../../config'),
|
|
|
|
common = require('../../lib/common'),
|
2017-03-13 14:44:44 +03:00
|
|
|
checkTheme;
|
2017-02-28 01:30:49 +03:00
|
|
|
|
2017-03-13 14:44:44 +03:00
|
|
|
checkTheme = function checkTheme(theme, isZip) {
|
|
|
|
var checkPromise,
|
|
|
|
// gscan can slow down boot time if we require on boot, for now nest the require.
|
|
|
|
gscan = require('gscan');
|
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, {
|
|
|
|
keepExtractedDir: true
|
|
|
|
});
|
2017-03-13 14:44:44 +03:00
|
|
|
} else {
|
|
|
|
checkPromise = gscan.check(theme.path);
|
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, {
|
|
|
|
onlyFatalErrors: config.get('env') === 'production'
|
|
|
|
});
|
2017-03-13 14:44:44 +03:00
|
|
|
|
2018-05-11 08:12:15 +03:00
|
|
|
// CASE: production and no fatal errors
|
|
|
|
// CASE: development returns fatal and none fatal errors, theme is only invalid if fatal errors
|
|
|
|
if (!checkedTheme.results.error.length ||
|
|
|
|
config.get('env') === 'development' && !checkedTheme.results.hasFatalErrors) {
|
|
|
|
return checkedTheme;
|
|
|
|
}
|
2017-03-13 14:44:44 +03:00
|
|
|
|
2018-05-11 08:12:15 +03:00
|
|
|
return Promise.reject(new common.errors.ThemeValidationError({
|
|
|
|
message: common.i18n.t('errors.api.themes.invalidTheme'),
|
|
|
|
errorDetails: checkedTheme.results.error,
|
|
|
|
context: checkedTheme
|
|
|
|
}));
|
|
|
|
}).catch(function (error) {
|
|
|
|
return Promise.reject(error);
|
|
|
|
});
|
2017-02-28 01:30:49 +03:00
|
|
|
};
|
|
|
|
|
2017-03-13 14:44:44 +03:00
|
|
|
module.exports.check = checkTheme;
|