Comparing version 0.0.2 to 0.0.3
@@ -14,2 +14,5 @@ #!/usr/bin/env node | ||
}; | ||
const warn = (obj) => { | ||
console.log('\x1B[33m', `【${(0, dayjs_1.default)().format('YYYY-MM-DD hh:mm:ss.SSS')}】【tant-intl】${obj.stack || obj}`, '\x1B[37m'); | ||
}; | ||
const success = (obj) => { | ||
@@ -25,3 +28,4 @@ console.log('\x1B[32m', `【${(0, dayjs_1.default)().format('YYYY-MM-DD hh:mm:ss.SSS')}】【tant-intl】${obj.stack || obj}`, '\x1B[37m'); | ||
_hasData = false; | ||
_keyStr = ''; | ||
_keyStr = {}; | ||
_loading = false; | ||
constructor() { | ||
@@ -46,8 +50,15 @@ const config = JSON.parse((0, fs_1.readFileSync)(`${projectDir}/intl.json`).toString()); | ||
init = async () => { | ||
this._loading = true; | ||
this._lanMap = {}; | ||
await Promise.all(this._projectIds.map(pid => this._queryList(pid))); | ||
await this._saveToFile(); | ||
this._loading = false; | ||
}; | ||
listen = async (cb) => { | ||
setInterval(async () => { | ||
if (this._loading) { | ||
warn('本次更新堵塞,下次周期重试'); | ||
return; | ||
} | ||
this._loading = true; | ||
this._hasData = false; | ||
@@ -59,2 +70,3 @@ await Promise.all(this._projectIds.map(pid => this._queryList(pid))); | ||
} | ||
this._loading = false; | ||
}, 30000); | ||
@@ -83,8 +95,5 @@ }; | ||
}); | ||
if (!(0, fs_1.existsSync)(`${projectDir}/public`)) { | ||
(0, fs_1.mkdirSync)(`${projectDir}/public`); | ||
} | ||
const path = `${projectDir}/public/intl`; | ||
if (!(0, fs_1.existsSync)(path)) { | ||
(0, fs_1.mkdirSync)(path); | ||
(0, fs_1.mkdirSync)(path, { recursive: true }); | ||
} | ||
@@ -104,14 +113,3 @@ (0, fs_1.readdirSync)(path).forEach((fileName) => { | ||
_queryList = async (pid) => { | ||
const lanList = await superagent_1.default | ||
.get(`https://api.phrase.com/v2/projects/${pid}/locales?per_page=100`) | ||
.set('Authorization', `token ${this._token}`) | ||
.then((response) => { | ||
if (response.status === 200) { | ||
return response.body || []; | ||
} | ||
return []; | ||
}) | ||
.catch((err) => { | ||
return []; | ||
}); | ||
const lanList = JSON.parse((await this.request(`https://api.phrase.com/v2/projects/${pid}/locales?per_page=100`)) || ''); | ||
let updateLan = []; | ||
@@ -139,19 +137,7 @@ let otherLan = []; // 不论翻译又没有变,每次都需要比较一次中文集合中key有无变化,有变化的话就需要全部重新加载 | ||
}); | ||
let allList = await Promise.all((updateLan).map(lan => superagent_1.default | ||
.get(`https://api.phrase.com/v2/projects/${pid}/locales/${lan.id}/download?tags=${this._tags}&file_format=csv&format_options[export_tags]=true&format_options[export_max_characters_allowed]=true`) | ||
.set('Authorization', `token ${this._token}`) | ||
.then((response) => { | ||
if (response.status === 200) { | ||
this._lanMap[lan.id].updateTime = lan.updateTime; | ||
return response.text || ''; | ||
} | ||
return ''; | ||
}) | ||
.catch((err) => { | ||
console.log(err); | ||
return ''; | ||
}))); | ||
let keyStr = this._keyStr; | ||
let allList = await Promise.all((updateLan).map(lan => this.request(`https://api.phrase.com/v2/projects/${pid}/locales/${lan.id}/download?tags=${this._tags}&file_format=csv&format_options[export_tags]=true&format_options[export_max_characters_allowed]=true`))); | ||
let keyStr = this._keyStr[pid]; | ||
for (let i = 0; i < allList.length; i += 1) { | ||
const lan = updateLan[i]; | ||
this._lanMap[lan.id].updateTime = lan.updateTime; | ||
const list = await csvtojsonV2().fromString(allList[i]); | ||
@@ -166,18 +152,5 @@ if (lan.code === 'zh-CN') { | ||
} | ||
if (keyStr !== this._keyStr) { // key变化或tag变化 | ||
this._keyStr = keyStr; | ||
allList = await Promise.all((otherLan).map(lan => superagent_1.default | ||
.get(`https://api.phrase.com/v2/projects/${pid}/locales/${lan.id}/download?tags=${this._tags}&file_format=csv&format_options[export_tags]=true&format_options[export_max_characters_allowed]=true`) | ||
.set('Authorization', `token ${this._token}`) | ||
.then((response) => { | ||
if (response.status === 200) { | ||
this._lanMap[lan.id].updateTime = lan.updateTime; | ||
return response.text || ''; | ||
} | ||
return ''; | ||
}) | ||
.catch((err) => { | ||
console.log(err); | ||
return ''; | ||
}))); | ||
if (keyStr !== this._keyStr[pid]) { // key变化或tag变化 | ||
this._keyStr[pid] = keyStr; | ||
allList = await Promise.all((otherLan).map(lan => this.request(`https://api.phrase.com/v2/projects/${pid}/locales/${lan.id}/download?tags=${this._tags}&file_format=csv&format_options[export_tags]=true&format_options[export_max_characters_allowed]=true`))); | ||
for (let i = 0; i < allList.length; i += 1) { | ||
@@ -191,2 +164,32 @@ this._hasData = true; | ||
}; | ||
request = (path) => new Promise((r) => { | ||
superagent_1.default | ||
.get(path) | ||
.set('Authorization', `token ${this._token}`) | ||
.then((response) => { | ||
if (response.status === 200) { | ||
r(response.text || response.body); | ||
return ''; | ||
} | ||
r(''); | ||
return ''; | ||
}) | ||
.catch((err) => { | ||
if (err?.response?.status === 429) { | ||
const date = Number(err?.response?.header['x-rate-limit-reset'] || 0) * 1000; | ||
const now = Date.now(); | ||
if (date) { | ||
setTimeout(async () => { | ||
r(await this.request(path)); | ||
}, date - now + 500); | ||
} | ||
else { | ||
r(''); | ||
} | ||
return ''; | ||
} | ||
r(''); | ||
return ''; | ||
}); | ||
}); | ||
} | ||
@@ -193,0 +196,0 @@ const cli = new Cli(); |
143
lib/cli.js
@@ -50,2 +50,5 @@ #!/usr/bin/env node | ||
}; | ||
var warn = function (obj) { | ||
console.log('\x1B[33m', "\u3010".concat((0, dayjs_1.default)().format('YYYY-MM-DD hh:mm:ss.SSS'), "\u3011\u3010tant-intl\u3011").concat(obj.stack || obj), '\x1B[37m'); | ||
}; | ||
var success = function (obj) { | ||
@@ -64,3 +67,4 @@ console.log('\x1B[32m', "\u3010".concat((0, dayjs_1.default)().format('YYYY-MM-DD hh:mm:ss.SSS'), "\u3011\u3010tant-intl\u3011").concat(obj.stack || obj), '\x1B[37m'); | ||
this._hasData = false; | ||
this._keyStr = ''; | ||
this._keyStr = {}; | ||
this._loading = false; | ||
this.init = function () { return __awaiter(_this, void 0, void 0, function () { | ||
@@ -71,2 +75,3 @@ var _this = this; | ||
case 0: | ||
this._loading = true; | ||
this._lanMap = {}; | ||
@@ -79,2 +84,3 @@ return [4 /*yield*/, Promise.all(this._projectIds.map(function (pid) { return _this._queryList(pid); }))]; | ||
_a.sent(); | ||
this._loading = false; | ||
return [2 /*return*/]; | ||
@@ -92,2 +98,7 @@ } | ||
case 0: | ||
if (this._loading) { | ||
warn('本次更新堵塞,下次周期重试'); | ||
return [2 /*return*/]; | ||
} | ||
this._loading = true; | ||
this._hasData = false; | ||
@@ -103,3 +114,5 @@ return [4 /*yield*/, Promise.all(this._projectIds.map(function (pid) { return _this._queryList(pid); }))]; | ||
_a.label = 3; | ||
case 3: return [2 /*return*/]; | ||
case 3: | ||
this._loading = false; | ||
return [2 /*return*/]; | ||
} | ||
@@ -136,8 +149,5 @@ }); | ||
}); | ||
if (!(0, fs_1.existsSync)("".concat(projectDir, "/public"))) { | ||
(0, fs_1.mkdirSync)("".concat(projectDir, "/public")); | ||
} | ||
path = "".concat(projectDir, "/public/intl"); | ||
if (!(0, fs_1.existsSync)(path)) { | ||
(0, fs_1.mkdirSync)(path); | ||
(0, fs_1.mkdirSync)(path, { recursive: true }); | ||
} | ||
@@ -159,20 +169,11 @@ (0, fs_1.readdirSync)(path).forEach(function (fileName) { | ||
this._queryList = function (pid) { return __awaiter(_this, void 0, void 0, function () { | ||
var lanList, updateLan, otherLan, allList, keyStr, i, lan, list, i, lan, list; | ||
var lanList, _a, _b, updateLan, otherLan, allList, keyStr, i, lan, list, i, lan, list; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, superagent_1.default | ||
.get("https://api.phrase.com/v2/projects/".concat(pid, "/locales?per_page=100")) | ||
.set('Authorization', "token ".concat(this._token)) | ||
.then(function (response) { | ||
if (response.status === 200) { | ||
return response.body || []; | ||
} | ||
return []; | ||
}) | ||
.catch(function (err) { | ||
return []; | ||
})]; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
_b = (_a = JSON).parse; | ||
return [4 /*yield*/, this.request("https://api.phrase.com/v2/projects/".concat(pid, "/locales?per_page=100"))]; | ||
case 1: | ||
lanList = _a.sent(); | ||
lanList = _b.apply(_a, [(_c.sent()) || '']); | ||
updateLan = []; | ||
@@ -201,27 +202,17 @@ otherLan = []; | ||
}); | ||
return [4 /*yield*/, Promise.all((updateLan).map(function (lan) { return superagent_1.default | ||
.get("https://api.phrase.com/v2/projects/".concat(pid, "/locales/").concat(lan.id, "/download?tags=").concat(_this._tags, "&file_format=csv&format_options[export_tags]=true&format_options[export_max_characters_allowed]=true")) | ||
.set('Authorization', "token ".concat(_this._token)) | ||
.then(function (response) { | ||
if (response.status === 200) { | ||
_this._lanMap[lan.id].updateTime = lan.updateTime; | ||
return response.text || ''; | ||
} | ||
return ''; | ||
}) | ||
.catch(function (err) { | ||
console.log(err); | ||
return ''; | ||
}); }))]; | ||
return [4 /*yield*/, Promise.all((updateLan).map(function (lan) { | ||
return _this.request("https://api.phrase.com/v2/projects/".concat(pid, "/locales/").concat(lan.id, "/download?tags=").concat(_this._tags, "&file_format=csv&format_options[export_tags]=true&format_options[export_max_characters_allowed]=true")); | ||
}))]; | ||
case 2: | ||
allList = _a.sent(); | ||
keyStr = this._keyStr; | ||
allList = _c.sent(); | ||
keyStr = this._keyStr[pid]; | ||
i = 0; | ||
_a.label = 3; | ||
_c.label = 3; | ||
case 3: | ||
if (!(i < allList.length)) return [3 /*break*/, 6]; | ||
lan = updateLan[i]; | ||
this._lanMap[lan.id].updateTime = lan.updateTime; | ||
return [4 /*yield*/, csvtojsonV2().fromString(allList[i])]; | ||
case 4: | ||
list = _a.sent(); | ||
list = _c.sent(); | ||
if (lan.code === 'zh-CN') { | ||
@@ -234,3 +225,3 @@ keyStr = JSON.stringify(list.map(function (v) { var _a; return ({ key: (_a = v.key_name) === null || _a === void 0 ? void 0 : _a.trim(), tags: v.tags }); })); | ||
this._lanMap[lan.id].list = list; | ||
_a.label = 5; | ||
_c.label = 5; | ||
case 5: | ||
@@ -240,22 +231,11 @@ i += 1; | ||
case 6: | ||
if (!(keyStr !== this._keyStr)) return [3 /*break*/, 11]; | ||
this._keyStr = keyStr; | ||
return [4 /*yield*/, Promise.all((otherLan).map(function (lan) { return superagent_1.default | ||
.get("https://api.phrase.com/v2/projects/".concat(pid, "/locales/").concat(lan.id, "/download?tags=").concat(_this._tags, "&file_format=csv&format_options[export_tags]=true&format_options[export_max_characters_allowed]=true")) | ||
.set('Authorization', "token ".concat(_this._token)) | ||
.then(function (response) { | ||
if (response.status === 200) { | ||
_this._lanMap[lan.id].updateTime = lan.updateTime; | ||
return response.text || ''; | ||
} | ||
return ''; | ||
}) | ||
.catch(function (err) { | ||
console.log(err); | ||
return ''; | ||
}); }))]; | ||
if (!(keyStr !== this._keyStr[pid])) return [3 /*break*/, 11]; | ||
this._keyStr[pid] = keyStr; | ||
return [4 /*yield*/, Promise.all((otherLan).map(function (lan) { | ||
return _this.request("https://api.phrase.com/v2/projects/".concat(pid, "/locales/").concat(lan.id, "/download?tags=").concat(_this._tags, "&file_format=csv&format_options[export_tags]=true&format_options[export_max_characters_allowed]=true")); | ||
}))]; | ||
case 7: | ||
allList = _a.sent(); | ||
allList = _c.sent(); | ||
i = 0; | ||
_a.label = 8; | ||
_c.label = 8; | ||
case 8: | ||
@@ -267,5 +247,5 @@ if (!(i < allList.length)) return [3 /*break*/, 11]; | ||
case 9: | ||
list = _a.sent(); | ||
list = _c.sent(); | ||
this._lanMap[lan.id].list = list; | ||
_a.label = 10; | ||
_c.label = 10; | ||
case 10: | ||
@@ -278,2 +258,43 @@ i += 1; | ||
}); }; | ||
this.request = function (path) { return new Promise(function (r) { | ||
superagent_1.default | ||
.get(path) | ||
.set('Authorization', "token ".concat(_this._token)) | ||
.then(function (response) { | ||
if (response.status === 200) { | ||
r(response.text || response.body); | ||
return ''; | ||
} | ||
r(''); | ||
return ''; | ||
}) | ||
.catch(function (err) { | ||
var _a, _b; | ||
if (((_a = err === null || err === void 0 ? void 0 : err.response) === null || _a === void 0 ? void 0 : _a.status) === 429) { | ||
var date = Number(((_b = err === null || err === void 0 ? void 0 : err.response) === null || _b === void 0 ? void 0 : _b.header['x-rate-limit-reset']) || 0) * 1000; | ||
var now = Date.now(); | ||
if (date) { | ||
setTimeout(function () { return __awaiter(_this, void 0, void 0, function () { | ||
var _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
_a = r; | ||
return [4 /*yield*/, this.request(path)]; | ||
case 1: | ||
_a.apply(void 0, [_b.sent()]); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }, date - now + 500); | ||
} | ||
else { | ||
r(''); | ||
} | ||
return ''; | ||
} | ||
r(''); | ||
return ''; | ||
}); | ||
}); }; | ||
var config = JSON.parse((0, fs_1.readFileSync)("".concat(projectDir, "/intl.json")).toString()); | ||
@@ -280,0 +301,0 @@ if (!config) { |
{ | ||
"name": "tant-intl", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "数数科技依托于phrase平台的多语言组件", | ||
@@ -5,0 +5,0 @@ "main": "es/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36923
725