Blame view

node_modules/yargs/lib/apply-extends.js 1.04 KB
aaac7fed   liuqimichale   add
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  var fs = require('fs')
  var path = require('path')
  var assign = require('./assign')
  var YError = require('./yerror')
  
  var previouslyVisitedConfigs = []
  
  function checkForCircularExtends (path) {
    if (previouslyVisitedConfigs.indexOf(path) > -1) {
      throw new YError("Circular extended configurations: '" + path + "'.")
    }
  }
  
  function getPathToDefaultConfig (cwd, pathToExtend) {
    return path.resolve(cwd, pathToExtend)
  }
  
  function applyExtends (config, cwd, subKey) {
    var defaultConfig = {}
  
    if (config.hasOwnProperty('extends')) {
      var pathToDefault = getPathToDefaultConfig(cwd, config.extends)
  
      checkForCircularExtends(pathToDefault)
  
      previouslyVisitedConfigs.push(pathToDefault)
      delete config.extends
  
      defaultConfig = JSON.parse(fs.readFileSync(pathToDefault, 'utf8'))
      if (subKey) {
        defaultConfig = defaultConfig[subKey] || {}
      }
      defaultConfig = applyExtends(defaultConfig, path.dirname(pathToDefault), subKey)
    }
  
    previouslyVisitedConfigs = []
  
    return assign(defaultConfig, config)
  }
  
  module.exports = applyExtends