Skip to content

Commit eb90064

Browse files
committed
增强 Run 函数的命令处理逻辑,如果不传递参数,则默认查找 package.json ,然后转到 use 命令
1 parent ba809d6 commit eb90064

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

internal/command/run.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,34 @@ func RunDirectly(cmd []string) error {
187187
}
188188

189189
func Run(cmd []string) error {
190-
if len(cmd) == 0 {
191-
return errors.New("commands is required")
192-
}
193-
194190
cwd, err := os.Getwd()
195191

196192
if err != nil {
197193
return errors.WithStack(err)
198194
}
199195

196+
if len(cmd) == 0 {
197+
packageJSONPath := util.LoopUpFile(cwd, "package.json")
198+
199+
if packageJSONPath != nil {
200+
util.Debug("Use node constraint from %s\n", *packageJSONPath)
201+
202+
constraint, err := node.GetConstraintFromPackageJSON(*packageJSONPath)
203+
204+
if err != nil {
205+
return err
206+
}
207+
208+
if constraint != nil {
209+
util.Debug("Use node constraint: %s\n", *constraint)
210+
return Use(constraint)
211+
}
212+
213+
} else {
214+
return errors.New("commands is required")
215+
}
216+
}
217+
200218
packageJSONPath := util.LoopUpFile(cwd, "package.json")
201219

202220
// If the package.json file is found, then use the node constraint in the package.json to run the command

0 commit comments

Comments
 (0)