taler-typescript-core

Wallet core logic and WebUIs for various components
Log | Files | Refs | Submodules | README | LICENSE

commit 6dbe349130e5c0a43c92423df962ea6d38cc9843
parent ba8099d07c95a525544d579d3e132c06a7b95d91
Author: Sebastian <sebasjm@gmail.com>
Date:   Fri, 29 Nov 2024 10:28:45 -0300

add prefix

Diffstat:
Mpackages/pogen/src/potextract.test.ts | 4++--
Mpackages/pogen/src/potextract.ts | 41+++++++++++++++++++++++++++++------------
2 files changed, 31 insertions(+), 14 deletions(-)

diff --git a/packages/pogen/src/potextract.test.ts b/packages/pogen/src/potextract.test.ts @@ -16,7 +16,7 @@ import test from "ava"; import * as ts from "typescript"; -import { processFile2 } from "./potextract.js"; +import { processFileForTesting } from "./potextract.js"; function wrapIntoFunction(src: string): string { return ` @@ -33,7 +33,7 @@ function process(src: string): string { wrapIntoFunction(src), ts.ScriptTarget.ES2020, ); - return processFile2(source).trim(); + return processFileForTesting(source).trim(); } test("should extract the key from inner body", (t) => { diff --git a/packages/pogen/src/potextract.ts b/packages/pogen/src/potextract.ts @@ -198,6 +198,7 @@ function formatScreenId( } function formatMsgComment( + projectPrefix: string, sourceFile: ts.SourceFile, outChunks: string[], line: number, @@ -208,7 +209,7 @@ function formatMsgComment( outChunks.push(`#. ${cl}\n`); } } - const fn = path.posix.relative(process.cwd(), sourceFile.fileName); + const fn = path.relative(projectPrefix, sourceFile.fileName); outChunks.push(`#: ${fn}:${line + 1}\n`); outChunks.push(`#, c-format\n`); } @@ -416,6 +417,7 @@ function processNode( sourceFile: ts.SourceFile, outChunks: string[], knownMessageIds: Set<string>, + projectPrefix: string, ) { switch (node.kind) { case ts.SyntaxKind.JsxElement: @@ -435,7 +437,7 @@ function processNode( knownMessageIds.add(msgid); const screenId = searchScreenId(parents, sourceFile); formatScreenId(sourceFile, outChunks, screenId); - formatMsgComment(sourceFile, outChunks, line, comment); + formatMsgComment(projectPrefix, sourceFile, outChunks, line, comment); formatMsgLine(outChunks, "msgctxt", context); formatMsgLine(outChunks, "msgid", content); outChunks.push(`msgstr ""\n`); @@ -446,7 +448,7 @@ function processNode( if (arrayEq(path, ["i18n", "TranslateSwitch"])) { let { line } = ts.getLineAndCharacterOfPosition(sourceFile, node.pos); let comment = getComment(sourceFile, preLastTokLine, lastTokLine, node); - formatMsgComment(sourceFile, outChunks, line, comment); + formatMsgComment(projectPrefix, sourceFile, outChunks, line, comment); let singularForm = getJsxSingular(node); if (!singularForm) { console.error("singular form missing"); @@ -518,7 +520,7 @@ function processNode( knownMessageIds.add(msgid); const screenId = searchScreenId(parents, sourceFile); formatScreenId(sourceFile, outChunks, screenId); - formatMsgComment(sourceFile, outChunks, line, comment); + formatMsgComment(projectPrefix, sourceFile, outChunks, line, comment); formatMsgLine(outChunks, "msgctxt", path.ctx); formatMsgLine(outChunks, "msgid", t1.template); formatMsgLine(outChunks, "msgid_plural", t2.template); @@ -552,7 +554,7 @@ function processNode( knownMessageIds.add(msgid); const screenId = searchScreenId(parents, sourceFile); formatScreenId(sourceFile, outChunks, screenId); - formatMsgComment(sourceFile, outChunks, line, comment); + formatMsgComment(projectPrefix, sourceFile, outChunks, line, comment); formatMsgLine(outChunks, "msgctxt", context); formatMsgLine(outChunks, "msgid", template); outChunks.push(`msgstr ""\n`); @@ -571,15 +573,14 @@ function processNode( sourceFile, outChunks, knownMessageIds, + projectPrefix ); }); } -export function processFile2(sourceFile: ts.SourceFile): string { - // let lastTokLine = 0; - // let preLastTokLine = 0; +export function processFileForTesting(sourceFile: ts.SourceFile): string { const result: string[] = new Array<string>(); - processNode([], sourceFile, 0, 0, sourceFile, result, new Set<string>()); + processNode([], sourceFile, 0, 0, sourceFile, result, new Set<string>(), ""); return result.join(""); } @@ -587,10 +588,25 @@ export function processFile( sourceFile: ts.SourceFile, outChunks: string[], knownMessageIds: Set<string>, + projectPrefix: string, ) { // let lastTokLine = 0; // let preLastTokLine = 0; - processNode([], sourceFile, 0, 0, sourceFile, outChunks, knownMessageIds); + processNode([], sourceFile, 0, 0, sourceFile, outChunks, knownMessageIds, projectPrefix); +} + +function searchIntoParents(directory: string, fileFlag: string) { + if (!path.isAbsolute(directory)) { + return searchIntoParents(path.join(process.cwd(), directory), fileFlag); + } + const parent = path.dirname(directory); + if (fs.existsSync(path.join(directory, fileFlag))) { + return directory; + } + if (parent === directory) { + return directory; + } + return searchIntoParents(parent, fileFlag); } export function potextract() { @@ -637,12 +653,13 @@ export function potextract() { header = DEFAULT_PO_HEADER; } - const chunks = [header]; + const gitRoot = searchIntoParents(process.cwd(), ".git"); + const chunks = [header]; const knownMessageIds = new Set<string>(); for (const f of ownFiles) { - processFile(f, chunks, knownMessageIds); + processFile(f, chunks, knownMessageIds, gitRoot); } const pot = chunks.join("");