美文网首页
npm 之 gh-home

npm 之 gh-home

作者: 织雪纱奈 | 来源:发表于2019-06-05 13:34 被阅读0次

功能

实现的是打开 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);
    }
})();

相关文章

  • npm 之 gh-home

    功能 实现的是打开 github 页面功能 meow 是命令行辅助工具gh-home --help 可以打印出传递...

  • node.js入门教程

    nodejs和npm的安装 体验一下 Hello World 之 Node.js 基础之Npm使用 Node之模块...

  • peerdependency的作用

    探讨npm依赖管理之peerDependencies 引言 想必前端同学对npm的devDependencies和...

  • Vue 入门之安装(Mac)

    Vue 入门之安装(Mac) Step 1 安装npm,npm全称为Node Package Manager,是一...

  • NPM之publish

    今天突发兴致想要往npm上上传个npm package,于是吭哧吭哧写了点代码准备上传。当我满怀期待输入npm p...

  • react-navigation之 StackNavigator

    react-navigation 之 StackNavigator npm install --save reac...

  • npm蜜汁报错之 npm audit fix

    百度了一下 看到了一个大神写的 链接在此:https://blog.csdn.net/chenjin_chenji...

  • 初探webpack小记

    webpack学习记录 安装webpack 全局安装webpack:npm install webpack -g之...

  • npm publish发布报错

    记录npm publish错误解决方案: 报错信息 修复方法用了淘宝镜像源 - 换成npm的源包名重复 - 删掉之...

  • npm install 之 gRPC

    我们这里探讨一下如何在npm install阶段,修改并编译的过程。其中涉及到一些npm的命令,一并解释和学习了。...

网友评论

      本文标题:npm 之 gh-home

      本文链接:https://www.haomeiwen.com/subject/yegjxctx.html