From c864592b8c42fa7e23d62aac7563cffd7126ae16 Mon Sep 17 00:00:00 2001 From: Vitaly Zakutny Date: Wed, 15 Nov 2023 23:39:21 +0300 Subject: [PATCH] feat: added command to create new localization file from master file --- src/commands/index.ts | 4 +++- src/commands/newLocale.ts | 41 ++++++++++++++++++++++++++++++++++++++ src/modifiers/newLocale.ts | 18 +++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/commands/newLocale.ts create mode 100644 src/modifiers/newLocale.ts diff --git a/src/commands/index.ts b/src/commands/index.ts index 14d0ac7..ca06293 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -1,11 +1,13 @@ import * as addString from './addString.js'; import * as change from './change.js'; import * as delString from './delString.js'; +import * as newLocale from './newLocale.js'; import * as sort from './sort.js'; export const commands = [ addString, delString, sort, - change + change, + newLocale ]; diff --git a/src/commands/newLocale.ts b/src/commands/newLocale.ts new file mode 100644 index 0000000..389d661 --- /dev/null +++ b/src/commands/newLocale.ts @@ -0,0 +1,41 @@ +import type { ArgumentsCamelCase, Argv } from 'yargs'; + +import { newLocale } from '../modifiers/newLocale'; + +export const command = 'new'; +export const description = 'Create new localization file from master file'; + +export function builder(yargs: Argv) { + return yargs + .option('name', { + alias: 'n', + desc: 'Name of new localization file', + demandOption: true, + type: 'string' + }) + .option('master', { + alias: 'm', + desc: 'Master localization file', + demandOption: true, + type: 'string' + }) + .option('directory', { + alias: 'dir', + desc: 'Directory of localization files', + type: 'string', + default: 'mock' + }) + .usage(`\nExample:\n $0 ${command} -n strings-kz.xml -m strings.xml`); +} + +export async function handler({ + name, + master, + directory +}: ArgumentsCamelCase<{ + name: string; + master: string; + directory: string; +}>) { + newLocale({ name, master, directory }); +} diff --git a/src/modifiers/newLocale.ts b/src/modifiers/newLocale.ts new file mode 100644 index 0000000..0bea49b --- /dev/null +++ b/src/modifiers/newLocale.ts @@ -0,0 +1,18 @@ +import * as fs from 'fs'; + +export function newLocale(options: { + name: string; + master: string; + directory: string; +}) { + const { name, master, directory } = options; + + fs.copyFile(`${directory}/${master}`, `${directory}/${name}`, (err) => { + if (err) { + console.error(err); + + return; + } + console.log(`${master} was copied to ${directory}/${name}`); + }); +}