Skip to content

Commit

Permalink
Merge pull request #20 from Pisyukaev/refactor-core-package
Browse files Browse the repository at this point in the history
  • Loading branch information
Pisyukaev authored Nov 21, 2023
2 parents 3b099ad + 6b531c7 commit 22d9fbc
Show file tree
Hide file tree
Showing 17 changed files with 201 additions and 465 deletions.
7 changes: 7 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @xml-locales/cli

## 0.0.5

### Patch Changes

- Updated dependencies
- [email protected]

## 0.0.4

### Patch Changes
Expand Down
6 changes: 4 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.4",
"version": "0.0.5",
"type": "module",
"bin": {
"xml-locales": "./dist/bin.js"
Expand Down Expand Up @@ -28,7 +28,9 @@
"dependencies": {
"@inquirer/prompts": "3.3.0",
"@types/yargs": "17.0.32",
"xml-locales": "workspace:0.0.2",
"fast-xml-parser": "^4.3.2",
"xml-formatter": "^3.6.0",
"xml-locales": "workspace:0.0.3",
"yargs": "17.7.2"
}
}
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

## 0.0.3

### Patch Changes

- refactor modifiers functions, utils and tests

## 0.0.2

### Patch Changes
Expand Down
6 changes: 1 addition & 5 deletions 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.2",
"version": "0.0.3",
"type": "module",
"keywords": [
"xml-locales",
Expand Down Expand Up @@ -33,9 +33,5 @@
"build": "tsup",
"test": "vitest run --coverage",
"test:watch": "vitest --ui --watch --coverage"
},
"dependencies": {
"fast-xml-parser": "4.3.2",
"xml-formatter": "3.6.0"
}
}
91 changes: 21 additions & 70 deletions packages/xml-locales/src/modifiers/add.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,30 @@
import { checkKeyExist, replaceValue } from '../utils/files.js';
// import { hasConflict } from '../../../cli/src/utils/queries.js';
import { checkKeyExist, replaceValue } from '../utils/helpers.js';
import { sort } from './sort.js';
import type { XmlJson } from '../utils/types.js';
import type { AddOptions } from '../utils/types.js';

export function add(options: {
key: string;
value: string;
directory: string;
sort?: 'asc' | 'desc';
accept: boolean;
}) {
let REPLACE_ALL = false;
let NEED_REPLACE = false;
export function add({ key, value, sortDirection, jsonXml }: AddOptions) {
const {
resources: { string }
} = jsonXml;

return async function ({
filePath,
jsonXml
}: {
filePath: string;
jsonXml: XmlJson;
}) {
const { key, value, sort: sortDirection, accept } = options;
const hasKey = checkKeyExist(key, string);

if (accept) {
REPLACE_ALL = true;
}
if (hasKey) {
const replacedStrings = replaceValue(string, key, value);

const {
resources: { string }
} = jsonXml;
jsonXml.resources.string = replacedStrings;
}

const hasKey = checkKeyExist(key, string);
if (!hasKey) {
jsonXml.resources.string.push({
key_name: key,
'#text': value
});
}

// if (!REPLACE_ALL && hasKey) {
// const answer = await hasConflict(filePath);
if (sortDirection) {
sort({ sortDirection, jsonXml });
}

// switch (answer) {
// case 'yes': {
// NEED_REPLACE = true;
// break;
// }
// case 'all': {
// REPLACE_ALL = true;
// break;
// }
// case 'skip': {
// NEED_REPLACE = false;
// break;
// }

// default:
// console.log('Cancelled');
// return;
// }
// }

const needReplace = REPLACE_ALL || NEED_REPLACE;

if (needReplace) {
const replacedStrings = replaceValue(string, key, value);

jsonXml.resources.string = replacedStrings;
}

if (!hasKey) {
jsonXml.resources.string.push({
key_name: key,
'#text': value
});
}

if (sortDirection) {
sort({ direction: sortDirection })({ jsonXml });
}

return jsonXml;
};
return jsonXml;
}
83 changes: 20 additions & 63 deletions packages/xml-locales/src/modifiers/change.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,28 @@
import { checkKeyValueExist, replace } from '../utils/files.js';
// import { hasConflict } from '../../../cli/src/utils/queries.js';
import { checkKeyValueExist, replace } from '../utils/helpers.js';
import { sort } from './sort.js';
import type { XmlJson } from '../utils/types.js';
import type { ChangeOptions } from '../utils/types.js';

export function change(options: {
oldKey: string;
newKey: string;
sort?: 'asc' | 'desc';
accept: boolean;
}) {
let REPLACE_ALL = false;
let NEED_REPLACE = false;
export function change({
oldKey,
newKey,
sortDirection,
jsonXml
}: ChangeOptions) {
const {
resources: { string }
} = jsonXml;

return async function ({
filePath,
jsonXml
}: {
filePath: string;
jsonXml: XmlJson;
}) {
const { oldKey, newKey, sort: sortDirection, accept } = options;
const hasKey = checkKeyValueExist(oldKey, string);

if (accept) {
REPLACE_ALL = true;
}
if (hasKey) {
const replacedStrings = replace(string, oldKey, newKey);

const {
resources: { string }
} = jsonXml;
jsonXml.resources.string = replacedStrings;
}

const hasKey = checkKeyValueExist(oldKey, string);
if (sortDirection) {
sort({ sortDirection, jsonXml });
}

// if (!REPLACE_ALL && hasKey) {
// const answer = await hasConflict(filePath);

// switch (answer) {
// case 'yes': {
// NEED_REPLACE = true;
// break;
// }
// case 'all': {
// REPLACE_ALL = true;
// break;
// }
// case 'skip': {
// NEED_REPLACE = false;
// break;
// }

// default:
// console.log('Cancelled');
// return;
// }
// }

const needReplace = REPLACE_ALL || NEED_REPLACE;

if (needReplace) {
const replacedStrings = replace(string, oldKey, newKey);

jsonXml.resources.string = replacedStrings;
}

if (sortDirection) {
sort({ direction: sortDirection })({ jsonXml });
}

return jsonXml;
};
return jsonXml;
}
82 changes: 18 additions & 64 deletions packages/xml-locales/src/modifiers/delete.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,26 @@
// import { checkKeyValueExist } from '../utils/files.js';
// import { deleteAnswer } from '../../../cli/src/utils/queries.js';
import { checkKeyValueExist } from '../utils/helpers.js';
import { sort } from './sort.js';
import type { XmlJson } from '../utils/types.js';
import type { DeleteOptions } from '../utils/types.js';

export function del(options: {
keyValue: string;
sort?: 'asc' | 'desc';
accept: boolean;
}) {
let DELETE_ALL = false;
let NEED_DELETE = false;
export function del({ keyValue, sort: sortDirection, jsonXml }: DeleteOptions) {
const {
resources: { string }
} = jsonXml;

return async function delString({
filePath,
jsonXml
}: {
filePath: string;
jsonXml: XmlJson;
}) {
const { keyValue, sort: sortDirection, accept } = options;
const hasKeyValue = checkKeyValueExist(keyValue, string);

if (accept) {
DELETE_ALL = true;
}
if (hasKeyValue) {
const filteredStrings = string.filter(
(element) =>
element['#text'] !== keyValue && element.key_name !== keyValue
);

const {
resources: { string }
} = jsonXml;
jsonXml.resources.string = filteredStrings;
}

// const hasKeyValue = checkKeyValueExist(keyValue, string);
// if (!accept && !DELETE_ALL && hasKeyValue) {
// const answer = await deleteAnswer(filePath);
if (sortDirection) {
sort({ sortDirection, jsonXml });
}

// switch (answer) {
// case 'yes': {
// NEED_DELETE = true;
// break;
// }
// case 'all': {
// DELETE_ALL = true;
// break;
// }
// case 'skip': {
// NEED_DELETE = false;
// break;
// }

// default:
// console.log('Cancelled');
// return;
// }
// }

const needDelete = DELETE_ALL || NEED_DELETE;

if (needDelete) {
const filteredStrings = string.filter(
(element) =>
element['#text'] !== keyValue && element.key_name !== keyValue
);

jsonXml.resources.string = filteredStrings;
}

if (sortDirection) {
sort({ direction: sortDirection })({ jsonXml });
}

return jsonXml;
};
return jsonXml;
}
27 changes: 13 additions & 14 deletions packages/xml-locales/src/modifiers/sort.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { StringElement, XmlJson } from '../utils/types.js';
import type {
SortDirection,
SortOptions,
StringElement
} from '../utils/types.js';

export function sortBy(
strElements: StringElement[],
direction: 'asc' | 'desc'
) {
export function sortBy(strElements: StringElement[], direction: SortDirection) {
const { compare } = new Intl.Collator();

const compareFn = (a: StringElement, b: StringElement) => {
Expand All @@ -19,15 +20,13 @@ export function sortBy(
return sortedElements;
}

export function sort({ direction }: { direction: 'asc' | 'desc' }) {
return function ({ jsonXml }: { jsonXml: XmlJson }) {
const {
resources: { string }
} = jsonXml;
export function sort({ sortDirection, jsonXml }: SortOptions) {
const {
resources: { string }
} = jsonXml;

const sortedString = sortBy(string, direction);
jsonXml.resources.string = sortedString;
const sortedString = sortBy(string, sortDirection);
jsonXml.resources.string = sortedString;

return jsonXml;
};
return jsonXml;
}
Loading

0 comments on commit 22d9fbc

Please sign in to comment.