Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions commands/lang/publish.ts
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')
}
}
82 changes: 82 additions & 0 deletions tests/commands/lang_publish.spec.ts
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',
},
])
})
})
Loading