forked from soldair/minify-registry-metadata
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
105 lines (89 loc) · 2.14 KB
/
index.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'use strict'
// take metadata and remove anything the cli doesnt need to install it.
const keep = [
'name',
'dist-tags',
]
const manifestKeep = [
'name',
'version',
'dependencies',
'optionalDependencies',
'devDependencies',
'bundleDependencies',
'peerDependencies',
'acceptDependencies',
'bin',
'_hasShrinkwrap',
'directories',
'dist',
'engines',
'deprecated',
'peerDependenciesMeta',
'funding',
'os',
'cpu',
]
const manifestSets = [
'versions',
'policyRestrictions',
'stagedVersions',
]
const isEmpty = val => {
return !val ||
(typeof val === 'object' && Object.keys(val).length === 0)
}
const minifyManifests = (doc, out, type) => {
if (!doc[type]) {
return
}
// policyRestrictions and staging have other metadata, and a versions obj
if (doc[type].versions) {
out[type] = {}
Object.keys(doc[type]).filter(k => k !== 'versions').forEach(k => {
out[type][k] = doc[type][k]
})
// ok, now minify the actual manifests on that object.
return minifyManifests(doc[type], out[type], 'versions')
}
// minify all the manifests
const smallVersions = {}
Object.keys(doc[type]).forEach(v => {
const manifest = doc[type][v]
const smallVersion = {}
if (manifest.bundledDependencies && !manifest.bundleDependencies) {
manifest.bundleDependencies = manifest.bundledDependencies
}
manifestKeep.forEach(field => {
if (!isEmpty(manifest[field])) {
smallVersion[field] = manifest[field]
}
})
if (manifest.scripts && (
manifest.scripts.preinstall ||
manifest.scripts.install ||
manifest.scripts.postinstall)) {
smallVersion.hasInstallScript = true
}
smallVersions[v] = smallVersion
})
out[type] = smallVersions
}
module.exports = doc => {
// not registry metadata
if (!doc) {
return false
}
const out = {}
for (let i = 0; i < keep.length; ++i) {
if (doc[keep[i]] !== undefined) {
out[keep[i]] = doc[keep[i]]
}
}
manifestSets.forEach(type => minifyManifests(doc, out, type))
const mtime = (doc.time || {}).modified
if (mtime) {
out.modified = mtime
}
return out
}