UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

174 lines (150 loc) 7.39 kB
#!/usr/bin/env node const { program } = require('commander'); const { mpUploadAndReportByOptions, writeEnvAndPrivateKeyByOptions, cleanChangelogScript, } = require('../lib'); const { version } = require('../package.json'); const { deployGithubPage } = require('./deploy-github-page'); const { deployPagesCommand } = require('./deploy-pages'); const { mergeBranchesCommand } = require('./merge-branches'); const { publish } = require('./publish'); const { uploadFile, uploadFromUrl, printResult } = require('./upload-cdn'); program .name('t-comm') .description('CLI for t-comm') .version(version); program.command('publish') .description('Publish to server with bash script') // .argument('<string>', 'string to split') // .option('--first', 'display just the first substring') .option('-s, --source <string>', 'source dir or file') .option('-t, --target <string>', 'target dir') .option('-n, --name <ip>', 'server host name') .option('-p, --password <string>', 'server host password') .option('-o, --port <number>', 'server port', 36000) .action(publish); program.command('mp-env') .description('Write Env and Private Key to File') // base .option('-b, --branch <string>', 'git branch') .option('-e --env <string>', 'publish env', 'test') .option('-r --root <string>', 'target file path', process.cwd()) // rainbow .option('-k, --configKey <string>', 'rainbow config key') .option('-a, --appid <string>', 'rainbow app id') .option('--envName <string>', 'rainbow env name', 'Default') .option('-g, --groupName <string>', 'rainbow group name') .action(writeEnvAndPrivateKeyByOptions); program.command('mp-upload') .description('Upload MiniProgram Bundle and Report') // base .option('-b, --branch <string>', 'git branch') .option('-e --env <string>', 'publish env', 'test') .option('-r --root <string>', 'target file path', process.cwd()) // rainbow .option('-k, --configKey <string>', 'rainbow config key') .option('-a, --appid <string>', 'rainbow app id') .option('--envName <string>', 'rainbow env name', 'Default') .option('-g, --groupName <string>', 'rainbow group name') // rd .option('--rdHost <string>', 'host of R&D platform') // bk .option('--bkStartType <string>', 'start type in bk ci') .option('--bkBuildUrl <string>', 'build url in bk ci') .option('--bkStartUserName <string>', 'start user name in bk ci') .option('--bkPipelineId <string>', 'pipeline id in bk ci') // commit .option('--commitAuthor <string>', 'author of last commit') .option('--commitMessage <string>', 'message of last commit') .option('--commitHash <string>', 'hash of last commit') // version .option('--mpVersion <string>', 'miniProgram bundle version') .action(mpUploadAndReportByOptions); program .command('deploy:github') .description('Deploy Github Page') .option('--repo <repo>', 'Repository Name') .option('--user <user>', 'User Name') .option('--email <email>', 'Login Email') .option('--dir <dir>', 'Target Directory') .option('--token <token>', 'Github Token') .option('--branch <branch>', 'Repository Branch') .option('--message <message>', 'Commit Message') .option('--increment <increment>', 'Is Increment') .option('--playgroundDir <playgroundDir>', 'Playground Dir') .option('--targetDirName <targetDirName>', 'Target Dir Name') .action(deployGithubPage); program .command('clean:changelog') .description('Clean changelog files by removing alpha/beta versions') .argument('<patterns...>', 'CHANGELOG.md file paths or glob patterns') .option('-d, --dry-run', 'Preview mode, do not actually modify files') .action(async (patterns, options) => { try { const args = [...patterns]; if (options.dryRun) { args.push('--dry-run'); } await cleanChangelogScript(args); } catch (error) { console.error('❌ Error cleaning changelog:', error.message); process.exit(1); } }); program .command('deploy:pages') .description('Deploy directory to OA Pages (pages.woa.com)') .argument('<cname>', 'Domain name, e.g. t-comm.pages.woa.com or t-comm') .argument('<directory>', 'Local directory to deploy') .option('-k, --api-key <key>', 'OA Pages API Key (defaults to OA_PAGES_API_KEY env)') .option('-v, --visibility <type>', 'Access permission: public | tof | whitelist') .option('-d, --description <text>', 'Site description (max 100 chars)') .option('--clean', 'Delete remote files before deploy (helpful when site is too large to upload)') .option('--clean-prefix <prefix...>', 'Only delete remote files matching these path prefixes (used with --clean). Defaults to all files') .option('--dry-run', 'Preview mode, do not actually upload') .action(deployPagesCommand); program .command('merge-branches') .description('Merge a source branch (or MR) into the target branch via TGit API') .argument('[mrUrl]', 'TGit MR URL, e.g. https://git.woa.com/group/project/-/merge_requests/123 (auto resolve project & branches)') .option('--mr <url>', 'TGit MR URL (alternative to positional argument)') .option('--id <id>', 'Project ID or full path (required when MR URL not provided)') .option('-s, --source <branch>', 'Source branch name or commit SHA') .option('-t, --target <branch>', 'Target branch name') .option('--source-project-id <id>', 'Source project ID for cross-project MR') .option('--merge-type <type>', 'merge | rebase | squash', 'merge') .option('--commit-message <message>', 'Commit message (required for rebase)') .option('--private-token <token>', 'TGit private token (defaults to GIT_WOA_PRIVATE_TOKEN env)') .option('--base-url <url>', 'TGit API base URL (defaults to GIT_WOA_BASE_URL env)') .action((mrUrl, options) => mergeBranchesCommand({ ...options, mr: options.mr || mrUrl })); program .command('upload-cdn') .description('上传文件/URL 到研发平台 CDN,返回 CDN 访问地址') .argument('[source]', '本地文件路径 或 远程 URL(需配合 --url)') .option('-r, --rid <rid>', '研发平台用户标识(默认读环境变量 PMD_RID)') .option('-t, --rtoken <rtoken>', '研发平台用户 token(默认读环境变量 PMD_RTOKEN)') .option('-u, --x-username <username>', '用户名(可选,默认读环境变量 T_COMM_X_USERNAME)') .option('--url', '将 source 作为远程 URL 下载后再上传') .option('-d, --dir <dir>', 'COS 目标子目录(如 my-project/designs)') .option('--api-url <url>', '上传 API 地址(默认内网地址)') .option('--site-name <name>', 'CDN 文件名前缀标识(默认 cli-upload)') .option('-j, --json', 'JSON 格式输出(等同设置环境变量 T_COMM_JSON_OUTPUT=1)') .action(async (source, options) => { if (options.json) process.env.T_COMM_JSON_OUTPUT = '1'; let result; if (options.url) { result = await uploadFromUrl(source, options); } else { if (!source) { console.error('❌ 请提供文件路径,或使用 --url 模式提供 URL'); console.error(' 示例: t-comm upload-cdn ./design.png --rid xxx --rtoken yyy'); console.error(' 示例: t-comm upload-cdn "https://..." --url --rid xxx --rtoken yyy'); process.exit(1); } result = await uploadFile(source, options); } printResult(result, source); }); program.parse();