Refactor common pattern in service files

- Use array destructuring
- Use @tryghost/errors
- Part of the big move towards decoupling, this gives visibility on what's being used where
- Biting off manageable chunks / fixing bits of code I'm refactoring for other reasons
This commit is contained in:
Hannah Wolfe 2020-04-30 20:26:12 +01:00
parent 3db4f8d331
commit 18ce837e9a
2 changed files with 8 additions and 8 deletions

View File

@ -5,7 +5,7 @@
const Promise = require('bluebird');
const fs = require('fs-extra');
const common = require('../../common');
const {i18n} = require('../../common');
/**
* Parse package.json and validate it has
@ -15,7 +15,7 @@ const common = require('../../common');
function parsePackageJson(path) {
return fs.readFile(path)
.catch(function () {
const err = new Error(common.i18n.t('errors.utils.parsepackagejson.couldNotReadPackage'));
const err = new Error(i18n.t('errors.utils.parsepackagejson.couldNotReadPackage'));
err.context = path;
return Promise.reject(err);
@ -31,18 +31,18 @@ function parsePackageJson(path) {
hasRequiredKeys = json.name && json.version;
if (!hasRequiredKeys) {
err = new Error(common.i18n.t('errors.utils.parsepackagejson.nameOrVersionMissing'));
err = new Error(i18n.t('errors.utils.parsepackagejson.nameOrVersionMissing'));
err.context = path;
err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'});
err.help = i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'});
return Promise.reject(err);
}
return json;
} catch (parseError) {
err = new Error(common.i18n.t('errors.utils.parsepackagejson.themeFileIsMalformed'));
err = new Error(i18n.t('errors.utils.parsepackagejson.themeFileIsMalformed'));
err.context = path;
err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'});
err.help = i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'});
return Promise.reject(err);
}

View File

@ -7,7 +7,7 @@ const _ = require('lodash');
const join = require('path').join;
const fs = require('fs-extra');
const parsePackageJson = require('./parse');
const common = require('../../common');
const errors = require('@tryghost/errors');
const notAPackageRegex = /^\.|_messages|README.md|node_modules|bower_components/i;
const packageJSONPath = 'package.json';
let readPackage;
@ -53,7 +53,7 @@ readPackage = function readPackage(packagePath, packageName) {
});
})
.catch(function (err) {
return Promise.reject(new common.errors.NotFoundError({
return Promise.reject(new errors.NotFoundError({
message: 'Package not found',
err: err,
help: 'path: ' + packagePath,