Ghost/core/server/utils/packages/read-packages.js
kirrg001 8680099765 🎨 gscan 1.1.0 & optimisations
refs #8222

- differentiate between errors and fatal errors
- use gscan errors in theme middleware
- Adds a new `error()` method to `currentActiveTheme` constructor which will return the errors we receive from gscan
- In middleware, if a theme couldn't be activated because it's invalid, we'll fetch the erros and send them to our error handler. We also use a new property `hideStack` to control, if the stack (in dev mode and if available) should be shown or the gscan errors (in prod mode, or in dev if no stack error)
- In our error handler we use this conditional to send a new property `gscan` to our error theme
- In `error.hbs` we'll iterate through possible `gscan` error objects and render them.
- remove stack printing
- stack for theme developers in development mode doesn't make sense
- stack in production doesn't make sense
- the stack is usually hard to read
- if you are developer you can read the error stack on the server log
- utils.packages: transform native error into Ghost error
- use `onlyFatalErrors` for gscan format and differeniate fatal errors vo.2
- optimise bootstrap error handling
- transform theme is missing into an error
- add new translation key
- show html tags for error.hbs template: rule
2017-06-06 13:07:50 +07:00

96 lines
2.9 KiB
JavaScript

/**
* Dependencies
*/
var parsePackageJson = require('./parse-package-json'),
errors = require('../../errors'),
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 (err) {
return Promise.reject(new errors.NotFoundError({
message: 'Package not found',
err: err,
help: 'path: ' + packagePath,
context: 'name: ' + packageName
}));
});
};
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;