1
1
const { render} = require ( 'tree-from-paths' ) ;
2
- const { parse } = require ( 'node-html-parser' ) ;
3
2
4
3
chrome . runtime . onMessage . addListener ( ( { action} , sender , sendRes ) => {
5
4
if ( action === 'run_main' ) {
6
5
modalShown ( )
7
6
. then ( getName )
8
- . then ( goToFindFile )
9
- . then ( goToPathEndpoint )
7
+ . then ( getTree )
10
8
. then ( createTree )
11
9
. catch ( err => {
12
10
//TODO - Maybe do some clean up here
@@ -15,16 +13,7 @@ chrome.runtime.onMessage.addListener(({action}, sender, sendRes)=>{
15
13
}
16
14
} )
17
15
//Make a directory tree and inject it into HTML
18
- const createTree = async ( paths ) => {
19
- document . querySelector ( '#hubtree-modal-inner' ) . innerHTML = render (
20
- paths ,
21
- '' ,
22
- ( parent , file , explicit ) => {
23
- // return `<a class='link' href='${parent}${file}'>${parent}${file}</a><br>`
24
- return `${ parent } ${ file } <br>`
25
- }
26
- )
27
- }
16
+ const createTree = async ( paths ) => document . querySelector ( '#hubtree-modal-inner' ) . innerHTML = render ( paths , '' , ( parent , file , explicit ) => `${ file } <br>` ) ;
28
17
29
18
//Add modal to webpage
30
19
const addModal = async ( ) => {
@@ -54,18 +43,14 @@ const getName = async () => {
54
43
if ( name && name . includes ( '/' ) ) return name ;
55
44
throw new Error ( 'Name does not exist' )
56
45
}
57
- //Go to find file to get the path for the tree-list endpoint
58
- const goToFindFile = async ( name ) => {
59
- const findFileUrl = `https://github.com/${ name } /find/master` ;
60
- const htmlStr = await fetch ( findFileUrl ) . then ( res => res . text ( ) ) ;
61
- const root = parse ( htmlStr ) ;
62
- const path = root . querySelector ( 'fuzzy-list' ) . getAttribute ( 'data-url' ) ;
63
- return path
64
- }
65
- //Make a request to the endpoint that gives us all files strings in an array.
66
- const goToPathEndpoint = async ( endpoint ) => {
67
- const getPathsUrl = `https://github.com${ endpoint } ` ;
68
- const paths = ( await fetch ( getPathsUrl ) . then ( res => res . json ( ) ) ) . paths ;
69
- if ( paths ) return paths ;
46
+
47
+ const getTree = async ( name ) => {
48
+ const endpoint = `https://api.github.com/repos/${ name } /git/trees/master?recursive=1`
49
+ const treeData = await fetch ( endpoint ) . then ( res => {
50
+ if ( res . ok ) return res . json ( ) ;
51
+ throw new Error ( 'Problem with request' )
52
+ } )
53
+ const paths = treeData . tree . map ( item => item . path ) ;
54
+ if ( paths . length > 0 ) return paths ;
70
55
throw new Error ( 'No paths' ) ;
71
56
}
0 commit comments