diff --git a/ghost/package-json/lib/package-json.js b/ghost/package-json/lib/package-json.js index 213a2a1521..b09aa11105 100644 --- a/ghost/package-json/lib/package-json.js +++ b/ghost/package-json/lib/package-json.js @@ -3,14 +3,17 @@ const Promise = require('bluebird'); const fs = require('fs-extra'); const join = require('path').join; const errors = require('@tryghost/errors'); +const tpl = require('@tryghost/tpl'); const notAPackageRegex = /^\.|_messages|README.md|node_modules|bower_components/i; const packageJSONPath = 'package.json'; -/** - * @typedef {Object} Ii18n - * @prop {(key: string) => string} t - */ +const messages = { + couldNotReadPackage: 'Could not read package.json file', + nameOrVersionMissing: '"name" or "version" is missing from theme package.json file.', + willBeRequired: 'This will be required in future. Please see {url}', + themeFileIsMalformed: 'Theme package.json file is malformed' +}; /** * # Package Utils @@ -23,15 +26,6 @@ const packageJSONPath = 'package.json'; * */ module.exports = class PackageJson { - /** - * Creates an instance of PackageJson, an util used to read and validate package.json files - * @param {Object} dependencies - * @param {Ii18n} dependencies.i18n - */ - constructor({i18n}) { - this.i18n = i18n; - } - /** * ### Filter Packages * Normalizes packages read by read-packages so that the themes module can use them. @@ -85,7 +79,7 @@ module.exports = class PackageJson { source = await fs.readFile(path); } catch (readError) { const err = new errors.IncorrectUsageError(); - err.message = this.i18n.t('errors.utils.parsepackagejson.couldNotReadPackage'); + err.message = tpl(messages.couldNotReadPackage); err.context = path; err.err = readError; @@ -96,10 +90,10 @@ module.exports = class PackageJson { json = JSON.parse(source); } catch (parseError) { const err = new errors.IncorrectUsageError(); - err.message = this.i18n.t('errors.utils.parsepackagejson.themeFileIsMalformed'); + err.message = tpl(messages.themeFileIsMalformed); err.context = path; err.err = parseError; - err.help = this.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/themes/'}); + err.help = tpl(messages.willBeRequired, {url: 'https://ghost.org/docs/themes/'}); return Promise.reject(err); } @@ -108,9 +102,9 @@ module.exports = class PackageJson { if (!hasRequiredKeys) { const err = new errors.IncorrectUsageError(); - err.message = this.i18n.t('errors.utils.parsepackagejson.nameOrVersionMissing'); + err.message = tpl(messages.nameOrVersionMissing); err.context = path; - err.help = this.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/themes/'}); + err.help = tpl(messages.willBeRequired, {url: 'https://ghost.org/docs/themes/'}); return Promise.reject(err); } diff --git a/ghost/package-json/package.json b/ghost/package-json/package.json index 2086b2e96f..de9e9354e7 100644 --- a/ghost/package-json/package.json +++ b/ghost/package-json/package.json @@ -27,6 +27,7 @@ }, "dependencies": { "@tryghost/errors": "^0.2.12", + "@tryghost/tpl": "^0.1.0", "bluebird": "^3.7.2", "fs-extra": "^10.0.0", "lodash": "^4.17.21" diff --git a/ghost/package-json/test/filter.test.js b/ghost/package-json/test/filter.test.js index 2ed3591d43..e86c558aa0 100644 --- a/ghost/package-json/test/filter.test.js +++ b/ghost/package-json/test/filter.test.js @@ -1,11 +1,7 @@ require('./utils'); const PackageJSON = require('../'); -const packageJSON = new PackageJSON({ - i18n: { - t: key => key - } -}); +const packageJSON = new PackageJSON(); describe('lib/fs/package-json', function () { // @TODO: introduce some non-theme package examples diff --git a/ghost/package-json/test/parse.test.js b/ghost/package-json/test/parse.test.js index 1e156c9067..ccb3ee3fd5 100644 --- a/ghost/package-json/test/parse.test.js +++ b/ghost/package-json/test/parse.test.js @@ -4,11 +4,7 @@ const tmp = require('tmp'); const fs = require('fs-extra'); const PackageJSON = require('../'); -const packageJSON = new PackageJSON({ - i18n: { - t: key => key - } -}); +const packageJSON = new PackageJSON(); describe('lib/fs/package-json: parse', function () { it('should parse valid package.json', function (done) { @@ -52,9 +48,9 @@ describe('lib/fs/package-json: parse', function () { done(new Error('packageJSON.parse succeeded, but should\'ve failed')); }) .catch(function (err) { - err.message.should.equal('errors.utils.parsepackagejson.nameOrVersionMissing'); + err.message.should.equal('"name" or "version" is missing from theme package.json file.'); err.context.should.equal(tmpFile.name); - err.help.should.equal('errors.utils.parsepackagejson.willBeRequired'); + err.help.should.equal('This will be required in future. Please see https://ghost.org/docs/themes/'); done(); }) @@ -78,9 +74,9 @@ describe('lib/fs/package-json: parse', function () { done(new Error('packageJSON.parse succeeded, but should\'ve failed')); }) .catch(function (err) { - err.message.should.equal('errors.utils.parsepackagejson.nameOrVersionMissing'); + err.message.should.equal('"name" or "version" is missing from theme package.json file.'); err.context.should.equal(tmpFile.name); - err.help.should.equal('errors.utils.parsepackagejson.willBeRequired'); + err.help.should.equal('This will be required in future. Please see https://ghost.org/docs/themes/'); done(); }) @@ -102,9 +98,9 @@ describe('lib/fs/package-json: parse', function () { done(new Error('packageJSON.parse succeeded, but should\'ve failed')); }) .catch(function (err) { - err.message.should.equal('errors.utils.parsepackagejson.themeFileIsMalformed'); + err.message.should.equal('Theme package.json file is malformed'); err.context.should.equal(tmpFile.name); - err.help.should.equal('errors.utils.parsepackagejson.willBeRequired'); + err.help.should.equal('This will be required in future. Please see https://ghost.org/docs/themes/'); done(); }) @@ -121,7 +117,7 @@ describe('lib/fs/package-json: parse', function () { done(new Error('packageJSON.parse succeeded, but should\'ve failed')); }) .catch(function (err) { - err.message.should.equal('errors.utils.parsepackagejson.couldNotReadPackage'); + err.message.should.equal('Could not read package.json file'); err.context.should.equal(tmpFile.name); done(); diff --git a/ghost/package-json/test/read.test.js b/ghost/package-json/test/read.test.js index 7f2fceeeef..7ea9232c88 100644 --- a/ghost/package-json/test/read.test.js +++ b/ghost/package-json/test/read.test.js @@ -5,11 +5,7 @@ const join = require('path').join; const fs = require('fs-extra'); const PackageJSON = require('../'); -const packageJSON = new PackageJSON({ - i18n: { - t: key => key - } -}); +const packageJSON = new PackageJSON(); describe('lib/fs/package-json: read', function () { describe('all', function () {