@@ -11,7 +11,8 @@ var imageStyles = {
11
11
lineHeight : 22 ,
12
12
watermarkEnabled : true
13
13
} ;
14
-
14
+ var repoRegexp = new RegExp ( '((http(s){0,1})|(git@[\\w\\.]{1,}))(:(//){0,1})([\\w\\.@:/\\-~]{1,})(\\.git)(/){0,1}' ) ;
15
+ var errorBlock = document . getElementById ( 'invalid-message' ) ;
15
16
var watermarkText = 'Generated by `foldertotree.cc`' ;
16
17
17
18
var body = document . getElementsByTagName ( 'body' ) [ 0 ] ;
@@ -24,6 +25,15 @@ function openSelectFile() {
24
25
document . getElementById ( 'my-awesome-dropzone' ) . click ( ) ;
25
26
}
26
27
28
+ function showErrorMessage ( label ) {
29
+ errorBlock . style . display = 'block' ;
30
+ errorBlock . innerHTML = label ;
31
+ }
32
+
33
+ function hideErrorMessage ( ) {
34
+ errorBlock . style . display = 'none' ;
35
+ }
36
+
27
37
function initDownload ( ) {
28
38
loading . style . display = 'block' ;
29
39
var canvas = document . getElementById ( 'tree-canvas' ) ;
@@ -43,21 +53,27 @@ function retry() {
43
53
}
44
54
45
55
function generateFromGithub ( ) {
56
+ var githubUrl = document . getElementById ( 'github-url' ) . value ;
57
+ if ( ! repoRegexp . test ( githubUrl ) ) {
58
+ this . showErrorMessage ( 'Invalid url, please provide url in this format: `https://github.com/styopdev/folder-to-tree.git`' ) ;
59
+ return ;
60
+ }
61
+
46
62
loading . style . display = 'block' ;
47
- var githubUrl = 'https://cors-anywhere.herokuapp.com/' ;
48
- githubUrl += document . getElementById ( 'github-url' ) . value ;
49
- githubUrl += '/archive/master.zip' ;
63
+ var repoUrl = 'https://cors-anywhere.herokuapp.com/' ;
64
+ repoUrl += githubUrl . substr ( 0 , githubUrl . length - 4 ) ;
65
+ repoUrl += '/archive/master.zip' ;
50
66
// TODO add githubUrl validation;
51
67
52
- JSZipUtils . getBinaryContent ( githubUrl , function ( err , data ) {
68
+ JSZipUtils . getBinaryContent ( repoUrl , function ( err , data ) {
53
69
if ( err ) {
54
70
throw err ; // or handle err
55
71
}
56
72
57
73
unzip
58
74
. loadAsync ( data )
59
- . then ( function ( data2 ) {
60
- processFile ( data2 ) ;
75
+ . then ( function ( zipData ) {
76
+ processFile ( zipData ) ;
61
77
} ) ;
62
78
} ) ;
63
79
}
@@ -85,6 +101,7 @@ function toTree(fileNameList, files) {
85
101
}
86
102
87
103
function processFile ( zip ) {
104
+ hideErrorMessage ( ) ;
88
105
fileNames = Object . keys ( zip . files ) ;
89
106
fileNamesTree = toTree ( fileNames , zip . files ) ;
90
107
0 commit comments