@@ -3,20 +3,17 @@ const path = require("path");
3
3
const util = require ( "util" ) ;
4
4
const execPromisified = util . promisify ( exec ) ;
5
5
const fs = require ( "fs" ) ;
6
-
7
- function getEnvData ( ) {
8
- const envFileData = fs . readFileSync (
9
- path . join ( __dirname , ".." , "env_config.json" )
10
- ) ;
11
-
12
- const envContent = envFileData . toString ( ) ;
13
- let envData = JSON . parse ( envContent ) [ 0 ] ;
14
-
15
- return {
16
- DATABASE_FILE : envData . databaseFile ,
17
- GITCONVEX_PORT : envData . port ,
18
- } ;
19
- }
6
+ const { getEnvData } = require ( "../utils/getEnvData" ) ;
7
+ const { gitCommitLogToDb } = require ( "../utils/sqliteDbAccess" ) ;
8
+
9
+ /**
10
+ * @param {String } repoName - Name of the repository
11
+ * @param {String } repoPath - Path were the repo is residing or where it should be cloned / initialized
12
+ * @param {boolean } initCheck - Switch to check if user has selected repo init option
13
+ * @param {boolean } cloneCheck - Switch to check if user has selected repo cloning option
14
+ * @param {String } cloneUrl - The If cloning switch is true, then this holds the URL of the remote repo
15
+ * @returns {Object } - created a new entry in the data file and retusn the status
16
+ */
20
17
21
18
async function addRepoHandler (
22
19
repoName ,
@@ -35,44 +32,58 @@ async function addRepoHandler(
35
32
}
36
33
37
34
function successResponse ( ) {
35
+ gitCommitLogToDb ( ) ;
38
36
return {
39
37
message : "REPO_DATA_UPDATED" ,
40
38
repoId : id ,
41
39
} ;
42
40
}
43
41
44
42
if ( cloneCheck ) {
45
- const cloneStatus = await execPromisified (
46
- `git clone "${ cloneUrl } " "./${ repoName } "` ,
47
- {
48
- cwd : repoPath ,
49
- windowsHide : true ,
43
+ try {
44
+ if ( cloneUrl . match ( / [ ^ a - z A - Z 0 - 9 - _ .~ @ # $ % : / ] / gi) ) {
45
+ throw new Error ( "Invalid clone URL string!" ) ;
46
+ }
47
+
48
+ if ( repoName . match ( / [ ^ a - z A - Z 0 - 9 - _ .\\ s ] / gi) ) {
49
+ throw new Error ( "Invalid repo name string!" ) ;
50
50
}
51
- )
52
- . then ( ( { stdout, stderr } ) => {
53
- console . log ( stdout ) ;
54
- console . log ( stderr ) ;
55
- if ( stdout || stderr ) {
51
+
52
+ const cloneStatus = await execPromisified (
53
+ `git clone "${ cloneUrl } " "./${ repoName } "` ,
54
+ {
55
+ cwd : repoPath ,
56
+ windowsHide : true ,
57
+ }
58
+ )
59
+ . then ( ( { stdout, stderr } ) => {
56
60
console . log ( stdout ) ;
57
- return true ;
58
- } else {
61
+ console . log ( stderr ) ;
62
+ if ( stdout || stderr ) {
63
+ console . log ( stdout ) ;
64
+ return true ;
65
+ } else {
66
+ return false ;
67
+ }
68
+ } )
69
+ . catch ( ( err ) => {
70
+ console . log ( err ) ;
59
71
return false ;
60
- }
61
- } )
62
- . catch ( ( err ) => {
63
- console . log ( err ) ;
64
- return false ;
65
- } ) ;
72
+ } ) ;
66
73
67
- console . log ( "CLONE STAT : " , cloneStatus ) ;
74
+ console . log ( "CLONE STAT : " , cloneStatus ) ;
68
75
69
- if ( cloneStatus ) {
70
- if ( repoPath . includes ( "\\" ) ) {
71
- repoPath = repoPath + "\\" + repoName ;
76
+ if ( cloneStatus ) {
77
+ if ( repoPath . includes ( "\\" ) ) {
78
+ repoPath = repoPath + "\\" + repoName ;
79
+ } else {
80
+ repoPath = repoPath + "/" + repoName ;
81
+ }
72
82
} else {
73
- repoPath = repoPath + "/" + repoName ;
83
+ return errorResponse ( ) ;
74
84
}
75
- } else {
85
+ } catch ( err ) {
86
+ console . log ( err ) ;
76
87
return errorResponse ( ) ;
77
88
}
78
89
}
0 commit comments