UNPKG

paco

Version:

Node package development/distribution utility kit

148 lines (121 loc) 3.74 kB
#!/usr/bin/env node 'use strict'; var fs = require('fs'); var path = require('path'); var deepExtend = require('deep-extend'); var objectify = require('node-objectify'); var pathHelpers = require('./paths'); // Default config var defaultPacorc = require('../pacorc-default.js'); function getFileAsJson(filePath) { try { return JSON.parse(fs.readFileSync(filePath)); } catch (ex) { return {}; } } function getLocalJSON(filename) { return getFileAsJson(path.resolve(process.cwd(), filename)); } function getPackageJSON() { return getLocalJSON('package.json'); } function getNearestPackageJson() { return getFileAsJson(getNearestPackageJSONPath(process.cwd())); } function getPacorc() { return getLocalJSON('.pacorc'); } function getNearestPacorc() { return getFileAsJson(getNearestPacorcPath(process.cwd())); } function getMergedPacorc() { var mergedConfigs = require('../pacorc-default'); var configPaths = pathHelpers.getTraversedFilesNamed('.pacorc'); configPaths.reverse(); var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = configPaths[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var configPath = _step.value; var config = getFileAsJson(configPath); mergedConfigs = deepExtend({}, mergedConfigs, config); } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } return mergedConfigs; } function getNearestPacorcPath(startPath) { return pathHelpers.getNearestPathToFileWithName('.pacorc', startPath || process.cwd()); } function getNearestPackageJSONPath(startPath) { return pathHelpers.getNearestPathToFileWithName('package.json', startPath || process.cwd()); } function getRootPacorcPath(startPath) { return pathHelpers.getHighestPathToFileNamed('.pacorc', startPath || process.cwd()); } /** * Returns a (derived) local paco config value * @param {String} key A config key * @return {any} A config value or an error */ function getConfig(key) { var allConfigs = getMergedPacorc(); var allConfigsMap = objectify(allConfigs); if (allConfigsMap.isSet(key)) { return allConfigsMap.get(key); } return new Error('There isn\'t a config named: ' + key); } /** * Saves a config to .pacorc * @param {String} key A config key * @param {any} value A config value */ function setConfig(key, value) { var configs = getNearestPacorc(); var defaultPacorcMap = objectify(defaultPacorc); if (defaultPacorcMap.isSet(key)) { var configsMap = objectify(configs); configsMap.set(key, value); savePacorc(configs); return value; } return new Error('There isn\'t a config named: ' + key); } function savePacorc(configs) { var configsContents = JSON.stringify(configs, null, ' '); var pacorcPath = getNearestPacorcPath(); fs.writeFile(pacorcPath, configsContents, { encoding: 'utf8' }, function (err) { if (err) { throw err; } }); } module.exports = { getFileAsJson: getFileAsJson, getLocalJSON: getLocalJSON, getPackageJSON: getPackageJSON, getNearestPackageJson: getNearestPackageJson, getPacorc: getPacorc, getNearestPacorc: getNearestPacorc, getMergedPacorc: getMergedPacorc, getNearestPacorcPath: getNearestPacorcPath, getNearestPackageJSONPath: getNearestPackageJSONPath, getRootPacorcPath: getRootPacorcPath, getConfig: getConfig, setConfig: setConfig };