This repository was archived by the owner on Jul 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 374
fix: lg files auto add placeholder for missing templates #5654
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8785165
compare and drop entry file changes, keep related changes commit
zhixzhan 2efd4cc
Auto create placeholder template used in dialog
zhixzhan b95fd4e
Merge branch 'main' into zhixzhan/lg-multilang-files-sync
srinaath d2b13d3
clean up
zhixzhan 26a482c
update tests
zhixzhan 4fb6593
Merge branch 'main' into zhixzhan/lg-multilang-files-sync
zhixzhan 5cac8bd
Merge branch 'main' into zhixzhan/lg-multilang-files-sync
boydc2014 d1ebc78
Merge branch 'main' into zhixzhan/lg-multilang-files-sync
srinaath 5103d2a
sync name change edting in all up view
zhixzhan bdb1db1
readOnly editor should take external value change
zhixzhan 6e31ccd
Merge branch 'main' into zhixzhan/lg-multilang-files-sync
boydc2014 b853627
lint
zhixzhan 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
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
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
58 changes: 58 additions & 0 deletions
58
Composer/packages/client/src/utils/__tests__/lgUtil.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,58 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { DialogInfo, LgFile } from '@bfc/shared'; | ||
|
|
||
| import { createMissingLgTemplatesForDialogs } from '../lgUtil'; | ||
|
|
||
| jest.mock('../../recoilModel/parsers/lgWorker', () => { | ||
| return { | ||
| addTemplates: (projectId, lgFile, templatesToAdd, lgFiles) => | ||
| require('@bfc/indexers/lib/utils/lgUtil').addTemplates(lgFile, templatesToAdd), | ||
| }; | ||
| }); | ||
|
|
||
| const bot1 = { | ||
| projectId: 'test', | ||
| lgFiles: [ | ||
| { | ||
| id: 'EmptyBot-Test.en-us', | ||
| templates: [{ name: 'SendActivity_Welcome' }, { name: 'SendActivity_jKvFxl' }], | ||
| content: `# SendActivity_Welcome | ||
| - welcome | ||
| # SendActivity_jKvFxl() | ||
| - 1`, | ||
| } as LgFile, | ||
| { | ||
| id: 'EmptyBot-Test.zh-cn', | ||
| templates: [{ name: 'SendActivity_Welcome' }, { name: 'SendActivity_8Oc1NU' }], | ||
| content: `# SendActivity_Welcome | ||
| - welcome | ||
| # SendActivity_8Oc1NU() | ||
| - 2`, | ||
| } as LgFile, | ||
| ], | ||
| dialogs: [ | ||
| { | ||
| id: 'EmptyBot-Test', | ||
| lgFile: 'EmptyBot-Test', | ||
| lgTemplates: [{ name: 'SendActivity_Welcome' }, { name: 'SendActivity_jKvFxl' }, { name: 'SendActivity_8Oc1NU' }], | ||
| } as DialogInfo, | ||
| ], | ||
| }; | ||
|
|
||
| describe('client lgUtil tests', () => { | ||
| it('should create missing lg templates for dialogs', async () => { | ||
| const { projectId, lgFiles, dialogs } = bot1; | ||
|
|
||
| const updatedLgFiles = await createMissingLgTemplatesForDialogs(projectId, dialogs, lgFiles); | ||
|
|
||
| expect(updatedLgFiles.length).toEqual(2); | ||
| expect(updatedLgFiles[0].id).toEqual('EmptyBot-Test.en-us'); | ||
| expect(updatedLgFiles[0].templates.length).toEqual(3); | ||
| expect(updatedLgFiles[0].templates.map(({ name }) => name)).toContain('SendActivity_8Oc1NU'); | ||
| expect(updatedLgFiles[1].id).toEqual('EmptyBot-Test.zh-cn'); | ||
| expect(updatedLgFiles[1].templates.length).toEqual(3); | ||
| expect(updatedLgFiles[1].templates.map(({ name }) => name)).toContain('SendActivity_jKvFxl'); | ||
| }); | ||
| }); |
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 |
|---|---|---|
| @@ -1,10 +1,45 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { DialogInfo, LgFile, LgTemplate } from '@bfc/shared'; | ||
| import difference from 'lodash/difference'; | ||
|
|
||
| import lgWorker from '../recoilModel/parsers/lgWorker'; | ||
|
|
||
| import { getBaseName } from './fileUtil'; | ||
|
|
||
| /** | ||
| * lgUtil.ts is a single place use lg-parser handle lg file operation. | ||
| * it's designed have no state, input text file, output text file. | ||
| * | ||
| * Auto create placeholder template used in dialog but did not exist in <dialog>.<*locale>.lg | ||
| */ | ||
|
|
||
| export * from '@bfc/indexers/lib/utils/lgUtil'; | ||
| export const createMissingLgTemplatesForDialogs = async ( | ||
| projectId: string, | ||
| dialogs: DialogInfo[], | ||
| lgFiles: LgFile[] | ||
| ): Promise<LgFile[]> => { | ||
| const updatedLgFiles: LgFile[] = []; | ||
|
|
||
| const templateNamesInDialog: Record<string, string[]> = dialogs.reduce((result, dialog: DialogInfo) => { | ||
| const { id, lgTemplates } = dialog; | ||
| result[id] = lgTemplates.map(({ name }) => name); | ||
| return result; | ||
| }, {}); | ||
|
|
||
| for (const lgFile of lgFiles) { | ||
| const dialogId = getBaseName(lgFile.id); | ||
| const templatesShouldExist = templateNamesInDialog[dialogId]; | ||
| const templatesInLgFile = lgFile.templates.map(({ name }) => name); | ||
| const templatesNotExist = difference(templatesShouldExist, templatesInLgFile); | ||
| if (templatesNotExist?.length) { | ||
| const templatesToAdd = templatesNotExist.map((name) => { | ||
| return { name, body: '-', parameters: [] } as LgTemplate; | ||
| }); | ||
| const updatedFile = (await lgWorker.addTemplates(projectId, lgFile, templatesToAdd, lgFiles)) as LgFile; | ||
| updatedLgFiles.push(updatedFile); | ||
| } else { | ||
| updatedLgFiles.push(lgFile); | ||
| } | ||
| } | ||
|
|
||
| return updatedLgFiles; | ||
| }; | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.