Skip to content

How do I run commands ? eg npm run dev #492

Answered by jeremytenjo
jeremytenjo asked this question in Q&A
Discussion options

You must be logged in to vote

Figured it out

    const restartCommand = () => {
      shellRef.current.kill()
      startCommand()
    }

    const startCommand = () => {
      const commandArgs = command.split(' ')
      commandArgs.shift()
      const shell = spawn('npm', commandArgs)

      // https://www.freecodecamp.org/news/node-js-child-processes-everything-you-need-to-know-e69498fe970a/
      shell.stdout.on('data', (data) => {
        const lines = data.toString('utf8')
        setOutput(lines)
      })

      shell.on('error', (error) => {
        setOutput(error.toString())
      })

      shellRef.current = shell
    }

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by jeremytenjo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant