forked from cdnjs/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
339 lines (287 loc) · 8.93 KB
/
main.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/cdnjs/tools/git"
"github.com/cdnjs/tools/npm"
"github.com/cdnjs/tools/packages"
"github.com/cdnjs/tools/util"
)
var (
// Store the number of validation errors
errCount uint = 0
// initialize checker debug logger
logger = util.GetCheckerLogger()
// regex for path in cdnjs/packages/
pckgPathRegex = regexp.MustCompile("^packages/([a-z0-9])/([a-zA-Z0-9._-]+).json$")
)
func main() {
var noPathValidation bool
flag.BoolVar(&noPathValidation, "no-path-validation", false, "If set, all package paths are accepted.")
flag.Parse()
if util.IsDebug() {
fmt.Println("Running in debug mode")
}
switch subcommand := flag.Arg(0); subcommand {
case "lint":
{
for _, path := range flag.Args()[1:] {
lintPackage(path, noPathValidation)
}
if errCount > 0 {
os.Exit(1)
}
}
case "show-files":
{
showFiles(flag.Arg(1), noPathValidation)
if errCount > 0 {
os.Exit(1)
}
}
default:
panic(fmt.Sprintf("unknown subcommand: `%s`", subcommand))
}
}
// Represents a version of a package,
// which could be a git version, npm version, etc.
type version interface {
Get() string // Get the version as a string.
Download(...interface{}) string // Download a version, returning the download dir.
Clean(string) // Clean a download dir.
}
func showFiles(pckgPath string, noPathValidation bool) {
// create context with file path prefix, checker logger
ctx := util.ContextWithEntries(util.GetCheckerEntries(pckgPath, logger)...)
// parse *Package from JSON
pckg := parseHumanPackage(ctx, pckgPath, noPathValidation)
if pckg == nil {
return
}
// autoupdate exists, download latest versions based on source
src := *pckg.Autoupdate.Source
var versions []version
var downloadDir, noVersionsErr string
switch src {
case "npm":
{
// get npm versions and sort
npmVersions, _ := npm.GetVersions(ctx, *pckg.Autoupdate.Target)
sort.Sort(npm.ByTimeStamp(npmVersions))
// cast to interface
for _, v := range npmVersions {
versions = append(versions, v)
}
// download into temp dir
if len(versions) > 0 {
downloadDir = npm.DownloadTar(ctx, npmVersions[0].Tarball)
}
// set err string if no versions
noVersionsErr = "no version found on npm"
}
case "git":
{
// make temp dir and clone
packageGitDir, direrr := ioutil.TempDir("", src)
util.Check(direrr)
out, cloneerr := git.Clone(ctx, *pckg.Autoupdate.Target, packageGitDir)
if cloneerr != nil {
err(ctx, fmt.Sprintf("could not clone repo: %s: %s\n", cloneerr, out))
return
}
// get git versions and sort
gitVersions, _ := git.GetVersions(ctx, packageGitDir)
sort.Sort(git.ByTimeStamp(gitVersions))
// cast to interface
for _, v := range gitVersions {
versions = append(versions, v)
}
// set download dir
downloadDir = packageGitDir
// set err string if no versions
noVersionsErr = "no tagged version found in git"
}
default:
{
panic(fmt.Sprintf("unknown autoupdate source: %s", src))
}
}
// clean up temp dir
defer os.RemoveAll(downloadDir)
// enforce at least one version
if len(versions) == 0 {
err(ctx, noVersionsErr)
return
}
// limit versions
if len(versions) > util.ImportAllMaxVersions {
versions = versions[:util.ImportAllMaxVersions]
}
// print info for first src version
printMostRecentVersion(ctx, pckg, downloadDir, versions[0])
// print aggregate info for the few last src versions
printLastVersions(ctx, pckg, downloadDir, versions[1:])
}
// Try to parse a *Package, outputting ci errors/warnings.
// If there is an issue, *Package will be nil.
func parseHumanPackage(ctx context.Context, pckgPath string, noPathValidation bool) *packages.Package {
if !noPathValidation {
// check package path matches regex
matches := pckgPathRegex.FindStringSubmatch(pckgPath)
if matches == nil {
err(ctx, fmt.Sprintf("package path `%s` does not match %s", pckgPath, pckgPathRegex.String()))
return nil
}
// check the package is going into the correct folder
// (ex. My-Package -> packages/m/My-Package.json)
actualDir, pckgName := matches[1], matches[2]
expectedDir := strings.ToLower(string(pckgName[0]))
if actualDir != expectedDir {
err(ctx, fmt.Sprintf("package `%s` must go into `%s` dir, not `%s` dir", pckgName, expectedDir, actualDir))
return nil
}
}
// parse package JSON
pckg, readerr := packages.ReadHumanJSONFile(ctx, pckgPath)
if readerr != nil {
if invalidHumanErr, ok := readerr.(packages.InvalidSchemaError); ok {
// output all schema errors
for _, resErr := range invalidHumanErr.Result.Errors() {
err(ctx, resErr.String())
}
} else {
err(ctx, readerr.Error())
}
return nil
}
checkFilename(ctx, pckg)
return pckg
}
// Prints the files of a package version, outputting debug
// messages if no valid files are present.
func printMostRecentVersion(ctx context.Context, p *packages.Package, dir string, v version) {
fmt.Printf("\nmost recent version: %s\n", v.Get())
downloadDir := v.Download(ctx, dir)
defer v.Clean(downloadDir)
filesToCopy := p.NpmFilesFrom(downloadDir)
if len(filesToCopy) == 0 {
errormsg := fmt.Sprintf("No files will be published for version %s.\n", v.Get())
for _, fileMap := range p.Autoupdate.FileMap {
for _, pattern := range fileMap.Files {
errormsg += fmt.Sprintf("[Click here to debug your glob pattern `%s`](%s).\n", pattern, makeGlobDebugLink(pattern, downloadDir))
}
}
err(ctx, errormsg)
return
}
var filenameFound bool
fmt.Printf("\n```\n")
for _, file := range filesToCopy {
fmt.Printf("%s\n", file.To)
if p.Filename != nil && !filenameFound && file.To == *p.Filename {
filenameFound = true
}
}
fmt.Printf("```\n")
if p.Filename != nil && !filenameFound {
err(ctx, fmt.Sprintf("Filename `%s` not found in most recent version `%s`.\n", *p.Filename, v.Get()))
}
}
// Prints the matching files of a number of last versions.
// Each previous version will be downloaded and cleaned up if necessary.
// For example, a temporary directory may be downloaded and then removed later.
func printLastVersions(ctx context.Context, p *packages.Package, dir string, versions []version) {
fmt.Printf("\n%d last version(s):\n", len(versions))
for _, version := range versions {
downloadDir := version.Download(ctx, dir)
defer version.Clean(downloadDir)
filesToCopy := p.NpmFilesFrom(downloadDir)
fmt.Printf("- %s: %d file(s) matched", version.Get(), len(filesToCopy))
if len(filesToCopy) > 0 {
fmt.Printf(" :heavy_check_mark:\n")
} else {
fmt.Printf(" :heavy_exclamation_mark:\n")
}
}
}
func makeGlobDebugLink(glob string, dir string) string {
encodedGlob := url.QueryEscape(glob)
allTests := ""
util.Check(filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
allTests += "&tests=" + url.QueryEscape(info.Name())
}
return nil
}))
return fmt.Sprintf("https://www.digitalocean.com/community/tools/glob?comments=true&glob=%s&matches=true%s&tests=", encodedGlob, allTests)
}
func checkGitHubPopularity(ctx context.Context, pckg *packages.Package) bool {
if !strings.Contains(*pckg.Repository.URL, "github.com") {
return false
}
if s := git.GetGitHubStars(*pckg.Repository.URL); s.Stars < util.MinGitHubStars {
warn(ctx, fmt.Sprintf("stars on GitHub is under %d", util.MinGitHubStars))
return false
}
return true
}
func checkFilename(ctx context.Context, pckg *packages.Package) {
// warn if filename is not present
// current, only a few packages have exceptions
// that allow them to have missing filenames
if pckg.Filename == nil {
warn(ctx, "filename is missing")
}
}
func lintPackage(pckgPath string, noPathValidation bool) {
// create context with file path prefix, checker logger
ctx := util.ContextWithEntries(util.GetCheckerEntries(pckgPath, logger)...)
util.Debugf(ctx, "Linting %s...\n", pckgPath)
// parse *Package from JSON
pckg := parseHumanPackage(ctx, pckgPath, noPathValidation)
if pckg == nil {
return
}
switch *pckg.Autoupdate.Source {
case "npm":
{
// check that it exists
if !npm.Exists(*pckg.Autoupdate.Target) {
err(ctx, "package doesn't exist on npm")
break
}
// check if it has enough downloads
if md := npm.GetMonthlyDownload(*pckg.Autoupdate.Target); md.Downloads < util.MinNpmMonthlyDownloads {
if !checkGitHubPopularity(ctx, pckg) {
warn(ctx, fmt.Sprintf("package download per month on npm is under %d", util.MinNpmMonthlyDownloads))
}
}
}
case "git":
{
checkGitHubPopularity(ctx, pckg)
}
default:
{
// schema will enforce npm or git, so panic
panic(fmt.Sprintf("unsupported .autoupdate.source: " + *pckg.Autoupdate.Source))
}
}
}
// wrapper around outputting a checker error
func err(ctx context.Context, s string) {
util.Errf(ctx, s)
errCount++
}
// wrapper around outputting a checker warning
func warn(ctx context.Context, s string) {
util.Warnf(ctx, s)
}