Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

Commit

Permalink
feat(repo): create new repo based on directory name and clone
Browse files Browse the repository at this point in the history
Create a new GitHub repository based on the name of the current directory & init with a README

re #696
  • Loading branch information
Ryan Garant committed Nov 10, 2019
1 parent bbb2a9c commit bed87f1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 36 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,12 @@ Create a new GitHub repository and clone on the current directory.
gh re --new foo --clone
```

Create a new GitHub repository based on the name of the current directory & init with a README

```
gh re --new --clone --init
```

Create a new GitHub repository for an organization.

```
Expand Down
83 changes: 47 additions & 36 deletions src/cmds/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,39 +95,6 @@ export async function run(options, done) {

if (options.browser) {
browser(options.user, options.repo)
} else if (options.clone && !options.new) {
await beforeHooks('repo.get', { options })

if (options.organization) {
user = options.organization
} else if (options.user) {
user = options.user
}

if (fs.existsSync(`${process.cwd()}/${options.repo}`)) {
logger.error(
`Can't clone ${logger.colors.green(
`${user}/${options.repo}`
)}. ${logger.colors.green(options.repo)} already exists in this directory.`
)
return
}

try {
var { data } = await getRepo(options)
} catch (err) {
throw new Error(
`Can't clone ${logger.colors.green(`${user}/${options.repo}`)}.\n${err}`
)
}

logger.log(data.html_url)

if (data) {
clone_(user, options.repo, getCloneUrl(options, config.api.ssh_host))
}

await afterHooks('repo.get', { options })
} else if (options.delete && !options.label) {
await beforeHooks('repo.delete', { options })

Expand Down Expand Up @@ -330,7 +297,40 @@ export async function run(options, done) {
}

await afterHooks('repo.search', { options })
} else if (options.new && !options.label) {
} else if (options.clone && !options.new && options.new !== '') {
await beforeHooks('repo.get', { options })

if (options.organization) {
user = options.organization
} else if (options.user) {
user = options.user
}

if (fs.existsSync(`${process.cwd()}/${options.repo}`)) {
logger.error(
`Can't clone ${logger.colors.green(
`${user}/${options.repo}`
)}. ${logger.colors.green(options.repo)} already exists in this directory.`
)
return
}

try {
var { data } = await getRepo(options)
} catch (err) {
throw new Error(
`Can't clone ${logger.colors.green(`${user}/${options.repo}`)}.\n${err}`
)
}

logger.log(data.html_url)

if (data) {
clone_(user, options.repo, getCloneUrl(options, config.api.ssh_host))
}

await afterHooks('repo.get', { options })
} else if ((options.new || options.new === '') && !options.label) {
if (!options.new.trim()) {
options = produce(options, draft => {
draft.new = getCurrentFolderName()
Expand Down Expand Up @@ -373,9 +373,20 @@ function browser(user, repo) {
openUrl(`${config.github_host}/${user}/${repo}`)
}

function clone_(user, repo, repo_url) {
/**
* If the current directory where gh was run from matches the repo name
* we will clone into the current directory
* otherwise we will clone into a new directory
*/
function clone_(user: string, repo: string, repoUrl: string): void {
const currentDir = process.cwd()
const currentDirName = currentDir.slice(currentDir.lastIndexOf('/') + 1)

const cloneUrl = url.parse(repoUrl).href

logger.log(`Cloning ${logger.colors.green(`${user}/${repo}`)}`)
git.clone(url.parse(repo_url).href, repo)

git.clone(cloneUrl, currentDirName === repo ? '.' : repo)
}

function createLabel(options, user): Promise<Octokit.IssuesCreateLabelResponse> {
Expand Down

0 comments on commit bed87f1

Please sign in to comment.