roc
Version:
Build modern web applications easily
108 lines (92 loc) • 4.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getAbsolutePath = getAbsolutePath;
exports.fileExists = fileExists;
exports.getRocPackageDependencies = getRocPackageDependencies;
exports.getRocNamespacedDependencies = getRocNamespacedDependencies;
exports.getRocPluginDependencies = getRocPluginDependencies;
exports.getPackageJson = getPackageJson;
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
/**
* Makes a path absolute if not already is that.
*
* @param {string} filepath - The filepath to make absolute.
* @param {string} [directory=process.cwd()] - The directory to resolve relative paths to. By default will use the
* current working directory.
*
* @returns {string} - An absolute path.
*/
function getAbsolutePath(filepath) {
let directory = arguments.length <= 1 || arguments[1] === undefined ? process.cwd() : arguments[1];
if (filepath) {
return _path2.default.isAbsolute(filepath) ? filepath : _path2.default.join(directory, filepath);
}
}
/**
* Verifies if a file exists.
*
* @param {string} filepath - The filepath to check. Will make it absolute if not already using {@link getAbsolutePath}.
* @param {string} [directory] - The directory to base the filepath on.
*
* @returns {boolean} - Whether or not it is a file.
*/
function fileExists(filepath, directory) {
filepath = getAbsolutePath(filepath, directory);
try {
return _fs2.default.statSync(filepath).isFile();
} catch (error) {
return false;
}
}
/**
* Gets the Roc package dependencies from a `package.json`.
*
* @param {Object} packageJson - A package.json file to fetch Roc package dependencies from.
*
* @returns {string[]} - An array with Roc packages that exists in the `package.json`.
*/
function getRocPackageDependencies(packageJson) {
return [].concat(_toConsumableArray(Object.keys(packageJson.dependencies || {})), _toConsumableArray(Object.keys(packageJson.devDependencies || {}))).filter(dependency => /^roc-package(-.+)/.test(dependency));
}
/**
* Gets @rocjs dependencies from a `package.json`.
*
* @param {Object} packageJson - A package.json file to fetch @rocjs dependencies from.
*
* @returns {string[]} - An array with @rocjs dependencies that exists in the `package.json`.
*/
function getRocNamespacedDependencies(packageJson) {
return [].concat(_toConsumableArray(Object.keys(packageJson.dependencies || {})), _toConsumableArray(Object.keys(packageJson.devDependencies || {}))).filter(dependency => /^@rocjs\/(.+)/.test(dependency));
}
/**
* Gets the Roc plugin dependencies from a `package.json`.
*
* @param {Object} packageJson - A package.json file to fetch Roc plugin dependencies from.
*
* @returns {string[]} - An array with Roc plugins that exists in the `package.json`.
*/
function getRocPluginDependencies(packageJson) {
return [].concat(_toConsumableArray(Object.keys(packageJson.dependencies || {})), _toConsumableArray(Object.keys(packageJson.devDependencies || {}))).filter(dependency => /^roc-plugin(-.+)/.test(dependency));
}
/**
* Reads a `package.json` file.
*
* @param {string} [directory=processs.cwd()] - In what directory to look for the `package.json`.
*
* @returns {Object|undefined} - The object in the `package.json` or undefined if it did not exists.
*/
function getPackageJson() {
let directory = arguments.length <= 0 || arguments[0] === undefined ? process.cwd() : arguments[0];
if (fileExists('package.json', directory)) {
return require(_path2.default.join(directory, 'package.json'));
}
return undefined;
}
//# sourceMappingURL=index.js.map