0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00
ghost/core/server/utils/packages/read-packages.js
Hannah Wolfe c70fbc2c7e 🎨 Collect & simplify package utils (#8080)
closes #8056

🎨 Collect together the package-related utils
- read directory actually reads a directory of packages
- parse package json is very tighly related to this

🎨 Move filterPaths -> packages.filterPackages
- this function is related to packages, not settings
- move the function to the new utils/packages
- add 100% test coverage

🎨 Simplify filterPackages code
🎨 Simplify reading of packages & themes
- This massively reduces all the complex code in the read packages & themes utils
- Added full test coverage

🎨 Improve & clarify active prop in filterPackages
- active is returned from API endpoints to combine data from multiple sources
- see https://github.com/TryGhost/Ghost/pull/8064#discussion_r103514810

🎨 Better error handling
🔥 Temporarily remove custom error templates
- we will reimplement this later when we have got a better concept of loading the active theme in place
- refs #8079
2017-03-01 14:09:31 +01:00

89 lines
2.7 KiB
JavaScript

/**
* Dependencies
*/
var parsePackageJson = require('./parse-package-json'),
Promise = require('bluebird'),
_ = require('lodash'),
join = require('path').join,
fs = require('fs'),
notAPackageRegex = /^\.|_messages|README.md|node_modules|bower_components/i,
packageJSONPath = 'package.json',
statFile = Promise.promisify(fs.stat),
readDir = Promise.promisify(fs.readdir),
readPackage,
readPackages,
processPackage;
/**
* Recursively read directory and find the packages in it
*/
processPackage = function processPackage(absolutePath, packageName) {
var pkg = {
name: packageName,
path: absolutePath
};
return parsePackageJson(join(absolutePath, packageJSONPath))
.then(function gotPackageJSON(packageJSON) {
pkg['package.json'] = packageJSON;
return pkg;
})
.catch(function noPackageJSON() {
// ignore invalid package.json for now,
// because Ghost does not rely/use them at the moment
// in the future, this .catch() will need to be removed,
// so that error is thrown on invalid json syntax
pkg['package.json'] = null;
return pkg;
});
};
readPackage = function readPackage(packagePath, packageName) {
var absolutePath = join(packagePath, packageName);
return statFile(absolutePath)
.then(function (stat) {
if (!stat.isDirectory()) {
return {};
}
return processPackage(absolutePath, packageName)
.then(function gotPackage(pkg) {
var res = {};
res[packageName] = pkg;
return res;
});
})
.catch(function () {
return Promise.reject(new Error('Package not found'));
});
};
readPackages = function readPackages(packagePath) {
return readDir(packagePath)
.filter(function (packageName) {
// Filter out things which are not packages by regex
if (packageName.match(notAPackageRegex)) {
return;
}
// Check the remaining items to ensure they are a directory
return statFile(join(packagePath, packageName)).then(function (stat) {
return stat.isDirectory();
});
})
.map(function readPackageJson(packageName) {
var absolutePath = join(packagePath, packageName);
return processPackage(absolutePath, packageName);
})
.then(function (packages) {
return _.keyBy(packages, 'name');
});
};
/**
* Expose Public API
*/
module.exports.all = readPackages;
module.exports.one = readPackage;