Skip to content

Commit

Permalink
Merge pull request #27 from Pisyukaev/print-diff-and-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pisyukaev authored Dec 5, 2023
2 parents 7ef0e6f + 504a133 commit 970e04b
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 233 deletions.
7 changes: 7 additions & 0 deletions .changeset/fair-yaks-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@xml-locales/cli': patch
---

feat: print diff
fix: --help flag now it works
chore: remove unused prompts
5 changes: 5 additions & 0 deletions .changeset/kind-birds-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xml-locales': patch
---

fix: filtering undefined xml nodes
3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
"test:watch": "vitest --ui --watch --coverage"
},
"dependencies": {
"@inquirer/prompts": "3.3.0",
"diff": "5.1.0",
"xml-locales": "workspace:0.0.4",
"yargs": "17.7.2"
},
"devDependencies": {
"@types/diff": "5.0.8",
"@types/yargs": "17.0.32"
}
}
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();
.help().argv;
6 changes: 5 additions & 1 deletion packages/cli/src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ArgumentsCamelCase, Argv } from 'yargs';

import { keyValueOptions } from '../options/key-value.js';
import { pathOption } from '../options/path.js';
import { Diff } from '../utils/diff.js';
import { readFiles, writeFile } from '../utils/files.js';

export const command = 'add';
Expand All @@ -24,6 +25,9 @@ export async function handler({
}>) {
const files = await readFiles(path);
for (const [path, file] of files) {
await writeFile(path, file.add({ key, value }).toXML());
const diff = new Diff(path, file);
const xml = file.add({ key, value }).toXML();
await writeFile(path, xml);
diff.print(xml);
}
}
7 changes: 6 additions & 1 deletion packages/cli/src/commands/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ArgumentsCamelCase, Argv } from 'yargs';

import { keyValueOptions } from '../options/key-value.js';
import { pathOption } from '../options/path.js';
import { Diff } from '../utils/diff.js';
import { readFiles, writeFile } from '../utils/files.js';

export const command = 'remove';
Expand All @@ -24,6 +25,8 @@ export async function handler({
}>) {
const files = await readFiles(path);
for (let [path, file] of files) {
const diff = new Diff(path, file);

if (key) {
file = file.deleteByKey(key);
}
Expand All @@ -32,6 +35,8 @@ export async function handler({
file = file.deleteByValue(value);
}

await writeFile(path, file.toXML());
const xml = file.toXML();
await writeFile(path, xml);
diff.print(xml);
}
}
6 changes: 5 additions & 1 deletion packages/cli/src/commands/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ArgumentsCamelCase, Argv } from 'yargs';

import { directionOption } from '../options/direction.js';
import { pathOption } from '../options/path.js';
import { Diff } from '../utils/diff.js';
import { readFiles, writeFile } from '../utils/files.js';

export const command = 'sort';
Expand All @@ -20,6 +21,9 @@ export async function handler({
}: ArgumentsCamelCase<{ path: string; direction: SortDirection }>) {
const files = await readFiles(path);
for (const [path, file] of files) {
await writeFile(path, file.sort(direction).toXML());
const diff = new Diff(path, file);
const xml = file.sort(direction).toXML();
await writeFile(path, xml);
diff.print(xml);
}
}
6 changes: 5 additions & 1 deletion packages/cli/src/commands/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ArgumentsCamelCase, Argv } from 'yargs';

import { oldKeyOrValueOptions } from '../options/key-value.js';
import { pathOption } from '../options/path.js';
import { Diff } from '../utils/diff.js';
import { readFiles, writeFile } from '../utils/files.js';

export const command = 'update';
Expand All @@ -24,6 +25,9 @@ export async function handler({
}>) {
const files = await readFiles(path);
for (const [path, file] of files) {
await writeFile(path, file.update({ oldValue, newValue }).toXML());
const diff = new Diff(path, file);
const xml = file.update({ oldValue, newValue }).toXML();
await writeFile(path, xml);
diff.print(xml);
}
}
55 changes: 55 additions & 0 deletions packages/cli/src/utils/diff.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { createPatch } from 'diff';
import { XmlLocales } from 'xml-locales';

export class Diff {
/** @internal */
private filePath: string;

/** @internal */
private actual: string;

constructor(filePath: string, xmlLocales: XmlLocales) {
this.filePath = filePath;
this.actual = xmlLocales.toXML();
}

print(expected: string): void {
const patch = createPatch('string', this.actual, expected);
const lines = patch.split('\n').slice(4).map(rework).filter(notNull);
console.log(
`${this.filePath}: ${
lines.length ? `\n${lines.join('\n')}` : 'no changes'
}\n`
);
}
}

function rework(line: string): string | null {
switch (line.charAt(0)) {
case '+':
return colorize('added', line);
case '-':
return colorize('removed', line);
case ' ':
return line;
case '@':
return null;
case '\\':
return null;
default:
return null;
}
}

function notNull<T extends string>(line: string | null): line is T {
return line !== null;
}

const colors = {
added: 32,
removed: 31
} as const;

function colorize(color: keyof typeof colors, string: string): string {
return `\u001b[${colors[color]}m${string}\u001b[0m`;
}
3 changes: 2 additions & 1 deletion packages/cli/src/utils/files.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'node:fs/promises';
import { resolve } from 'node:path';
import { XmlLocales } from 'xml-locales';

async function scanPath(path: string): Promise<string[]> {
Expand All @@ -9,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) => `${path}/${file}`);
.map((file) => resolve(`${path}/${file}`));
}
} catch (err) {
throw new Error(
Expand Down
49 changes: 0 additions & 49 deletions packages/cli/src/utils/prompts.ts

This file was deleted.

8 changes: 7 additions & 1 deletion packages/xml-locales/src/xml-locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ export class XmlLocales {
if (xmlData) {
const parsedXml = this.xmlParser.xmlToJson(xmlData);
if (Array.isArray(parsedXml.resources.string)) {
this.xmlData = parsedXml;
const filteredNodes = parsedXml.resources.string.filter(
(element) => element !== undefined
);

this.xmlData = new XmlJsonData({
resources: { string: filteredNodes }
});
} else {
this.xmlData = new XmlJsonData({
resources: { string: [parsedXml.resources.string] }
Expand Down
Loading

0 comments on commit 970e04b

Please sign in to comment.