UNPKG

@channel.io/tolgee-cli

Version:

Tolgee CLI tool for managing translations

92 lines (91 loc) 3.18 kB
#!/usr/bin/env node "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.processAllFiles = processAllFiles; exports.pullXml = pullXml; const string_utils_1 = require("../utils/string.utils"); const xml_js_1 = require("xml-js"); const fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); /** * JSON 파일을 XML로 변환하는 함수 */ function convertJsonToXml(jsonContent) { const result = Object.entries(jsonContent) .map(([key, value]) => [key, value ?? ""]) .filter(([key, value]) => typeof value === "string") .map(([key, value]) => [key, value]) .map(([key, value]) => [key.replace(/-/g, "_"), value]) .map(([key, value]) => [key, (0, string_utils_1.escapeStr)(value)]) .map(([key, value]) => { let v = { _text: value }; if ((0, string_utils_1.includesHtmlTag)(value)) { v = { _cdata: value }; } return { _attributes: { name: key, formatted: (0, string_utils_1.includesFormatStringMoreThanOne)(value) ? "false" : undefined, }, ...v, }; }); const xml = (0, xml_js_1.js2xml)({ _declaration: { _attributes: { version: "1.0", encoding: "UTF-8", }, }, resources: { string: result, }, }, { compact: true, spaces: 2, }); return xml; } /** * 모든 파일을 처리하는 함수 */ async function processAllFiles(files, outputDir) { const resultFiles = []; for (const file of files) { try { // JSON 파일 내용 읽기 const jsonFile = await fs_extra_1.default.readFile(file.path, "utf-8"); const jsonContent = JSON.parse(jsonFile); // 지정된 포맷으로 변환 const content = convertJsonToXml(jsonContent); // 파일명에서 확장자 제거하고 새 확장자 추가 const fileName = path_1.default.parse(file.name).name; const outputPath = path_1.default.join(outputDir, `${fileName}.xml`); // 변환된 파일 저장 await fs_extra_1.default.writeFile(outputPath, content + "\n"); // 원본 JSON 파일 제거 await fs_extra_1.default.remove(file.path); resultFiles.push(outputPath); } catch (error) { console.error(`파일 처리 중 오류 발생 (${file.name}):`, error); } } return resultFiles; } async function pullXml(tolgee, options) { const { files } = await tolgee.extractAndSaveFiles({ outputDir: options.outputDir, projectId: options.projectId, tags: options.tags, excludeTags: options.excludeTags, }); // 모든 파일 처리 const resultFiles = await processAllFiles(files, options.outputDir); return resultFiles; }