mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-25 11:55:03 +03:00
8687772604
refs #5940, #5923 - make read-directory ignore invalid package.json files - display a warning about invalid package.json files on startup - add tests to ensure read-directory continues, even with invalid package.json files
55 lines
1.4 KiB
JavaScript
55 lines
1.4 KiB
JavaScript
/**
|
|
* Dependencies
|
|
*/
|
|
|
|
var Promise = require('bluebird'),
|
|
fs = require('fs'),
|
|
|
|
readFile = Promise.promisify(fs.readFile);
|
|
|
|
/**
|
|
* Parse package.json and validate it has
|
|
* all the required fields
|
|
*/
|
|
|
|
function parsePackageJson(path) {
|
|
return readFile(path)
|
|
.catch(function () {
|
|
var err = new Error('Could not read package.json file');
|
|
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('"name" or "version" is missing from theme package.json file.');
|
|
err.context = path;
|
|
err.help = 'This will be required in future. Please see http://docs.ghost.org/themes/';
|
|
|
|
return Promise.reject(err);
|
|
}
|
|
|
|
return json;
|
|
} catch (parseError) {
|
|
err = new Error('Theme package.json file is malformed');
|
|
err.context = path;
|
|
err.help = 'This will be required in future. Please see http://docs.ghost.org/themes/';
|
|
|
|
return Promise.reject(err);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Expose `parsePackageJson`
|
|
*/
|
|
|
|
module.exports = parsePackageJson;
|