Skip to content

Commit

Permalink
Pass a silenced writestream down to renderTasks when the json flag is…
Browse files Browse the repository at this point in the history
… provided
  • Loading branch information
jamesmengo committed Apr 20, 2024
1 parent ce946c4 commit 954bef7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
34 changes: 32 additions & 2 deletions packages/theme/src/cli/utilities/theme-ui.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {themeComponent, themesComponent} from './theme-ui.js'
import {themeComponent, themesComponent, silenceableRenderTasks, SilentWriteStream} from './theme-ui.js'
import {Theme} from '@shopify/cli-kit/node/themes/types'
import {test, describe, expect} from 'vitest'
import {test, describe, expect, vi} from 'vitest'

import {Task} from '@shopify/cli-kit/node/ui'

describe('themeComponent', () => {
test('returns the ui for a theme', async () => {
Expand All @@ -25,6 +27,34 @@ describe('themesComponent', () => {
})
})
})
// todo: these tests will pass no matter what
describe('SilentWriteStream', () => {
test('write method should not output anything', () => {
const mock = vi.spyOn(process.stdout, 'write').mockImplementation(() => true)
const silentWriteStream = new SilentWriteStream(1)

silentWriteStream.write()

expect(mock).not.toHaveBeenCalled()
})
})

describe('silenceableRenderTasks', () => {
test('should not write to process.stdout when silent flag is true', async () => {
const mock = vi.spyOn(process.stdout, 'write' as any).mockImplementation(() => true)

const tasks: Task[] = [
{
title: 'task 1',
task: async () => {},
},
]

await silenceableRenderTasks(tasks, true)

expect(mock).not.toHaveBeenCalled()
})
})

function theme(id: number) {
return {id, name: `theme ${id}`} as Theme
Expand Down
16 changes: 15 additions & 1 deletion packages/theme/src/cli/utilities/theme-ui.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Theme} from '@shopify/cli-kit/node/themes/types'
import {renderConfirmationPrompt, renderWarning} from '@shopify/cli-kit/node/ui'
import {renderConfirmationPrompt, renderWarning, renderTasks, Task} from '@shopify/cli-kit/node/ui'
import {WriteStream} from 'tty'

export function themeComponent(theme: Theme) {
return [
Expand Down Expand Up @@ -31,3 +32,16 @@ export async function currentDirectoryConfirmed(force: boolean) {
message: 'Do you want to proceed?',
})
}

export class SilentWriteStream extends WriteStream {
write(): boolean {
return true
}
}

export async function silenceableRenderTasks(tasks: Task[], silent?: boolean) {
if (tasks.length > 0) {
const renderOptions = silent ? {stdout: new SilentWriteStream(1)} : undefined
await renderTasks(tasks, {renderOptions})
}
}
14 changes: 5 additions & 9 deletions packages/theme/src/cli/utilities/theme-uploader.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import {partitionThemeFiles, readThemeFilesFromDisk} from './theme-fs.js'
import {applyIgnoreFilters} from './asset-ignore.js'
import {silenceableRenderTasks} from './theme-ui.js'
import {AdminSession} from '@shopify/cli-kit/node/session'
import {Result, Checksum, Theme, ThemeFileSystem} from '@shopify/cli-kit/node/themes/types'
import {AssetParams, bulkUploadThemeAssets, deleteThemeAsset} from '@shopify/cli-kit/node/themes/api'
import {fileSize} from '@shopify/cli-kit/node/fs'
import {Task, renderTasks as renderTaskOriginal} from '@shopify/cli-kit/node/ui'
import {Task} from '@shopify/cli-kit/node/ui'
import {outputDebug, outputInfo, outputNewline, outputWarn} from '@shopify/cli-kit/node/output'

interface UploadOptions {
path: string
nodelete?: boolean
ignore?: string[]
only?: string[]
json?: boolean
}
type FileBatch = Checksum[]

Expand All @@ -33,8 +35,8 @@ export async function uploadTheme(
const uploadTasks = await buildUploadTasks(remoteChecksums, themeFileSystem, options, theme, session, uploadResults)

// The task execution mechanism processes tasks sequentially in the order they are added.
await renderTasks(deleteTasks)
await renderTasks(uploadTasks)
await silenceableRenderTasks(deleteTasks, options.json)
await silenceableRenderTasks(uploadTasks, options.json)

reportFailedUploads(uploadResults)
return uploadResults
Expand Down Expand Up @@ -321,12 +323,6 @@ async function handleFailedUploads(
return handleBulkUpload(failedUploadParams, themeId, session, count + 1)
}

async function renderTasks(tasks: Task[]) {
if (tasks.length > 0) {
await renderTaskOriginal(tasks)
}
}

function reportFailedUploads(uploadResults: Map<string, Result>) {
for (const [key, result] of uploadResults.entries()) {
if (!result.success) {
Expand Down

0 comments on commit 954bef7

Please sign in to comment.