2015-10-01 00:30:16 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Node.js core.
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
// Public node modules.
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* `parseJSONFile()`
|
|
|
|
*
|
|
|
|
* Read a json file at the specified path.
|
|
|
|
* If an error occurs, call cb(err), and dont throw!
|
|
|
|
*
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
2016-07-11 13:03:35 +02:00
|
|
|
exports.parseJSONFile = (path, cb) => {
|
2015-10-01 00:30:16 +02:00
|
|
|
if (!cb) {
|
|
|
|
throw new Error('Callback required!');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cb === 'sync') {
|
|
|
|
let jsonString;
|
|
|
|
|
|
|
|
try {
|
|
|
|
jsonString = fs.readFileSync(path, 'utf-8');
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return andThen(jsonString);
|
|
|
|
}
|
|
|
|
|
2016-07-11 13:03:35 +02:00
|
|
|
fs.readFile(path, 'utf-8', (err, file) => {
|
2015-10-01 00:30:16 +02:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
andThen(file);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Attempt to parse JSON, then return.
|
|
|
|
function andThen(json) {
|
|
|
|
let err;
|
|
|
|
|
|
|
|
try {
|
|
|
|
json = JSON.parse(json);
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
json = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse failed.
|
|
|
|
if (err) {
|
|
|
|
if (cb === 'sync') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success.
|
|
|
|
if (cb === 'sync') {
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(null, json);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* `getJSONFileSync()`
|
|
|
|
*
|
|
|
|
* Synchronous version of `getJSONFile()`.
|
|
|
|
* Returns false if json file cannot be read or parsed.
|
|
|
|
*
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
2016-07-11 13:03:35 +02:00
|
|
|
exports.parseJSONFileSync = path => {
|
2015-10-01 00:30:16 +02:00
|
|
|
return exports.parseJSONFile(path, 'sync');
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* `getPackage()`
|
|
|
|
*
|
|
|
|
* Read `package.json` file in the directory at the specified
|
|
|
|
* path. If an error occurs, call `cb(err)`, and dont throw!
|
|
|
|
*
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
2016-07-11 13:03:35 +02:00
|
|
|
exports.getPackage = (path, cb) => {
|
2016-03-18 11:12:50 +01:00
|
|
|
path = _.trimEnd(path, '/');
|
2015-10-01 00:30:16 +02:00
|
|
|
path += '/package.json';
|
|
|
|
|
2016-07-11 13:03:35 +02:00
|
|
|
exports.parseJSONFile(path, (err, json) => {
|
2015-10-01 00:30:16 +02:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success: ensure dependencies are at least an empty object
|
|
|
|
json.dependencies = json.dependencies || {};
|
|
|
|
if (cb === 'sync') {
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(null, json);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* `getPackageSync()`
|
|
|
|
*
|
|
|
|
* Synchronous version of `getPackage()`
|
|
|
|
* Returns `false` if `package.json` cannot be read or parsed.
|
|
|
|
*
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
2016-07-11 13:03:35 +02:00
|
|
|
exports.getPackageSync = path => {
|
2016-03-18 11:12:50 +01:00
|
|
|
path = _.trimEnd(path, '/');
|
2015-10-01 00:30:16 +02:00
|
|
|
path += '/package.json';
|
|
|
|
|
|
|
|
// Success: ensure dependencies are at least an empty object.
|
|
|
|
const json = exports.parseJSONFileSync(path, 'sync');
|
|
|
|
|
|
|
|
if (!json) {
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
|
|
|
json.dependencies = json.dependencies || {};
|
|
|
|
return json;
|
|
|
|
};
|