fetch.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. 'use strict'
  2. const duck = require('protoduck')
  3. const Fetcher = duck.define(['spec', 'opts', 'manifest'], {
  4. packument: ['spec', 'opts'],
  5. manifest: ['spec', 'opts'],
  6. tarball: ['spec', 'opts'],
  7. fromManifest: ['manifest', 'spec', 'opts'],
  8. clearMemoized () {}
  9. }, { name: 'Fetcher' })
  10. module.exports = Fetcher
  11. module.exports.packument = packument
  12. function packument (spec, opts) {
  13. const fetcher = getFetcher(spec.type)
  14. return fetcher.packument(spec, opts)
  15. }
  16. module.exports.manifest = manifest
  17. function manifest (spec, opts) {
  18. const fetcher = getFetcher(spec.type)
  19. return fetcher.manifest(spec, opts)
  20. }
  21. module.exports.tarball = tarball
  22. function tarball (spec, opts) {
  23. return getFetcher(spec.type).tarball(spec, opts)
  24. }
  25. module.exports.fromManifest = fromManifest
  26. function fromManifest (manifest, spec, opts) {
  27. return getFetcher(spec.type).fromManifest(manifest, spec, opts)
  28. }
  29. const fetchers = {}
  30. module.exports.clearMemoized = clearMemoized
  31. function clearMemoized () {
  32. Object.keys(fetchers).forEach(k => {
  33. fetchers[k].clearMemoized()
  34. })
  35. }
  36. function getFetcher (type) {
  37. if (!fetchers[type]) {
  38. // This is spelled out both to prevent sketchy stuff and to make life
  39. // easier for bundlers/preprocessors.
  40. switch (type) {
  41. case 'alias':
  42. fetchers[type] = require('./fetchers/alias')
  43. break
  44. case 'directory':
  45. fetchers[type] = require('./fetchers/directory')
  46. break
  47. case 'file':
  48. fetchers[type] = require('./fetchers/file')
  49. break
  50. case 'git':
  51. fetchers[type] = require('./fetchers/git')
  52. break
  53. case 'hosted':
  54. fetchers[type] = require('./fetchers/hosted')
  55. break
  56. case 'range':
  57. fetchers[type] = require('./fetchers/range')
  58. break
  59. case 'remote':
  60. fetchers[type] = require('./fetchers/remote')
  61. break
  62. case 'tag':
  63. fetchers[type] = require('./fetchers/tag')
  64. break
  65. case 'version':
  66. fetchers[type] = require('./fetchers/version')
  67. break
  68. default:
  69. throw new Error(`Invalid dependency type requested: ${type}`)
  70. }
  71. }
  72. return fetchers[type]
  73. }