diff --git a/packages/plugin-cloudflare/src/install-cloudflared.test.ts b/packages/plugin-cloudflare/src/install-cloudflared.test.ts index b0edfd54302..cc57551f7bd 100644 --- a/packages/plugin-cloudflare/src/install-cloudflared.test.ts +++ b/packages/plugin-cloudflare/src/install-cloudflared.test.ts @@ -1,18 +1,19 @@ import install, {CURRENT_CLOUDFLARE_VERSION, versionIsGreaterThan} from './install-cloudflared.js' import * as fsActions from '@shopify/cli-kit/node/fs' import * as http from '@shopify/cli-kit/node/http' +import * as system from '@shopify/cli-kit/node/system' import {beforeEach, describe, expect, test, vi} from 'vitest' import util from 'util' import {WriteStream} from 'fs' -// eslint-disable-next-line no-restricted-imports -import * as childProcess from 'child_process' -vi.mock('child_process') +vi.mock('@shopify/cli-kit/node/system') vi.mock('stream') describe('install-cloudflare', () => { beforeEach(() => { + vi.mocked(system.captureOutputWithExitCode).mockResolvedValue({stdout: '', stderr: '', exitCode: 0}) + vi.mocked(system.exec).mockResolvedValue(undefined) vi.spyOn(util, 'promisify').mockReturnValue(vi.fn().mockReturnValue(Promise.resolve())) vi.spyOn(http, 'fetch').mockReturnValue(Promise.resolve({ok: true, body: {pipe: vi.fn()}} as any)) vi.spyOn(fsActions, 'fileExistsSync').mockReturnValueOnce(false) @@ -98,9 +99,11 @@ describe('install-cloudflare', () => { // Given const env = {} vi.spyOn(fsActions, 'fileExistsSync').mockReturnValueOnce(true) - vi.spyOn(childProcess, 'execFileSync').mockReturnValue( - `cloudflared version ${CURRENT_CLOUDFLARE_VERSION} (built 2023-03-13-1444 UTC)`, - ) + vi.mocked(system.captureOutputWithExitCode).mockResolvedValue({ + stdout: `cloudflared version ${CURRENT_CLOUDFLARE_VERSION} (built 2023-03-13-1444 UTC)`, + stderr: '', + exitCode: 0, + }) // When await install(env, 'win32', 'x64') @@ -113,7 +116,11 @@ describe('install-cloudflare', () => { // Given const env = {} vi.spyOn(fsActions, 'fileExistsSync').mockReturnValueOnce(true) - vi.spyOn(childProcess, 'execFileSync').mockReturnValue(`cloudflared version 2000.0.0 (built 2023-03-13-1444 UTC)`) + vi.mocked(system.captureOutputWithExitCode).mockResolvedValue({ + stdout: `cloudflared version 2000.0.0 (built 2023-03-13-1444 UTC)`, + stderr: '', + exitCode: 0, + }) // When await install(env, 'darwin', 'x64') diff --git a/packages/plugin-cloudflare/src/install-cloudflared.ts b/packages/plugin-cloudflare/src/install-cloudflared.ts index 37664c67290..34b3adff7fe 100644 --- a/packages/plugin-cloudflare/src/install-cloudflared.ts +++ b/packages/plugin-cloudflare/src/install-cloudflared.ts @@ -10,11 +10,10 @@ import { unlinkFileSync, createFileWriteStream, } from '@shopify/cli-kit/node/fs' +import {captureOutputWithExitCode, exec} from '@shopify/cli-kit/node/system' import {fileURLToPath} from 'url' import util from 'util' import {pipeline} from 'stream' -// eslint-disable-next-line no-restricted-imports -import {execSync, execFileSync} from 'child_process' export const CURRENT_CLOUDFLARE_VERSION = '2024.8.2' const CLOUDFLARE_REPO = `https://github.com/cloudflare/cloudflared/releases/download/${CURRENT_CLOUDFLARE_VERSION}/` @@ -82,7 +81,8 @@ export default async function install(env = process.env, platform = process.plat if (fileExistsSync(binTarget)) { // --version returns an string like "cloudflared version 2023.3.1 (built 2023-03-13-1444 UTC)" try { - const versionArray = execFileSync(binTarget, ['--version'], {encoding: 'utf8'}).split(' ') + const {stdout} = await captureOutputWithExitCode(binTarget, ['--version']) + const versionArray = stdout.split(' ') const versionNumber = versionArray.length > 2 ? versionArray[2] : '0.0.0' const needsUpdate = versionIsGreaterThan(CURRENT_CLOUDFLARE_VERSION, versionNumber!) if (!needsUpdate) { @@ -132,7 +132,7 @@ async function installWindows(file: string, binTarget: string) { async function installMacos(file: string, binTarget: string) { await downloadFile(file, `${binTarget}.tgz`) const filename = basename(`${binTarget}.tgz`) - execSync(`tar -xzf ${filename}`, {cwd: dirname(binTarget)}) + await exec('tar', ['-xzf', filename], {cwd: dirname(binTarget)}) unlinkFileSync(`${binTarget}.tgz`) await renameFile(`${dirname(binTarget)}/cloudflared`, binTarget) }