2020-11-24 00:48:32 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const join = require('path').join;
|
|
|
|
const errors = require('@tryghost/errors');
|
|
|
|
|
|
|
|
const notAPackageRegex = /^\.|_messages|README.md|node_modules|bower_components/i;
|
|
|
|
const packageJSONPath = 'package.json';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* # Package Utils
|
|
|
|
*
|
|
|
|
* Ghost has / is in the process of gaining support for several different types of sub-packages:
|
|
|
|
* - Themes: have always been packages, but we're going to lean more heavily on npm & package.json in future
|
|
|
|
* - 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) {
|
|
|
|
this.i18n = i18n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Filter Packages
|
|
|
|
* Normalizes packages read by read-packages so that the themes module can use them.
|
|
|
|
* Iterates over each package and return an array of objects which are simplified representations of the package
|
|
|
|
* with 3 properties:
|
|
|
|
* - `name` - the package name
|
|
|
|
* - `package` - contents of the package.json or false if there isn't one
|
|
|
|
* - `active` - set to true if this package is active
|
|
|
|
* This data structure is used for listings of packages provided over the API and as such
|
|
|
|
* deliberately combines multiple sources of information in order to be efficient.
|
|
|
|
*
|
|
|
|
* TODO: simplify the package.json representation to contain only fields we use
|
|
|
|
*
|
|
|
|
* @param {object} packages as returned by read-packages
|
|
|
|
* @param {array/string} active as read from the settings object
|
|
|
|
* @returns {Array} of objects with useful info about themes
|
|
|
|
*/
|
|
|
|
filter(packages, active) {
|
|
|
|
// turn active into an array if it isn't one, so this function can deal with lists and one-offs
|
|
|
|
if (!Array.isArray(active)) {
|
|
|
|
active = [active];
|
|
|
|
}
|
|
|
|
|
|
|
|
return _.reduce(packages, function (result, pkg, key) {
|
|
|
|
let item = {};
|
|
|
|
if (!key.match(notAPackageRegex)) {
|
|
|
|
item = {
|
|
|
|
name: key,
|
|
|
|
package: pkg['package.json'] || false,
|
|
|
|
active: _.indexOf(active, key) !== -1
|
|
|
|
};
|
|
|
|
|
|
|
|
result.push(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}, []);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse package.json and validate it has
|
|
|
|
* all the required fields
|
|
|
|
*/
|
2020-11-24 18:57:56 +03:00
|
|
|
async parse(path) {
|
|
|
|
let source;
|
|
|
|
let json;
|
|
|
|
|
|
|
|
try {
|
|
|
|
source = await fs.readFile(path);
|
|
|
|
} catch (readError) {
|
|
|
|
const err = new Error(this.i18n.t('errors.utils.parsepackagejson.couldNotReadPackage'));
|
|
|
|
err.context = path;
|
|
|
|
err.err = readError;
|
|
|
|
|
|
|
|
return Promise.reject(err);
|
|
|
|
}
|
2020-11-24 00:48:32 +03:00
|
|
|
|
2020-11-24 18:57:56 +03:00
|
|
|
try {
|
|
|
|
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/'});
|
2020-11-24 00:48:32 +03:00
|
|
|
|
2020-11-24 18:57:56 +03:00
|
|
|
return Promise.reject(err);
|
|
|
|
}
|
2020-11-24 00:48:32 +03:00
|
|
|
|
2020-11-24 18:57:56 +03:00
|
|
|
const hasRequiredKeys = json.name && json.version;
|
2020-11-24 00:48:32 +03:00
|
|
|
|
2020-11-24 18:57:56 +03:00
|
|
|
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/'});
|
2020-11-24 00:48:32 +03:00
|
|
|
|
2020-11-24 18:57:56 +03:00
|
|
|
return Promise.reject(err);
|
|
|
|
}
|
2020-11-24 00:48:32 +03:00
|
|
|
|
2020-11-24 18:57:56 +03:00
|
|
|
return json;
|
2020-11-24 00:48:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursively read directory and find the packages in it
|
|
|
|
*
|
|
|
|
* @returns {object}
|
|
|
|
*/
|
2020-11-24 18:57:56 +03:00
|
|
|
async processPackage(absolutePath, packageName) {
|
2020-11-24 00:48:32 +03:00
|
|
|
const pkg = {
|
|
|
|
name: packageName,
|
|
|
|
path: absolutePath
|
|
|
|
};
|
2020-11-24 18:57:56 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
const packageJSON = await this.parse(join(absolutePath, packageJSONPath));
|
|
|
|
pkg['package.json'] = packageJSON;
|
|
|
|
} catch (err) {
|
|
|
|
// 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;
|
2020-11-24 00:48:32 +03:00
|
|
|
}
|
|
|
|
|
2020-11-24 18:57:56 +03:00
|
|
|
async readPackage(packagePath, packageName) {
|
2020-11-24 00:48:32 +03:00
|
|
|
const absolutePath = join(packagePath, packageName);
|
|
|
|
|
2020-11-24 18:57:56 +03:00
|
|
|
try {
|
|
|
|
const stat = await fs.stat(absolutePath);
|
|
|
|
if (!stat.isDirectory()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const pkg = await this.processPackage(absolutePath, packageName);
|
|
|
|
const res = {};
|
|
|
|
res[packageName] = pkg;
|
|
|
|
return res;
|
|
|
|
} catch (err) {
|
|
|
|
return Promise.reject(new errors.NotFoundError({
|
|
|
|
message: 'Package not found',
|
|
|
|
err: err,
|
|
|
|
help: 'path: ' + packagePath,
|
|
|
|
context: 'name: ' + packageName
|
|
|
|
}));
|
|
|
|
}
|
2020-11-24 00:48:32 +03:00
|
|
|
}
|
|
|
|
|
2020-11-25 13:44:15 +03:00
|
|
|
async readPackages(packagePath) {
|
|
|
|
const dirContents = await fs.readdir(packagePath);
|
2020-11-24 00:48:32 +03:00
|
|
|
|
2020-11-25 13:44:15 +03:00
|
|
|
const packageNames = dirContents
|
2020-11-24 00:48:32 +03:00
|
|
|
.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 fs.stat(join(packagePath, packageName)).then(function (stat) {
|
|
|
|
return stat.isDirectory();
|
|
|
|
});
|
|
|
|
});
|
2020-11-25 13:44:15 +03:00
|
|
|
|
|
|
|
const packages = await Promise.all(packageNames
|
|
|
|
.map((packageName) => {
|
|
|
|
const absolutePath = join(packagePath, packageName);
|
|
|
|
return this.processPackage(absolutePath, packageName);
|
|
|
|
}));
|
|
|
|
|
|
|
|
return _.keyBy(packages, 'name');
|
2020-11-24 00:48:32 +03:00
|
|
|
}
|
|
|
|
};
|