-
Notifications
You must be signed in to change notification settings - Fork 46
/
fetch.js
82 lines (73 loc) · 2.12 KB
/
fetch.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
'use strict'
const duck = require('protoduck')
const Fetcher = duck.define(['spec', 'opts', 'manifest'], {
packument: ['spec', 'opts'],
manifest: ['spec', 'opts'],
tarball: ['spec', 'opts'],
fromManifest: ['manifest', 'spec', 'opts'],
clearMemoized () {}
}, { name: 'Fetcher' })
module.exports = Fetcher
module.exports.packument = packument
function packument (spec, opts) {
const fetcher = getFetcher(spec.type)
return fetcher.packument(spec, opts)
}
module.exports.manifest = manifest
function manifest (spec, opts) {
const fetcher = getFetcher(spec.type)
return fetcher.manifest(spec, opts)
}
module.exports.tarball = tarball
function tarball (spec, opts) {
return getFetcher(spec.type).tarball(spec, opts)
}
module.exports.fromManifest = fromManifest
function fromManifest (manifest, spec, opts) {
return getFetcher(spec.type).fromManifest(manifest, spec, opts)
}
const fetchers = {}
module.exports.clearMemoized = clearMemoized
function clearMemoized () {
Object.keys(fetchers).forEach(k => {
fetchers[k].clearMemoized()
})
}
function getFetcher (type) {
if (!fetchers[type]) {
// This is spelled out both to prevent sketchy stuff and to make life
// easier for bundlers/preprocessors.
switch (type) {
case 'alias':
fetchers[type] = require('./fetchers/alias')
break
case 'directory':
fetchers[type] = require('./fetchers/directory')
break
case 'file':
fetchers[type] = require('./fetchers/file')
break
case 'git':
fetchers[type] = require('./fetchers/git')
break
case 'hosted':
fetchers[type] = require('./fetchers/hosted')
break
case 'range':
fetchers[type] = require('./fetchers/range')
break
case 'remote':
fetchers[type] = require('./fetchers/remote')
break
case 'tag':
fetchers[type] = require('./fetchers/tag')
break
case 'version':
fetchers[type] = require('./fetchers/version')
break
default:
throw new Error(`Invalid dependency type requested: ${type}`)
}
}
return fetchers[type]
}