const cp = require('child_process')

const p = npmRun(['build:watch'])
p.stderr.on('error', function (data) {
  console.log(data)
})

p.stderr.on('data', function (data) {
  if (/打包成功/.test(data.toString())) {
    npmRun(['serve:start'], {
      stdio: 'inherit',
    })
  }
})

// process.stdout.write('xx')

function npmRun(args, options) {
  if (process.platform !== 'win32') {
    return cp.spawn('npm', ['run', ...args], options)
  } else {
    return cp.spawn('cmd.exe', ['/s', '/c', 'npm', 'run', ...args], options)
  }
}
