hostAdminClient.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const hostAdminUrl = Cypress.env('ADMIN_CLIENT_URL') || 'http://host-admin'
  2. export async function dockerCompose(cmd: string, ...args: string[]) {
  3. return await fetchJSON(`${hostAdminUrl}/docker/compose/${cmd}`, {
  4. method: 'POST',
  5. body: JSON.stringify({
  6. args,
  7. }),
  8. })
  9. }
  10. export async function mongoInit() {
  11. return await fetchJSON(`${hostAdminUrl}/mongo/init`, {
  12. method: 'POST',
  13. })
  14. }
  15. export async function resetData() {
  16. return await fetchJSON(`${hostAdminUrl}/reset/data`, {
  17. method: 'POST',
  18. })
  19. }
  20. export async function reconfigure({
  21. pro = false,
  22. version = 'latest',
  23. vars = {},
  24. withDataDir = false,
  25. }) {
  26. return await fetchJSON(`${hostAdminUrl}/reconfigure`, {
  27. method: 'POST',
  28. body: JSON.stringify({
  29. pro,
  30. version,
  31. vars,
  32. withDataDir,
  33. }),
  34. })
  35. }
  36. async function fetchJSON(
  37. input: RequestInfo,
  38. init?: RequestInit
  39. ): Promise<{ stdout: string; stderr: string }> {
  40. if (init?.body) {
  41. init.headers = { 'Content-Type': 'application/json' }
  42. }
  43. const res = await fetch(input, init)
  44. const { error, stdout, stderr } = await res.json()
  45. if (error) {
  46. console.error(input, init, 'failed:', error)
  47. if (stdout) console.log(stdout)
  48. if (stderr) console.warn(stderr)
  49. const err = new Error(error.message)
  50. Object.assign(err, error)
  51. throw err
  52. }
  53. return { stdout, stderr }
  54. }
  55. export async function runScript({
  56. cwd,
  57. script,
  58. args = [],
  59. }: {
  60. cwd: string
  61. script: string
  62. args?: string[]
  63. }) {
  64. return await fetchJSON(`${hostAdminUrl}/run/script`, {
  65. method: 'POST',
  66. body: JSON.stringify({
  67. cwd,
  68. script,
  69. args,
  70. }),
  71. })
  72. }
  73. export async function getRedisKeys() {
  74. const { stdout } = await fetchJSON(`${hostAdminUrl}/redis/keys`, {
  75. method: 'GET',
  76. })
  77. return stdout.split('\n')
  78. }