这是indexloc提供的服务,不要输入任何密码
Skip to content

Add exclude scope option for cli #543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
35 changes: 35 additions & 0 deletions cli/internal/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Options:
--help Show this message.
--scope Specify package(s) to act as entry points for task
execution. Supports globs.
--exclude-scope Specify package(s) to remove from scope.
--cache-dir Specify local filesystem cache directory.
(default "./node_modules/.cache/turbo")
--concurrency Limit the concurrency of task execution. Use 1 for
Expand Down Expand Up @@ -197,6 +198,16 @@ func (c *RunCommand) Run(args []string) int {
}
}

//Exclude packages
if len(runOptions.excludeScope) > 0 {
err = removePackages(ctx, runOptions.excludeScope)

if err != nil {
c.logError(c.Config.Logger, "", fmt.Errorf("Invalid exclude scope: %w", err))
return 1
}
}

// Scoped packages
// Unwind scope globs
scopePkgs, err := getScopedPackages(ctx, runOptions.scope)
Expand Down Expand Up @@ -704,6 +715,8 @@ type RunOptions struct {
globalDeps []string
// Filtered list of package entrypoints
scope []string
// Exclude from scope
excludeScope []string
// Force execution to be serially one-at-a-time
concurrency int
// Whether to execute in parallel (defaults to false)
Expand Down Expand Up @@ -766,6 +779,10 @@ func parseRunArgs(args []string, cwd string) (*RunOptions, error) {
if len(arg[len("--scope="):]) > 0 {
runOptions.scope = append(runOptions.scope, arg[len("--scope="):])
}
case strings.HasPrefix(arg, "--exclude-scope="):
if len(arg[len("--exclude-scope="):]) > 0 {
runOptions.excludeScope = append(runOptions.excludeScope, arg[len("--exclude-scope="):])
}
case strings.HasPrefix(arg, "--ignore="):
if len(arg[len("--ignore="):]) > 0 {
runOptions.ignore = append(runOptions.ignore, arg[len("--ignore="):])
Expand Down Expand Up @@ -878,6 +895,24 @@ func getScopedPackages(ctx *context.Context, scopePatterns []string) (scopePkgs
return scopedPkgs, nil
}

func removePackages(ctx *context.Context, excludePatterns []string) (err error) {
var packages []string

glob, err := filter.Compile(excludePatterns)
if err != nil {
return err
}
for _, f := range ctx.PackageNames {
if !glob.Match(f) {
packages = append(packages, f)
}
}

ctx.PackageNames = packages

return nil
}

// logError logs an error and outputs it to the UI.
func (c *RunCommand) logError(log hclog.Logger, prefix string, err error) {
log.Error(prefix, "error", err)
Expand Down
73 changes: 71 additions & 2 deletions cli/internal/run/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ func TestParseConfig(t *testing.T) {
cacheFolder: filepath.FromSlash("node_modules/.cache/turbo"),
},
},
{
"scope",
[]string{"foo", "--exclude-scope=foo", "--exclude-scope=blah"},
&RunOptions{
includeDependents: true,
stream: true,
bail: true,
dotGraph: "",
concurrency: 10,
includeDependencies: false,
cache: true,
forceExecution: false,
profile: "",
excludeScope: []string{"foo", "blah"},
cacheFolder: filepath.FromSlash("node_modules/.cache/turbo"),
},
},
{
"concurrency",
[]string{"foo", "--concurrency=12"},
Expand Down Expand Up @@ -150,7 +167,7 @@ func TestScopedPackages(t *testing.T) {
cases := []struct {
Name string
Ctx *context.Context
Patttern []string
Pattern []string
Expected util.Set
}{
{
Expand Down Expand Up @@ -189,11 +206,63 @@ func TestScopedPackages(t *testing.T) {

for i, tc := range cases {
t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
actual, err := getScopedPackages(tc.Ctx, tc.Patttern)
actual, err := getScopedPackages(tc.Ctx, tc.Pattern)
if err != nil {
t.Fatalf("invalid scope parse: %#v", err)
}
assert.EqualValues(t, tc.Expected, actual)
})
}
}

func TestExcludePackages(t *testing.T) {
cases := []struct {
Name string
Ctx *context.Context
Pattern []string
Expected []string
}{
{
"starts with @",
&context.Context{
PackageNames: []string{"@sample/app", "sample-app", "jared"},
},
[]string{"@sample/*"},
[]string{"sample-app", "jared"},
},
{
"return an array of matches",
&context.Context{
PackageNames: []string{"foo", "bar", "baz"},
},
[]string{"f*"},
[]string{"bar", "baz"},
},
{
"return an array of matches",
&context.Context{
PackageNames: []string{"foo", "bar", "baz"},
},
[]string{"f*", "bar"},
[]string{"baz"},
},
{
"return matches in the order the list were defined",
&context.Context{
PackageNames: []string{"foo", "bar", "baz"},
},
[]string{"*a*", "!f*"},
[]string{"foo"},
},
}

for i, tc := range cases {
t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
err := removePackages(tc.Ctx, tc.Pattern)
if err != nil {
t.Fatalf("invalid scope parse: %#v", err)
}
assert.EqualValues(t, tc.Expected, tc.Ctx.PackageNames)
})
}
}