@channel.io/tolgee-cli
Version:
Tolgee CLI tool for managing translations
90 lines (89 loc) • 3.5 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TolgeeService = void 0;
const axios_1 = __importDefault(require("axios"));
const adm_zip_1 = __importDefault(require("adm-zip"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
class TolgeeService {
constructor(options) {
const { apiKey, apiUrl } = options;
this.client = axios_1.default.create({
baseURL: `${apiUrl}/v2`,
headers: {
"X-API-Key": apiKey,
},
});
}
async exportData(projectId, { tags, excludeTags, }) {
const response = await this.client.get(`/projects/${projectId}/export`, {
responseType: "arraybuffer",
params: {
format: "JSON",
structureDelimiter: "",
messageFormat: "C_SPRINTF",
filterTagIn: tags,
filterTagNotIn: excludeTags,
filterState: "TRANSLATED,UNTRANSLATED,REVIEWED",
},
});
return response;
}
async extractAndSaveFiles({ projectId, outputDir, tags, excludeTags, }) {
try {
const response = await this.exportData(projectId, {
tags,
excludeTags,
});
// zip 데이터를 Buffer로 변환
const zipBuffer = Buffer.from(response.data);
// 임시 폴더 및 출력 폴더 생성
const extractDir = path_1.default.join(process.cwd(), outputDir);
await fs_extra_1.default.ensureDir(extractDir);
// zip 파일을 임시로 저장
const zipPath = path_1.default.join(extractDir, "export.zip");
await fs_extra_1.default.writeFile(zipPath, zipBuffer);
// zip 파일 해체
const zip = new adm_zip_1.default(zipPath);
zip.extractAllTo(extractDir, true);
// get file names
const fileNames = zip.getEntries().map((entry) => entry.name);
// 해체된 파일들 목록 수집 및 출력
const fileInfo = [];
for (const file of fileNames) {
const filePath = path_1.default.join(extractDir, file);
const stats = await fs_extra_1.default.stat(filePath);
if (stats.isDirectory()) {
const subFiles = await fs_extra_1.default.readdir(filePath);
for (const subFile of subFiles) {
fileInfo.push({
name: `${file}/${subFile}`,
path: path_1.default.join(filePath, subFile),
});
}
}
else {
const info = {
name: file.replace(".json", ""),
path: filePath,
};
fileInfo.push(info);
}
}
// 임시 파일 정리
await fs_extra_1.default.remove(zipPath);
return {
extractedPath: extractDir,
files: fileInfo,
};
}
catch (error) {
console.error(error);
throw error;
}
}
}
exports.TolgeeService = TolgeeService;