Skip to content

Commit

Permalink
Improve version listing
Browse files Browse the repository at this point in the history
  • Loading branch information
xtuc committed Dec 30, 2019
1 parent 267b545 commit 6138172
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 22 deletions.
12 changes: 6 additions & 6 deletions cmd/algolia/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ func indexPackage(p Package, index *algoliasearch.Index) error {
github, githuberr := getGitHubMeta(repository)
if githuberr != nil {
fmt.Printf("%s", githuberr)
if !strings.Contains(githuberr.Error(), "404 Not Found") {
// 404 Not Found errors are mostly. It's caused by a misconfiguration.
return fmt.Errorf("Fatal error `%s`", githuberr)
}
}

sri, srierr := getSRI(&p)
Expand Down Expand Up @@ -306,12 +310,8 @@ func main() {

for _, p := range j.Packages {
fmt.Printf("%s: ", p.Name)
err := indexPackage(p, tmpIndex)
if err != nil {
fmt.Printf("%s\n", err)
} else {
fmt.Printf("Ok\n")
}
util.Check(indexPackage(p, tmpIndex))
fmt.Printf("Ok\n")
}
fmt.Printf("Ok\n")

Expand Down
6 changes: 4 additions & 2 deletions cmd/packages/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func encodeJson(packages []outputPackage) (string, error) {

buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetIndent("", " ")
encoder.SetEscapeHTML(false)
err := encoder.Encode(&out)
return buffer.String(), err
Expand Down Expand Up @@ -64,7 +63,10 @@ func main() {
ctx := util.ContextWithName(f)

p, err := packages.ReadPackageJSON(ctx, f)
util.Check(err)
if err != nil {
util.Printf(ctx, "error while processing package: %s\n", err)
continue
}

if p.Version == "" {
util.Printf(ctx, "version is invalid\n")
Expand Down
71 changes: 71 additions & 0 deletions packages/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package packages

import (
"context"
"os/exec"
"path"
"path/filepath"
"strings"

"github.com/xtuc/cdnjs-go/util"
)

// We first list all the versions (and top-level package.json)
// in the package and pass the list to git ls-tree which is
// going to filter out those not in the tree
func GitListPackageVersions(ctx context.Context, basePath string) []string {
filesOnFs, err := filepath.Glob(path.Join(basePath, "*"))
util.Check(err)

filteredFilesOnFs := make([]string, 0)

// filter out package.json
for _, file := range filesOnFs {
if !strings.HasSuffix(file, "package.json") && strings.Trim(file, " ") != "" {
filteredFilesOnFs = append(filteredFilesOnFs, file)
}
}

args := []string{
"ls-tree", "--name-only", "origin/master",
}
args = append(args, filteredFilesOnFs...)

out, err := exec.Command("git", args...).Output()
util.Check(err)

outFiles := strings.Split(string(out), "\n")

filteredOutFiles := make([]string, 0)
// remove basePath from the output
for _, v := range outFiles {
if strings.Trim(v, " ") != "" {
filteredOutFiles = append(
filteredOutFiles, strings.ReplaceAll(v, basePath+"/", ""))
}
}

// Debug mode
diff := arrDiff(filteredFilesOnFs, outFiles)
if len(diff) > 0 {
util.Printf(ctx, "found staged version: %+q\n", diff)
}
// Debug mode

return filteredOutFiles
}

func arrDiff(a, b []string) (diff []string) {
m := make(map[string]bool)

for _, item := range b {
m[item] = true
}

for _, item := range a {
if _, ok := m[item]; !ok {
diff = append(diff, item)
}
}
return
}
17 changes: 3 additions & 14 deletions packages/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import (
"fmt"
"io/ioutil"
"path"
"path/filepath"
"sort"
"strings"

"github.com/xtuc/cdnjs-go/util"

"github.com/pkg/errors"
)

const (
PACKAGES_PATH = "./ajax/libs"
PACKAGES_PATH = "ajax/libs"
)

type Repository struct {
Expand Down Expand Up @@ -83,7 +81,7 @@ func ReadPackageJSON(ctx context.Context, file string) (*Package, error) {

data, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "failed to read %s", file)
}

jsonerr := json.Unmarshal(data, &jsondata)
Expand Down Expand Up @@ -233,16 +231,7 @@ func (p *Package) path() string {
}

func (p *Package) Versions() (versions []string) {
files, err := filepath.Glob(path.Join(p.path(), "*"))
util.Check(err)
// filter out package.json
for _, file := range files {
if !strings.HasSuffix(file, "package.json") {
parts := strings.Split(file, "/")
versions = append(versions, parts[3])
}
}
return versions
return GitListPackageVersions(p.ctx, p.path())
}

func (p *Package) files(version string) []string {
Expand Down

0 comments on commit 6138172

Please sign in to comment.