Skip to content

Commit

Permalink
chore: rename functions and files
Browse files Browse the repository at this point in the history
  • Loading branch information
blurrah committed Jul 1, 2024
1 parent 1fe1438 commit 12f90af
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/write.test.ts → src/cache.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, test } from "vitest";
import { updateCache } from "./write";
import { updateCache } from "./main";

describe("cache", () => {
test("writes output to empty cache", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/bin/index.ts → src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { processFiles } from "../write";
import { processFiles } from "./main";

// Setup yargs to use the command modules from the commands directory

Expand Down
16 changes: 8 additions & 8 deletions src/write.ts → src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from "fs";
import * as glob from "glob";
import { findTranslationsUsage } from "./parse";
import * as fs from "node:fs";
import { extractLabelsFromFile } from "./parser";

/**
* Recursive type for labels which can be nested objects containing strings
Expand Down Expand Up @@ -46,7 +46,7 @@ export async function processFiles(
);

for (const file of files) {
const data = await findTranslationsUsage(file);
const data = await extractLabelsFromFile(file);

// Update cache if we get results from a file
if (Object.keys(data).length > 0) {
Expand Down Expand Up @@ -88,20 +88,20 @@ export function updateCache({
// or use the namespace with name as a value
for (const value of values) {
currentCache[value] =
getLabelFromExisting([...keys, value], source) || `${key}.${value}`;
getLabelFromData(source, [...keys, value]) || `${key}.${value}`;
}
}
}

/**
* Traverse through label data and find existing label or return undefined
* @param path Array of keys to traverse through
* @param source
* @param source Label data object to get label from
* @returns String value if available or undefined if not
*/
function getLabelFromExisting(
path: Array<string>,
source: LabelData
function getLabelFromData(
source: LabelData,
path: Array<string>
): string | undefined {
let current: LabelData | string = source;
for (const key of path) {
Expand Down
6 changes: 3 additions & 3 deletions src/parse.test.ts → src/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, test } from "vitest";
import { parseSource } from "./parse";
import { extractLabels } from "./parser";

describe("Test parseSource", () => {
test("should parse source", async () => {
Expand Down Expand Up @@ -37,7 +37,7 @@ describe("Test parseSource", () => {
}
`;

const result = await parseSource("MyComponent.tsx", source);
const result = await extractLabels("MyComponent.tsx", source);
const expected = {
MyComponent: new Set(["foobar", "foodiebar", "title"]),
};
Expand All @@ -63,7 +63,7 @@ describe("Test parseSource", () => {
)
`;

const result = await parseSource("MyComponent.tsx", source);
const result = await extractLabels("MyComponent.tsx", source);

const expected = {
ProductListing: new Set(["foobar", "title", "results"]),
Expand Down
6 changes: 3 additions & 3 deletions src/parse.ts → src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ interface Scope {
parentScope: Scope | null;
}

export async function findTranslationsUsage(filePath: string) {
export async function extractLabelsFromFile(filePath: string) {
const fileContent = await fsPromises.readFile(filePath, "utf8");
return parseSource(filePath, fileContent);
return extractLabels(filePath, fileContent);
}

export async function parseSource(filename: string, source: string) {
export async function extractLabels(filename: string, source: string) {
const sourceFile = ts.createSourceFile(
filename,
source,
Expand Down
2 changes: 1 addition & 1 deletion tsup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defineConfig } from "tsup";

export default defineConfig([
{
entry: ["src/bin/index.ts"],
entry: ["src/cli.ts"],
clean: true,
splitting: false,
dts: false,
Expand Down

0 comments on commit 12f90af

Please sign in to comment.