Skip to content

Commit

Permalink
DRY repository name parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlath committed May 3, 2019
1 parent f41be25 commit e041302
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 10 deletions.
7 changes: 0 additions & 7 deletions lib/get_repo.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/get_repo_data.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const repo = require('./get_repo')()
const repo = require('./repo_name')
const { grey } = require('chalk')

const getPagesSequentially = require('./get_pages_sequentially')
Expand Down
16 changes: 16 additions & 0 deletions lib/repo_name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { execSync } = require('child_process')

// Support the following remote URL format
// - git@github.com:owner/repo-name.git
// - https://github.com/owner/repo-name
// - https://github.com/owner/repo-name.git

const getName = remoteCmd => {
const cmd = `${remoteCmd} | awk '{printf $2}' | sed 's|^.*:||' | sed 's|//github.com/||' | sed 's|\.git$||'`
return execSync(cmd).toString().trim()
}

const githubOriginRemote = getName('git remote -v | grep origin | grep github.com')
const firstGithubRemote = getName('git remote -v | grep github.com | head -n 1')

module.exports = githubOriginRemote.match(/.+\/.+/) != null ? githubOriginRemote : firstGithubRemote
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"bin": {
"backup-github-repo": "./bin/backup_github_repo",
"download-github-repo-json": "./scripts/download_json",
"download-github-repo-html": "./scripts/download_html"
"download-github-repo-html": "./scripts/download_html",
"repository-name": "./scripts/repository_name"
},
"main": "index.js",
"directories": {
Expand Down
4 changes: 3 additions & 1 deletion scripts/download_html
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env bash
set -eu

repo=$(git remote -v | grep github.com | head -n 1 | awk '{printf $2}' | sed 's|^.*:||' | sed 's|//github.com/||' | sed 's|\.git$||')
# repository-name is defined in package.json
repo=$(repository-name)

echo "repo: $repo"
last_id=$(jd "./repo-backup/data.json" ._keys._last)
echo "entries: $last_id"
Expand Down
2 changes: 2 additions & 0 deletions scripts/repository_name
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
console.log(require('../lib/repo_name.js'))

0 comments on commit e041302

Please sign in to comment.