-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(localization): generate translations with readonly and as const
- Loading branch information
1 parent
0279d8b
commit cea1241
Showing
6 changed files
with
230 additions
and
19 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
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
137 changes: 137 additions & 0 deletions
137
packages/@o3r/localization/schematics/ng-update/v11-6/index.spec.ts
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,137 @@ | ||
import * as fs from 'node:fs'; | ||
import * as path from 'node:path'; | ||
import { | ||
Tree, | ||
} from '@angular-devkit/schematics'; | ||
import { | ||
SchematicTestRunner, | ||
type UnitTestTree, | ||
} from '@angular-devkit/schematics/testing'; | ||
|
||
const migrationPath = path.join(__dirname, '..', '..', '..', 'migration.json'); | ||
|
||
const notMigrated = `import type { | ||
Translation, | ||
} from '@o3r/core'; | ||
export interface LocalizationPresTranslation extends Translation { | ||
/** | ||
* Key for the message in the speech bubble until a destination is selected | ||
*/ | ||
welcome: string; | ||
/** | ||
* Key for the message in the speech bubble when a destination is selected, you can use \`{ cityName }\` to display the name of the city selected | ||
*/ | ||
welcomeWithCityName: string; | ||
/** | ||
* Key for the question at the top of the form | ||
*/ | ||
question: string; | ||
/** | ||
* Key for the label for the destination input | ||
*/ | ||
destinationLabel: string; | ||
/** | ||
* Key for the label for the departure date input | ||
*/ | ||
departureLabel: string; | ||
/** | ||
* Key for the placeholder for the destination input | ||
*/ | ||
destinationPlaceholder: string; | ||
/** | ||
* Key for the city names' dictionary | ||
*/ | ||
cityName: string; | ||
} | ||
export const translations: LocalizationPresTranslation = { | ||
welcome: 'o3r-localization-pres.welcome', | ||
welcomeWithCityName: 'o3r-localization-pres.welcomeWithCityName', | ||
question: 'o3r-localization-pres.question', | ||
destinationLabel: 'o3r-localization-pres.destinationLabel', | ||
departureLabel: 'o3r-localization-pres.departureLabel', | ||
cityName: 'o3r-localization-pres.cityName', | ||
destinationPlaceholder: 'o3r-localization-pres.destinationPlaceholder' | ||
}; | ||
`; | ||
|
||
const migrated = `import type { | ||
Translation, | ||
} from '@o3r/core'; | ||
export interface LocalizationPresTranslation extends Translation { | ||
/** | ||
* Key for the message in the speech bubble until a destination is selected | ||
*/ | ||
welcome: string; | ||
/** | ||
* Key for the message in the speech bubble when a destination is selected, you can use \`{ cityName }\` to display the name of the city selected | ||
*/ | ||
welcomeWithCityName: string; | ||
/** | ||
* Key for the question at the top of the form | ||
*/ | ||
question: string; | ||
/** | ||
* Key for the label for the destination input | ||
*/ | ||
destinationLabel: string; | ||
/** | ||
* Key for the label for the departure date input | ||
*/ | ||
departureLabel: string; | ||
/** | ||
* Key for the placeholder for the destination input | ||
*/ | ||
destinationPlaceholder: string; | ||
/** | ||
* Key for the city names' dictionary | ||
*/ | ||
cityName: string; | ||
} | ||
export const translations: Readonly<LocalizationPresTranslation> = { | ||
welcome: 'o3r-localization-pres.welcome', | ||
welcomeWithCityName: 'o3r-localization-pres.welcomeWithCityName', | ||
question: 'o3r-localization-pres.question', | ||
destinationLabel: 'o3r-localization-pres.destinationLabel', | ||
departureLabel: 'o3r-localization-pres.departureLabel', | ||
cityName: 'o3r-localization-pres.cityName', | ||
destinationPlaceholder: 'o3r-localization-pres.destinationPlaceholder' | ||
} as const; | ||
`; | ||
|
||
describe('Update', () => { | ||
let initialTree: Tree; | ||
let runner: SchematicTestRunner; | ||
beforeEach(() => { | ||
initialTree = Tree.empty(); | ||
initialTree.create('angular.json', fs.readFileSync(path.resolve(__dirname, '..', '..', '..', 'testing', 'mocks', 'angular.mocks.json'))); | ||
initialTree.create('package.json', fs.readFileSync(path.resolve(__dirname, '..', '..', '..', 'testing', 'mocks', 'package.mocks.json'))); | ||
initialTree.create('.eslintrc.json', fs.readFileSync(path.resolve(__dirname, '..', '..', '..', 'testing', 'mocks', '__dot__eslintrc.mocks.json'))); | ||
runner = new SchematicTestRunner('schematics', migrationPath); | ||
}); | ||
|
||
describe('Update v11.6', () => { | ||
let tree: UnitTestTree; | ||
const notMigratedPath = 'src/components/not-migrated.translation.ts'; | ||
const migratedPath = 'src/components/migrated.translation.ts'; | ||
|
||
beforeEach(async () => { | ||
initialTree.create(notMigratedPath, notMigrated); | ||
initialTree.create(migratedPath, migrated); | ||
tree = await runner.runSchematic('migration-v11_6', {}, initialTree); | ||
}); | ||
|
||
it('should migrate the not migrated file', () => { | ||
const newText = tree.readText(notMigratedPath); | ||
expect(newText).not.toEqual(notMigrated); | ||
expect(newText).toEqual(migrated); | ||
}); | ||
|
||
it('should not change the file already migrated', () => { | ||
expect(tree.readText(migratedPath)).toEqual(migrated); | ||
}); | ||
}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
packages/@o3r/localization/schematics/ng-update/v11-6/index.ts
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,28 @@ | ||
import type { | ||
Rule, | ||
Tree, | ||
} from '@angular-devkit/schematics'; | ||
import { | ||
createSchematicWithMetricsIfInstalled, | ||
findFilesInTree, | ||
} from '@o3r/schematics'; | ||
|
||
const regexp = /translations\s*:\s*(?:Readonly<)?([A-Z][\w]*)(?:>)?\s*=\s*({[^;]+})\s*(as\s*const)?\s*;/g; | ||
|
||
function updateV116Fn(): Rule { | ||
return (tree: Tree) => { | ||
const files = findFilesInTree(tree.getDir(''), (filePath) => /translation\.ts$/.test(filePath)); | ||
files.forEach(({ content, path }) => { | ||
const str = content.toString(); | ||
const newContent = str.replaceAll(regexp, 'translations: Readonly<$1> = $2 as const;'); | ||
if (newContent !== str) { | ||
tree.overwrite(path, newContent); | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
/** | ||
* Update of Otter configuration V11.6 | ||
*/ | ||
export const updateV116 = createSchematicWithMetricsIfInstalled(updateV116Fn); |