command

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2025 License: MPL-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RunCommand = &cli.Command{
	Name:      "run",
	Usage:     "Run npm script and expose port",
	UsageText: "localhost run [start]",
	Action: func(ctx context.Context, c *cli.Command) error {
		scriptName := c.Args().First()
		if scriptName == "" {
			return cli.ShowSubcommandHelp(c)
		}

		cwd, err := os.Getwd()
		if err != nil {
			return util.WrapError(err, "unable to find current working directory")
		}
		pkgJsonPath := filepath.Join(cwd, "package.json")
		jsonBytes, err := os.ReadFile(pkgJsonPath)
		if err != nil {
			return util.WrapError(err, "error reading package.json")
		}
		pkg := new(PkgJson)
		err = json.Unmarshal(jsonBytes, pkg)
		if err != nil {
			return util.WrapError(err, "error parsing package.json (%s)", pkgJsonPath)
		}

		scriptCommand, hasScriptCommand := pkg.Scripts[scriptName]
		if !hasScriptCommand {
			return fmt.Errorf("unable to find npm script: %s", scriptName)
		}
		scriptCommand = strings.TrimSpace(scriptCommand)

		cmd := exec.Command("sh", "-c", scriptCommand)

		cmd.Env = os.Environ()
		cmd.Stdin = os.Stdin

		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr

		err = cmd.Run()

		return err
	},
}

Functions

This section is empty.

Types

type PkgJson

type PkgJson struct {
	Scripts map[string]string `json:"scripts"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL