-
Notifications
You must be signed in to change notification settings - Fork 251
[CLI UI kit - Themes adoption] Extract the ownership of development themes #1169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
5a040b6
Extract the ownership of development themes
4e6bb07
Merge branch 'main' into fix-509
b4b3db8
Changes after code review
77b6f6c
Merge branch 'main' into fix-509
0cbd918
Resolve merge conflicts after latest changes on `main`
ffd39cf
Pass `--overwrite-json` flag to Ruby CLI
39dc618
Merge branch 'main' into fix-509
c6ee55b
Update manifests
ebce87f
Store one development theme ID per store
c12c930
Merge branch 'main' into fix-509
1a206a2
Move Development Theme ID Store into separate store, for storing host…
8a169a1
Merge branch 'main' into fix-509
76804ce
Update oclif.manifest.json
fd65276
Use `getThemeStore`
a428292
Merge branch 'main' into fix-509
24faa1b
Merge branch 'main' into fix-509
150749b
Do not let `shopify theme dev --theme-editor-sync` always override lo…
c1af075
Move refactoring from fix-532 into fix-509
bf21e40
Merge branch 'main' into fix-509
0ec8ec8
Refresh manifests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,6 @@ | ||
| --- | ||
| '@shopify/cli-kit': patch | ||
| '@shopify/theme': patch | ||
| --- | ||
|
|
||
| Extract the ownership of development themes |
This file contains hidden or 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
25 changes: 25 additions & 0 deletions
25
packages/cli-kit/src/public/node/themes/generate-theme-name.test.ts
This file contains hidden or 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,25 @@ | ||
| import {generateThemeName} from './generate-theme-name.js' | ||
| import {beforeEach, describe, expect, it, vi} from 'vitest' | ||
| import {hostname} from 'os' | ||
| import {randomBytes} from 'crypto' | ||
|
|
||
| vi.mock('os') | ||
| vi.mock('crypto') | ||
|
|
||
| describe('generateThemeName', () => { | ||
| const context = 'Development' | ||
|
|
||
| beforeEach(() => { | ||
| vi.mocked(randomBytes).mockImplementation(() => Buffer.from([1, 2, 3])) | ||
| }) | ||
|
|
||
| it('should not truncate if the theme name is below the API limit', () => { | ||
| vi.mocked(hostname).mockReturnValue('Mac-Book-Pro.My-Router') | ||
| expect(generateThemeName(context)).toEqual('Development (010203-Mac-Book-Pro)') | ||
| }) | ||
|
|
||
| it('should truncate if the theme name is above the API limit', () => { | ||
| vi.mocked(hostname).mockReturnValue('theme-dev-lan-very-long-name-that-will-be-truncated') | ||
| expect(generateThemeName(context)).toEqual('Development (010203-theme-dev-lan-very-long-name-t)') | ||
| }) | ||
| }) |
15 changes: 15 additions & 0 deletions
15
packages/cli-kit/src/public/node/themes/generate-theme-name.ts
This file contains hidden or 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,15 @@ | ||
| import {replaceInvalidCharacters} from './replace-invalid-characters.js' | ||
| import {randomBytes} from 'crypto' | ||
| import {hostname} from 'os' | ||
|
|
||
| const API_NAME_LIMIT = 50 | ||
|
|
||
| export function generateThemeName(context: string): string { | ||
| const hostNameWithoutDomain = hostname().split('.')[0]! | ||
| const hash = randomBytes(3).toString('hex') | ||
|
|
||
| const name = `${context} ()` | ||
| const hostNameCharacterLimit = API_NAME_LIMIT - name.length - hash.length | ||
| const identifier = replaceInvalidCharacters(`${hash}-${hostNameWithoutDomain.substring(0, hostNameCharacterLimit)}`) | ||
| return `${context} (${identifier})` | ||
| } |
This file contains hidden or 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
14 changes: 14 additions & 0 deletions
14
packages/cli-kit/src/public/node/themes/replace-invalid-characters.test.ts
This file contains hidden or 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,14 @@ | ||
| import {replaceInvalidCharacters} from './replace-invalid-characters.js' | ||
| import {describe, expect, it} from 'vitest' | ||
|
|
||
| describe('replaceInvalidCharacters', () => { | ||
| it('should replace unused ASCII characters', () => { | ||
| const asciiStringChar = '\x8F' | ||
| expect(replaceInvalidCharacters(`theme-dev-${asciiStringChar}.lan`)).toEqual('theme-dev---lan') | ||
| }) | ||
|
|
||
| it('should not replace non-latin letters and marks', () => { | ||
| const hostName = 'ÇaVaこんにちはПривіт' | ||
| expect(replaceInvalidCharacters(hostName)).toEqual(hostName) | ||
| }) | ||
| }) |
8 changes: 8 additions & 0 deletions
8
packages/cli-kit/src/public/node/themes/replace-invalid-characters.ts
This file contains hidden or 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,8 @@ | ||
| export function replaceInvalidCharacters(identifier: string) { | ||
| const findAllMatches = 'g' | ||
| const enablesUnicodeSupport = 'u' | ||
| return identifier.replace( | ||
| new RegExp(/[^\p{Letter}\p{Number}\p{Mark}-]/, `${findAllMatches}${enablesUnicodeSupport}`), | ||
| '-', | ||
| ) | ||
| } |
This file contains hidden or 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,50 @@ | ||
| import {fetchTheme, createTheme} from './themes-api.js' | ||
| import {DEVELOPMENT_THEME_ROLE, Theme} from './models/theme.js' | ||
| import {generateThemeName} from './generate-theme-name.js' | ||
| import {AdminSession} from '@shopify/cli-kit/node/session' | ||
| import {BugError} from '@shopify/cli-kit/node/error' | ||
|
|
||
| export abstract class ThemeManager { | ||
| protected themeId: string | undefined | ||
| protected abstract setTheme(themeId: string): void | ||
| protected abstract removeTheme(): void | ||
| protected abstract context: string | ||
|
|
||
| constructor(protected adminSession: AdminSession) {} | ||
|
|
||
| async findOrCreate(): Promise<Theme> { | ||
| let theme = await this.fetch() | ||
| if (!theme) { | ||
| theme = await this.create() | ||
| } | ||
| return theme | ||
| } | ||
|
|
||
| protected async fetch() { | ||
| if (!this.themeId) { | ||
| return | ||
| } | ||
| const theme = await fetchTheme(parseInt(this.themeId, 10), this.adminSession) | ||
| if (!theme) { | ||
| this.removeTheme() | ||
| } | ||
| return theme | ||
| } | ||
|
|
||
| private async create() { | ||
| const name = generateThemeName(this.context) | ||
| const role = DEVELOPMENT_THEME_ROLE | ||
| const theme = await createTheme( | ||
| { | ||
| name, | ||
| role, | ||
| }, | ||
| this.adminSession, | ||
| ) | ||
| if (!theme) { | ||
| throw new BugError(`Could not create theme with name "${name}" and role "${role}"`) | ||
| } | ||
| this.setTheme(theme.id.toString()) | ||
| return theme | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletion
2
...heme/src/cli/utilities/theme-urls.test.ts → ...src/public/node/themes/theme-urls.test.ts
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
...ges/theme/src/cli/utilities/theme-urls.ts → ...-kit/src/public/node/themes/theme-urls.ts
This file contains hidden or 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
File renamed without changes.
This file contains hidden or 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
→ #1264 (comment)