@@ -11,7 +11,8 @@ var imageStyles = {
1111 lineHeight : 22 ,
1212 watermarkEnabled : true
1313} ;
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' ) ;
1516var watermarkText = 'Generated by `foldertotree.cc`' ;
1617
1718var body = document . getElementsByTagName ( 'body' ) [ 0 ] ;
@@ -24,6 +25,15 @@ function openSelectFile() {
2425 document . getElementById ( 'my-awesome-dropzone' ) . click ( ) ;
2526}
2627
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+
2737function initDownload ( ) {
2838 loading . style . display = 'block' ;
2939 var canvas = document . getElementById ( 'tree-canvas' ) ;
@@ -43,21 +53,27 @@ function retry() {
4353}
4454
4555function 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+
4662 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' ;
5066 // TODO add githubUrl validation;
5167
52- JSZipUtils . getBinaryContent ( githubUrl , function ( err , data ) {
68+ JSZipUtils . getBinaryContent ( repoUrl , function ( err , data ) {
5369 if ( err ) {
5470 throw err ; // or handle err
5571 }
5672
5773 unzip
5874 . loadAsync ( data )
59- . then ( function ( data2 ) {
60- processFile ( data2 ) ;
75+ . then ( function ( zipData ) {
76+ processFile ( zipData ) ;
6177 } ) ;
6278 } ) ;
6379}
@@ -85,6 +101,7 @@ function toTree(fileNameList, files) {
85101}
86102
87103function processFile ( zip ) {
104+ hideErrorMessage ( ) ;
88105 fileNames = Object . keys ( zip . files ) ;
89106 fileNamesTree = toTree ( fileNames , zip . files ) ;
90107
0 commit comments