flatten.js 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. // use the defined flattening function, and copy over any scoped
  2. // registries and registry-specific "nerfdart" configs verbatim
  3. //
  4. // TODO: make these getters so that we only have to make dirty
  5. // the thing that changed, and then flatten the fields that
  6. // could have changed when a config.set is called.
  7. //
  8. // TODO: move nerfdart auth stuff into a nested object that
  9. // is only passed along to paths that end up calling npm-registry-fetch.
  10. const definitions = require('./definitions.js')
  11. const flatten = (obj, flat = {}) => {
  12. for (const [key, val] of Object.entries(obj)) {
  13. const def = definitions[key]
  14. if (def && def.flatten)
  15. def.flatten(key, obj, flat)
  16. else if (/@.*:registry$/i.test(key) || /^\/\//.test(key))
  17. flat[key] = val
  18. }
  19. // XXX make this the bin/npm-cli.js file explicitly instead
  20. // otherwise using npm programmatically is a bit of a pain.
  21. flat.npmBin = require.main ? require.main.filename
  22. : /* istanbul ignore next - not configurable property */ undefined
  23. flat.nodeBin = process.env.NODE || process.execPath
  24. // XXX should this be sha512? is it even relevant?
  25. flat.hashAlgorithm = 'sha1'
  26. return flat
  27. }
  28. module.exports = flatten