Skip to content

Commit

Permalink
chore(cli): refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
crashmax-dev committed Nov 29, 2023
1 parent d57d43d commit 36f8b5b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "xml-locales-workspaces",
"version": "0.0.0",
"type": "module",
"private": true,
"description": "Tool for locales in xml files",
"workspaces": [
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/utils/files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('readFiles', () => {
await readFiles('locales/doesNotExist.xml');
} catch (e: any) {
expect(e.message).toBe(
"Error scanning or reading path locales/doesNotExist.xml: ENOENT: no such file or directory, stat 'locales/doesNotExist.xml'"
`Error scanning or reading path "locales/doesNotExist.xml": ENOENT: no such file or directory, stat 'locales/doesNotExist.xml'`
);
}
});
Expand All @@ -53,7 +53,7 @@ describe('readFiles', () => {
await readFiles('path/does/not/exist');
} catch (e: any) {
expect(e.message).toBe(
"Error scanning or reading path path/does/not/exist: ENOENT: no such file or directory, stat 'path/does/not/exist'"
`Error scanning or reading path "path/does/not/exist": ENOENT: no such file or directory, stat 'path/does/not/exist'`
);
}
});
Expand Down
28 changes: 17 additions & 11 deletions packages/cli/src/utils/files.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import * as fs from 'node:fs/promises';
import fs from 'node:fs/promises';
import { XmlLocales } from 'xml-locales';

async function scanPath(path: string): Promise<string[]> {
try {
const statPath = await fs.stat(path);

if (statPath.isDirectory()) {
const files = await fs.readdir(path);
return files
const paths = await fs.readdir(path);
return paths
.filter((file) => file.endsWith('.xml'))
.map((file) => `${path}/${file}`);
}
} catch (e: any) {
throw new Error(`Error scanning or reading path ${path}: ${e.message}`);
} catch (err) {
throw new Error(
`Error scanning or reading path "${path}": ${(err as Error).message}`
);
}

return path.endsWith('.xml') ? [path] : [];
if (path.endsWith('.xml')) {
return [path];
}

return [];
}

export async function readFiles(path: string): Promise<[string, XmlLocales][]> {
Expand All @@ -30,15 +36,15 @@ export async function readFiles(path: string): Promise<[string, XmlLocales][]> {
}

return files;
} catch (e: any) {
throw new Error(`Error reading file ${path}: ${e.message}`);
} catch (err) {
throw new Error(`Error reading file "${path}": ${(err as Error).message}`);
}
}

export async function writeFile(path: string, content: string) {
export async function writeFile(path: string, content: string): Promise<void> {
try {
await fs.writeFile(path, content, 'utf-8');
} catch (e: any) {
throw new Error(`Error writing file ${path}: ${e.message}`);
} catch (err) {
throw new Error(`Error writing file "${path}": ${(err as Error).message}`);
}
}

0 comments on commit 36f8b5b

Please sign in to comment.