From 18ce837e9ac4a1001b309cbd3bb6c8b52befca84 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Thu, 30 Apr 2020 20:26:12 +0100 Subject: [PATCH] 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 --- ghost/package-json/lib/parse.js | 12 ++++++------ ghost/package-json/lib/read.js | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/ghost/package-json/lib/parse.js b/ghost/package-json/lib/parse.js index a07d867869..023615373f 100644 --- a/ghost/package-json/lib/parse.js +++ b/ghost/package-json/lib/parse.js @@ -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); } diff --git a/ghost/package-json/lib/read.js b/ghost/package-json/lib/read.js index e6c94d29c6..732a1af688 100644 --- a/ghost/package-json/lib/read.js +++ b/ghost/package-json/lib/read.js @@ -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,