forked from cdnjs/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
npm.go
155 lines (121 loc) · 4.13 KB
/
npm.go
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"context"
"fmt"
"os"
"path"
"sort"
"github.com/cdnjs/tools/git"
"github.com/cdnjs/tools/npm"
"github.com/cdnjs/tools/packages"
"github.com/cdnjs/tools/util"
)
func updateNpm(ctx context.Context, pckg *packages.Package) ([]newVersionToCommit, []version) {
var newVersionsToCommit []newVersionToCommit
var allVersions []version
existingVersionSet := pckg.Versions()
util.Debugf(ctx, "existing npm versions: %v\n", existingVersionSet)
npmVersions, _ := npm.GetVersions(ctx, *pckg.Autoupdate.Target)
lastExistingVersion, allExisting := npm.GetMostRecentExistingVersion(ctx, existingVersionSet, npmVersions)
// add all existing versions to all versions list
for _, v := range allExisting {
allVersions = append(allVersions, version(v))
}
if lastExistingVersion != nil {
util.Debugf(ctx, "last existing version: %s\n", lastExistingVersion.Version)
versionDiff := npmVersionDiff(npmVersions, existingVersionSet)
sort.Sort(npm.ByTimeStamp(versionDiff))
newNpmVersions := make([]npm.Version, 0)
for i := len(versionDiff) - 1; i >= 0; i-- {
v := versionDiff[i]
if v.TimeStamp.After(lastExistingVersion.TimeStamp) {
newNpmVersions = append(newNpmVersions, v)
}
}
sort.Sort(sort.Reverse(npm.ByTimeStamp(npmVersions)))
newVersionsToCommit = doUpdateNpm(ctx, pckg, newNpmVersions)
} else {
if len(existingVersionSet) > 0 {
// all existing versions are not on npm anymore
util.Debugf(ctx, "all existing versions not on npm: %s\n", *pckg.Name)
}
// Import all the versions since we have no current npm versions locally.
// Limit the number of version to an arbitrary number to avoid publishing
// too many outdated versions.
sort.Sort(sort.Reverse(npm.ByTimeStamp(npmVersions)))
if len(npmVersions) > util.ImportAllMaxVersions {
npmVersions = npmVersions[len(npmVersions)-util.ImportAllMaxVersions:]
}
// Reverse the array to have the older versions first
// It matters when we will commit the updates
sort.Sort(sort.Reverse(npm.ByTimeStamp(npmVersions)))
newVersionsToCommit = doUpdateNpm(ctx, pckg, npmVersions)
}
// add all new versions to list of all versions
for _, v := range newVersionsToCommit {
allVersions = append(allVersions, version(v))
}
return newVersionsToCommit, allVersions
}
func doUpdateNpm(ctx context.Context, pckg *packages.Package, versions []npm.Version) []newVersionToCommit {
newVersionsToCommit := make([]newVersionToCommit, 0)
if len(versions) == 0 {
return newVersionsToCommit
}
for _, version := range versions {
pckgpath := path.Join(pckg.LibraryPath(), version.Version)
if _, err := os.Stat(pckgpath); !os.IsNotExist(err) {
util.Debugf(ctx, "%s already exists; aborting", pckgpath)
continue
}
if git.IsPathIgnored(ctx, util.GetCDNJSPath(), pckgpath) {
util.Debugf(ctx, "%s is ignored by git; aborting\n", pckgpath)
continue
}
tarballDir := npm.DownloadTar(ctx, version.Tarball)
filesToCopy := pckg.NpmFilesFrom(tarballDir)
if len(filesToCopy) > 0 {
util.Check(os.MkdirAll(pckgpath, os.ModePerm))
for _, fileMoveOp := range filesToCopy {
absFrom := path.Join(tarballDir, fileMoveOp.From)
absDest := path.Join(pckgpath, fileMoveOp.To)
if _, err := os.Stat(path.Dir(absDest)); os.IsNotExist(err) {
util.Check(os.MkdirAll(path.Dir(absDest), os.ModePerm))
}
util.Debugf(ctx, "%s -> %s\n", absFrom, absDest)
err := util.MoveFile(
ctx,
absFrom,
absDest,
)
if err != nil {
fmt.Println("could not move file:", err)
}
}
newVersionsToCommit = append(newVersionsToCommit, newVersionToCommit{
versionPath: pckgpath,
newVersion: version.Version,
pckg: pckg,
timestamp: version.TimeStamp,
})
} else {
util.Debugf(ctx, "no files matched")
}
// clean up temporary tarball dir
util.Check(os.RemoveAll(tarballDir))
}
return newVersionsToCommit
}
func npmVersionDiff(a []npm.Version, b []string) []npm.Version {
diff := make([]npm.Version, 0)
m := make(map[string]bool)
for _, item := range b {
m[item] = true
}
for _, item := range a {
if _, ok := m[item.Version]; !ok {
diff = append(diff, item)
}
}
return diff
}