【手把手帶你擼一個腳手架】第三步, 獲取 github 專案資訊

圈圈Dei圈發表於2018-11-14

系列文章:

腳手架, 作為一個工具, 主要是用來搬運我們已經準備好的專案模板(webpack 配置, koa 專案雛形等等), 這一步我們就來建立用於搬運的專案模板. 並通過介面獲取相關資訊 github Api 文件

ps: 這一步基於 github 若有沒使用過的小夥伴, 請加油

2018-11-15-00-33-46

建立一個專門用於維護專案模板的專案組

為了不和自己平時寫的各種辣雞程式碼混雜在一起, 這裡我專門建立了一個 organization 不會建立的兄弟們請 度娘

  • 目錄切換到剛剛建立的 organization 上

2018-11-15-00-38-51

  • 建立一個專案

2018-11-15-00-39-18

  • 向建立的專案中新增一個檔案, 上傳到 github 並打好 tag

2018-11-15-00-44-17

準備工作完成 ^_^

2018-11-15-00-49-08

通過 github 開放 api 獲取專案資訊

baseUrl: api.github.com

作為一個腳手架, 我們需要獲取的專案資訊包含以下幾個:

=========================== 補充 ==============================================

ps 由於 github 開放 api 有請求次數限制(未授權每小時 60 次), 所以可能存在下圖的情況, 解決方法是在請求中加入認證資訊(authToken), 詳情參考系列文章下一篇, 本節的臨時處理方案為執行命令時候新增自己的 github 賬號密碼即可.

2018-12-19-23-48-57

獲取組織所屬專案列表文件

我們可以嘗試一下獲取剛剛建立的 organization 下的專案目錄

curl https://api.github.com/orgs/learn-cli-organization/repos

// 如果報了上圖所示錯誤, 在 curl 和介面地址中間加入 -u "你的github賬號:你的github密碼" 中間的冒號不要刪除, 作為分割
curl -u "你的github賬號:你的github密碼" https://api.github.com/orgs/learn-cli-organization/repos

// 返回的結果
[
  {
    "id": 157579674,
    "node_id": "MDEwOlJlcG9zaXRvcnkxNTc1Nzk2NzQ=",
    "name": "demo",
    "full_name": "learn-cli-organization/demo",
    "private": false,
    "owner": {
      "login": "learn-cli-organization",
      "id": 45043923,
      "node_id": "MDEyOk9yZ2FuaXphdGlvbjQ1MDQzOTIz",
      "avatar_url": "https://avatars2.githubusercontent.com/u/45043923?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/learn-cli-organization",
      "html_url": "https://github.com/learn-cli-organization",
      "followers_url": "https://api.github.com/users/learn-cli-organization/followers",
      "following_url": "https://api.github.com/users/learn-cli-organization/following{/other_user}",
      "gists_url": "https://api.github.com/users/learn-cli-organization/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/learn-cli-organization/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/learn-cli-organization/subscriptions",
      "organizations_url": "https://api.github.com/users/learn-cli-organization/orgs",
      "repos_url": "https://api.github.com/users/learn-cli-organization/repos",
      "events_url": "https://api.github.com/users/learn-cli-organization/events{/privacy}",
      "received_events_url": "https://api.github.com/users/learn-cli-organization/received_events",
      "type": "Organization",
      "site_admin": false
    },
    "html_url": "https://github.com/learn-cli-organization/demo",
    "description": null,
    "fork": false,
    "url": "https://api.github.com/repos/learn-cli-organization/demo",
    "forks_url": "https://api.github.com/repos/learn-cli-organization/demo/forks",
    "keys_url": "https://api.github.com/repos/learn-cli-organization/demo/keys{/key_id}",
    "collaborators_url": "https://api.github.com/repos/learn-cli-organization/demo/collaborators{/collaborator}",
    "teams_url": "https://api.github.com/repos/learn-cli-organization/demo/teams",
    "hooks_url": "https://api.github.com/repos/learn-cli-organization/demo/hooks",
    "issue_events_url": "https://api.github.com/repos/learn-cli-organization/demo/issues/events{/number}",
    "events_url": "https://api.github.com/repos/learn-cli-organization/demo/events",
    "assignees_url": "https://api.github.com/repos/learn-cli-organization/demo/assignees{/user}",
    "branches_url": "https://api.github.com/repos/learn-cli-organization/demo/branches{/branch}",
    "tags_url": "https://api.github.com/repos/learn-cli-organization/demo/tags",
    "blobs_url": "https://api.github.com/repos/learn-cli-organization/demo/git/blobs{/sha}",
    "git_tags_url": "https://api.github.com/repos/learn-cli-organization/demo/git/tags{/sha}",
    "git_refs_url": "https://api.github.com/repos/learn-cli-organization/demo/git/refs{/sha}",
    "trees_url": "https://api.github.com/repos/learn-cli-organization/demo/git/trees{/sha}",
    "statuses_url": "https://api.github.com/repos/learn-cli-organization/demo/statuses/{sha}",
    "languages_url": "https://api.github.com/repos/learn-cli-organization/demo/languages",
    "stargazers_url": "https://api.github.com/repos/learn-cli-organization/demo/stargazers",
    "contributors_url": "https://api.github.com/repos/learn-cli-organization/demo/contributors",
    "subscribers_url": "https://api.github.com/repos/learn-cli-organization/demo/subscribers",
    "subscription_url": "https://api.github.com/repos/learn-cli-organization/demo/subscription",
    "commits_url": "https://api.github.com/repos/learn-cli-organization/demo/commits{/sha}",
    "git_commits_url": "https://api.github.com/repos/learn-cli-organization/demo/git/commits{/sha}",
    "comments_url": "https://api.github.com/repos/learn-cli-organization/demo/comments{/number}",
    "issue_comment_url": "https://api.github.com/repos/learn-cli-organization/demo/issues/comments{/number}",
    "contents_url": "https://api.github.com/repos/learn-cli-organization/demo/contents/{+path}",
    "compare_url": "https://api.github.com/repos/learn-cli-organization/demo/compare/{base}...{head}",
    "merges_url": "https://api.github.com/repos/learn-cli-organization/demo/merges",
    "archive_url": "https://api.github.com/repos/learn-cli-organization/demo/{archive_format}{/ref}",
    "downloads_url": "https://api.github.com/repos/learn-cli-organization/demo/downloads",
    "issues_url": "https://api.github.com/repos/learn-cli-organization/demo/issues{/number}",
    "pulls_url": "https://api.github.com/repos/learn-cli-organization/demo/pulls{/number}",
    "milestones_url": "https://api.github.com/repos/learn-cli-organization/demo/milestones{/number}",
    "notifications_url": "https://api.github.com/repos/learn-cli-organization/demo/notifications{?since,all,participating}",
    "labels_url": "https://api.github.com/repos/learn-cli-organization/demo/labels{/name}",
    "releases_url": "https://api.github.com/repos/learn-cli-organization/demo/releases{/id}",
    "deployments_url": "https://api.github.com/repos/learn-cli-organization/demo/deployments",
    "created_at": "2018-11-14T16:41:01Z",
    "updated_at": "2018-11-14T16:42:39Z",
    "pushed_at": "2018-11-14T16:43:18Z",
    "git_url": "git://github.com/learn-cli-organization/demo.git",
    "ssh_url": "git@github.com:learn-cli-organization/demo.git",
    "clone_url": "https://github.com/learn-cli-organization/demo.git",
    "svn_url": "https://github.com/learn-cli-organization/demo",
    "homepage": null,
    "size": 0,
    "stargazers_count": 0,
    "watchers_count": 0,
    "language": "JavaScript",
    "has_issues": true,
    "has_projects": true,
    "has_downloads": true,
    "has_wiki": true,
    "has_pages": false,
    "forks_count": 0,
    "mirror_url": null,
    "archived": false,
    "open_issues_count": 0,
    "license": null,
    "forks": 0,
    "open_issues": 0,
    "watchers": 0,
    "default_branch": "master",
    "permissions": {
      "admin": false,
      "push": false,
      "pull": true
    }
  }
]
複製程式碼

拿到這個陣列說明我們已經能夠獲取到專案組中的所有專案啦, 一波猝不及防的商業互吹 ^_^

16705bca01c5e44c

獲取指定專案的版本號 文件

通過前一個介面, 我們成功的獲取到了專案組中所有的專案資訊, 接下來我們可以通過以下介面獲取到指定專案的版本資訊(就是 tags)

curl https://api.github.com/repos/learn-cli-organization/demo/tags

// 如果報了上圖所示錯誤, 在 curl 和介面地址中間加入 -u "你的github賬號:你的github密碼" 中間的冒號不要刪除, 作為分割
curl -u "你的github賬號:你的github密碼" https://api.github.com/repos/learn-cli-organization/demo/tags

// 返回結果
[
  {
    "name": "v0.0.1",
    "zipball_url": "https://api.github.com/repos/learn-cli-organization/demo/zipball/v0.0.1",
    "tarball_url": "https://api.github.com/repos/learn-cli-organization/demo/tarball/v0.0.1",
    "commit": {
      "sha": "00f0dda86e5f922e2ae406c25e19b44b2463f690",
      "url": "https://api.github.com/repos/learn-cli-organization/demo/commits/00f0dda86e5f922e2ae406c25e19b44b2463f690"
    },
    "node_id": "MDM6UmVmMTU3NTc5Njc0OnYwLjAuMQ=="
  }
]
複製程式碼

下集預告: 到目前為止, 我們已經能夠獲取到專案資訊. 下一步我們會將結合 inquirer.js 實現命令列互動式的動態獲取這些資訊

2018-11-15-01-19-33

相關文章