dev.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. },
  47. },
  48. {
  49. title: 'Launching server',
  50. task: async (_, task) => {
  51. const shouldStop = false
  52. const cmd = spawn('node', ['node_modules/nuxt/bin/nuxt.mjs', 'dev', '--port', '3000'])
  53. task.title = 'Command spawned'
  54. cmd.stdout.on('data', (data) => {
  55. task.output = `${task.output === undefined ? '-----LOGGING STARTED-----' : task.output}\n${data}`
  56. })
  57. cmd.stderr.on('data', (data) => {
  58. task.output = `${task.output === undefined ? '-----LOGGING STARTED-----' : task.output}\n${data}`
  59. })
  60. cmd.on('close', (code) => {
  61. task.title = `${task.output}\nProcess stopped with code: ${code}`
  62. task.skip()
  63. cmd.kill()
  64. process.exit()
  65. })
  66. process.on('SIGINT', async function () {
  67. task.title = 'Server stopped'
  68. task.skip()
  69. cmd.kill()
  70. process.exit()
  71. })
  72. while (!shouldStop) {
  73. await new Promise(resolve => setTimeout(resolve, 500))
  74. }
  75. },
  76. },
  77. ], {
  78. registerSignalListeners: false,
  79. },
  80. ).run()