这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ require (
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf // indirect
golang.org/x/text v0.3.6 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
4 changes: 2 additions & 2 deletions cli/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,5 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c h1:grhR+C34yXImVGp7EzNk+DTIk+323eIUWOmEevy6bDo=
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion cli/internal/backends/nodejs/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"turbo/internal/api"
"turbo/internal/fs"

"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
)

// nodejsPatterns is the FilenamePatterns value for NodejsBackend.
Expand Down
8 changes: 4 additions & 4 deletions cli/internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func WithGraph(rootpath string, config *config.Config) Option {
}

// this should go into the bacend abstraction
if c.Backend.Name == "nodejs-yarn" && !fs.CheckIfWindows() {
if c.Backend.Name == "nodejs-yarn" {
lockfile, err := fs.ReadLockfile(config.Cache.Dir)
if err != nil {
return fmt.Errorf("yarn.lock: %w", err)
Expand Down Expand Up @@ -154,7 +154,7 @@ func WithGraph(rootpath string, config *config.Config) Option {
globalDeps.Add(val)
}
}
if c.Backend.Name != "nodejs-yarn" || fs.CheckIfWindows() {
if c.Backend.Name != "nodejs-yarn" {
// If we are not in Yarn, add the specfile and lockfile to global deps
globalDeps.Add(c.Backend.Specfile)
globalDeps.Add(c.Backend.Lockfile)
Expand Down Expand Up @@ -296,7 +296,7 @@ func (c *Context) ResolveWorkspaceRootDeps() (*fs.PackageJSON, error) {
for dep, version := range pkg.PeerDependencies {
pkg.UnresolvedExternalDeps[dep] = version
}
if c.Backend.Name == "nodejs-yarn" && !fs.CheckIfWindows() {
if c.Backend.Name == "nodejs-yarn" {
pkg.SubLockfile = make(fs.YarnLockfile)
c.ResolveDepGraph(&lockfileWg, pkg.UnresolvedExternalDeps, depSet, seen, pkg)
lockfileWg.Wait()
Expand Down Expand Up @@ -441,7 +441,7 @@ func (c *Context) parsePackageJSON(buildFilePath string) error {
}

func (c *Context) ResolveDepGraph(wg *sync.WaitGroup, unresolvedDirectDeps map[string]string, resolveDepsSet mapset.Set, seen mapset.Set, pkg *fs.PackageJSON) {
if fs.CheckIfWindows() || c.Backend.Name != "nodejs-yarn" {
if c.Backend.Name != "nodejs-yarn" {
return
}
for directDepName, unresolvedVersion := range unresolvedDirectDeps {
Expand Down
45 changes: 9 additions & 36 deletions cli/internal/fs/lockfile.go
Original file line number Diff line number Diff line change
@@ -1,57 +1,31 @@
//go:build !windows
// +build !windows

package fs

import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"

"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
)

type LockfileEntry struct {
// resolved version for the particular entry based on the provided semver revision
Version string `yaml:"version"`
Resolved string `yaml:"resolved"`
Integrity string `yaml:"integrity"`
// the list of unresolved modules and revisions (e.g. type-detect : ^4.0.0)
Dependencies map[string]string `yaml:"dependencies,omitempty"`
// the list of unresolved modules and revisions (e.g. type-detect : ^4.0.0)
OptionalDependencies map[string]string `yaml:"optionalDependencies,omitempty"`
}

type YarnLockfile map[string]*LockfileEntry

func md5sum(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer file.Close()

hash := md5.New()
if _, err := io.Copy(hash, file); err != nil {
return "", err
}
return hex.EncodeToString(hash.Sum(nil)), nil
}

// ReadLockfile will read `yarn.lock` into memory (either from the cache or fresh)
func ReadLockfile(cacheDir string) (*YarnLockfile, error) {
var lockfile YarnLockfile
var prettyLockFile = YarnLockfile{}
hash, err := HashFile("yarn.lock")

if err != nil {
return &YarnLockfile{}, fmt.Errorf("failed to hash lockfile: %w", err)
}
contentsOfLock, err := ioutil.ReadFile(filepath.Join(cacheDir, fmt.Sprintf("%v-turbo-lock.yaml", hash)))
if err != nil {
contentsB, err := ioutil.ReadFile("yarn.lock")
if err != nil {
return nil, fmt.Errorf("yarn.lock: %w", err)
return nil, fmt.Errorf("reading yarn.lock: %w", err)
}
lines := strings.Split(string(contentsB), "\n")
r := regexp.MustCompile(`^[\w"]`)
Expand Down Expand Up @@ -80,8 +54,7 @@ func ReadLockfile(cacheDir string) (*YarnLockfile, error) {

err = yaml.Unmarshal([]byte(next), &lockfile)
if err != nil {
fmt.Println("unmarshal")
return &YarnLockfile{}, err
return &YarnLockfile{}, fmt.Errorf("could not unmarshal lockfile: %w", err)
}
// This final step is important, it splits any deps with multiple-resolutions
// (e.g. "@babel/generator@^7.13.0, @babel/generator@^7.13.9":) into separate
Expand Down
101 changes: 101 additions & 0 deletions cli/internal/fs/lockfile_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//go:build windows
// +build windows

package fs

import (
"fmt"
"io/ioutil"
"path/filepath"
"regexp"
"strings"

"gopkg.in/yaml.v3"
)

// ReadLockfile will read `yarn.lock` into memory (either from the cache or fresh)
func ReadLockfile(cacheDir string) (*YarnLockfile, error) {
var lockfile YarnLockfile
var prettyLockFile = YarnLockfile{}
hash, err := HashFile("yarn.lock")
if err != nil {
return &YarnLockfile{}, fmt.Errorf("failed to hash lockfile: %w", err)
}
contentsOfLock, err := ioutil.ReadFile(filepath.Join(cacheDir, fmt.Sprintf("%v-turbo-lock.yaml", hash)))
if err != nil {
contentsB, err := ioutil.ReadFile("yarn.lock")
if err != nil {
return nil, fmt.Errorf("reading yarn.lock: %w", err)
}
lines := strings.Split(strings.TrimRight(string(contentsB), "\r\n"), "\r\n")
r := regexp.MustCompile(`^[\w"]`)
double := regexp.MustCompile(`\:\"\:`)
l := regexp.MustCompile("\"|:\r\n$")
o := regexp.MustCompile(`\"\s\"`)
// deals with colons
// integrity sha-... -> integrity: sha-...
// "@apollo/client" latest -> "@apollo/client": latest
// "@apollo/client" "0.0.0" -> "@apollo/client": "0.0.0"
// apollo-client "0.0.0" -> apollo-client: "0.0.0"
a := regexp.MustCompile(`(\w|\")\s(\"|\w)`)

for i, line := range lines {
if r.MatchString(line) {
first := fmt.Sprintf("\"%v\":", l.ReplaceAllString(line, ""))
lines[i] = double.ReplaceAllString(first, "\":")
}
}
output := o.ReplaceAllString(strings.Join(lines, "\r\n"), "\": \"")

next := a.ReplaceAllStringFunc(output, func(m string) string {
parts := a.FindStringSubmatch(m)
return fmt.Sprintf("%s: %s", parts[1], parts[2])
})

err = yaml.Unmarshal([]byte(next), &lockfile)
if err != nil {
return &YarnLockfile{}, fmt.Errorf("could not unmarshal lockfile: %w", err)
}
// This final step is important, it splits any deps with multiple-resolutions
// (e.g. "@babel/generator@^7.13.0, @babel/generator@^7.13.9":) into separate
// entries in our map
// TODO: make concurrent
for key, val := range lockfile {
if strings.Contains(key, ",") {
for _, v := range strings.Split(key, ", ") {
prettyLockFile[strings.TrimSpace(v)] = val
}

} else {
prettyLockFile[key] = val
}
}

better, err := yaml.Marshal(&prettyLockFile)
if err != nil {
fmt.Println(err.Error())
return &YarnLockfile{}, err
}
if err = EnsureDir(cacheDir); err != nil {
fmt.Println(err.Error())
return &YarnLockfile{}, err
}
if err = EnsureDir(filepath.Join(cacheDir, fmt.Sprintf("%v-turbo-lock.yaml", hash))); err != nil {
fmt.Println(err.Error())
return &YarnLockfile{}, err
}
if err = ioutil.WriteFile(filepath.Join(cacheDir, fmt.Sprintf("%v-turbo-lock.yaml", hash)), []byte(better), 0644); err != nil {
fmt.Println(err.Error())
return &YarnLockfile{}, err
}
} else {
if contentsOfLock != nil {
err = yaml.Unmarshal(contentsOfLock, &prettyLockFile)
if err != nil {
return &YarnLockfile{}, fmt.Errorf("could not unmarshal yaml: %w", err)
}
}
}

return &prettyLockFile, nil
}
14 changes: 14 additions & 0 deletions cli/internal/fs/yarn_lockfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fs

type LockfileEntry struct {
// resolved version for the particular entry based on the provided semver revision
Version string `yaml:"version"`
Resolved string `yaml:"resolved"`
Integrity string `yaml:"integrity"`
// the list of unresolved modules and revisions (e.g. type-detect : ^4.0.0)
Dependencies map[string]string `yaml:"dependencies,omitempty"`
// the list of unresolved modules and revisions (e.g. type-detect : ^4.0.0)
OptionalDependencies map[string]string `yaml:"optionalDependencies,omitempty"`
}

type YarnLockfile map[string]*LockfileEntry
2 changes: 1 addition & 1 deletion cli/internal/prune/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/hashicorp/go-hclog"
"github.com/mitchellh/cli"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
)

// PruneCommand is a Command implementation that tells Turbo to run a task
Expand Down
5 changes: 4 additions & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
"version": "0.0.0",
"scripts": {
"clean": "make clean",
"build": "CGO_ENABLED=0 go build ./cmd/turbo",
"build": "cross-env CGO_ENABLED=0 go build ./cmd/turbo",
"test": "go test -race ./internal/...",
"publish": "make publish-all",
"format": "go fmt ./cmd/... ./internal/...",
"lint": "go vet ./cmd/... ./internal/..."
},
"devDependencies": {
"cross-env": "^7.0.3"
}
}
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2634,6 +2634,13 @@ create-hmac@^1.1.0, create-hmac@^1.1.4, create-hmac@^1.1.7:
safe-buffer "^5.0.1"
sha.js "^2.4.8"

cross-env@^7.0.3:
version "7.0.3"
resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-7.0.3.tgz#865264b29677dc015ba8418918965dd232fc54cf"
integrity sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==
dependencies:
cross-spawn "^7.0.1"

cross-spawn@^5.0.1:
version "5.1.0"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-5.1.0.tgz#e8bd0efee58fcff6f8f94510a0a554bbfa235449"
Expand All @@ -2643,7 +2650,7 @@ cross-spawn@^5.0.1:
shebang-command "^1.2.0"
which "^1.2.9"

cross-spawn@^7.0.2, cross-spawn@^7.0.3:
cross-spawn@^7.0.1, cross-spawn@^7.0.2, cross-spawn@^7.0.3:
version "7.0.3"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==
Expand Down