UNPKG

roc

Version:

Build modern web applications easily

89 lines (71 loc) 2.64 kB
'use strict'; exports.__esModule = true; exports.getAbsolutePath = getAbsolutePath; exports.fileExists = fileExists; exports.getRocDependencies = getRocDependencies; exports.getPackageJson = getPackageJson; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } require('source-map-support/register'); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); /** * 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) { var directory = arguments.length <= 1 || arguments[1] === undefined ? process.cwd() : arguments[1]; if (filepath) { return _path2['default'].isAbsolute(filepath) ? filepath : _path2['default'].join(directory, filepath); } } /** * Verifys 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 dependencies from a `package.json`. * * @param {Object} packageJson - A package.json file to fetch Roc dependencies from. * * @returns {string[]} - An array with Roc extensions that exists in the `package.json`. */ function getRocDependencies(packageJson) { return [].concat(Object.keys(packageJson.dependencies || {}), Object.keys(packageJson.devDependencies || {})).filter(function (dependecy) { return (/^roc(-.+)/.test(dependecy) ); }); } /** * 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() { var 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