Skip to content

Commit

Permalink
chore(cli): reusable command options
Browse files Browse the repository at this point in the history
  • Loading branch information
crashmax-dev committed Nov 29, 2023
1 parent 36f8b5b commit 2235c6d
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 97 deletions.
2 changes: 0 additions & 2 deletions packages/cli/mock/locales/notXml.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

<resources>
<string name="key1">value1</string>
<string name="key2">value2</string>
</resources>

9 changes: 4 additions & 5 deletions packages/cli/mock/locales/strings1.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

<resources>
<string name="key1">value1</string>
<string name="key2">value2</string>
</resources>
<resources>
<string name="key1">value1</string>
<string name="key2">value2</string>
</resources>
3 changes: 1 addition & 2 deletions packages/cli/mock/locales/strings2.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<resources>
<string name="key1">Value1</string>
<string name="key2">Value2</string>
<string name="kkk">vvvv</string>
<resources></resources>
<string name="key3">Value3</string>
</resources>
2 changes: 1 addition & 1 deletion packages/cli/src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ yargs(hideBin(process.argv))
.usage('$0 <cmd> [args]')
// @ts-ignore
.command(commands)
.help().argv;
.help();
30 changes: 6 additions & 24 deletions packages/cli/src/commands/add.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down
26 changes: 5 additions & 21 deletions packages/cli/src/commands/remove.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down
25 changes: 6 additions & 19 deletions packages/cli/src/commands/sort.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down
28 changes: 5 additions & 23 deletions packages/cli/src/commands/update.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down
11 changes: 11 additions & 0 deletions packages/cli/src/options/direction.ts
Original file line number Diff line number Diff line change
@@ -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']
});
}
31 changes: 31 additions & 0 deletions packages/cli/src/options/key-value.ts
Original file line number Diff line number Diff line change
@@ -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'
});
}
11 changes: 11 additions & 0 deletions packages/cli/src/options/path.ts
Original file line number Diff line number Diff line change
@@ -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()
});
}

0 comments on commit 2235c6d

Please sign in to comment.