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