Sha256: d475e451ec86dff19034dd5ef471a05b0502988ef4b3a8d8a6401c34fd721cd8

Contents?: true

Size: 1.45 KB

Versions: 41

Compression:

Stored size: 1.45 KB

Contents

'use strict'
const fs = require('fs')
const path = require('path')
const YError = require('./yerror')

let previouslyVisitedConfigs = []

function checkForCircularExtends (cfgPath) {
  if (previouslyVisitedConfigs.indexOf(cfgPath) > -1) {
    throw new YError(`Circular extended configurations: '${cfgPath}'.`)
  }
}

function getPathToDefaultConfig (cwd, pathToExtend) {
  return path.resolve(cwd, pathToExtend)
}

function applyExtends (config, cwd) {
  let defaultConfig = {}

  if (config.hasOwnProperty('extends')) {
    if (typeof config.extends !== 'string') return defaultConfig
    const isPath = /\.json|\..*rc$/.test(config.extends)
    let pathToDefault = null
    if (!isPath) {
      try {
        pathToDefault = require.resolve(config.extends)
      } catch (err) {
        // most likely this simply isn't a module.
      }
    } else {
      pathToDefault = getPathToDefaultConfig(cwd, config.extends)
    }
    // maybe the module uses key for some other reason,
    // err on side of caution.
    if (!pathToDefault && !isPath) return config

    checkForCircularExtends(pathToDefault)

    previouslyVisitedConfigs.push(pathToDefault)

    defaultConfig = isPath ? JSON.parse(fs.readFileSync(pathToDefault, 'utf8')) : require(config.extends)
    delete config.extends
    defaultConfig = applyExtends(defaultConfig, path.dirname(pathToDefault))
  }

  previouslyVisitedConfigs = []

  return Object.assign({}, defaultConfig, config)
}

module.exports = applyExtends

Version data entries

41 entries across 29 versions & 11 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/webpack-dev-server/node_modules/yargs/lib/apply-extends.js
optimacms-0.1.61 spec/dummy/node_modules/yargs/lib/apply-extends.js
disco_app-0.18.0 test/dummy/node_modules/webpack-cli/node_modules/yargs/lib/apply-extends.js
disco_app-0.18.0 test/dummy/node_modules/yargs/lib/apply-extends.js
disco_app-0.18.2 test/dummy/node_modules/yargs/lib/apply-extends.js
disco_app-0.18.2 test/dummy/node_modules/webpack-cli/node_modules/yargs/lib/apply-extends.js
disco_app-0.16.1 test/dummy/node_modules/yargs/lib/apply-extends.js
disco_app-0.15.2 test/dummy/node_modules/yargs/lib/apply-extends.js
disco_app-0.18.4 test/dummy/node_modules/yargs/lib/apply-extends.js
disco_app-0.18.1 test/dummy/node_modules/yargs/lib/apply-extends.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/yargs/lib/apply-extends.js
disco_app-0.14.0 test/dummy/node_modules/yargs/lib/apply-extends.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/yargs/lib/apply-extends.js
tang-0.2.1 spec/tang_app/node_modules/yargs/lib/apply-extends.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/yargs/lib/apply-extends.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/yargs/lib/apply-extends.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/yargs/lib/apply-extends.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/yargs/lib/apply-extends.js
tang-0.2.0 spec/tang_app/node_modules/yargs/lib/apply-extends.js
tang-0.1.0 spec/tang_app/node_modules/yargs/lib/apply-extends.js