Ghost/core/server/services/themes/validate.js
Hannah Wolfe 82ef700d81
Refactored theme service to use async/await
refs: https://github.com/TryGhost/Team/issues/831

- We prefer async/await over promise chains because it makes the code much more readable
- the Theme Service needs further work and this should make that work much easier
   - e.g. https://github.com/TryGhost/Team/issues/831
   - e.g. fixing up the index.js file
2021-07-07 12:28:55 +01:00

77 lines
2.6 KiB
JavaScript

const debug = require('@tryghost/debug')('themes');
const _ = require('lodash');
const Promise = require('bluebird');
const fs = require('fs-extra');
const config = require('../../../shared/config');
const tpl = require('@tryghost/tpl');
const errors = require('@tryghost/errors');
const messages = {
invalidTheme: 'Theme is not compatible or contains errors.'
};
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;
};
const check = async function check(theme, isZip) {
debug('Begin: Check');
// gscan can slow down boot time if we require on boot, for now nest the require.
const gscan = require('gscan');
let checkedTheme;
if (isZip) {
debug('zip mode');
checkedTheme = await gscan.checkZip(theme, {
keepExtractedDir: true,
checkVersion: 'canary'
});
} else {
debug('non-zip mode');
checkedTheme = await gscan.check(theme.path, {
checkVersion: 'canary'
});
}
checkedTheme = gscan.format(checkedTheme, {
onlyFatalErrors: config.get('env') === 'production',
checkVersion: 'canary'
});
debug('End: Check');
return checkedTheme;
};
const checkSafe = async function checkSafe(theme, isZip) {
const checkedTheme = await check(theme, isZip);
if (canActivate(checkedTheme)) {
return checkedTheme;
}
// 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);
}
return Promise.reject(new errors.ThemeValidationError({
message: tpl(messages.invalidTheme),
errorDetails: Object.assign(
_.pick(checkedTheme, ['checkedVersion', 'name', 'path', 'version']), {
errors: checkedTheme.results.error
}
)
}));
};
module.exports.check = check;
module.exports.checkSafe = checkSafe;
module.exports.canActivate = canActivate;