Made the package-json module ready to be exported (#12451)

no issue
This commit is contained in:
Thibaut Patel 2020-12-10 11:37:43 +01:00 committed by GitHub
parent 6d42c391c5
commit bd3afeb112
5 changed files with 41 additions and 12 deletions

View File

@ -1,4 +1,4 @@
const PackageJson = require('./package-json');
const {i18n} = require('../../common');
module.exports = new PackageJson(i18n);
module.exports = new PackageJson({i18n});

View File

@ -7,6 +7,11 @@ const errors = require('@tryghost/errors');
const notAPackageRegex = /^\.|_messages|README.md|node_modules|bower_components/i;
const packageJSONPath = 'package.json';
/**
* @typedef {Object} Ii18n
* @prop {(key: string) => string} t
*/
/**
* # Package Utils
*
@ -15,9 +20,15 @@ const packageJSONPath = 'package.json';
* - Adapters: replace fundamental pieces like storage, will become npm modules
*
* These utils facilitate loading, reading, managing etc, packages from the file system.
*
*/
module.exports = class PackageJson {
constructor(i18n) {
/**
* 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;
}

View File

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

View File

@ -1,7 +1,13 @@
const should = require('should');
const tmp = require('tmp');
const fs = require('fs-extra');
const packageJSON = require('../../../../../core/server/lib/fs/package-json');
const PackageJSON = require('../../../../../core/server/lib/fs/package-json/package-json');
const packageJSON = new PackageJSON({
i18n: {
t: key => key
}
});
describe('lib/fs/package-json: parse', function () {
it('should parse valid package.json', function (done) {
@ -45,9 +51,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('"name" or "version" is missing from theme package.json file.');
err.message.should.equal('errors.utils.parsepackagejson.nameOrVersionMissing');
err.context.should.equal(tmpFile.name);
err.help.should.equal('This will be required in future. Please see https://ghost.org/docs/api/handlebars-themes/');
err.help.should.equal('errors.utils.parsepackagejson.willBeRequired');
done();
})
@ -71,9 +77,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('"name" or "version" is missing from theme package.json file.');
err.message.should.equal('errors.utils.parsepackagejson.nameOrVersionMissing');
err.context.should.equal(tmpFile.name);
err.help.should.equal('This will be required in future. Please see https://ghost.org/docs/api/handlebars-themes/');
err.help.should.equal('errors.utils.parsepackagejson.willBeRequired');
done();
})
@ -95,9 +101,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('Theme package.json file is malformed');
err.message.should.equal('errors.utils.parsepackagejson.themeFileIsMalformed');
err.context.should.equal(tmpFile.name);
err.help.should.equal('This will be required in future. Please see https://ghost.org/docs/api/handlebars-themes/');
err.help.should.equal('errors.utils.parsepackagejson.willBeRequired');
done();
})
@ -114,7 +120,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('Could not read package.json file');
err.message.should.equal('errors.utils.parsepackagejson.couldNotReadPackage');
err.context.should.equal(tmpFile.name);
done();

View File

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