2021-06-09 16:11:23 +03:00
|
|
|
/*
|
|
|
|
* # Package Utils
|
|
|
|
*
|
|
|
|
* Ghost has 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.
|
|
|
|
*
|
|
|
|
*/
|
2020-11-24 00:48:32 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const join = require('path').join;
|
|
|
|
const errors = require('@tryghost/errors');
|
2021-06-09 16:11:23 +03:00
|
|
|
const parse = require('./parse');
|
2020-11-24 00:48:32 +03:00
|
|
|
|
2021-06-09 17:10:36 +03:00
|
|
|
// Require bluebird with its own namespace and use it explicitly where we need additional features
|
|
|
|
const Bluebird = require('bluebird');
|
|
|
|
|
2020-11-24 00:48:32 +03:00
|
|
|
const notAPackageRegex = /^\.|_messages|README.md|node_modules|bower_components/i;
|
|
|
|
const packageJSONPath = 'package.json';
|
|
|
|
|
2021-06-09 17:10:36 +03:00
|
|
|
/**
|
|
|
|
* @typedef {object} PackageList
|
|
|
|
* @typedef {object} Package
|
|
|
|
*/
|
|
|
|
|
2020-11-24 00:48:32 +03:00
|
|
|
/**
|
2021-06-09 16:11:23 +03:00
|
|
|
* Recursively read directory and find the packages in it
|
2021-01-19 23:59:45 +03:00
|
|
|
*
|
2021-06-09 16:11:23 +03:00
|
|
|
* @param {string} absolutePath
|
|
|
|
* @param {string} packageName
|
2021-06-09 17:10:36 +03:00
|
|
|
* @returns {Promise<Package>}
|
2020-11-24 00:48:32 +03:00
|
|
|
*/
|
2021-06-09 16:11:23 +03:00
|
|
|
async function processPackage(absolutePath, packageName) {
|
|
|
|
const pkg = {
|
|
|
|
name: packageName,
|
|
|
|
path: absolutePath
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const packageJSON = await 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;
|
2020-11-24 00:48:32 +03:00
|
|
|
}
|
|
|
|
|
2021-06-09 16:11:23 +03:00
|
|
|
return pkg;
|
|
|
|
}
|
2020-11-24 18:57:56 +03:00
|
|
|
|
2021-06-09 16:11:23 +03:00
|
|
|
/**
|
|
|
|
* ### Filter Packages
|
2021-06-09 17:10:36 +03:00
|
|
|
* Normalizes packages read by readPackages so that the themes module can use them.
|
2021-06-09 16:11:23 +03:00
|
|
|
* Iterates over each package and return an array of objects which are simplified representations of the package
|
|
|
|
* with 3 properties:
|
2021-06-09 17:10:36 +03:00
|
|
|
*
|
|
|
|
* @typedef {object} SimplePackage
|
|
|
|
* @prop {string} name - the package name
|
|
|
|
* @prop {object|boolean} package - contents of the package.json or false if there isn't one
|
|
|
|
* @prop {boolean} active - set to true if this package is active
|
|
|
|
*
|
2021-06-09 16:11:23 +03:00
|
|
|
* 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
|
|
|
|
*
|
2021-06-09 17:10:36 +03:00
|
|
|
* @param {PackageList} packages object made up of packages keyed by name as returned by readPackages
|
|
|
|
* @param {array|string} [active] optional set of names of packages that are active
|
|
|
|
* @returns {Array<SimplePackage>} array of objects with useful info about themes
|
2021-06-09 16:11:23 +03:00
|
|
|
*/
|
|
|
|
function 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];
|
|
|
|
}
|
2020-11-24 00:48:32 +03:00
|
|
|
|
2021-06-09 16:11:23 +03:00
|
|
|
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
|
|
|
|
};
|
2020-11-24 00:48:32 +03:00
|
|
|
|
2021-06-09 16:11:23 +03:00
|
|
|
result.push(item);
|
2020-11-24 18:57:56 +03:00
|
|
|
}
|
2020-11-24 00:48:32 +03:00
|
|
|
|
2021-06-09 16:11:23 +03:00
|
|
|
return result;
|
|
|
|
}, []);
|
|
|
|
}
|
2020-11-24 00:48:32 +03:00
|
|
|
|
2021-06-09 16:11:23 +03:00
|
|
|
/**
|
|
|
|
* @param {string} packagePath
|
|
|
|
* @param {string} packageName
|
2021-06-09 17:10:36 +03:00
|
|
|
* @returns {Promise<Package>}
|
2021-06-09 16:11:23 +03:00
|
|
|
*/
|
|
|
|
async function readPackage(packagePath, packageName) {
|
|
|
|
const absolutePath = join(packagePath, packageName);
|
2020-11-24 18:57:56 +03:00
|
|
|
|
2021-06-09 16:11:23 +03:00
|
|
|
try {
|
|
|
|
const stat = await fs.stat(absolutePath);
|
|
|
|
if (!stat.isDirectory()) {
|
|
|
|
return {};
|
2020-11-24 18:57:56 +03:00
|
|
|
}
|
|
|
|
|
2021-06-09 16:11:23 +03:00
|
|
|
const pkg = await 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
|
|
|
}
|
2021-06-09 16:11:23 +03:00
|
|
|
}
|
2020-11-24 00:48:32 +03:00
|
|
|
|
2021-06-09 16:11:23 +03:00
|
|
|
/**
|
|
|
|
* @param {string} packagePath
|
2021-06-09 17:10:36 +03:00
|
|
|
* @returns {Promise<PackageList>}
|
2021-06-09 16:11:23 +03:00
|
|
|
*/
|
2021-06-09 17:10:36 +03:00
|
|
|
async function readPackages(packagePath) {
|
|
|
|
return Bluebird.resolve(fs.readdir(packagePath))
|
2021-06-09 16:11:23 +03:00
|
|
|
.filter(function (packageName) {
|
|
|
|
// Filter out things which are not packages by regex
|
|
|
|
if (packageName.match(notAPackageRegex)) {
|
|
|
|
return;
|
2020-11-24 18:57:56 +03:00
|
|
|
}
|
2021-06-09 16:11:23 +03:00
|
|
|
// Check the remaining items to ensure they are a directory
|
2021-06-09 17:10:36 +03:00
|
|
|
return fs.stat(join(packagePath, packageName))
|
|
|
|
.then(function (stat) {
|
|
|
|
return stat.isDirectory();
|
|
|
|
});
|
2021-06-09 16:11:23 +03:00
|
|
|
})
|
|
|
|
.map(function readPackageJson(packageName) {
|
|
|
|
const absolutePath = join(packagePath, packageName);
|
|
|
|
return processPackage(absolutePath, packageName);
|
|
|
|
})
|
|
|
|
.then(function (packages) {
|
|
|
|
return _.keyBy(packages, 'name');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
filter,
|
|
|
|
readPackage,
|
|
|
|
readPackages
|
2020-11-24 00:48:32 +03:00
|
|
|
};
|