-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
51 changed files
with
185 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#!/usr/bin/env ts-node-script | ||
import { mkdir, writeFile } from "fs/promises"; | ||
import path from "node:path"; | ||
import { dirname } from "path"; | ||
import { getPackageFileContent, PackageSchema } from "../src"; | ||
import { readXml } from "../src/package-xml"; | ||
import { findCaptionsAndDescriptions, getWidgetXMLName, type Result } from "../src/widget-file-xml"; | ||
async function main(): Promise<void> { | ||
console.log(`Creating translation i18n format...`); | ||
const path = process.cwd(); | ||
|
||
const raw = await getPackageFileContent(path); | ||
// To get better error output from zod use empty objects | ||
const target = { | ||
mxpackage: {}, | ||
marketplace: {}, | ||
repository: {}, | ||
testProject: {}, | ||
...raw | ||
}; | ||
|
||
// First, check common fields | ||
const info = PackageSchema.parse(target); | ||
|
||
switch (info.mxpackage.type) { | ||
case "widget": { | ||
createTranslation(path); | ||
break; | ||
} | ||
case "module": { | ||
// TODO: ? | ||
break; | ||
} | ||
case "jsactions": { | ||
// TODO: ? | ||
break; | ||
} | ||
} | ||
} | ||
|
||
export async function createTranslation(cwd: string) { | ||
const widgetXMLName = await getWidgetXMLName(cwd); | ||
if (widgetXMLName) { | ||
const obj = await readXml(path.join(cwd, "src", widgetXMLName)); | ||
const res = findCaptionsAndDescriptions(obj); | ||
let fileName = "result"; | ||
if (obj && typeof obj === "object" && obj["widget"] && obj["widget"]["@_id"]) { | ||
fileName = obj["widget"]["@_id"].toLowerCase(); | ||
} | ||
writeResultToFile(res, `dist/locales/en-US/${fileName}.json`); | ||
writeResultToFile(res, `../../../dist/locales/en-US/${fileName}.json`); | ||
} | ||
} | ||
|
||
// Function to ensure a directory exists | ||
async function ensureDirectoryExists(filePath: string): Promise<void> { | ||
const dir = dirname(filePath); | ||
await mkdir(dir, { recursive: true }); | ||
} | ||
|
||
// Function to write JSON result to a file | ||
async function writeResultToFile(result: Result, filename: string): Promise<void> { | ||
await ensureDirectoryExists(filename); | ||
const jsonContent = JSON.stringify(result, null, 2); | ||
await writeFile(filename, jsonContent, "utf-8"); | ||
console.log(`JSON result has been written to ${filename}`); | ||
} | ||
|
||
main().catch(e => { | ||
console.error(e); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { readPackageXml } from "./package-xml"; | ||
export interface Result { | ||
[key: string]: string; | ||
} | ||
|
||
export async function getWidgetXMLName(cwd: string): Promise<string | undefined> { | ||
try { | ||
const packageXml = (await readPackageXml(cwd)) as { | ||
package?: { | ||
clientModule?: { | ||
widgetFiles: any; | ||
}; | ||
}; | ||
}; | ||
return packageXml?.package?.clientModule?.widgetFiles.widgetFile["@_path"]; | ||
} catch { | ||
return undefined; | ||
} | ||
} | ||
|
||
// Function to recursively find all captions and descriptions | ||
export function findCaptionsAndDescriptions(obj: any, result: Result = {}): Result { | ||
for (const key in obj) { | ||
if (typeof obj[key] === "object") { | ||
findCaptionsAndDescriptions(obj[key], result); | ||
} else if (obj[key].trim() !== "") { | ||
if ( | ||
key.includes("@_caption") || | ||
key.toLowerCase().includes("caption") || | ||
key.toLowerCase().includes("description") | ||
) { | ||
if (!result[obj[key]]) { | ||
result[obj[key]] = obj[key]; | ||
} | ||
} | ||
} | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.