mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-14 12:14:41 +01:00
feat(cli): add locale command (#2586)
This commit is contained in:
@@ -3,13 +3,13 @@ import { resolve } from 'pathe'
|
|||||||
import { defineCommand } from 'citty'
|
import { defineCommand } from 'citty'
|
||||||
import { consola } from 'consola'
|
import { consola } from 'consola'
|
||||||
import { splitByCase, upperFirst, camelCase, kebabCase } from 'scule'
|
import { splitByCase, upperFirst, camelCase, kebabCase } from 'scule'
|
||||||
import { appendFile, sortFile } from '../utils.mjs'
|
import { appendFile, sortFile } from '../../utils.mjs'
|
||||||
import templates from '../templates.mjs'
|
import templates from '../../templates.mjs'
|
||||||
|
|
||||||
export default defineCommand({
|
export default defineCommand({
|
||||||
meta: {
|
meta: {
|
||||||
name: 'init',
|
name: 'component',
|
||||||
description: 'Init a new component.'
|
description: 'Make a new component.'
|
||||||
},
|
},
|
||||||
args: {
|
args: {
|
||||||
name: {
|
name: {
|
||||||
14
cli/commands/make/index.mjs
Normal file
14
cli/commands/make/index.mjs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import { defineCommand } from 'citty'
|
||||||
|
import component from './component.mjs'
|
||||||
|
import locale from './locale.mjs'
|
||||||
|
|
||||||
|
export default defineCommand({
|
||||||
|
meta: {
|
||||||
|
name: 'make',
|
||||||
|
description: 'Commands to create new Nuxt UI entities.'
|
||||||
|
},
|
||||||
|
subCommands: {
|
||||||
|
component,
|
||||||
|
locale
|
||||||
|
}
|
||||||
|
})
|
||||||
53
cli/commands/make/locale.mjs
Normal file
53
cli/commands/make/locale.mjs
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import { existsSync, promises as fsp } from 'node:fs'
|
||||||
|
import { resolve } from 'pathe'
|
||||||
|
import { consola } from 'consola'
|
||||||
|
import { appendFile, sortFile } from '../../utils.mjs'
|
||||||
|
import { defineCommand } from 'citty'
|
||||||
|
|
||||||
|
export default defineCommand({
|
||||||
|
meta: {
|
||||||
|
name: 'locale',
|
||||||
|
description: 'Make a new locale.'
|
||||||
|
},
|
||||||
|
args: {
|
||||||
|
code: {
|
||||||
|
description: 'Locale code to create. For example: en.',
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
name: {
|
||||||
|
description: 'Locale name to create. For example: English.',
|
||||||
|
required: true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async setup({ args }) {
|
||||||
|
const path = resolve('.')
|
||||||
|
const localePath = resolve(path, `src/runtime/locale`)
|
||||||
|
|
||||||
|
const originLocaleFilePath = resolve(localePath, 'en.ts')
|
||||||
|
const newLocaleFilePath = resolve(localePath, `${args.code}.ts`)
|
||||||
|
|
||||||
|
// Validate locale code
|
||||||
|
if (existsSync(newLocaleFilePath)) {
|
||||||
|
consola.error(`🚨 ${args.code} already exists!`)
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!args.code.match(/^[a-z]{2}(?:_[a-z]{2,4})?$/)) {
|
||||||
|
consola.error(`🚨 ${args.code} is not a valid locale code!\nExample: en or en_us`)
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create new locale export
|
||||||
|
const localeExportFile = resolve(localePath, `index.ts`)
|
||||||
|
await appendFile(localeExportFile, `export { default as ${args.code} } from './${args.code}'`)
|
||||||
|
await sortFile(localeExportFile)
|
||||||
|
|
||||||
|
// Create new locale file
|
||||||
|
await fsp.copyFile(originLocaleFilePath, newLocaleFilePath)
|
||||||
|
const localeFile = await fsp.readFile(newLocaleFilePath, 'utf-8')
|
||||||
|
const rewrittenLocaleFile = localeFile.replace(/defineLocale\('(.*)'/, `defineLocale('${args.name}'`)
|
||||||
|
await fsp.writeFile(newLocaleFilePath, rewrittenLocaleFile)
|
||||||
|
|
||||||
|
consola.success(`🪄 Generated ${newLocaleFilePath}`)
|
||||||
|
}
|
||||||
|
})
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
import { defineCommand, runMain } from 'citty'
|
import { defineCommand, runMain } from 'citty'
|
||||||
import init from './commands/init.mjs'
|
import make from './commands/make/index.mjs'
|
||||||
|
|
||||||
const main = defineCommand({
|
const main = defineCommand({
|
||||||
meta: {
|
meta: {
|
||||||
@@ -8,7 +8,7 @@ const main = defineCommand({
|
|||||||
description: 'Nuxt UI CLI'
|
description: 'Nuxt UI CLI'
|
||||||
},
|
},
|
||||||
subCommands: {
|
subCommands: {
|
||||||
init
|
make
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -31,5 +31,11 @@ const localesList = getLocaleKeys().map(locale => [locale, locales[locale].name]
|
|||||||
here
|
here
|
||||||
</ProseA>
|
</ProseA>
|
||||||
</ProseP>
|
</ProseP>
|
||||||
|
<Tip>
|
||||||
|
Use CLI command <ProseCode>nuxt-ui make locale</ProseCode> to create a new locale
|
||||||
|
<ProsePre language="bash">
|
||||||
|
nuxt-ui make locale --code "en" --name "English"
|
||||||
|
</ProsePre>
|
||||||
|
</Tip>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
Reference in New Issue
Block a user