Skip to content

Commit 6ea892c

Browse files
author
Gyumeijie
committed
feat: finish parsing the repo url
1 parent 1b81f49 commit 6ea892c

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.idea/*
2+
node_modules/*

download.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var url = require('url');
2+
3+
4+
const AUTHOR = 1;
5+
const REPOSITORY = 2;
6+
const BRANCH = 4;
7+
8+
9+
var parameters = {
10+
url: "https://github.com/reduxjs/redux/tree/master/examples/async",
11+
fileName: undefined,
12+
rootDirectory: undefined
13+
};
14+
15+
16+
function parseInfo(parameters) {
17+
var repoPath = url.parse(parameters.url, true).pathname;
18+
var splitPath = repoPath.split("/");
19+
var info = {};
20+
21+
info.author = splitPath[AUTHOR];
22+
info.repository = splitPath[REPOSITORY];
23+
info.branch = splitPath[BRANCH];
24+
25+
info.rootName = splitPath[splitPath.length-1];
26+
if(!!splitPath[BRANCH]){
27+
info.resPath = repoPath.substring(
28+
repoPath.indexOf(splitPath[BRANCH])+splitPath[BRANCH].length+1
29+
);
30+
}
31+
32+
info.urlPrefix = "https://api.github.com/repos/"+ info.author+"/"+info.repository+"/contents/";
33+
info.urlPostfix = "?ref="+info.branch;
34+
35+
if(!parameters.fileName || parameters.fileName == ""){
36+
info.downloadFileName = info.rootName;
37+
} else {
38+
info.downloadFileName = parameters.fileName;
39+
}
40+
41+
if(parameters.rootDirectory == "false"){
42+
info.rootDirectoryName = "";
43+
} else if (!parameters.rootDirectory || parameters.rootDirectory == "" ||
44+
parameters.rootDirectory == "true"){
45+
info.rootDirectoryName = info.rootName+"/";
46+
} else {
47+
info.rootDirectoryName = parameters.rootDirectory+"/";
48+
}
49+
50+
return info;
51+
}
52+

0 commit comments

Comments
 (0)