| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import fetch from 'node-fetch';
- import fs from 'fs';
- import path from 'path';
- import unzipper from 'unzipper';
- import { pipeline } from 'node:stream';
- import { promisify } from 'node:util';
- import { createReadStream, createWriteStream } from 'node:fs';
- const streamPipeline = promisify(pipeline);
- /**
- * 下载并解压离线包
- * @param {string} url 下载地址
- * @param {string} destDir 解压目标目录
- */
- async function downloadAndExtract(url, destDir) {
- const zipPath = path.join(destDir, 'temp.zip');
- console.log(`开始下载语言包: ${url}`);
- const res = await fetch(url, {
- method: 'GET',
- headers: {
- //'X-API-Key':'tgpak_gm2f6ntnor2gy4ztnfuw65twoj2wu2tdovzwwztqoe4ts5q'
- },
- });
- if (!res.ok) {
- throw new Error(`下载语言包失败: ${res.status} ${res.statusText}`);
- }
- // zip文件保存到本地目录
- await streamPipeline(res.body, createWriteStream(zipPath));
- console.log('语言包下载完成,开始解压...');
- // 解压到指定目录
- await streamPipeline(createReadStream(zipPath), unzipper.Extract({ path: destDir }));
- console.log('语言包解压完成!');
- // 删除临时压缩包
- fs.unlinkSync(zipPath);
- }
- // 示例调用
- const url =
- 'http://192.168.0.211:9012/v2/projects/export?ak=tgpak_gqzf64twmzswcnzwoztgim3rn52hayjyomzwwythgbugumy';
- const dest = './src/locales/lang';
- await downloadAndExtract(url, dest);
|