-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
yarn.go
102 lines (83 loc) · 3.11 KB
/
yarn.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
package packagemanager
import (
"fmt"
"os/exec"
"path/filepath"
"strings"
"github.com/Masterminds/semver"
"github.com/vercel/turbo/cli/internal/fs"
"github.com/vercel/turbo/cli/internal/lockfile"
"github.com/vercel/turbo/cli/internal/turbopath"
)
var nodejsYarn = PackageManager{
Name: "nodejs-yarn",
Slug: "yarn",
Command: "yarn",
Specfile: "package.json",
Lockfile: "yarn.lock",
PackageDir: "node_modules",
ArgSeparator: []string{"--"},
getWorkspaceGlobs: func(rootpath turbopath.AbsoluteSystemPath) ([]string, error) {
pkg, err := fs.ReadPackageJSON(rootpath.UntypedJoin("package.json"))
if err != nil {
return nil, fmt.Errorf("package.json: %w", err)
}
if len(pkg.Workspaces) == 0 {
return nil, fmt.Errorf("package.json: no workspaces found. Turborepo requires Yarn workspaces to be defined in the root package.json")
}
return pkg.Workspaces, nil
},
getWorkspaceIgnores: func(pm PackageManager, rootpath turbopath.AbsoluteSystemPath) ([]string, error) {
// function: https://github.com/yarnpkg/yarn/blob/3119382885ea373d3c13d6a846de743eca8c914b/src/config.js#L799
// Yarn is unique in ignore patterns handling.
// The only time it does globbing is for package.json or yarn.json and it scopes the search to each workspace.
// For example: `apps/*/node_modules/**/+(package.json|yarn.json)`
// The `extglob` `+(package.json|yarn.json)` (from micromatch) after node_modules/** is redundant.
globs, err := pm.getWorkspaceGlobs(rootpath)
if err != nil {
return nil, err
}
ignores := make([]string, len(globs))
for i, glob := range globs {
ignores[i] = filepath.Join(glob, "/node_modules/**")
}
return ignores, nil
},
canPrune: func(cwd turbopath.AbsoluteSystemPath) (bool, error) {
return true, nil
},
// Versions older than 2.0 are yarn, after that they become berry
Matches: func(manager string, version string) (bool, error) {
if manager != "yarn" {
return false, nil
}
v, err := semver.NewVersion(version)
if err != nil {
return false, fmt.Errorf("could not parse yarn version: %w", err)
}
c, err := semver.NewConstraint("<2.0.0-0")
if err != nil {
return false, fmt.Errorf("could not create constraint: %w", err)
}
return c.Check(v), nil
},
// Detect for yarn needs to identify which version of yarn is running on the system.
detect: func(projectDirectory turbopath.AbsoluteSystemPath, packageManager *PackageManager) (bool, error) {
specfileExists := projectDirectory.UntypedJoin(packageManager.Specfile).FileExists()
lockfileExists := projectDirectory.UntypedJoin(packageManager.Lockfile).FileExists()
// Short-circuit, definitely not Yarn.
if !specfileExists || !lockfileExists {
return false, nil
}
cmd := exec.Command("yarn", "--version")
cmd.Dir = projectDirectory.ToString()
out, err := cmd.Output()
if err != nil {
return false, fmt.Errorf("could not detect yarn version: %w", err)
}
return packageManager.Matches(packageManager.Slug, strings.TrimSpace(string(out)))
},
UnmarshalLockfile: func(contents []byte) (lockfile.Lockfile, error) {
return lockfile.DecodeYarnLockfile(contents)
},
}