Skip to content

Commit

Permalink
[cli] Use githubToken to clone submodules if provided (#2)
Browse files Browse the repository at this point in the history
* [chore] Install dependency `git-url-parse`

* [cli] Use `githubToken` if provided

* [chore] Add type declarations for `git-url-parse`
  • Loading branch information
junhoyeo committed Sep 13, 2022
1 parent d872843 commit 8878cb1
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"start": "node lib/index.js"
},
"dependencies": {
"git-url-parse": "^13.0.0",
"zx": "^7.0.8"
},
"devDependencies": {
Expand Down
12 changes: 10 additions & 2 deletions src/commands/clone.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import chalk from 'chalk';
import GitUrlParse from 'git-url-parse';
import path from 'path';
import * as zx from 'zx';

Expand All @@ -16,6 +17,12 @@ const insideDir = async (path: string, callback: () => Promise<void>) => {
}
};

const convertToAuthURL = (url: string, githubToken: string): string => {
const parsed = GitUrlParse(url);
parsed.token = githubToken;
return GitUrlParse.stringify(parsed, 'https');
};

type CloneOptions = {
githubToken: string;
depth: number;
Expand All @@ -26,6 +33,7 @@ export const clone = async ({ githubToken, depth, submodules }: CloneOptions) =>

for (const submodule of submodules) {
const submoduleDir = path.join(rootDir, submodule.path);
const submoduleURL = !!githubToken ? convertToAuthURL(submodule.url, githubToken) : submodule.url;

await zx.$`rm -rf ${submoduleDir}`.catch(() => {
/* ignored */
Expand All @@ -37,9 +45,9 @@ export const clone = async ({ githubToken, depth, submodules }: CloneOptions) =>
await insideDir(submoduleDir, async () => {
await zx.$`git init`;
try {
await zx.$`git remote add origin ${submodule.url}`;
await zx.$`git remote add origin ${submoduleURL}`;
} catch {
await zx.$`git remote set-url origin ${submodule.url}`;
await zx.$`git remote set-url origin ${submoduleURL}`;
}
await zx.$`git fetch origin ${submodule.commitHash} --depth=${depth} --no-tags`;
await zx.$`git reset --hard FETCH_HEAD`;
Expand Down
21 changes: 21 additions & 0 deletions src/types/git-url-parse.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
declare module 'git-url-parse' {
export type ParsedGitUrlObject = {
token: string;
};
export type Protocol =
| 'ssh'
| 'git+ssh'
| 'ssh+git'
| 'ftp'
| 'ftps'
| 'http'
| 'https';
function GitURLParse(url: string): ParsedGitUrlObject;
namespace GitURLParse {
export function stringify(
parsed: ParsedGitUrlObject,
protocol?: Protocol,
): string;
}
export default GitURLParse;
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"outDir": "lib",
"strict": true,
"esModuleInterop": true,
"resolveJsonModule": true
"resolveJsonModule": true,
"typeRoots": ["node_modules/@types", "src/types"]
}
}
41 changes: 41 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,21 @@ gensync@^1.0.0-beta.2:
resolved "https://registry.yarnpkg.com/gensync/-/gensync-1.0.0-beta.2.tgz#32a6ee76c3d7f52d46b2b1ae5d93fea8580a25e0"
integrity sha512-3hN7NaskYvMDLQY55gnW3NQ+mesEAepTqlg+VEbj7zzqEMBVNhzcGYYeqFo/TlYz6eQiFcp1HcsCZO+nGgS8zg==

git-up@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/git-up/-/git-up-7.0.0.tgz#bace30786e36f56ea341b6f69adfd83286337467"
integrity sha512-ONdIrbBCFusq1Oy0sC71F5azx8bVkvtZtMJAsv+a6lz5YAmbNnLD6HAB4gptHZVLPR8S2/kVN6Gab7lryq5+lQ==
dependencies:
is-ssh "^1.4.0"
parse-url "^8.1.0"

git-url-parse@^13.0.0:
version "13.0.0"
resolved "https://registry.yarnpkg.com/git-url-parse/-/git-url-parse-13.0.0.tgz#9a18d0eaec579fb6379c368aecb09f00b544669c"
integrity sha512-X1kozCqKL82dMrCLi4vie9SHDC+QugKskAMs4VUbIkhURKg5yDwxDmf6Ixg73J+/xVgK5TXKhzn8a94nHJHpnA==
dependencies:
git-up "^7.0.0"

glob-parent@^5.1.2:
version "5.1.2"
resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-5.1.2.tgz#869832c58034fe68a4093c17dc15e8340d8401c4"
Expand Down Expand Up @@ -518,6 +533,13 @@ is-number@^7.0.0:
resolved "https://registry.yarnpkg.com/is-number/-/is-number-7.0.0.tgz#7535345b896734d5f80c4d06c50955527a14f12b"
integrity sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==

is-ssh@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/is-ssh/-/is-ssh-1.4.0.tgz#4f8220601d2839d8fa624b3106f8e8884f01b8b2"
integrity sha512-x7+VxdxOdlV3CYpjvRLBv5Lo9OJerlYanjwFrPR9fuGPjCiNiCzFgAWpiLAohSbsnH4ZAys3SBh+hq5rJosxUQ==
dependencies:
protocols "^2.0.1"

isexe@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
Expand Down Expand Up @@ -604,6 +626,20 @@ node-releases@^2.0.6:
resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-2.0.6.tgz#8a7088c63a55e493845683ebf3c828d8c51c5503"
integrity sha512-PiVXnNuFm5+iYkLBNeq5211hvO38y63T0i2KKh2KnUs3RpzJ+JtODFjkD8yjLwnDkTYF1eKXheUwdssR+NRZdg==

parse-path@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/parse-path/-/parse-path-7.0.0.tgz#605a2d58d0a749c8594405d8cc3a2bf76d16099b"
integrity sha512-Euf9GG8WT9CdqwuWJGdf3RkUcTBArppHABkO7Lm8IzRQp0e2r/kkFnmhu4TSK30Wcu5rVAZLmfPKSBBi9tWFog==
dependencies:
protocols "^2.0.0"

parse-url@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/parse-url/-/parse-url-8.1.0.tgz#972e0827ed4b57fc85f0ea6b0d839f0d8a57a57d"
integrity sha512-xDvOoLU5XRrcOZvnI6b8zA6n9O9ejNk/GExuz1yBuWUGn9KA97GI6HTs6u02wKara1CeVmZhH+0TZFdWScR89w==
dependencies:
parse-path "^7.0.0"

path-type@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b"
Expand Down Expand Up @@ -631,6 +667,11 @@ prettier@^2.7.1:
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.7.1.tgz#e235806850d057f97bb08368a4f7d899f7760c64"
integrity sha512-ujppO+MkdPqoVINuDFDRLClm7D78qbDt0/NR+wp5FqEZOoTNAjPHWj17QRhu7geIHJfcNhRk1XVQmF8Bp3ye+g==

protocols@^2.0.0, protocols@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/protocols/-/protocols-2.0.1.tgz#8f155da3fc0f32644e83c5782c8e8212ccf70a86"
integrity sha512-/XJ368cyBJ7fzLMwLKv1e4vLxOju2MNAIokcr7meSaNcVbWz/CPcW22cP04mwxOErdA5mwjA8Q6w/cdAQxVn7Q==

ps-tree@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/ps-tree/-/ps-tree-1.2.0.tgz#5e7425b89508736cdd4f2224d028f7bb3f722ebd"
Expand Down

0 comments on commit 8878cb1

Please sign in to comment.