-
-
Notifications
You must be signed in to change notification settings - Fork 662
feat(i18n): add command to publish localization templates #5037
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
Open
guoyunhe
wants to merge
6
commits into
adonisjs:6.x
Choose a base branch
from
guoyunhe:lang-publish
base: 6.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+162
β0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9fe88ea
feat(i18n): add command to publish localization templates
guoyunhe ff2ec4f
fix: update import paths to use node: prefix for fs modules
guoyunhe 19c6765
feat(i18n): add merge flag for localization template publishing
guoyunhe c603c9c
feat(lang): add mkdir for validator.json destination and implement teβ¦
guoyunhe 97c822b
refactor(tests): format log messages and improve test readability in β¦
guoyunhe 5bee8f7
refactor(tests): remove outdated comments and clarify message mergingβ¦
guoyunhe 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,80 @@ | ||
| /* | ||
| * @adonisjs/core | ||
| * | ||
| * (c) AdonisJS | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| import { BaseCommand, flags } from '../../modules/ace/main.js' | ||
| import { writeFile, readFile, mkdir } from 'node:fs/promises' | ||
| import { existsSync } from 'node:fs' | ||
| import { dirname } from 'node:path' | ||
|
|
||
| /** | ||
| * Publish localization templates | ||
| */ | ||
| export default class MakeCommand extends BaseCommand { | ||
| static commandName = 'lang:publish' | ||
| static description = 'Eject localization templates to resources/lang/en directory' | ||
|
|
||
| @flags.boolean({ description: 'Merge with existing "validator.json" file' }) | ||
| declare merge?: boolean | ||
|
|
||
| async run() { | ||
| let messages: any = {} | ||
| try { | ||
| const vine = await import('@vinejs/vine/defaults') | ||
| messages = vine.messages | ||
| } catch { | ||
| this.exitCode = 1 | ||
| this.logger.error('Vine is not installed. No localization templates to publish.') | ||
| return | ||
| } | ||
|
|
||
| const destination = this.app.languageFilesPath('en', 'validator.json') | ||
|
|
||
| if (existsSync(destination) && !this.merge) { | ||
| this.exitCode = 1 | ||
| this.logger.error( | ||
| 'File "resources/lang/en/validator.json" already exists. Use "--merge" flag to update the file' | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| let validatorMessages = { | ||
| shared: { | ||
| messages: {}, | ||
| }, | ||
| } | ||
|
|
||
| if (existsSync(destination)) { | ||
| try { | ||
| validatorMessages = JSON.parse(await readFile(destination, 'utf-8')) | ||
| } catch { | ||
| this.exitCode = 1 | ||
| this.logger.error( | ||
| 'Failed to parse existing validator.json. Overwriting with default contents.' | ||
| ) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| validatorMessages = { | ||
| ...validatorMessages, | ||
| shared: { | ||
| ...validatorMessages.shared, | ||
| messages: { | ||
| ...messages, | ||
| ...validatorMessages.shared?.messages, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| await mkdir(dirname(destination), { recursive: true }) | ||
| await writeFile(destination, JSON.stringify(validatorMessages, null, 2)) | ||
|
|
||
| this.logger.success('Localization template published to resources/lang/en/validator.json') | ||
| } | ||
| } | ||
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,82 @@ | ||
| import { test } from '@japa/runner' | ||
| import { AceFactory } from '../../factories/core/ace.js' | ||
| import LangPublishCommand from '../../commands/lang/publish.js' | ||
|
|
||
| test.group('Lang publish', () => { | ||
| test('publish validator messages', async ({ assert, fs }) => { | ||
| const ace = await new AceFactory().make(fs.baseUrl) | ||
| await ace.app.init() | ||
| ace.ui.switchMode('raw') | ||
|
|
||
| const command = await ace.create(LangPublishCommand, []) | ||
| await command.exec() | ||
|
|
||
| await assert.fileExists('resources/lang/en/validator.json') | ||
| const json = await fs.contentsJson('resources/lang/en/validator.json') | ||
| assert.isObject(json.shared) | ||
| assert.isObject(json.shared.messages) | ||
| assert.isTrue(Object.keys(json.shared.messages).length > 0) | ||
|
|
||
| assert.deepEqual(ace.ui.logger.getLogs(), [ | ||
| { | ||
| message: | ||
| '[ green(success) ] Localization template published to resources/lang/en/validator.json', | ||
| stream: 'stdout', | ||
| }, | ||
| ]) | ||
| }) | ||
|
|
||
| test('skip when validator messages file already exists', async ({ assert, fs }) => { | ||
| const ace = await new AceFactory().make(fs.baseUrl) | ||
| await ace.app.init() | ||
| ace.ui.switchMode('raw') | ||
|
|
||
| await fs.create('resources/lang/en/validator.json', JSON.stringify({ old: true })) | ||
|
|
||
| const command = await ace.create(LangPublishCommand, []) | ||
| await command.exec() | ||
|
|
||
| const json = await fs.contentsJson('resources/lang/en/validator.json') | ||
| assert.deepEqual(json, { old: true }) | ||
|
|
||
| assert.deepEqual(ace.ui.logger.getLogs(), [ | ||
| { | ||
| message: | ||
| '[ red(error) ] File "resources/lang/en/validator.json" already exists. Use "--merge" flag to update the file', | ||
| stream: 'stderr', | ||
| }, | ||
| ]) | ||
| }) | ||
|
|
||
| test('merge when validator messages file already exists and --merge is used', async ({ | ||
| assert, | ||
| fs, | ||
| }) => { | ||
| const ace = await new AceFactory().make(fs.baseUrl) | ||
| await ace.app.init() | ||
| ace.ui.switchMode('raw') | ||
|
|
||
| await fs.create( | ||
| 'resources/lang/en/validator.json', | ||
| JSON.stringify({ | ||
| shared: { messages: { required: 'foo' } }, | ||
| other: true, | ||
| }) | ||
| ) | ||
|
|
||
| const command = await ace.create(LangPublishCommand, ['--merge']) | ||
| await command.exec() | ||
|
|
||
| const json = await fs.contentsJson('resources/lang/en/validator.json') | ||
| assert.property(json, 'other') | ||
| assert.equal(json.shared.messages.required, 'foo') | ||
|
|
||
| assert.deepEqual(ace.ui.logger.getLogs(), [ | ||
| { | ||
| message: | ||
| '[ green(success) ] Localization template published to resources/lang/en/validator.json', | ||
| stream: 'stdout', | ||
| }, | ||
| ]) | ||
| }) | ||
| }) |
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.
Uh oh!
There was an error while loading. Please reload this page.