Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add label:get command and tests #5

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 44 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ Label management command.
<!-- tocstop -->

# Usage
```sh-session
$ npm install @cli107/transverto --save-dev
$ ctv --help
```shell
npm install @cli107/transverto --save-dev
```

```shell
ctv --help
```

# Commands
Expand All @@ -27,6 +30,7 @@ $ ctv --help
* [`ctv label [ADD] [DELETE] [GET] [REPLACE] [SYNC]`](#ctv-label-add-delete-get-replace-sync)
* [`ctv label:add [LABEL]`](#ctv-labeladd-label)
* [`ctv label:delete LABEL`](#ctv-labeldelete-label)
* [`ctv label:get [LABEL]`](#ctv-labelget-label)
* [`ctv label:replace LABEL`](#ctv-labelreplace-label)
* [`ctv label:sync`](#ctv-labelsync)

Expand Down Expand Up @@ -137,7 +141,7 @@ _See code: [src/commands/init.ts](https://github.com/4746/transverto/blob/v1.1.0

## `ctv label [ADD] [DELETE] [GET] [REPLACE] [SYNC]`

Represents a label management command.
Label management command.

```
USAGE
Expand All @@ -151,7 +155,7 @@ ARGUMENTS
SYNC A command to update labels synchronously.

DESCRIPTION
Represents a label management command.
Label management command.
```

_See code: [src/commands/label/index.ts](https://github.com/4746/transverto/blob/v1.1.0/src/commands/label/index.ts)_
Expand Down Expand Up @@ -213,6 +217,38 @@ EXAMPLES

_See code: [src/commands/label/delete.ts](https://github.com/4746/transverto/blob/v1.1.0/src/commands/label/delete.ts)_

## `ctv label:get [LABEL]`

Display a list of translations for the specified label

```
USAGE
$ ctv label:get [LABEL] [--json] [-f <value>]

ARGUMENTS
LABEL A label key

FLAGS
-f, --fromLangCode=<value> The language code of source text.

GLOBAL FLAGS
--json Format output as json.

DESCRIPTION
Display a list of translations for the specified label

EXAMPLES
$ ctv label:get

$ ctv label:get hello.world

$ ctv label:get hello.world -f="en"

$ ctv label:get hello.world -fen
```

_See code: [src/commands/label/get.ts](https://github.com/4746/transverto/blob/v1.1.0/src/commands/label/get.ts)_

## `ctv label:replace LABEL`

Replace the label
Expand Down Expand Up @@ -274,15 +310,16 @@ _See code: [src/commands/label/sync.ts](https://github.com/4746/transverto/blob/
"basePath": "dist/i18n",
"basePathEnum": "dist/i18n/language.ts",
"bing": {
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0"
"userAgent": "..."
},
"engine": "bing",
"engineUseCache": false,
"labelValidation": "^[a-z0-9\\.\\-\\_]{3,100}$",
"languages": [
"en"
],
"nameEnum": "LanguageLabel"
"nameEnum": "LanguageLabel",
"userAgent": "..."
}
```

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cli107/transverto",
"description": "CLI transverto label management",
"description": "Generate automatically translations for your i18n json translate files",
"keywords": [
"transverto",
"translate",
Expand All @@ -12,7 +12,7 @@
],
"author": "Vadim",
"license": "MIT",
"version": "1.1.0",
"version": "1.2.0",
"bugs": "https://github.com/4746/transverto/issues",
"homepage": "https://github.com/4746/transverto",
"repository": "4746/transverto",
Expand All @@ -27,7 +27,7 @@
"prepack": "npm run build && oclif manifest && oclif readme",
"prepare": "npm run build",
"test": "mocha --forbid-only \"test/**/*.test.ts\"",
"version": "oclif readme && git add README.md",
"version": "oclif readme",
"win:delete:node_modules": "rd /s /q \"node_modules\"",
"npm:cache:clean": "npm cache clean --force",
"generate-config:bing": "node scripts/bing.generate-config.mjs",
Expand Down
119 changes: 119 additions & 0 deletions src/commands/label/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import {Args, Flags, ux} from '@oclif/core'
import chalk from "chalk";

import {LabelBaseCommand} from "../../shared/label-base.command.js";

/**
* node --loader ts-node/esm --no-warnings=ExperimentalWarning ./bin/dev label:get
* node --loader ts-node/esm --no-warnings=ExperimentalWarning ./bin/dev label:get hello.world
* node --loader ts-node/esm --no-warnings=ExperimentalWarning ./bin/dev label:get hello.world -f="en"
*/
export default class LabelGet extends LabelBaseCommand<typeof LabelGet> {
static args = {
label: Args.string({default: null, description: 'A label key', requiredOrDefaulted: true}),
}

static description = 'Display a list of translations for the specified label'

static enableJsonFlag = true;

static examples = [
'<%= config.bin %> <%= command.id %>',
'<%= config.bin %> <%= command.id %> hello.world',
'<%= config.bin %> <%= command.id %> hello.world -f="en"',
'<%= config.bin %> <%= command.id %> hello.world -fen',
]

static flags = {
fromLangCode: Flags.string({char: 'f', description: 'The language code of source text.'}),
}

public async run(): Promise<NonNullable<object> | void> {
const {args, flags} = await this.parse(LabelGet);

await this.readCliConfig();

const mapLangs = await this.getTranslationLanguages();

const rows = [];

let code: string;
if (flags.fromLangCode) {
code = await this.getLangCode(this.cliConfig.languages, flags.fromLangCode, this.cliConfig.langCodeDefault);

const data = this.findLabels(args.label, mapLangs[code].translateFlatten).map((row) => {
return {code, ...row};
});

rows.push(...data)
} else {
for (code in mapLangs) {
const data = this.findLabels(args.label, mapLangs[code].translateFlatten).map((row) => {
return {code, ...row};
});

rows.push(...data)
}
}

if (this.jsonEnabled()) {
return rows
}

ux.table(rows.map((v, k) => ({
...v, id: (k + 1).toString(),
})), {
id: {
header: '#',
minWidth: 7,
},
// eslint-disable-next-line
code: {
get: (row) => {
return chalk.yellow(row.code);
},
header: 'Code',
minWidth: 10,
},
label: {
get: (row) => {
return chalk.green(row.label);
},
header: 'Label',
minWidth: 20,
},
trans: {
get: (row) => {
return chalk.cyan(row.trans);
},
header: 'Translate',
minWidth: 20,
}
}, {
'no-truncate': true
})
}

private findLabels(label: string, translateFlatten: Record<string, string | string[]>) {
const rows = [];

if (label in translateFlatten) {
rows.push({
label,
trans: translateFlatten[label]
});
}

let labelKey: string;
for (labelKey in translateFlatten) {
if (labelKey.startsWith(`${label}.`)) {
rows.push({
label: labelKey,
trans: translateFlatten[labelKey]
});
}
}

return rows;
}
}
2 changes: 1 addition & 1 deletion src/commands/label/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class Label extends Command {
sync: Args.string({description: 'A command to update labels synchronously.', required: false}),
}

static description = 'Represents a label management command.'
static description = 'Label management command.'

async run(): Promise<void> {
// const {args} = await this.parse(Label)
Expand Down
10 changes: 10 additions & 0 deletions test/commands/label/get.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {expect, test} from '@oclif/test'

describe('label:get', () => {
test
.stdout()
.command(['label:get', 'hello.world'])
.it('runs label:get hello.world', ctx => {
expect(ctx.expectation).to.contain('label:get hello.world')
})
})