| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- const hostAdminUrl = Cypress.env('ADMIN_CLIENT_URL') || 'http://host-admin'
- export async function dockerCompose(cmd: string, ...args: string[]) {
- return await fetchJSON(`${hostAdminUrl}/docker/compose/${cmd}`, {
- method: 'POST',
- body: JSON.stringify({
- args,
- }),
- })
- }
- export async function reconfigure({
- pro = false,
- version = 'latest',
- vars = {},
- withDataDir = false,
- resetData = false,
- }): Promise<{ previousConfigServer: string }> {
- return await fetchJSON(`${hostAdminUrl}/reconfigure`, {
- method: 'POST',
- body: JSON.stringify({
- pro,
- version,
- vars,
- withDataDir,
- resetData,
- }),
- })
- }
- async function fetchJSON<T = { stdout: string; stderr: string }>(
- input: RequestInfo,
- init?: RequestInit
- ): Promise<T> {
- if (init?.body) {
- init.headers = { 'Content-Type': 'application/json' }
- }
- let res
- for (let attempt = 0; attempt < 5; attempt++) {
- try {
- res = await fetch(input, init)
- break
- } catch {
- await sleep(3_000)
- }
- }
- if (!res) {
- res = await fetch(input, init)
- }
- const { error, stdout, stderr, ...rest } = await res.json()
- if (error) {
- console.error(input, init, 'failed:', error)
- if (stdout) console.log(stdout)
- if (stderr) console.warn(stderr)
- const err = new Error(error.message)
- Object.assign(err, error)
- throw err
- }
- return { stdout, stderr, ...rest }
- }
- export async function runScript({
- cwd,
- script,
- args = [],
- }: {
- cwd: string
- script: string
- args?: string[]
- }) {
- return await fetchJSON(`${hostAdminUrl}/run/script`, {
- method: 'POST',
- body: JSON.stringify({
- cwd,
- script,
- args,
- }),
- })
- }
- export async function getRedisKeys() {
- const { stdout } = await fetchJSON(`${hostAdminUrl}/redis/keys`, {
- method: 'GET',
- })
- return stdout.split('\n')
- }
- async function sleep(ms: number) {
- return new Promise(resolve => {
- setTimeout(resolve, ms)
- })
- }
|