UNPKG

@cliz/translate-weekly

Version:

Docschina weekly translate tool

110 lines (109 loc) 3.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.pr = exports.commit = exports.start = void 0; const cli_1 = require("@cliz/cli"); const config_1 = require("../config"); const common_1 = require("./common"); async function start(period, logger) { logger.info('拉取远程更新 ...'); await cli_1.api.$ `git checkout ${config_1.default.branch.master}`; await cli_1.api.$ `git fetch origin`; 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 cli_1.api.fs.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 = `lint/${period}`; logger.info(`切换到 Lint 分支 ${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('✅ 请开始你的 Lint'); } 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(lint): ${period}.md, remove the AD, job, sponsor and useless blank lines\"`; logger.info('4. ✅ 提交 Lint 完成 ...'); } 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 = `lint/${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(lint): ${period}.md, remove the AD, job, sponsor and useless blank lines\" -b 'See Files'`; logger.info('3. ✅ 发起 PR 完成,请在浏览器端操作'); } exports.pr = pr;