-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from jaebradley/refactor-code-search
Refactor code search
- Loading branch information
Showing
14 changed files
with
642 additions
and
225 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,9 @@ | ||
#!/usr/bin/env node | ||
|
||
/* eslint-disable no-console */ | ||
|
||
import CodeSearchOptionsSelector from '../services/CodeSearchOptionsSelector'; | ||
import SearchService from '../services/SearchService'; | ||
import MatchResultsParser from '../services/MatchResultsParser'; | ||
import MatchingFilesSelector from '../services/MatchingFilesSelector'; | ||
import GitHubCredentialSaver from '../services/GitHubCredentialSaver'; | ||
|
||
async function executeCodeCommand() { | ||
if (!GitHubCredentialSaver.hasCredential()) { | ||
GitHubCredentialSaver.save(); | ||
} | ||
|
||
const searchService = new SearchService(); | ||
const optionsService = new CodeSearchOptionsSelector(); | ||
const options = await optionsService.selectSearchOptions(); | ||
const results = await searchService.searchCode(options); | ||
const matches = MatchResultsParser.parse(results); | ||
const filesSelector = new MatchingFilesSelector(); | ||
await filesSelector.selectMatchingFile(matches); | ||
} | ||
import CodeSearchCommandService from '../services/code/CodeSearchCommandService'; | ||
|
||
try { | ||
executeCodeCommand(); | ||
CodeSearchCommandService.execute(); | ||
} catch (e) { | ||
console.error(`Rut ro! Unexpected error: ${e}`); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import GitHubClient from 'github'; | ||
|
||
class RepositorySearcher { | ||
constructor(authorizationToken) { | ||
this.client = new GitHubClient({ headers: { 'user-agent': 'jaebradley' } }); | ||
this.client.authenticate({ | ||
type: 'token', | ||
token: authorizationToken, | ||
}); | ||
} | ||
|
||
async search(organizationName, repoNameContains) { | ||
return this.client.search.repos({ q: `org:${organizationName} ${repoNameContains}` }); | ||
} | ||
} | ||
|
||
export default RepositorySearcher; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import CodeSearchParametersPrompter from './CodeSearchParametersPrompter'; | ||
import GitHubDataStorer from '../GitHubDataStorer'; | ||
import MatchResultsParser from './MatchResultsParser'; | ||
import MatchingFilesSelector from './MatchingFilesSelector'; | ||
import RepositorySearcher from '../RepositorySearcher'; | ||
import SetupCommandService from '../../services/SetupCommandService'; | ||
import SearchOptions from '../../data/SearchOptions'; | ||
import CodeSearcher from './CodeSearcher'; | ||
|
||
|
||
class CodeSearchCommandService { | ||
static async execute() { | ||
const hasAuthorizationToken = await GitHubDataStorer.hasAuthorizationToken(); | ||
|
||
if (!hasAuthorizationToken) { | ||
await SetupCommandService.execute(); | ||
} | ||
|
||
const authorizationToken = await GitHubDataStorer.getAuthorizationToken(); | ||
const repositorySearcher = new RepositorySearcher(authorizationToken); | ||
const prompter = new CodeSearchParametersPrompter(repositorySearcher); | ||
|
||
const { | ||
queryString, | ||
organizationName, | ||
repositoryName, | ||
language, | ||
} = await prompter.promptSearchParameters(); | ||
|
||
const options = new SearchOptions(queryString, organizationName, repositoryName, language); | ||
|
||
const codeSearcher = new CodeSearcher(authorizationToken); | ||
|
||
const results = await codeSearcher.search(options); | ||
|
||
const matches = MatchResultsParser.parse(results); | ||
|
||
const filesSelector = new MatchingFilesSelector(); | ||
await filesSelector.selectMatchingFile(matches); | ||
} | ||
} | ||
|
||
export default CodeSearchCommandService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import inquirer from 'inquirer'; | ||
import InquirerAutocompletePrompt from 'inquirer-autocomplete-prompt'; | ||
import fuzzy from 'fuzzy'; | ||
|
||
import GitHubLanguages from '../../data/constants/github/languages'; | ||
|
||
inquirer.registerPrompt('autocomplete', InquirerAutocompletePrompt); | ||
|
||
class CodeSearchParametersPrompter { | ||
constructor(repositorySearcher) { | ||
this.repositorySearcher = repositorySearcher; | ||
this.repositoryNames = null; | ||
} | ||
|
||
async promptSearchParameters() { | ||
return inquirer.prompt([ | ||
{ | ||
name: 'queryString', | ||
message: 'Input your search string', | ||
validate: str => str.length > 0, | ||
type: 'input', | ||
}, | ||
{ | ||
name: 'organizationName', | ||
message: 'Input optional organization/user name', | ||
type: 'input', | ||
}, | ||
{ | ||
name: 'repositoryName', | ||
message: 'Input optional repository name', | ||
type: 'autocomplete', | ||
source: (answersSoFar, searchTerm) => { | ||
const searchTermValue = searchTerm || ''; | ||
return this.findMatchingRepositories(answersSoFar.organizationName, searchTermValue); | ||
}, | ||
}, | ||
{ | ||
name: 'language', | ||
message: 'Choose optional language', | ||
type: 'autocomplete', | ||
source: (answersSoFar, input) => ( | ||
Promise.resolve(fuzzy.filter(input || '', GitHubLanguages).map(match => match.original)) | ||
), | ||
}, | ||
]); | ||
} | ||
|
||
async findMatchingRepositories(organizationName, repositoryName) { | ||
return this.repositorySearcher | ||
.search(organizationName, repositoryName) | ||
.then(result => result.data.items.map(repository => repository.name)) | ||
.then((repositoryNames) => { | ||
let names = ['None']; | ||
names = names.concat(repositoryNames); | ||
return fuzzy.filter(repositoryName, names).map(match => match.original); | ||
}); | ||
} | ||
} | ||
|
||
export default CodeSearchParametersPrompter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import GitHubClient from 'github'; | ||
|
||
class CodeSearcher { | ||
constructor(authorizationToken) { | ||
this.authorizationToken = authorizationToken; | ||
this.client = new GitHubClient({ | ||
headers: { | ||
accept: 'application/vnd.github.v3.text-match+json', | ||
'user-agent': 'jaebradley', | ||
}, | ||
}); | ||
this.client.authenticate({ | ||
type: 'token', | ||
token: this.authorizationToken, | ||
}); | ||
} | ||
|
||
async search(options) { | ||
return this.client.search.code({ q: CodeSearcher.generateQueryString(options) }); | ||
} | ||
|
||
static generateQueryString(options) { | ||
let queryString = ''; | ||
if (options.organizationName) { | ||
if (options.repositoryName && options.repositoryName !== 'None') { | ||
queryString += `repo:${options.organizationName}/${options.repositoryName}`; | ||
} else { | ||
queryString += `org:${options.organizationName}`; | ||
} | ||
} | ||
|
||
queryString += ` ${options.queryString}`; | ||
|
||
if (options.language && options.language !== 'None') { | ||
queryString += ` language:${options.language}`; | ||
} | ||
|
||
return queryString; | ||
} | ||
} | ||
|
||
export default CodeSearcher; |
Oops, something went wrong.