Skip to content

Commit

Permalink
Merge pull request #29 from Pisyukaev/multiple-changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pisyukaev authored Feb 13, 2024
2 parents cc9ab41 + 639fff8 commit 6d1e3d0
Show file tree
Hide file tree
Showing 16 changed files with 221 additions and 126 deletions.
11 changes: 11 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# @xml-locales/cli

## 1.0.0

### Major Changes

- Add multiple changes, update tests

### Patch Changes

- Updated dependencies
- [email protected]

## 0.0.8

### Patch Changes
Expand Down
24 changes: 12 additions & 12 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,45 +38,45 @@ npx @xml-locales/cli <command> [...args]

### Add Command

The `add` command is used to add a new key-value pair to the XML localization file or files from directory.
The `add` command is used to add a new key(s)-value(s) pair to the XML localization file or files from directory.

```sh
xml-locales add --path path/to/file_or_directory --key newKey --value newValue
xml-locales add --path path/to/file_or_directory --keys newKey --values newValue
```

| Flag | Alias | Default | Description |
|---------|-------|---------|-----------------------------------------------------------------------------|
| `--path`| `-p` | `process.cwd()` | The path to the XML localization file or directory where the key-value pair will be added. |
| `--key` | `-k` | None | The new key to be added. |
| `--value`| `-v` | None | The value to be associated with the new key. |
| `--keys` | `-k` | None | The new key(s) to be added. |
| `--values`| `-v` | None | The value(s) to be associated with the new key. |

### Remove Command

The `remove` command is used to remove a key-value pair from the XML localization file or files from directory.
The `remove` command is used to remove a key(s)-value(s) pair from the XML localization file or files from directory.

```sh
xml-locales remove --path path/to/file_or_directory --key keyToRemove --value valueToRemove
xml-locales remove --path path/to/file_or_directory --keys keyToRemove --values valueToRemove
```

| Flag | Alias | Default | Description |
|---------|-------|---------|-----------------------------------------------------------------------------|
| `--path`| `-p` | `process.cwd()` | The path to the XML localization file or directory where the key-value pair will be removed. |
| `--key` | `-k` | None | Removed string by the key. |
| `--value`| `-v` | None | Or removed string by the value. |
| `--keys` | `-k` | None | Removed string by the key(s). |
| `--values`| `-v` | None | Or removed string by the value(s). |

### Update Command

The `update` command is used to update a key-value pair in the XML localization file or files from directory.
The `update` command is used to update a key(s)-value(s) pair in the XML localization file or files from directory.

```sh
xml-locales update --path path/to/file_or_directory --oldValue oldValue --newValue newValue
xml-locales update --path path/to/file_or_directory --old oldValue --new newValue
```

| Flag | Alias | Default | Description |
|------------|-------|---------|-----------------------------------------------------------------------------|
| `--path` | `-p` | `process.cwd()` | The path to the XML localization file or directory where the key-value pair will be updated. |
| `--oldValue` | `-o` | None | The old key or value to be updated. |
| `--newValue`| `-n` | None | The new key or value to replace the old one. |
| `--old` | `-o` | None | The old key(s) or value(s) to be updated. |
| `--new`| `-n` | None | The new key(s) or value(s) to replace the old one. |

### Sort Command

Expand Down
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xml-locales/cli",
"version": "0.0.8",
"version": "1.0.0",
"type": "module",
"bin": {
"xml-locales": "./dist/bin.js"
Expand All @@ -27,7 +27,7 @@
},
"dependencies": {
"diff": "5.1.0",
"xml-locales": "workspace:0.0.5",
"xml-locales": "workspace:1.0.0",
"yargs": "17.7.2"
},
"devDependencies": {
Expand Down
17 changes: 11 additions & 6 deletions packages/cli/src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,28 @@ export const description = 'Add one localization string in files';

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"`
`\nExample:\n $0 ${command} --path "path/to/file/or/directory" --keys "some_key" --values "some_value"`
);
}

export async function handler({
key,
value,
keys,
values,
path
}: ArgumentsCamelCase<{
key: string;
value: string;
keys: string[];
values: string[];
path: string;
}>) {
const files = await readFiles(path);
for (const [path, file] of files) {
const diff = new Diff(path, file);
const xml = file.add({ key, value }).toXML();

if (keys.length !== values.length) {
throw new Error('The number of keys and values must be the same');
}

const xml = file.add({ keys, values }).toXML();
await writeFile(path, xml);
diff.print(xml);
}
Expand Down
18 changes: 9 additions & 9 deletions packages/cli/src/commands/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@ export const description = 'Remove one localization string in files';

export function builder(yargs: Argv) {
return keyValueOptions(pathOption(yargs), 'remove').usage(
`\nExample:\n ${command} --path "path/to/file/or/directory" --key "some_key" --value "or_some_value"`
`\nExample:\n ${command} --path "path/to/file/or/directory" --keys "some_key" --values "or_some_value"`
);
}

export async function handler({
key,
value,
keys,
values,
path
}: ArgumentsCamelCase<{
key: string;
value: string;
keys: string[];
values: string[];
path: string;
}>) {
const files = await readFiles(path);
for (let [path, file] of files) {
const diff = new Diff(path, file);

if (key) {
file = file.deleteByKey(key);
if (keys) {
file = file.deleteByKey(keys);
}

if (value) {
file = file.deleteByValue(value);
if (values) {
file = file.deleteByValue(values);
}

const xml = file.toXML();
Expand Down
15 changes: 10 additions & 5 deletions packages/cli/src/commands/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@ export function builder(yargs: Argv) {
}

export async function handler({
oldValue,
newValue,
old: oldValues,
new: newValues,
path
}: ArgumentsCamelCase<{
oldValue: string;
newValue: string;
old: string[];
new: string[];
path: string;
}>) {
const files = await readFiles(path);
for (const [path, file] of files) {
const diff = new Diff(path, file);
const xml = file.update({ oldValue, newValue }).toXML();

if (oldValues.length !== newValues.length) {
throw new Error('The number of old and new values must be the same');
}

const xml = file.update({ oldValues, newValues }).toXML();
await writeFile(path, xml);
diff.print(xml);
}
Expand Down
24 changes: 12 additions & 12 deletions packages/cli/src/options/key-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ import type { Argv } from 'yargs';

export function keyValueOptions(argv: Argv, action: string) {
return argv
.option('key', {
.option('keys', {
alias: 'k',
desc: `Key of ${action} string`,
type: 'string'
desc: `Key(s) of ${action} string(s)`,
type: 'array'
})
.option('value', {
.option('values', {
alias: 'v',
desc: `Value of ${action} string`,
type: 'string'
desc: `Value(s) of ${action} string(s)`,
type: 'array'
});
}

export function oldKeyOrValueOptions(argv: Argv) {
return argv
.option('oldValue', {
.option('old', {
alias: 'o',
desc: 'Old key or value of update string',
desc: 'Old key(s) or value(s) of update string(s)',
demandOption: true,
type: 'string'
type: 'array'
})
.option('newValue', {
.option('new', {
alias: 'n',
desc: 'New key or value of update string',
desc: 'New key(s) or value(s) of update string(s)',
demandOption: true,
type: 'string'
type: 'array'
});
}
4 changes: 2 additions & 2 deletions packages/cli/src/utils/files.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'node:fs/promises';
import { resolve } from 'node:path';
import { relative, resolve } from 'node:path';
import { XmlLocales } from 'xml-locales';

async function scanPath(path: string): Promise<string[]> {
Expand All @@ -10,7 +10,7 @@ async function scanPath(path: string): Promise<string[]> {
const paths = await fs.readdir(path);
return paths
.filter((file) => file.endsWith('.xml'))
.map((file) => resolve(`${path}/${file}`));
.map((file) => relative(process.cwd(), resolve(`${path}/${file}`)));
}
} catch (err) {
throw new Error(
Expand Down
6 changes: 6 additions & 0 deletions packages/xml-locales/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# xml-locales

## 1.0.0

### Major Changes

- Add multiple changes, update tests

## 0.0.5

### Patch Changes
Expand Down
16 changes: 8 additions & 8 deletions packages/xml-locales/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const xmlData = `
`
const xmlLocales = new XmlLocales(xmlData)
const jsonData = xmlLocales.add({key: 'newKey', value: 'newValue'}).toXML()
const jsonData = xmlLocales.add({keys: ['newKey'], values: ['newValue']}).toXML()
console.log(jsonData)
```
Expand Down Expand Up @@ -111,9 +111,9 @@ const xmlData = `
`

const xmlLocales = new XmlLocales(xmlData)
const jsonData = xmlLocales.add({key: 'newKey', value: 'newValue'})
.update({oldValue: 'key1', newValue: 'firstKey'})
.update({oldValue: 'value2', newValue: 'secondValue'})
const jsonData = xmlLocales.add({keys: ['newKey'], values: ['newValue']})
.update({oldValues: ['key1'], newValues: ['firstKey']})
.update({oldValues: ['value2'], newValues: ['secondValue']})
.toXML()

console.log(jsonData)
Expand Down Expand Up @@ -141,7 +141,7 @@ const xmlData = `
`

const xmlLocales = new XmlLocales(xmlData)
const jsonData = xmlLocales.add({key: 'newKey', value: 'newValue'})
const jsonData = xmlLocales.add({keys: ['newKey'], values: ['newValue']})
.deleteByKey('key1')
.deleteByValue('value2')
.toXML()
Expand Down Expand Up @@ -220,9 +220,9 @@ const xmlData = `

const xmlLocales = new XmlLocales(xmlData)

const jsonData = xmlLocales.add({key: 'newKey', value: 'newValue'})
.add({key: 'newKey2', value: 'newValue2'})
.update({oldValue: 'key1', newValue: 'firstKey'})
const jsonData = xmlLocales.add({keys: ['newKey'], values: ['newValue']})
.add({keys: ['newKey2'], values: ['newValue2']})
.update({oldValues: ['key1'], newValues: ['firstKey']})
.sort('desc')
.toXML()

Expand Down
2 changes: 1 addition & 1 deletion packages/xml-locales/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "xml-locales",
"description": "Tool for locales in xml files",
"version": "0.0.5",
"version": "1.0.0",
"type": "module",
"keywords": [
"xml-locales",
Expand Down
39 changes: 1 addition & 38 deletions packages/xml-locales/src/__snapshots__/xml-locales.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ XmlJsonData {
exports[`XmlLocales > XmlLocales constructor > should return json object 1`] = `
XmlJsonData {
"resources": {
"#text": "
",
"string": [
{
"#text": "value1",
Expand All @@ -34,44 +30,11 @@ XmlJsonData {
}
`;

exports[`XmlLocales > should return json object 1`] = `
XmlJsonData {
"resources": {
"#text": "
",
"string": [
{
"#text": "value1",
"key_name": "key1",
},
{
"#text": "value2",
"key_name": "key2",
},
],
},
}
`;

exports[`XmlLocales > should return xml string 1`] = `
"<resources>
<string name=\\"key1\\">value1</string>
<string name=\\"key2\\">value2</string>
</resources>"
`;
exports[`XmlLocales > to XML > should return xml string with formatting 1`] = `
"<resources>
<string name=\\"key1\\">value1</string>
<string name=\\"key2\\">value2</string>
</resources>"
`;
exports[`XmlLocales > to XML > should return xml string without formatting 1`] = `
"<resources><string name=\\"key1\\">value1</string><string name=\\"key2\\">value2</string>
</resources>"
`;
exports[`XmlLocales > to XML > should return xml string without formatting 1`] = `"<resources><string name=\\"key1\\">value1</string><string name=\\"key2\\">value2</string></resources>"`;
Loading

0 comments on commit 6d1e3d0

Please sign in to comment.