mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-17 23:44:39 -05:00
Moved zip folder, read csv and package-json to lib/fs
refs #9178, refs 849e97640f
- i've reconsidered, these modules belong to lib
- prettify package-json module
This commit is contained in:
commit
69c1e2647d
4 changed files with 215 additions and 0 deletions
44
ghost/package-json/lib/filter.js
Normal file
44
ghost/package-json/lib/filter.js
Normal file
|
@ -0,0 +1,44 @@
|
|||
var _ = require('lodash'),
|
||||
notAPackageRegex = /^\.|_messages|README.md/i,
|
||||
filterPackages;
|
||||
|
||||
/**
|
||||
* ### Filter Packages
|
||||
* Normalizes packages read by read-packages so that the apps and themes modules 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 apps / themes
|
||||
*/
|
||||
filterPackages = function filterPackages(packages, active) {
|
||||
// turn active into an array (so themes and apps can be checked the same)
|
||||
if (!Array.isArray(active)) {
|
||||
active = [active];
|
||||
}
|
||||
|
||||
return _.reduce(packages, function (result, pkg, key) {
|
||||
var item = {};
|
||||
if (!key.match(notAPackageRegex)) {
|
||||
item = {
|
||||
name: key,
|
||||
package: pkg['package.json'] || false,
|
||||
active: _.indexOf(active, key) !== -1
|
||||
};
|
||||
|
||||
result.push(item);
|
||||
}
|
||||
|
||||
return result;
|
||||
}, []);
|
||||
};
|
||||
|
||||
module.exports = filterPackages;
|
27
ghost/package-json/lib/index.js
Normal file
27
ghost/package-json/lib/index.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
* # 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: an early version of apps, replace fundamental pieces like storage, will become npm modules
|
||||
* - Apps: plugins that can be installed whilst Ghost is running & modify behaviour
|
||||
* - More?
|
||||
*
|
||||
* These utils facilitate loading, reading, managing etc, packages from the file system.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
get read() {
|
||||
return require('./read');
|
||||
},
|
||||
|
||||
get parse() {
|
||||
return require('./parse');
|
||||
},
|
||||
|
||||
get filter() {
|
||||
return require('./filter');
|
||||
}
|
||||
};
|
53
ghost/package-json/lib/parse.js
Normal file
53
ghost/package-json/lib/parse.js
Normal file
|
@ -0,0 +1,53 @@
|
|||
/**
|
||||
* Dependencies
|
||||
*/
|
||||
|
||||
var Promise = require('bluebird'),
|
||||
fs = require('fs-extra'),
|
||||
common = require('../../common');
|
||||
|
||||
/**
|
||||
* Parse package.json and validate it has
|
||||
* all the required fields
|
||||
*/
|
||||
|
||||
function parsePackageJson(path) {
|
||||
return fs.readFile(path)
|
||||
.catch(function () {
|
||||
var err = new Error(common.i18n.t('errors.utils.parsepackagejson.couldNotReadPackage'));
|
||||
err.context = path;
|
||||
|
||||
return Promise.reject(err);
|
||||
})
|
||||
.then(function (source) {
|
||||
var hasRequiredKeys, json, err;
|
||||
|
||||
try {
|
||||
json = JSON.parse(source);
|
||||
|
||||
hasRequiredKeys = json.name && json.version;
|
||||
|
||||
if (!hasRequiredKeys) {
|
||||
err = new Error(common.i18n.t('errors.utils.parsepackagejson.nameOrVersionMissing'));
|
||||
err.context = path;
|
||||
err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'http://docs.ghost.org/themes/'});
|
||||
|
||||
return Promise.reject(err);
|
||||
}
|
||||
|
||||
return json;
|
||||
} catch (parseError) {
|
||||
err = new Error(common.i18n.t('errors.utils.parsepackagejson.themeFileIsMalformed'));
|
||||
err.context = path;
|
||||
err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'http://docs.ghost.org/themes/'});
|
||||
|
||||
return Promise.reject(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Expose `parsePackageJson`
|
||||
*/
|
||||
|
||||
module.exports = parsePackageJson;
|
91
ghost/package-json/lib/read.js
Normal file
91
ghost/package-json/lib/read.js
Normal file
|
@ -0,0 +1,91 @@
|
|||
/**
|
||||
* Dependencies
|
||||
*/
|
||||
var Promise = require('bluebird'),
|
||||
_ = require('lodash'),
|
||||
join = require('path').join,
|
||||
fs = require('fs-extra'),
|
||||
parsePackageJson = require('./parse'),
|
||||
common = require('../../common'),
|
||||
|
||||
notAPackageRegex = /^\.|_messages|README.md|node_modules|bower_components/i,
|
||||
packageJSONPath = 'package.json',
|
||||
|
||||
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 fs.stat(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 common.errors.NotFoundError({
|
||||
message: 'Package not found',
|
||||
err: err,
|
||||
help: 'path: ' + packagePath,
|
||||
context: 'name: ' + packageName
|
||||
}));
|
||||
});
|
||||
};
|
||||
|
||||
readPackages = function readPackages(packagePath) {
|
||||
return fs.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 fs.stat(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;
|
Loading…
Add table
Reference in a new issue