功能
实现的是打开 github 页面功能
meow 是命令行辅助工具
gh-home --help 可以打印出传递的文本
并且 cli.input[0] 把 -- 配置的参数给去掉了
比如 gh-home --lang en 123
cli.input[0] 是 123 而不是 --lang
const meow = require('meow');
const gitRemoteOriginUrl = require('git-remote-origin-url');
const gitRemoteUpstreamUrl = require('git-remote-upstream-url');
const githubUrlFromGit = require('github-url-from-git');
const open = require('open');
const execa = require('execa');
const cli = meow(`
Usage
$ gh-home [repo | user/repo]
Examples
$ gh-home
$ gh-home myrepo
$ gh-home avajs/ava
`);
const repo = cli.input[0];
(async () => {
if (repo) {
// 如果包含/ 直接打开
if (repo.includes('/')) {
await open(`https://github.com/${repo}`);
} else {
// 不包含 前面自动加用户
const {stdout: user} = await execa('git', ['config', '--global', 'github.user']);
await open(`https://github.com/${user}/${repo}`);
}
} else {
let url;
try {
try {
// 如果是git remote 仓库
url = await gitRemoteUpstreamUrl();
} catch (_) {
url = await gitRemoteOriginUrl();
}
url = githubUrlFromGit(url);
} catch (_) {
console.error('Couldn\'t find the remote origin or upstream. Ensure it\'s set and you\'re in a repo.\n\n $ git remote add origin https://github.com/user/repo.git');
process.exit(1);
}
if (!url) {
console.error('Couldn\'t find the repo\'s GitHub URL. Ensure you are inside a Git repo that points to GitHub.');
process.exit(1);
}
await open(url);
}
})();
网友评论