const nodemon = require("nodemon")

var server

module.exports = function () {
  this.nodemon = function (opts) {
    return new Promise((resolve, reject) => {
      if (!server) {
        server = nodemon(opts)
          .on("start", function () {
            this.log("nodemon started.")
          }.bind(this))
          .on("crash", function () {
            this.error("script crashed for some reason")
            reject()
          }.bind(this))
      } else {
        server.emit("restart")
      }
      resolve()
    })
  }
}

const nodemon = require("nodemon")

var server
var started = false

module.exports = function () {
  this.nodemon = function (opts) {
    return new Promise((resolve, reject) => {
      if (started) { server.emit("restart") }
      else {
        server = nodemon(opts)
          .on("start", function () {
            this.log("nodemon started.")
            resolve()
          }.bind(this))
          .on("crash", function () {
            this.error("script crashed for some reason")
            reject()
          }.bind(this))
      }
    })
  }
}

Add started flag and move resolve into event :art:
Add started flag and move resolve into event :art:
