Removed use of i18n in package-json

- We are going to get rid of the internal i18n tool because it doesn't solve a real use case
- Instead, we have a new tpl utility that does basic string interpolation
- This makes this module less complex as it no longer requires dependency injection to work
This commit is contained in:
Hannah Wolfe 2021-06-09 14:05:36 +01:00
parent 224c6996f1
commit dd129e0920
5 changed files with 23 additions and 40 deletions

View File

@ -3,14 +3,17 @@ const Promise = require('bluebird');
const fs = require('fs-extra'); const fs = require('fs-extra');
const join = require('path').join; const join = require('path').join;
const errors = require('@tryghost/errors'); const errors = require('@tryghost/errors');
const tpl = require('@tryghost/tpl');
const notAPackageRegex = /^\.|_messages|README.md|node_modules|bower_components/i; const notAPackageRegex = /^\.|_messages|README.md|node_modules|bower_components/i;
const packageJSONPath = 'package.json'; const packageJSONPath = 'package.json';
/** const messages = {
* @typedef {Object} Ii18n couldNotReadPackage: 'Could not read package.json file',
* @prop {(key: string) => string} t 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 * # Package Utils
@ -23,15 +26,6 @@ const packageJSONPath = 'package.json';
* *
*/ */
module.exports = class PackageJson { 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 * ### Filter Packages
* Normalizes packages read by read-packages so that the themes module can use them. * 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); source = await fs.readFile(path);
} catch (readError) { } catch (readError) {
const err = new errors.IncorrectUsageError(); const err = new errors.IncorrectUsageError();
err.message = this.i18n.t('errors.utils.parsepackagejson.couldNotReadPackage'); err.message = tpl(messages.couldNotReadPackage);
err.context = path; err.context = path;
err.err = readError; err.err = readError;
@ -96,10 +90,10 @@ module.exports = class PackageJson {
json = JSON.parse(source); json = JSON.parse(source);
} catch (parseError) { } catch (parseError) {
const err = new errors.IncorrectUsageError(); const err = new errors.IncorrectUsageError();
err.message = this.i18n.t('errors.utils.parsepackagejson.themeFileIsMalformed'); err.message = tpl(messages.themeFileIsMalformed);
err.context = path; err.context = path;
err.err = parseError; 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); return Promise.reject(err);
} }
@ -108,9 +102,9 @@ module.exports = class PackageJson {
if (!hasRequiredKeys) { if (!hasRequiredKeys) {
const err = new errors.IncorrectUsageError(); const err = new errors.IncorrectUsageError();
err.message = this.i18n.t('errors.utils.parsepackagejson.nameOrVersionMissing'); err.message = tpl(messages.nameOrVersionMissing);
err.context = path; 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); return Promise.reject(err);
} }

View File

@ -27,6 +27,7 @@
}, },
"dependencies": { "dependencies": {
"@tryghost/errors": "^0.2.12", "@tryghost/errors": "^0.2.12",
"@tryghost/tpl": "^0.1.0",
"bluebird": "^3.7.2", "bluebird": "^3.7.2",
"fs-extra": "^10.0.0", "fs-extra": "^10.0.0",
"lodash": "^4.17.21" "lodash": "^4.17.21"

View File

@ -1,11 +1,7 @@
require('./utils'); require('./utils');
const PackageJSON = require('../'); const PackageJSON = require('../');
const packageJSON = new PackageJSON({ const packageJSON = new PackageJSON();
i18n: {
t: key => key
}
});
describe('lib/fs/package-json', function () { describe('lib/fs/package-json', function () {
// @TODO: introduce some non-theme package examples // @TODO: introduce some non-theme package examples

View File

@ -4,11 +4,7 @@ const tmp = require('tmp');
const fs = require('fs-extra'); const fs = require('fs-extra');
const PackageJSON = require('../'); const PackageJSON = require('../');
const packageJSON = new PackageJSON({ const packageJSON = new PackageJSON();
i18n: {
t: key => key
}
});
describe('lib/fs/package-json: parse', function () { describe('lib/fs/package-json: parse', function () {
it('should parse valid package.json', function (done) { 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')); done(new Error('packageJSON.parse succeeded, but should\'ve failed'));
}) })
.catch(function (err) { .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.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(); done();
}) })
@ -78,9 +74,9 @@ describe('lib/fs/package-json: parse', function () {
done(new Error('packageJSON.parse succeeded, but should\'ve failed')); done(new Error('packageJSON.parse succeeded, but should\'ve failed'));
}) })
.catch(function (err) { .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.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(); done();
}) })
@ -102,9 +98,9 @@ describe('lib/fs/package-json: parse', function () {
done(new Error('packageJSON.parse succeeded, but should\'ve failed')); done(new Error('packageJSON.parse succeeded, but should\'ve failed'));
}) })
.catch(function (err) { .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.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(); done();
}) })
@ -121,7 +117,7 @@ describe('lib/fs/package-json: parse', function () {
done(new Error('packageJSON.parse succeeded, but should\'ve failed')); done(new Error('packageJSON.parse succeeded, but should\'ve failed'));
}) })
.catch(function (err) { .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); err.context.should.equal(tmpFile.name);
done(); done();

View File

@ -5,11 +5,7 @@ const join = require('path').join;
const fs = require('fs-extra'); const fs = require('fs-extra');
const PackageJSON = require('../'); const PackageJSON = require('../');
const packageJSON = new PackageJSON({ const packageJSON = new PackageJSON();
i18n: {
t: key => key
}
});
describe('lib/fs/package-json: read', function () { describe('lib/fs/package-json: read', function () {
describe('all', function () { describe('all', function () {