@channel.io/tolgee-cli
Version:
Tolgee CLI tool for managing translations
81 lines (80 loc) • 2.75 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.processAllFiles = processAllFiles;
exports.pullJson = pullJson;
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
/**
* JSON 객체에서 null 값을 빈 문자열로 변환하는 함수
*/
function convertNullToEmptyString(obj) {
if (obj === null) {
return "";
}
if (Array.isArray(obj)) {
return obj.map(convertNullToEmptyString);
}
if (typeof obj === "object" && obj !== null) {
const result = {};
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
result[key] = convertNullToEmptyString(obj[key]);
}
}
return result;
}
return obj;
}
/**
* JSON 파일 내용을 포맷팅하는 함수
*/
function formatJsonContent(jsonContent) {
// null 값을 빈 문자열로 변환
const processedContent = convertNullToEmptyString(jsonContent);
return JSON.stringify(processedContent, null, 2);
}
/**
* 모든 JSON 파일을 처리하는 함수
*/
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);
// JSON 포맷팅
const content = formatJsonContent(jsonContent);
// 파일명에서 확장자 제거하고 새 확장자 추가
const fileName = path_1.default.parse(file.name).name;
const outputPath = path_1.default.join(outputDir, `${fileName}.json`);
// 원본 JSON 파일 제거
await fs_extra_1.default.remove(file.path);
// 포맷팅된 JSON 파일 저장
await fs_extra_1.default.writeFile(outputPath, content + "\n");
resultFiles.push(outputPath);
}
catch (error) {
console.error(`파일 처리 중 오류 발생 (${file.name}):`, error);
}
}
return resultFiles;
}
/**
* JSON 파일을 pull하는 메인 함수
*/
async function pullJson(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;
}