diff --git a/packages/cli/mock/locales/notXml.txt b/packages/cli/mock/locales/notXml.txt index d051377..18e1a15 100644 --- a/packages/cli/mock/locales/notXml.txt +++ b/packages/cli/mock/locales/notXml.txt @@ -1,6 +1,4 @@ - value1 value2 - diff --git a/packages/cli/mock/locales/strings1.xml b/packages/cli/mock/locales/strings1.xml index 8479f21..eef9898 100644 --- a/packages/cli/mock/locales/strings1.xml +++ b/packages/cli/mock/locales/strings1.xml @@ -1,5 +1,4 @@ - - - value1 - value2 - + + value1 + value2 + \ No newline at end of file diff --git a/packages/cli/mock/locales/strings2.xml b/packages/cli/mock/locales/strings2.xml index e70f0d7..951e39d 100644 --- a/packages/cli/mock/locales/strings2.xml +++ b/packages/cli/mock/locales/strings2.xml @@ -1,6 +1,5 @@ Value1 Value2 - vvvv - + Value3 \ No newline at end of file diff --git a/packages/cli/src/bin.ts b/packages/cli/src/bin.ts index 0f3ab13..4e59983 100644 --- a/packages/cli/src/bin.ts +++ b/packages/cli/src/bin.ts @@ -9,4 +9,4 @@ yargs(hideBin(process.argv)) .usage('$0 [args]') // @ts-ignore .command(commands) - .help().argv; + .help(); diff --git a/packages/cli/src/commands/add.ts b/packages/cli/src/commands/add.ts index b75533d..6c2c140 100644 --- a/packages/cli/src/commands/add.ts +++ b/packages/cli/src/commands/add.ts @@ -1,34 +1,16 @@ import type { ArgumentsCamelCase, Argv } from 'yargs'; +import { keyValueOptions } from '../options/key-value.js'; +import { pathOption } from '../options/path.js'; import { readFiles, writeFile } from '../utils/files.js'; export const command = 'add'; export const description = 'Add one localization string in files'; -export function builder(yargs: Argv) { - return yargs - .option('path', { - alias: 'p', - desc: 'Path of file or directory to adding string', - demandOption: true, - type: 'string', - default: process.cwd() - }) - .option('key', { - alias: 'k', - desc: 'Key of adding string', - demandOption: true, - type: 'string' - }) - .option('value', { - alias: 'v', - desc: 'Value of adding string', - demandOption: true, - type: 'string' - }) - .usage( - `\nExample:\n $0 ${command} --path "path/to/file/or/directory" --key "some_key" --value "some_value"` - ); +export function builder(argv: Argv) { + return keyValueOptions(pathOption(argv), 'add').usage( + `\nExample:\n $0 ${command} --path "path/to/file/or/directory" --key "some_key" --value "some_value"` + ); } export async function handler({ diff --git a/packages/cli/src/commands/remove.ts b/packages/cli/src/commands/remove.ts index 786fff8..8cc919d 100644 --- a/packages/cli/src/commands/remove.ts +++ b/packages/cli/src/commands/remove.ts @@ -1,32 +1,16 @@ import type { ArgumentsCamelCase, Argv } from 'yargs'; +import { keyValueOptions } from '../options/key-value.js'; +import { pathOption } from '../options/path.js'; import { readFiles, writeFile } from '../utils/files.js'; export const command = 'remove'; export const description = 'Remove one localization string in files'; export function builder(yargs: Argv) { - return yargs - .option('path', { - alias: 'p', - desc: 'Path of file or directory to adding string', - demandOption: true, - type: 'string', - default: process.cwd() - }) - .option('key', { - alias: 'k', - desc: 'Key of remove string', - type: 'string' - }) - .option('value', { - alias: 'v', - desc: 'Value of remove string', - type: 'string' - }) - .usage( - `\nExample:\n ${command} --path "path/to/file/or/directory" --key "some_key" --value "or_some_value"` - ); + return keyValueOptions(pathOption(yargs), 'remove').usage( + `\nExample:\n ${command} --path "path/to/file/or/directory" --key "some_key" --value "or_some_value"` + ); } export async function handler({ diff --git a/packages/cli/src/commands/sort.ts b/packages/cli/src/commands/sort.ts index 7ac3cfa..4e24a5c 100644 --- a/packages/cli/src/commands/sort.ts +++ b/packages/cli/src/commands/sort.ts @@ -1,30 +1,17 @@ -import { SortDirection } from 'xml-locales'; +import type { SortDirection } from 'xml-locales'; import type { ArgumentsCamelCase, Argv } from 'yargs'; +import { directionOption } from '../options/direction.js'; +import { pathOption } from '../options/path.js'; import { readFiles, writeFile } from '../utils/files.js'; export const command = 'sort'; export const description = 'Sorted keys of strings by asc or desc'; export function builder(yargs: Argv) { - return yargs - .option('path', { - alias: 'p', - desc: 'Path of file or directory to adding string', - demandOption: true, - type: 'string', - default: process.cwd() - }) - .option('direction', { - alias: 'd', - desc: 'Direction of key sort', - type: 'string', - default: 'asc', - choices: ['asc', 'desc'] - }) - .usage( - `\nExample:\n ${command} --path "path/to/file/or/directory" --direction "desc"` - ); + return directionOption(pathOption(yargs)).usage( + `\nExample:\n ${command} --path "path/to/file/or/directory" --direction "desc"` + ); } export async function handler({ diff --git a/packages/cli/src/commands/update.ts b/packages/cli/src/commands/update.ts index 36f9fe6..072e99d 100644 --- a/packages/cli/src/commands/update.ts +++ b/packages/cli/src/commands/update.ts @@ -1,34 +1,16 @@ import type { ArgumentsCamelCase, Argv } from 'yargs'; +import { oldKeyOrValueOptions } from '../options/key-value.js'; +import { pathOption } from '../options/path.js'; import { readFiles, writeFile } from '../utils/files.js'; export const command = 'update'; export const description = 'Update key or value of localization string'; export function builder(yargs: Argv) { - return yargs - .option('path', { - alias: 'p', - desc: 'Path of file or directory to adding string', - demandOption: true, - type: 'string', - default: process.cwd() - }) - .option('oldValue', { - alias: 'o', - desc: 'Old key or value of changing string', - demandOption: true, - type: 'string' - }) - .option('newValue', { - alias: 'n', - desc: 'New key or value of changing string', - demandOption: true, - type: 'string' - }) - .usage( - `\nExample:\n ${command} --path "path/to/file/or/directory" --old "some_key_or_value" --new "some_new_key_or_value"` - ); + return oldKeyOrValueOptions(pathOption(yargs)).usage( + `\nExample:\n ${command} --path "path/to/file/or/directory" --old "some_key_or_value" --new "some_new_key_or_value"` + ); } export async function handler({ diff --git a/packages/cli/src/options/direction.ts b/packages/cli/src/options/direction.ts new file mode 100644 index 0000000..6658425 --- /dev/null +++ b/packages/cli/src/options/direction.ts @@ -0,0 +1,11 @@ +import type { Argv } from 'yargs'; + +export function directionOption(argv: Argv) { + return argv.option('direction', { + alias: 'd', + desc: 'Direction of key sort', + type: 'string', + default: 'asc', + choices: ['asc', 'desc'] + }); +} diff --git a/packages/cli/src/options/key-value.ts b/packages/cli/src/options/key-value.ts new file mode 100644 index 0000000..b68adea --- /dev/null +++ b/packages/cli/src/options/key-value.ts @@ -0,0 +1,31 @@ +import type { Argv } from 'yargs'; + +export function keyValueOptions(argv: Argv, action: string) { + return argv + .option('key', { + alias: 'k', + desc: `Key of ${action} string`, + type: 'string' + }) + .option('value', { + alias: 'v', + desc: `Value of ${action} string`, + type: 'string' + }); +} + +export function oldKeyOrValueOptions(argv: Argv) { + return argv + .option('oldValue', { + alias: 'o', + desc: 'Old key or value of update string', + demandOption: true, + type: 'string' + }) + .option('newValue', { + alias: 'n', + desc: 'New key or value of update string', + demandOption: true, + type: 'string' + }); +} diff --git a/packages/cli/src/options/path.ts b/packages/cli/src/options/path.ts new file mode 100644 index 0000000..da4cc61 --- /dev/null +++ b/packages/cli/src/options/path.ts @@ -0,0 +1,11 @@ +import type { Argv } from 'yargs'; + +export function pathOption(argv: Argv) { + return argv.option('path', { + alias: 'p', + desc: 'Path of file or directory to adding string', + demandOption: true, + type: 'string', + default: process.cwd() + }); +}