dev.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { exec, spawn } from 'node:child_process'
  2. import { promisify } from 'node:util'
  3. import { Listr } from 'listr2'
  4. import { logLogo } from './logo'
  5. const execAsync = promisify(exec)
  6. async function runCommand(
  7. command: string,
  8. task?: {
  9. output: string
  10. },
  11. disableStdout: boolean | undefined = false,
  12. ) {
  13. try {
  14. const { stdout, stderr } = await execAsync(command)
  15. if (!disableStdout) {
  16. if (stderr) {
  17. if (task) {
  18. task.output = stderr
  19. }
  20. else { console.log(stderr) }
  21. }
  22. else {
  23. if (task) {
  24. task.output = stdout
  25. }
  26. else { console.log(stdout) }
  27. }
  28. }
  29. }
  30. catch (error) {
  31. if (!disableStdout) {
  32. if (task) {
  33. task.output = String(error)
  34. }
  35. else { console.log(String(error)) }
  36. }
  37. }
  38. }
  39. logLogo()
  40. new Listr(
  41. [
  42. {
  43. title: 'Clearing up cache...',
  44. task: async (_, task) => {
  45. await runCommand('nuxt cleanup', task)
  46. await runCommand('nuxt prepare', task)
  47. },
  48. },
  49. {
  50. title: 'Launching server',
  51. task: async (_, task) => {
  52. const shouldStop = false
  53. const cmd = spawn('node', ['node_modules/nuxt/bin/nuxt.mjs', 'dev', '--port', '3000'])
  54. task.title = 'Command spawned'
  55. cmd.stdout.on('data', (data) => {
  56. task.output = `${task.output === undefined ? '-----LOGGING STARTED-----' : task.output}\n${data}`
  57. })
  58. cmd.stderr.on('data', (data) => {
  59. task.output = `${task.output === undefined ? '-----LOGGING STARTED-----' : task.output}\n${data}`
  60. })
  61. cmd.on('close', (code) => {
  62. task.title = `${task.output}\nProcess stopped with code: ${code}`
  63. task.skip()
  64. cmd.kill()
  65. process.exit()
  66. })
  67. process.on('SIGINT', async function () {
  68. task.title = 'Server stopped'
  69. task.skip()
  70. cmd.kill()
  71. process.exit()
  72. })
  73. while (!shouldStop) {
  74. await new Promise(resolve => setTimeout(resolve, 500))
  75. }
  76. },
  77. },
  78. ], {
  79. registerSignalListeners: false,
  80. },
  81. ).run()