Skip to content

Commit

Permalink
Merge pull request #12 from Pisyukaev:prettier
Browse files Browse the repository at this point in the history
Fix prettier
  • Loading branch information
Pisyukaev authored Nov 15, 2023
2 parents e090b99 + f95cf44 commit 38a31f9
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 92 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@pisyukaev/tsconfig": "^2.0.1",
"@types/node": "^20.4.2",
"nodemon": "^3.0.1",
"prettier": "^2.8.8",
"ts-node": "^10.9.1",
"tsup": "^7.1.0",
"typescript": "^5.1.6",
Expand Down
21 changes: 12 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions src/commands/change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ export function builder(yargs: Argv) {
desc: 'Sorted keys of strings by asc or desc',
type: 'string'
});
}

}

export async function handler({
oldkey: oldKeyValue,
Expand All @@ -43,12 +42,14 @@ export async function handler({
directory: string;
sort?: 'asc' | 'desc';
}>) {
console.log(oldKeyValue, newKeyValue);

console.log(oldKeyValue, newKeyValue)

modificationFile(change({
oldKey: oldKeyValue,
newKey: newKeyValue,
sort }),
directory);
modificationFile(
change({
oldKey: oldKeyValue,
newKey: newKeyValue,
sort
}),
directory
);
}
2 changes: 1 addition & 1 deletion src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as addString from './addString.js';
import * as change from './change.js';
import * as delString from './delString.js';
import * as sort from './sort.js';
import * as change from './change.js'

export const commands = [
addString,
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ yargs(hideBin(process.argv))
// @ts-ignore
.command(commands)
.help().argv;

6 changes: 3 additions & 3 deletions src/modifiers/add.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { checkKeyExist, replaceValue } from '../utils/files';
import { hasConflict } from '../utils/queries';
import { sort } from './sort';
import { checkKeyExist, replaceValue } from '../utils/files.js';
import { hasConflict } from '../utils/queries.js';
import { sort } from './sort.js';

export function add(options: {
key: string;
Expand Down
2 changes: 1 addition & 1 deletion src/modifiers/change.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { checkKeyValueExist, replace } from '../utils/files';
import { checkKeyValueExist, replace } from '../utils/files';
import { hasConflict } from '../utils/queries';
import { sort } from './sort';

Expand Down
6 changes: 3 additions & 3 deletions src/tests/mock/data/stringElements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const STRING_ELEMENTS = [
key_name: 'testKey2',
'#text': 'text2'
}
]
];

export const REPLACED_VALUE_ELEMENTS = [
{
Expand All @@ -18,7 +18,7 @@ export const REPLACED_VALUE_ELEMENTS = [
key_name: 'testKey2',
'#text': 'text2'
}
]
];

export const REPLACED_KEY_ELEMENTS = [
{
Expand All @@ -29,4 +29,4 @@ export const REPLACED_KEY_ELEMENTS = [
key_name: 'testKey2',
'#text': 'text2'
}
]
];
104 changes: 60 additions & 44 deletions src/tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { describe, expect, test } from 'vitest';

import { checkKeyExist, checkKeyValueExist, getFilesFromDir, replace, replaceValue } from '../utils/files';
import { REPLACED_VALUE_ELEMENTS,REPLACED_KEY_ELEMENTS, STRING_ELEMENTS } from './mock/data/stringElements';


import {
checkKeyExist,
checkKeyValueExist,
getFilesFromDir,
replace,
replaceValue
} from '../utils/files';
import {
REPLACED_KEY_ELEMENTS,
REPLACED_VALUE_ELEMENTS,
STRING_ELEMENTS
} from './mock/data/stringElements';

describe('Get xml files path', () => {
test('Files in mock folder', async () => {
Expand All @@ -21,68 +29,76 @@ describe('Get xml files path', () => {

describe('checkKeyExist', () => {
test('has key', () => {
const hasKey = checkKeyExist('testKey1', STRING_ELEMENTS)
const hasKey = checkKeyExist('testKey1', STRING_ELEMENTS);

expect(hasKey).toBeTruthy()
})
expect(hasKey).toBeTruthy();
});

test('has no key', () => {
const hasKey = checkKeyExist('testKey', STRING_ELEMENTS)
const hasKey = checkKeyExist('testKey', STRING_ELEMENTS);

expect(hasKey).toBeFalsy()
})
})
expect(hasKey).toBeFalsy();
});
});

describe('replace', () => {
test('replace key', () => {
const replacedStrings = replace(STRING_ELEMENTS, 'testKey1', 'newKey')
expect(replacedStrings).toMatchObject(REPLACED_KEY_ELEMENTS)
})
const replacedStrings = replace(STRING_ELEMENTS, 'testKey1', 'newKey');

expect(replacedStrings).toMatchObject(REPLACED_KEY_ELEMENTS);
});

test('replace value', () => {
const replacedStrings = replace(STRING_ELEMENTS, 'text1', 'newValue')
expect(replacedStrings).toMatchObject(REPLACED_VALUE_ELEMENTS)
})
})
const replacedStrings = replace(STRING_ELEMENTS, 'text1', 'newValue');

expect(replacedStrings).toMatchObject(REPLACED_VALUE_ELEMENTS);
});
});

describe('checkKeyValueExist', () => {
describe('key', () => {
test('key exist', () => {
const hasKey = checkKeyValueExist('testKey1', STRING_ELEMENTS)
expect(hasKey).toBeTruthy()
})
const hasKey = checkKeyValueExist('testKey1', STRING_ELEMENTS);
expect(hasKey).toBeTruthy();
});

test('key no exist', () => {
const hasKey = checkKeyValueExist('someKey', STRING_ELEMENTS)
expect(hasKey).toBeFalsy()
})
})
const hasKey = checkKeyValueExist('someKey', STRING_ELEMENTS);
expect(hasKey).toBeFalsy();
});
});

describe('value', () => {
test('value exist', () => {
const hasKey = checkKeyValueExist('text1', STRING_ELEMENTS)
expect(hasKey).toBeTruthy()
})

test('value no exist', () => {
const hasKey = checkKeyValueExist('someValue', STRING_ELEMENTS)
expect(hasKey).toBeFalsy()
})
})
})
const hasKey = checkKeyValueExist('text1', STRING_ELEMENTS);
expect(hasKey).toBeTruthy();
});

test('value no exist', () => {
const hasKey = checkKeyValueExist('someValue', STRING_ELEMENTS);
expect(hasKey).toBeFalsy();
});
});
});

describe('replaceValue', () => {
test('replaced value', () => {
const replacedStrings = replaceValue(STRING_ELEMENTS, 'testKey1', 'newValue')
const replacedStrings = replaceValue(
STRING_ELEMENTS,
'testKey1',
'newValue'
);

expect(replacedStrings).toMatchObject(REPLACED_VALUE_ELEMENTS)
})
expect(replacedStrings).toMatchObject(REPLACED_VALUE_ELEMENTS);
});

test('does not replace value', () => {
const replacedStrings = replaceValue(STRING_ELEMENTS, 'someKey', 'someValue')
const replacedStrings = replaceValue(
STRING_ELEMENTS,
'someKey',
'someValue'
);

expect(replacedStrings).toMatchObject(STRING_ELEMENTS)
})
})
expect(replacedStrings).toMatchObject(STRING_ELEMENTS);
});
});
44 changes: 23 additions & 21 deletions src/utils/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function getFilesFromDir(dirName: string) {
fs.readdir(dirName, (err, files) => {
if (err) {
reject(err);
return;
}

const allFileIsXML = files.every((file) => file.endsWith('xml'));
Expand Down Expand Up @@ -56,30 +57,31 @@ export function replaceValue(
return replacedStrings;
}

export function replace(strElements: StringElement[],
oldKeyValue: string,
newKeyValue: string
) {
const replaced = strElements.map((element) => {
if(element.key_name === oldKeyValue) {
return {
...element,
key_name: newKeyValue
}
}
export function replace(
strElements: StringElement[],
oldKeyValue: string,
newKeyValue: string
) {
const replaced = strElements.map((element) => {
if (element.key_name === oldKeyValue) {
return {
...element,
key_name: newKeyValue
};
}

if(element['#text'] === oldKeyValue) {
return {
...element,
'#text': newKeyValue
}
}
if (element['#text'] === oldKeyValue) {
return {
...element,
'#text': newKeyValue
};
}

return element
})
return element;
});

return replaced
}
return replaced;
}

export async function modificationFile(
callback: ({
Expand Down

0 comments on commit 38a31f9

Please sign in to comment.