testRelease.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const spawn = require('cross-spawn')
  2. const TOTAL_SPAWN_STEPS = 8
  3. const main = async () => {
  4. let currentSpawnStep = 0
  5. const logProgress = (message) => {
  6. currentSpawnStep += 1
  7. console.log(`\n\x1b[36m${currentSpawnStep}/${TOTAL_SPAWN_STEPS} ${message}...\x1b[0m\n`)
  8. }
  9. const spawnPromise = (command, args, message) => new Promise((resolve, reject) => {
  10. logProgress(message)
  11. const childProcess = spawn(command, args, { stdio: 'inherit' })
  12. childProcess.on('close', (code) => {
  13. if (code === 0) {
  14. resolve()
  15. } else {
  16. reject(new Error(`${command} ${args.join(' ')} exited with code ${code}`))
  17. }
  18. })
  19. })
  20. const spawnYarnStart = async (message) => new Promise((resolve, reject) => {
  21. logProgress(message)
  22. process.env.FORCE_COLOR = true
  23. const childProcess = spawn('yarn', ['start'], { env: process.env })
  24. childProcess.stdout.on('data', data => {
  25. process.stdout.write(data)
  26. if (data.toString().includes('server is up')) {
  27. console.log('\nContinue to next step? (Y/n)...')
  28. }
  29. })
  30. childProcess.stderr.on('data', data => { process.stderr.write(data) })
  31. process.stdin.setRawMode(true)
  32. process.stdin.once('data', data => {
  33. childProcess.kill()
  34. process.stdin.setRawMode(false)
  35. if (data.toString() === 'n') {
  36. reject(new Error('Aborted'))
  37. } else {
  38. resolve()
  39. }
  40. })
  41. })
  42. try {
  43. await spawnPromise('yarn', ['install'], 'Preparing install for TSC and ESLint checks')
  44. await spawnPromise('yarn', ['tsc'], 'Typescript checks')
  45. await spawnPromise('yarn', ['eslint'], 'ESLint checks')
  46. await spawnPromise('yarn', ['test'], 'Run unit tests')
  47. await spawnPromise('yarn', ['install', '--production'], 'Preparing install for production launch')
  48. await spawnPromise('yarn', ['build'], 'Production build')
  49. await spawnYarnStart('Production start')
  50. await spawnPromise('yarn', ['install'], 'Testing successful! Reverting to development install')
  51. } catch (e) {
  52. console.error(e)
  53. } finally {
  54. process.exit(0)
  55. }
  56. }
  57. main()