-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a32c4f8
commit 05db127
Showing
4 changed files
with
86 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import {generateBlock} from './blocks.js' | ||
import {describe, expect, test, vi} from 'vitest' | ||
import {fileExists, writeFile} from '@shopify/cli-kit/node/fs' | ||
import {joinPath} from '@shopify/cli-kit/node/path' | ||
import {outputInfo} from '@shopify/cli-kit/node/output' | ||
|
||
vi.mock('@shopify/cli-kit/node/fs') | ||
vi.mock('@shopify/cli-kit/node/path') | ||
vi.mock('@shopify/cli-kit/node/output') | ||
|
||
describe('generateBlock', () => { | ||
const mockOptions = { | ||
name: 'test-block', | ||
type: 'basic', | ||
path: 'theme', | ||
} as const | ||
|
||
test('creates a new block file with correct content', async () => { | ||
vi.mocked(fileExists).mockResolvedValue(false) | ||
vi.mocked(joinPath).mockReturnValue('theme/blocks/test-block.liquid') | ||
|
||
await generateBlock(mockOptions) | ||
|
||
const expectedContent = `{% schema %} | ||
{ | ||
"name": "test-block", | ||
"settings": [] | ||
} | ||
{% endschema %}` | ||
|
||
expect(writeFile).toHaveBeenCalledWith('theme/blocks/test-block.liquid', expectedContent) | ||
expect(outputInfo).toHaveBeenCalledWith('Created block: theme/blocks/test-block.liquid') | ||
}) | ||
|
||
test('throws error if block already exists', async () => { | ||
vi.mocked(fileExists).mockResolvedValue(true) | ||
vi.mocked(joinPath).mockReturnValue('theme/blocks/test-block.liquid') | ||
|
||
await expect(generateBlock(mockOptions)).rejects.toThrow( | ||
'Block test-block already exists at theme/blocks/test-block.liquid', | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {BlockType} from '../../utilities/generator.js' | ||
import {fileExists, writeFile} from '@shopify/cli-kit/node/fs' | ||
import {joinPath} from '@shopify/cli-kit/node/path' | ||
import {outputInfo} from '@shopify/cli-kit/node/output' | ||
|
||
export interface BlockGeneratorOptions { | ||
name: string | ||
type: BlockType | ||
path: string | ||
} | ||
|
||
export async function generateBlock(options: BlockGeneratorOptions) { | ||
const blockPath = joinPath(options.path, 'blocks', `${options.name}.liquid`) | ||
|
||
// Check if block already exists | ||
if (await fileExists(blockPath)) { | ||
throw new Error(`Block ${options.name} already exists at ${blockPath}`) | ||
} | ||
|
||
// Write the file | ||
await writeFile(blockPath, generateBlockContent(options)) | ||
outputInfo(`Created block: ${blockPath}`) | ||
} | ||
|
||
function generateBlockContent(options: BlockGeneratorOptions): string { | ||
const baseSchema = { | ||
name: options.name, | ||
settings: [], | ||
} | ||
|
||
return `{% schema %} | ||
${JSON.stringify(baseSchema, null, 2)} | ||
{% endschema %}` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters