mobileBuild.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import * as fs from 'node:fs'
  2. import { exec } from 'node:child_process'
  3. import { promisify } from 'node:util'
  4. import { Listr } from 'listr2'
  5. import consola from 'consola'
  6. import { logLogo } from './logo'
  7. const execAsync = promisify(exec)
  8. async function runCommand(
  9. command: string,
  10. task?: {
  11. output: string
  12. },
  13. disableStdout: boolean | undefined = false,
  14. ) {
  15. try {
  16. const { stdout, stderr } = await execAsync(command)
  17. if (!disableStdout) {
  18. if (stderr) {
  19. if (task) {
  20. task.output = stderr
  21. }
  22. else { consola.log(stderr) }
  23. }
  24. else {
  25. if (task) {
  26. task.output = stdout
  27. }
  28. else { consola.log(stdout) }
  29. }
  30. }
  31. }
  32. catch (error) {
  33. if (!disableStdout) {
  34. if (task) {
  35. task.output = String(error)
  36. }
  37. else {
  38. consola.log(String(error))
  39. }
  40. }
  41. }
  42. }
  43. logLogo()
  44. try {
  45. await new Listr(
  46. [
  47. {
  48. title: 'Building nuxt',
  49. task: (_, task): Listr =>
  50. task.newListr(
  51. [
  52. {
  53. title: 'Removing old build',
  54. task: async (_) => {
  55. fs.rmSync('./.nuxt', { recursive: true, force: true })
  56. fs.rmSync('./.output', { recursive: true, force: true })
  57. },
  58. },
  59. {
  60. title: 'Re-building nuxt sources',
  61. task: async () => {
  62. await runCommand('nuxt build', task)
  63. await runCommand('nuxt generate', task)
  64. },
  65. },
  66. ],
  67. ),
  68. },
  69. {
  70. title: 'Building capacitor',
  71. task: (_, task): Listr =>
  72. task.newListr(
  73. [
  74. {
  75. title: 'Clearing old mobile builds',
  76. task: async (_) => {
  77. fs.rmSync('./android', { recursive: true, force: true })
  78. fs.rmSync('./ios', { recursive: true, force: true })
  79. },
  80. },
  81. {
  82. title: 'Adding mobile sources',
  83. task: async (_, task) => {
  84. await runCommand('npx cap add android', task)
  85. await runCommand('npx cap add ios', task)
  86. await runCommand('npx cap sync', task)
  87. },
  88. },
  89. ],
  90. ),
  91. },
  92. ],
  93. { concurrent: false, rendererOptions: { collapseSubtasks: false } },
  94. ).run()
  95. }
  96. catch (e) {
  97. console.error(e)
  98. }