-
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add github search resolver * Use github search resolver as fallback for less * Use github search resolver as fallback for sass * Use github search resolver as fallback for haskell
- Loading branch information
1 parent
588ae13
commit 7c785a5
Showing
10 changed files
with
100 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
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
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,20 @@ | ||
import { extname } from 'path'; | ||
|
||
export default function ({ path, target }) { | ||
const [, user, repo] = path.split('/'); | ||
const extension = extname(target); | ||
|
||
let params = `q=${target}+in:path+filename:${target}+repo:${user}/${repo}`; | ||
if (extension) { | ||
params += `+extension:${extension}`; | ||
} | ||
|
||
const url = `https://api.github.com/search/code?${params}`; | ||
|
||
return async function githubSearch() { | ||
const response = await fetch(url); | ||
const json = await response.json(); | ||
|
||
return json.items[0].html_url; | ||
}; | ||
} |
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
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
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
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
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,45 @@ | ||
import assert from 'assert'; | ||
import sinon from 'sinon'; | ||
import githubSearch from '../../lib/resolver/github-search.js'; | ||
|
||
describe('github-search', () => { | ||
const sandbox = sinon.sandbox.create(); | ||
const fetchStub = sinon.stub(window, 'fetch'); | ||
|
||
const path = '/octo/foo.txt'; | ||
const target = 'bar.txt'; | ||
|
||
afterEach(() => { | ||
fetchStub.reset(); | ||
sandbox.reset(); | ||
}); | ||
|
||
after(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
it('returns a function', () => { | ||
assert.deepEqual( | ||
typeof githubSearch({ path, target }), | ||
'function', | ||
); | ||
}); | ||
|
||
it('calls the github search api', () => { | ||
githubSearch({ path, target })(); | ||
|
||
assert.deepEqual( | ||
fetchStub.args[0][0], | ||
'https://api.github.com/search/code?q=bar.txt+in:path+filename:bar.txt+repo:octo/foo.txt+extension:.txt', | ||
); | ||
}); | ||
|
||
it('does not append query param "extension" when target does not have a file extension', () => { | ||
githubSearch({ path, target: 'bar' })(); | ||
|
||
assert.deepEqual( | ||
fetchStub.args[0][0], | ||
'https://api.github.com/search/code?q=bar+in:path+filename:bar+repo:octo/foo.txt', | ||
); | ||
}); | ||
}); |