From 824ba5629183bc4cd59321213558770db211f6e5 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 9 Nov 2024 20:14:29 +0500 Subject: [PATCH] feat(cli): add locale command (#2586) --- cli/commands/{init.mjs => make/component.mjs} | 8 +-- cli/commands/make/index.mjs | 14 +++++ cli/commands/make/locale.mjs | 53 +++++++++++++++++++ cli/index.mjs | 4 +- .../components/content/SupportedLanguages.vue | 6 +++ 5 files changed, 79 insertions(+), 6 deletions(-) rename cli/commands/{init.mjs => make/component.mjs} (93%) create mode 100644 cli/commands/make/index.mjs create mode 100644 cli/commands/make/locale.mjs diff --git a/cli/commands/init.mjs b/cli/commands/make/component.mjs similarity index 93% rename from cli/commands/init.mjs rename to cli/commands/make/component.mjs index e6d1a820..1c9704e0 100644 --- a/cli/commands/init.mjs +++ b/cli/commands/make/component.mjs @@ -3,13 +3,13 @@ import { resolve } from 'pathe' import { defineCommand } from 'citty' import { consola } from 'consola' import { splitByCase, upperFirst, camelCase, kebabCase } from 'scule' -import { appendFile, sortFile } from '../utils.mjs' -import templates from '../templates.mjs' +import { appendFile, sortFile } from '../../utils.mjs' +import templates from '../../templates.mjs' export default defineCommand({ meta: { - name: 'init', - description: 'Init a new component.' + name: 'component', + description: 'Make a new component.' }, args: { name: { diff --git a/cli/commands/make/index.mjs b/cli/commands/make/index.mjs new file mode 100644 index 00000000..1255b876 --- /dev/null +++ b/cli/commands/make/index.mjs @@ -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 + } +}) diff --git a/cli/commands/make/locale.mjs b/cli/commands/make/locale.mjs new file mode 100644 index 00000000..98581759 --- /dev/null +++ b/cli/commands/make/locale.mjs @@ -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}`) + } +}) diff --git a/cli/index.mjs b/cli/index.mjs index e1bc724f..ee84999c 100755 --- a/cli/index.mjs +++ b/cli/index.mjs @@ -1,6 +1,6 @@ #!/usr/bin/env node import { defineCommand, runMain } from 'citty' -import init from './commands/init.mjs' +import make from './commands/make/index.mjs' const main = defineCommand({ meta: { @@ -8,7 +8,7 @@ const main = defineCommand({ description: 'Nuxt UI CLI' }, subCommands: { - init + make } }) diff --git a/docs/app/components/content/SupportedLanguages.vue b/docs/app/components/content/SupportedLanguages.vue index d04b724b..907c2fe0 100644 --- a/docs/app/components/content/SupportedLanguages.vue +++ b/docs/app/components/content/SupportedLanguages.vue @@ -31,5 +31,11 @@ const localesList = getLocaleKeys().map(locale => [locale, locales[locale].name] here + + Use CLI command nuxt-ui make locale to create a new locale + + nuxt-ui make locale --code "en" --name "English" + +