Skip to content

Commit

Permalink
命令行工具github...
Browse files Browse the repository at this point in the history
windows中文时自动切换utf8编码,防止乱码
  • Loading branch information
zetaloop committed May 8, 2024
1 parent 10807a0 commit e0877e2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 26 deletions.
13 changes: 7 additions & 6 deletions app/src/cli/commands/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,27 @@ interface ICloneArgs extends mriArgv {
}

export const command: ICommandModule = {
command: 'clone <url|slug>',
description: 'Clone a repository',
command: 'clone <网址|标识符>',
description: '克隆一个储存库',
args: [
{
name: 'url|slug',
name: '网址|标识符',
required: true,
description: 'The URL or the GitHub owner/name alias to clone',
description:
'储存库的 URL 地址,或者 GitHub "用户/储存库名" 格式的标识符',
type: 'string',
},
],
options: {
branch: {
type: 'string',
aliases: ['b'],
description: 'The branch to checkout after cloning',
description: '克隆完成后要检出的分支',
},
},
handler({ _: [cloneUrl], branch }: ICloneArgs) {
if (!cloneUrl) {
throw new CommandError('Clone URL must be specified')
throw new CommandError('必须指定克隆地址')
}
try {
const _ = new URL(cloneUrl)
Expand Down
36 changes: 24 additions & 12 deletions app/src/cli/commands/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { commands, ICommandModule, IOption } from '../load-commands'
import { dasherizeOption, printTable } from '../util'

export const command: ICommandModule = {
command: 'help [command]',
description: 'Show the help page for a command',
command: 'help [命令]',
description: '显示命令的帮助页面',
handler({ _: [command] }) {
if (command) {
printCommandHelp(command, commands[command])
Expand All @@ -17,23 +17,35 @@ export const command: ICommandModule = {
}

function printHelp() {
console.log(chalk.underline('Commands:'))
console.log(chalk.underline('命令:'))
const table: string[][] = []
for (const commandName of Object.keys(commands)) {
const command = commands[commandName]
table.push([chalk.bold(command.command), command.description])
}
printTable(table)
console.log(
`\nRun ${chalk.bold(
`github help ${chalk.gray('<command>')}`
)} for details about each command`
`\n运行 ${chalk.bold(
`github help ${chalk.gray('<命令>')}`
)} 查看各个命令的详细帮助信息`
// )} for details about each command`
)
}

function typeTranslator(type: string) {
switch (type) {
case 'string':
return '字符串'
case 'boolean':
return '布尔值'
default:
return type
}
}

function printCommandHelp(name: string, command: ICommandModule) {
if (!command) {
console.log(`Unrecognized command: ${chalk.bold.red.underline(name)}`)
console.log(`未知命令: ${chalk.bold.red.underline(name)}`)
printHelp()
return
}
Expand All @@ -51,7 +63,7 @@ function printCommandHelp(name: string, command: ICommandModule) {
}
const { options, args } = command
if (options) {
console.log(chalk.underline('\nOptions:'))
console.log(chalk.underline('\n选项:'))
printTable(
Object.keys(options)
.map(k => [k, options[k]] as [string, IOption])
Expand All @@ -61,18 +73,18 @@ function printCommandHelp(name: string, command: ICommandModule) {
.map(x => chalk.bold.blue(x))
.join(chalk.gray(', ')),
option.description,
chalk.gray(`[${chalk.underline(option.type)}]`),
chalk.gray(`[${chalk.underline(typeTranslator(option.type))}]`),
])
)
}
if (args && args.length) {
console.log(chalk.underline('\nArguments:'))
console.log(chalk.underline('\n参数:'))
printTable(
args.map(arg => [
(arg.required ? chalk.bold : chalk).blue(arg.name),
arg.required ? chalk.gray('(required)') : '',
arg.required ? chalk.gray('(必填)') : '',
arg.description,
chalk.gray(`[${chalk.underline(arg.type)}]`),
chalk.gray(`[${chalk.underline(typeTranslator(arg.type))}]`),
])
)
}
Expand Down
16 changes: 8 additions & 8 deletions app/src/cli/commands/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { openDesktop } from '../open-desktop'
import { parseRemote } from '../../lib/remote-parsing'

export const command: ICommandModule = {
command: 'open <path>',
aliases: ['<path>'],
description: 'Open a git repository in GitHub Desktop',
command: 'open <路径>',
aliases: ['<路径>'],
description: '用 GitHub Desktop 打开一个 git 储存库',
args: [
{
name: 'path',
description: 'The path to the repository to open',
name: '路径',
description: '储存库文件夹路径',
type: 'string',
required: false,
},
Expand All @@ -26,9 +26,9 @@ export const command: ICommandModule = {
//Check if the pathArg is a remote url
if (parseRemote(pathArg) != null) {
console.log(
`\nYou cannot open a remote URL in GitHub Desktop\n` +
`Use \`${chalk.bold(`git clone ` + pathArg)}\`` +
` instead to initiate the clone`
`\n无法直接打开远程地址\n` +
`请先使用 \`${chalk.bold(`git clone ` + pathArg)}\`` +
` 来克隆它`
)
} else {
const repositoryPath = Path.resolve(process.cwd(), pathArg)
Expand Down
6 changes: 6 additions & 0 deletions app/src/cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ const supportsCommand = (name: string) => Object.hasOwn(commands, name)

;(function attemptRun(name: string) {
try {
if (__WIN32__) {
// Solve Chinese garbled problem on Windows
const { execSync } = require('child_process')
execSync('chcp 65001')
}

if (supportsCommand(name)) {
runCommand(name)
} else if (name.startsWith('--')) {
Expand Down

0 comments on commit e0877e2

Please sign in to comment.