Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions packages/cli-kit/src/public/node/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,17 @@ describe('createFileReadStream', () => {
await touchFile(filePath)
await appendFile(filePath, content)
const stream = createFileReadStream(filePath)
let data = ''
stream.on('data', (chunk) => {
data += chunk as string
})
stream.on('end', () => {
expect(data).toBe(content)

await new Promise<void>((resolve, reject) => {
let data = ''
stream.on('data', (chunk) => {
data += chunk as string
})
stream.on('end', () => {
expect(data).toBe(content)
resolve()
})
stream.on('error', reject)
})
})
})
Expand All @@ -272,12 +277,17 @@ describe('createFileReadStream', () => {
await appendFile(filePath, content)

const stream = createFileReadStream(filePath, {start: 1, end: 7})
let data = ''
stream.on('data', (chunk) => {
data += chunk as string
})
stream.on('end', () => {
expect(data).toBe('est-con')

await new Promise<void>((resolve, reject) => {
let data = ''
stream.on('data', (chunk) => {
data += chunk as string
})
stream.on('end', () => {
expect(data).toBe('est-con')
resolve()
})
stream.on('error', reject)
})
})
})
Expand Down
51 changes: 35 additions & 16 deletions packages/cli/src/cli/commands/docs/generate.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import {CommandData, CommandWithMarkdown, extractCommandData, writeCommandFlagInterface} from './generate.js'
import {writeFile} from '@shopify/cli-kit/node/fs'
import {inTemporaryDirectory, readFile} from '@shopify/cli-kit/node/fs'
import {cwd, joinPath} from '@shopify/cli-kit/node/path'
import {describe, test, vi, expect} from 'vitest'

vi.mock('@shopify/cli-kit/node/fs')
vi.mock('@shopify/cli-kit/node/path', async (importOriginal) => {
const actual = await importOriginal<typeof import('@shopify/cli-kit/node/path')>()
return {
...actual,
cwd: vi.fn(),
}
})

const testCommand: CommandWithMarkdown = {
aliases: [],
Expand Down Expand Up @@ -45,12 +52,18 @@ describe('extractCommandData', () => {
})

describe('writeCommandFlagInterface', () => {
test('calls writeFile with the correct content', async () => {
await writeCommandFlagInterface(testCommand, commandData)
test('writes the correct content to the file', async () => {
await inTemporaryDirectory(async (tmpDir) => {
// Given
vi.mocked(cwd).mockReturnValue(tmpDir)
const filePath = joinPath(tmpDir, 'docs-shopify.dev/commands/interfaces/topic-test-command.interface.ts')

// When
await writeCommandFlagInterface(testCommand, commandData)

expect(writeFile).toHaveBeenCalledWith(
expect.stringContaining('docs-shopify.dev/commands/interfaces/topic-test-command.interface.ts'),
`// This is an autogenerated file. Don't edit this file manually.
// Then
const content = await readFile(filePath)
expect(content).toBe(`// This is an autogenerated file. Don't edit this file manually.
/**
* The following flags are available for the \`topic test-command\` command:
* @publicDocs
Expand All @@ -68,24 +81,30 @@ export interface topictestcommand {
*/
'-a, --flag2'?: ''
}
`,
)
`)
})
})

test('calls writeFile with the correct content for a command with no flags', async () => {
await writeCommandFlagInterface({...testCommand, flags: {}}, commandData)
test('writes the correct content for a command with no flags', async () => {
await inTemporaryDirectory(async (tmpDir) => {
// Given
vi.mocked(cwd).mockReturnValue(tmpDir)
const filePath = joinPath(tmpDir, 'docs-shopify.dev/commands/interfaces/topic-test-command.interface.ts')

// When
await writeCommandFlagInterface({...testCommand, flags: {}}, commandData)

expect(writeFile).toHaveBeenCalledWith(
expect.stringContaining('docs-shopify.dev/commands/interfaces/topic-test-command.interface.ts'),
`// This is an autogenerated file. Don't edit this file manually.
// Then
const content = await readFile(filePath)
expect(content).toBe(`// This is an autogenerated file. Don't edit this file manually.
/**
* The following flags are available for the \`topic test-command\` command:
* @publicDocs
*/
export interface topictestcommand {

}
`,
)
`)
})
})
})
10 changes: 5 additions & 5 deletions packages/cli/src/cli/commands/docs/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Command as oclifCommand} from '@oclif/core'
import {mkdir, rmdir, writeFile} from '@shopify/cli-kit/node/fs'
import {cwd, joinPath} from '@shopify/cli-kit/node/path'

const docsPath = joinPath(cwd(), '/docs-shopify.dev/commands')
const docsPath = () => joinPath(cwd(), '/docs-shopify.dev/commands')

export type CommandWithMarkdown = oclifCommand.Loadable & {descriptionWithMarkdown?: string}

Expand All @@ -21,8 +21,8 @@ export default class DocsGenerate extends Command {
const commands = this.config.commands as CommandWithMarkdown[]

// Remove all files and recreate the folder. To make sure we don't leave any orphaned files.
await rmdir(docsPath)
await mkdir(docsPath)
await rmdir(docsPath())
await mkdir(docsPath())

// Short by length to ensure that we first generate the interfaces for the parent topics to detect hidden ones.
const sortedCommands = commands
Expand Down Expand Up @@ -96,6 +96,6 @@ export interface ${interfaceName} {
${flagsDetails}
}
`
await mkdir(`${docsPath}/interfaces`)
await writeFile(`${docsPath}/interfaces/${fileName}.interface.ts`, commandContent)
await mkdir(joinPath(docsPath(), 'interfaces'))
await writeFile(joinPath(docsPath(), 'interfaces', `${fileName}.interface.ts`), commandContent)
}
Loading