Refactored package-json lib to use more async-await

- this helps get rid of all the promise chaining and indentation,
  resulting in cleaner code
This commit is contained in:
Daniel Lockyer 2020-11-24 15:57:56 +00:00
parent 9d29686f6a
commit 0971506948

View File

@ -64,43 +64,42 @@ module.exports = class PackageJson {
* Parse package.json and validate it has * Parse package.json and validate it has
* all the required fields * all the required fields
*/ */
parse(path) { async parse(path) {
const self = this; let source;
let json;
return fs.readFile(path) try {
.catch(function () { source = await fs.readFile(path);
const err = new Error(self.i18n.t('errors.utils.parsepackagejson.couldNotReadPackage')); } catch (readError) {
err.context = path; const err = new Error(this.i18n.t('errors.utils.parsepackagejson.couldNotReadPackage'));
err.context = path;
err.err = readError;
return Promise.reject(err); return Promise.reject(err);
}) }
.then(function (source) {
let hasRequiredKeys;
let json;
let err;
try { try {
json = JSON.parse(source); json = JSON.parse(source);
} catch (parseError) {
const err = new Error(this.i18n.t('errors.utils.parsepackagejson.themeFileIsMalformed'));
err.context = path;
err.err = parseError;
err.help = this.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'});
hasRequiredKeys = json.name && json.version; return Promise.reject(err);
}
if (!hasRequiredKeys) { const hasRequiredKeys = json.name && json.version;
err = new Error(self.i18n.t('errors.utils.parsepackagejson.nameOrVersionMissing'));
err.context = path;
err.help = self.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'});
return Promise.reject(err); if (!hasRequiredKeys) {
} const err = new Error(this.i18n.t('errors.utils.parsepackagejson.nameOrVersionMissing'));
err.context = path;
err.help = this.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'});
return json; return Promise.reject(err);
} catch (parseError) { }
err = new Error(self.i18n.t('errors.utils.parsepackagejson.themeFileIsMalformed'));
err.context = path;
err.help = self.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'});
return Promise.reject(err); return json;
}
});
} }
/** /**
@ -108,50 +107,47 @@ module.exports = class PackageJson {
* *
* @returns {object} * @returns {object}
*/ */
processPackage(absolutePath, packageName) { async processPackage(absolutePath, packageName) {
const pkg = { const pkg = {
name: packageName, name: packageName,
path: absolutePath path: absolutePath
}; };
return this.parse(join(absolutePath, packageJSONPath))
.then(function gotPackageJSON(packageJSON) { try {
pkg['package.json'] = packageJSON; const packageJSON = await this.parse(join(absolutePath, packageJSONPath));
return pkg; pkg['package.json'] = packageJSON;
}) } catch (err) {
.catch(function noPackageJSON() { // ignore invalid package.json for now,
// ignore invalid package.json for now, // because Ghost does not rely/use them at the moment
// because Ghost does not rely/use them at the moment // in the future, this .catch() will need to be removed,
// in the future, this .catch() will need to be removed, // so that error is thrown on invalid json syntax
// so that error is thrown on invalid json syntax pkg['package.json'] = null;
pkg['package.json'] = null; }
return pkg;
}); return pkg;
} }
readPackage(packagePath, packageName) { async readPackage(packagePath, packageName) {
const self = this;
const absolutePath = join(packagePath, packageName); const absolutePath = join(packagePath, packageName);
return fs.stat(absolutePath)
.then(function (stat) {
if (!stat.isDirectory()) {
return {};
}
return self.processPackage(absolutePath, packageName) try {
.then(function gotPackage(pkg) { const stat = await fs.stat(absolutePath);
const res = {}; if (!stat.isDirectory()) {
res[packageName] = pkg; return {};
return res; }
});
}) const pkg = await this.processPackage(absolutePath, packageName);
.catch(function (err) { const res = {};
return Promise.reject(new errors.NotFoundError({ res[packageName] = pkg;
message: 'Package not found', return res;
err: err, } catch (err) {
help: 'path: ' + packagePath, return Promise.reject(new errors.NotFoundError({
context: 'name: ' + packageName message: 'Package not found',
})); err: err,
}); help: 'path: ' + packagePath,
context: 'name: ' + packageName
}));
}
} }
readPackages(packagePath) { readPackages(packagePath) {