2015-10-06 16:36:56 +03:00
|
|
|
/**
|
|
|
|
* Dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
var Promise = require('bluebird'),
|
|
|
|
fs = require('fs'),
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n = require('../i18n'),
|
2015-10-06 16:36:56 +03:00
|
|
|
|
|
|
|
readFile = Promise.promisify(fs.readFile);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse package.json and validate it has
|
|
|
|
* all the required fields
|
|
|
|
*/
|
|
|
|
|
|
|
|
function parsePackageJson(path) {
|
|
|
|
return readFile(path)
|
|
|
|
.catch(function () {
|
2015-11-12 15:29:45 +03:00
|
|
|
var err = new Error(i18n.t('errors.utils.parsepackagejson.couldNotReadPackage'));
|
2015-10-06 16:36:56 +03:00
|
|
|
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) {
|
2015-11-12 15:29:45 +03:00
|
|
|
err = new Error(i18n.t('errors.utils.parsepackagejson.nameOrVersionMissing'));
|
2015-10-06 16:36:56 +03:00
|
|
|
err.context = path;
|
2015-11-12 15:29:45 +03:00
|
|
|
err.help = i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'http://docs.ghost.org/themes/'});
|
2015-10-06 16:36:56 +03:00
|
|
|
|
|
|
|
return Promise.reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return json;
|
2015-10-28 17:13:54 +03:00
|
|
|
} catch (parseError) {
|
2015-11-12 15:29:45 +03:00
|
|
|
err = new Error(i18n.t('errors.utils.parsepackagejson.themeFileIsMalformed'));
|
2015-10-06 16:36:56 +03:00
|
|
|
err.context = path;
|
2015-11-12 15:29:45 +03:00
|
|
|
err.help = i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'http://docs.ghost.org/themes/'});
|
2015-10-06 16:36:56 +03:00
|
|
|
|
|
|
|
return Promise.reject(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expose `parsePackageJson`
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = parsePackageJson;
|