|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const path = require('path'); |
| 4 | +const os = require('os'); |
| 5 | +const { readJson, writeJson } = require('./file'); |
| 6 | + |
| 7 | +exports.getMergedConfig = function(dir, home) { |
| 8 | + const globalConfig = exports.getConfig(true, home); |
| 9 | + const localConfig = exports.getConfig(false, dir); |
| 10 | + return Object.assign(globalConfig, localConfig); |
| 11 | +}; |
| 12 | + |
| 13 | +exports.getConfig = function(isGlobal, dir) { |
| 14 | + const configPath = exports.getConfigPath(isGlobal, dir); |
| 15 | + return readJson(configPath); |
| 16 | +}; |
| 17 | + |
| 18 | +exports.getConfigPath = function(isGlobal, dir) { |
| 19 | + if (isGlobal) { |
| 20 | + const home = exports.getHomeDir(dir); |
| 21 | + const ncurcPath = path.join(home, '.ncurc'); |
| 22 | + return ncurcPath; |
| 23 | + } else { |
| 24 | + const ncuDir = exports.getNcuDir(dir); |
| 25 | + const configPath = path.join(ncuDir, 'config'); |
| 26 | + return configPath; |
| 27 | + } |
| 28 | +}; |
| 29 | + |
| 30 | +exports.writeConfig = function(isGlobal, obj, dir) { |
| 31 | + writeJson(exports.getConfigPath(isGlobal, dir), obj); |
| 32 | +}; |
| 33 | + |
| 34 | +exports.updateConfig = function(isGlobal, obj, dir) { |
| 35 | + const config = exports.getConfig(isGlobal, dir); |
| 36 | + const configPath = exports.getConfigPath(isGlobal, dir); |
| 37 | + writeJson(configPath, Object.assign(config, obj)); |
| 38 | +}; |
| 39 | + |
| 40 | +exports.getHomeDir = function(home) { |
| 41 | + if (process.env.XDG_CONFIG_HOME) { |
| 42 | + return process.env.XDG_CONFIG_HOME; |
| 43 | + } |
| 44 | + return home || os.homedir(); |
| 45 | +}; |
| 46 | + |
| 47 | +exports.getNcuDir = function(dir) { |
| 48 | + return path.join(dir || process.cwd(), '.ncu'); |
| 49 | +}; |
0 commit comments