@cliz/translate-weekly
Version:
Docschina weekly translate tool
66 lines (65 loc) • 2.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLatestPost = exports.release = void 0;
const cli_1 = require("@cliz/cli");
const config_1 = require("../config");
async function release(period, latestPost, logger) {
const { latest, histories } = await cli_1.api.fs.json.load(config_1.default.dataPath);
const originLatestPeriod = parseInt(latest.period);
if (originLatestPeriod >= period) {
throw new Error(`第 ${period} 已发布,请检查后再发布`);
}
const _lastest = latestPost;
const _histories = [
latest,
...histories,
]
.sort((a, b) => {
return parseInt(b.period) - parseInt(a.period);
});
const _data = {
latest: _lastest,
histories: _histories,
};
await cli_1.api.fs.writeFile(config_1.default.dataPath, JSON.stringify(_data, null, 2), { encoding: 'utf-8' });
logger.info('4. 提交发布内容 ...');
await cli_1.api.$ `git add .`;
await cli_1.api.$ `git commit -m "chore: release ${period}"`;
const tag = `@translate/${period}`;
logger.info(`5. 创建发布版本: ${tag} ...`);
await cli_1.api.$ `git tag ${tag}`;
logger.info('6. 开始发布 ...');
await cli_1.api.$ `git push origin ${tag}`;
await cli_1.api.$ `git push origin ${config_1.default.branch.master}`;
logger.info('7. ✅ 发布完成');
}
exports.release = release;
async function getLatestPost(period) {
var _a, _b, _c, _d, _e;
const titleRegex = /^title:([^\n]+)/m;
const bannerRegex = /!\[\]\(([^\]]+)\)/m;
const currentMdPath = cli_1.api.path.join(config_1.default.docsDir, `${period}.md`);
const text = await cli_1.api.fs.readFile(currentMdPath, { encoding: 'utf-8' });
let title;
let banner;
if (!titleRegex.test(text)) {
throw new Error(`title[1]: Cannot found title in docs/${period}.md`);
}
title = (_d = (_c = (_b = (_a = titleRegex.exec(text)) === null || _a === void 0 ? void 0 : _a[1]) === null || _b === void 0 ? void 0 : _b.split('-')) === null || _c === void 0 ? void 0 : _c[1]) === null || _d === void 0 ? void 0 : _d.trim();
if (!title) {
throw new Error(`title[2]: Cannot found title in docs/${period}.md`);
}
if (!bannerRegex.test(text)) {
throw new Error(`banner[1]: Cannot found banner in docs/${period}.md`);
}
banner = (_e = bannerRegex.exec(text)) === null || _e === void 0 ? void 0 : _e[1];
if (!banner) {
throw new Error(`banner[2]: Cannot found banner in docs/${period}.md`);
}
return {
period,
title,
banner,
};
}
exports.getLatestPost = getLatestPost;