@cliz/translate-weekly
Version:
Docschina weekly translate tool
111 lines (110 loc) • 3.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.pr = exports.commit = exports.start = void 0;
const cli_1 = require("@cliz/cli");
const fs = require("fs");
const config_1 = require("../config");
const common_1 = require("./common");
async function start(period, logger) {
logger.info('拉取远程更新 ...');
await cli_1.api.$ `git fetch origin`;
await cli_1.api.$ `git checkout ${config_1.default.branch.master}`;
await cli_1.api.$ `git merge origin/${config_1.default.branch.master}`;
if (!period) {
const files = await cli_1.api.fs.listDir(config_1.default.docsDir);
const docs = await Promise.all(files
.filter(e => /\.md$/.test(e.name))
.map(async (file) => {
const stat = await fs.promises.stat(file.absolutePath);
const content = await cli_1.api.fs.readFile(file.absolutePath, { encoding: 'utf-8' });
const isTranslated = /> 编辑/.test(content.slice(0, 512));
return {
period: +file.name.replace('.md', ''),
isTranslated,
createdAt: cli_1.doreamon.date(stat.ctimeMs).format('YYYY-MM-DD'),
};
}));
const periods = docs
.sort((a, b) => {
return b.period - a.period;
})
.map(e => {
return {
name: `第 ${e.period} 期`,
value: e.period,
};
});
const answers = await cli_1.inquirer.prompt([
{
type: 'list',
name: 'period',
message: '请选择期数',
choices: periods,
default: periods[0],
loop: false,
},
]);
period = answers.period;
}
const branch = `translate/${period}`;
logger.info(`切换到 Translate 分支 ${branch} ...`);
try {
await cli_1.api.$ `git rev-parse --verify ${branch}`;
await cli_1.api.$ `git checkout ${branch}`;
}
catch (error) {
await cli_1.api.$ `git checkout -b ${branch} origin/${config_1.default.branch.master}`;
}
logger.info('更新最新分支数据 ...');
try {
await (0, common_1.setLastestPeriod)(period);
}
catch (error) {
logger.error(`请确认当前路径是否在翻译根目录`);
process.exit(1);
}
logger.info('✅ 请开始你的 Translate');
}
exports.start = start;
async function commit(logger) {
let period;
try {
period = await (0, common_1.getLastestPeriod)();
}
catch (error) {
logger.error(`请确认当前路径是否在翻译根目录`);
process.exit(1);
}
logger.info('1. 添加最新期数 ...');
await cli_1.api.$ `git add .LATEST`;
logger.info(`2. 添加 ${period}.md ...`);
await cli_1.api.$ `git add docs/${period}.md`;
logger.info('3. 添加备注 ...');
await cli_1.api.$ `git commit -m \"chore(translate): ${period}.md\"`;
logger.info('4. ✅ 提交 Translate 完成 ...');
}
exports.commit = commit;
async function pr(logger) {
let period;
try {
period = await (0, common_1.getLastestPeriod)();
}
catch (error) {
logger.error(`请确认当前路径是否在翻译根目录`);
process.exit(1);
}
const branch = `translate/${period}`;
try {
await cli_1.api.$ `which gh`;
}
catch (error) {
logger.error(`GitHub CLI not found, please install from https://cli.github.com/`);
process.exit(1);
}
logger.info('1. 提交分支到远程 ...');
await cli_1.api.$ `git push origin ${branch}`;
logger.info('2. 发起 PR ...');
await cli_1.api.$ `gh pr create -w -t \"chore(translate): ${period}.md\" -b 'See Files'`;
logger.info('3. ✅ 发起 PR 完成,请在浏览器端操作');
}
exports.pr = pr;