jvcs 1.3.9 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/controllers/clone.js +4 -19
  2. package/controllers/login.js +2 -2
  3. package/controllers/verifyOtp.js +2 -2
  4. package/index.js +1 -1
  5. package/package.json +1 -1
  6. package/.history/controllers/clone_20251207132416.js +0 -148
  7. package/.history/controllers/clone_20251209174929.js +0 -148
  8. package/.history/controllers/clone_20251209174930.js +0 -148
  9. package/.history/controllers/clone_20251209174932.js +0 -148
  10. package/.history/controllers/clone_20251209174937.js +0 -148
  11. package/.history/controllers/clone_20251209174942.js +0 -148
  12. package/.history/controllers/clone_20251209174945.js +0 -148
  13. package/.history/controllers/clone_20251209174952.js +0 -148
  14. package/.history/controllers/clone_20251209174954.js +0 -147
  15. package/.history/controllers/clone_20251209175017.js +0 -147
  16. package/.history/controllers/clone_20251209175039.js +0 -147
  17. package/.history/controllers/clone_20251209175328.js +0 -148
  18. package/.history/controllers/clone_20251209180150.js +0 -148
  19. package/.history/controllers/clone_20251209180151.js +0 -148
  20. package/.history/controllers/clone_20251209180305.js +0 -148
  21. package/.history/controllers/clone_20251209180307.js +0 -148
  22. package/.history/controllers/clone_20251209180311.js +0 -148
  23. package/.history/controllers/clone_20251209180318.js +0 -148
  24. package/.history/controllers/clone_20251209180322.js +0 -148
  25. package/.history/controllers/clone_20251209180326.js +0 -148
  26. package/.history/controllers/clone_20251209180329.js +0 -148
  27. package/.history/controllers/clone_20251209180331.js +0 -148
  28. package/.history/controllers/clone_20251209180341.js +0 -148
  29. package/.history/controllers/clone_20251209180346.js +0 -148
  30. package/.history/controllers/clone_20251209180359.js +0 -148
  31. package/.history/controllers/clone_20251209180406.js +0 -148
  32. package/.history/controllers/clone_20251209180559.js +0 -150
  33. package/.history/controllers/clone_20251209180609.js +0 -152
  34. package/.history/controllers/clone_20251209180625.js +0 -153
  35. package/.history/controllers/clone_20251209180645.js +0 -153
  36. package/.history/controllers/clone_20251209181759.js +0 -163
  37. package/.history/controllers/clone_20251209181801.js +0 -163
  38. package/.history/controllers/clone_20251209181845.js +0 -163
  39. package/.history/package_20251207161744.json +0 -19
  40. package/.history/package_20251209175047.json +0 -19
  41. package/.history/package_20251209175339.json +0 -19
  42. package/.history/package_20251209180154.json +0 -19
  43. package/.history/package_20251209180409.json +0 -19
  44. package/.history/package_20251209180410.json +0 -19
  45. package/.history/package_20251209180648.json +0 -19
  46. package/.history/package_20251209181850.json +0 -19
@@ -8,21 +8,16 @@ const drive = getDriveClient()
8
8
  async function findFolderIdByName(name,parentId=null) {
9
9
 
10
10
  const safeName = name.replace(/'/g, "\\'");
11
- let q = `(name='${safeName}') and (mimeType='application/vnd.google-apps.folder') and (trashed=false)`;
12
- if (parentId) q = `('${parentId}' in parents) and ` + q
13
-
14
- console.log("Executing Drive Query:", q); // DEBUG
11
+ const parts = [`name='${safeName}'`, "trashed=false"];
12
+ if (parentId) parts.push(`'${parentId}' in parents`);
15
13
 
14
+ const q = parts.join(" and ");
16
15
  const res = await drive.files.list({
17
16
  q,
18
- fields: "files(id, name, mimeType)",
19
- spaces: "drive",
20
- includeItemsFromAllDrives: true,
21
- supportsAllDrives: true
17
+ fields: "files(id, name)",
22
18
  });
23
19
 
24
20
  const files = (res.data && res.data.files) || [];
25
- console.log("Search result:", files); // DEBUG
26
21
  if (files.length === 0) return null;
27
22
  return files[0].id;
28
23
  }
@@ -131,16 +126,6 @@ async function cloneCmd(username,reponame) {
131
126
  const userFolder = await findFolderIdByName(username, rootFolder);
132
127
  if (!userFolder) throw new Error(`User folder '${username}' not found.`);
133
128
 
134
- console.log("\nListing folders under user:", username);
135
-
136
- const list = await drive.files.list({
137
- q: `'${userFolder}' in parents and mimeType='application/vnd.google-apps.folder' and trashed=false`,
138
- fields: "files(id, name)"
139
- });
140
-
141
- console.log("Folders inside user folder:", list.data.files);
142
-
143
-
144
129
  const repoFolder = await findFolderIdByName(reponame, userFolder);
145
130
  if (!repoFolder) throw new Error(`Repository folder '${reponame}' not found.`);
146
131
 
@@ -23,11 +23,11 @@ async function login(loginData) {
23
23
  }
24
24
 
25
25
  const configPath = path.join(dirPath, "config.json");
26
- fs.writeFileSync(configPath,JSON.stringify({email: loginData.email,username: loginData.username,token: data.token, CLIENT_ID:"835069827989-3spob55ioa2ocudi3mo8u2ni2ecqohh7.apps.googleusercontent.com",CLIENT_SECRET:"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",REDIRECT_URI:"https://developers.google.com/oauthplayground",REFRESH_TOKEN:"1//04K1Dngxu-L1LCgYIARAAGAQSNwF-L9IrU-ouGKWovW-4uH9M2xloDeE2oEgg8cRusediXHrX7Jec5JupLsyNBe4Yus9BbtOM1Rs"},null,2));
26
+ fs.writeFileSync(configPath,JSON.stringify({email: loginData.email,username: loginData.username,token: data.token, CLIENT_ID:"835069827989-3spob55ioa2ocudi3mo8u2ni2ecqohh7.apps.googleusercontent.com",CLIENT_SECRET:"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",REDIRECT_URI:"https://developers.google.com/oauthplayground",REFRESH_TOKEN:"1//04K_J7FFHF_jgCgYIARAAGAQSNwF-L9Ir9fMgTe1C8MQf0CCTDcIKXg1qjxAtAhtRZQ8_9xG82__9VQ935CMLTzuBDB9B_24tzLU"},null,2));
27
27
  }
28
28
  else {
29
29
  throw new Error(data.message)
30
30
  }
31
31
  }
32
32
 
33
- module.exports = login
33
+ module.exports = login
@@ -41,7 +41,7 @@ async function verifyOtp(signupData) {
41
41
  }
42
42
 
43
43
  const configPath = path.join(dirPath, "config.json");
44
- fs.writeFileSync(configPath,JSON.stringify({email: signupData.email,username: signupData.username,token: data.token, CLIENT_ID:"835069827989-3spob55ioa2ocudi3mo8u2ni2ecqohh7.apps.googleusercontent.com",CLIENT_SECRET:"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",REDIRECT_URI:"https://developers.google.com/oauthplayground",REFRESH_TOKEN:"1//04K1Dngxu-L1LCgYIARAAGAQSNwF-L9IrU-ouGKWovW-4uH9M2xloDeE2oEgg8cRusediXHrX7Jec5JupLsyNBe4Yus9BbtOM1Rs"},null,2));
44
+ fs.writeFileSync(configPath,JSON.stringify({email: signupData.email,username: signupData.username,token: data.token, CLIENT_ID:"835069827989-3spob55ioa2ocudi3mo8u2ni2ecqohh7.apps.googleusercontent.com",CLIENT_SECRET:"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",REDIRECT_URI:"https://developers.google.com/oauthplayground",REFRESH_TOKEN:"1//04K_J7FFHF_jgCgYIARAAGAQSNwF-L9Ir9fMgTe1C8MQf0CCTDcIKXg1qjxAtAhtRZQ8_9xG82__9VQ935CMLTzuBDB9B_24tzLU"},null,2));
45
45
  }
46
46
  else if(data.status === "user") {
47
47
  console.log(chalk.yellow(data.message))
@@ -52,4 +52,4 @@ async function verifyOtp(signupData) {
52
52
 
53
53
  }
54
54
 
55
- module.exports = verifyOtp
55
+ module.exports = verifyOtp
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env node
1
+ #!/usr/bin/env node
2
2
 
3
3
  const yargs = require("yargs");
4
4
  const { hideBin } = require("yargs/helpers");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jvcs",
3
- "version": "1.3.9",
3
+ "version": "1.4.0",
4
4
  "bin": {
5
5
  "jvcs": "./index.js"
6
6
  },
@@ -1,148 +0,0 @@
1
- const fs = require("fs");
2
- const path = require("path");
3
- const chalk = require("chalk");
4
- const { checkGlobalConfig, getGlobalConfig } = require("./utility");
5
- const { getDriveClient } = require("../config/drive-config");
6
- const drive = getDriveClient()
7
-
8
- async function findFolderIdByName(name,parentId=null) {
9
-
10
- const safeName = name.replace(/'/g, "\\'");
11
- const parts = [`name='${safeName}'`, "trashed=false"];
12
- if (parentId) parts.push(`'${parentId}' in parents`);
13
-
14
- const q = parts.join(" and ");
15
- const res = await drive.files.list({
16
- q,
17
- fields: "files(id, name)",
18
- });
19
-
20
- const files = (res.data && res.data.files) || [];
21
- if (files.length === 0) return null;
22
- return files[0].id;
23
- }
24
-
25
- async function downloadFolderFromDrive(folderId, destPath) {
26
- try {
27
- fs.mkdirSync(destPath, { recursive: true });
28
-
29
- const res = await drive.files.list({
30
- q: `'${folderId}' in parents and trashed=false`,
31
- fields: "files(id, name, mimeType)",
32
- });
33
-
34
- const files = res.data.files;
35
- if(!files.length) return;
36
-
37
- for(const file of files) {
38
-
39
- if(file.name === "jvcs_hashcode.json" || file.name === "meta.json")
40
- continue
41
-
42
- const filePath = path.join(destPath, file.name);
43
-
44
- if(file.mimeType === "application/vnd.google-apps.folder") {
45
- // create folder locally
46
- if(!fs.existsSync(filePath)) fs.mkdirSync(filePath);
47
- await downloadFolderFromDrive(file.id, filePath);
48
- }
49
- else {
50
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
51
- const dest = fs.createWriteStream(filePath);
52
- const resFile = await drive.files.get(
53
- { fileId: file.id, alt: "media" },
54
- { responseType: "stream" }
55
- )
56
-
57
- await new Promise((resolve,reject)=> {
58
- resFile.data.on("end", resolve)
59
- .on("error",reject)
60
- .pipe(dest)
61
- })
62
- }
63
-
64
- console.log(chalk.gray(`Downloaded: ${file.name}`));
65
- }
66
- }
67
- catch (err) {
68
- console.log(chalk.red("Error downloading folder: " + err.message));
69
- }
70
- }
71
-
72
-
73
- async function getVisibilityAndLatestCommit(username,reponame,configData) {
74
-
75
- try {
76
-
77
- const response = await fetch("https://version-control-system-mebn.onrender.com/cloneRepo", {
78
- method:"POST",
79
- headers: {
80
- 'Content-Type':"application/json"
81
- },
82
- body: JSON.stringify({username,reponame,token:configData.token})
83
- })
84
-
85
- const data = await response.json()
86
- return data
87
- }
88
- catch(error) {
89
- console.log(chalk.red(error || error.message))
90
- }
91
- }
92
-
93
- async function cloneCmd(username,reponame) {
94
-
95
- if(!username || !reponame) {
96
- console.log(chalk.red("Path is not provided properly"))
97
- return
98
- }
99
-
100
- if(!checkGlobalConfig()) {
101
- console.log(chalk.red("No existing session found. Please login or signup."));
102
- console.log(chalk.green("jvcs --help for help"));
103
- return;
104
- }
105
-
106
- const configData = getGlobalConfig();
107
- if(!configData) {
108
- console.log(chalk.red("No existing session found. Please login or signup."));
109
- return;
110
- }
111
-
112
- console.log(username,reponame)
113
-
114
- const response = await getVisibilityAndLatestCommit(username,reponame,configData)
115
- if(response.status === false) {
116
- console.log(chalk.red(`${response.message}`))
117
- return
118
- }
119
-
120
- const latestCommit = response.commitId
121
- // find the repo from drive GithubClone/username/reponame/commit_lastestCommit
122
-
123
- const rootFolder = await findFolderIdByName("GithubClone")
124
- if(!rootFolder) throw new Error("Root folder not found on cloud.");
125
-
126
- const userFolder = await findFolderIdByName(username, rootFolder);
127
- if (!userFolder) throw new Error(`User folder '${username}' not found.`);
128
-
129
- const repoFolder = await findFolderIdByName(reponame, userFolder);
130
- if (!repoFolder) throw new Error(`Repository folder '${reponame}' not found.`);
131
-
132
- const commitFolder = await findFolderIdByName(`commit_${latestCommit}`, repoFolder);
133
- if (!commitFolder) throw new Error(`Commit folder 'commit_${latestCommit}' not found.`);
134
-
135
- console.log(chalk.green(`Found repository, downloading...`));
136
-
137
- const destPath = path.join(process.cwd(), reponame);
138
- if(fs.existsSync(destPath)) {
139
- console.log(chalk.red(`Destination '${reponame}' already exists. Remove or rename it and retry.`));
140
- return;
141
- }
142
-
143
- await downloadFolderFromDrive(commitFolder, destPath);
144
-
145
- console.log(chalk.green(`Repository cloned successfully into ./${reponame}`));
146
- }
147
-
148
- module.exports = cloneCmd
@@ -1,148 +0,0 @@
1
- const fs = require("fs");
2
- const path = require("path");
3
- const chalk = require("chalk");
4
- const { checkGlobalConfig, getGlobalConfig } = require("./utility");
5
- const { getDriveClient } = require("../config/drive-config");
6
- const drive = getDriveClient()
7
-
8
- async function findFolderIdByName(name,parentId=null) {
9
-
10
- const safeName = name.replace(/'/g, "\\'");
11
-
12
- if (parentId) parts.push(`'${parentId}' in parents`);
13
-
14
- const q = parts.join(" and ");
15
- const res = await drive.files.list({
16
- q,
17
- fields: "files(id, name)",
18
- });
19
-
20
- const files = (res.data && res.data.files) || [];
21
- if (files.length === 0) return null;
22
- return files[0].id;
23
- }
24
-
25
- async function downloadFolderFromDrive(folderId, destPath) {
26
- try {
27
- fs.mkdirSync(destPath, { recursive: true });
28
-
29
- const res = await drive.files.list({
30
- q: `'${folderId}' in parents and trashed=false`,
31
- fields: "files(id, name, mimeType)",
32
- });
33
-
34
- const files = res.data.files;
35
- if(!files.length) return;
36
-
37
- for(const file of files) {
38
-
39
- if(file.name === "jvcs_hashcode.json" || file.name === "meta.json")
40
- continue
41
-
42
- const filePath = path.join(destPath, file.name);
43
-
44
- if(file.mimeType === "application/vnd.google-apps.folder") {
45
- // create folder locally
46
- if(!fs.existsSync(filePath)) fs.mkdirSync(filePath);
47
- await downloadFolderFromDrive(file.id, filePath);
48
- }
49
- else {
50
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
51
- const dest = fs.createWriteStream(filePath);
52
- const resFile = await drive.files.get(
53
- { fileId: file.id, alt: "media" },
54
- { responseType: "stream" }
55
- )
56
-
57
- await new Promise((resolve,reject)=> {
58
- resFile.data.on("end", resolve)
59
- .on("error",reject)
60
- .pipe(dest)
61
- })
62
- }
63
-
64
- console.log(chalk.gray(`Downloaded: ${file.name}`));
65
- }
66
- }
67
- catch (err) {
68
- console.log(chalk.red("Error downloading folder: " + err.message));
69
- }
70
- }
71
-
72
-
73
- async function getVisibilityAndLatestCommit(username,reponame,configData) {
74
-
75
- try {
76
-
77
- const response = await fetch("https://version-control-system-mebn.onrender.com/cloneRepo", {
78
- method:"POST",
79
- headers: {
80
- 'Content-Type':"application/json"
81
- },
82
- body: JSON.stringify({username,reponame,token:configData.token})
83
- })
84
-
85
- const data = await response.json()
86
- return data
87
- }
88
- catch(error) {
89
- console.log(chalk.red(error || error.message))
90
- }
91
- }
92
-
93
- async function cloneCmd(username,reponame) {
94
-
95
- if(!username || !reponame) {
96
- console.log(chalk.red("Path is not provided properly"))
97
- return
98
- }
99
-
100
- if(!checkGlobalConfig()) {
101
- console.log(chalk.red("No existing session found. Please login or signup."));
102
- console.log(chalk.green("jvcs --help for help"));
103
- return;
104
- }
105
-
106
- const configData = getGlobalConfig();
107
- if(!configData) {
108
- console.log(chalk.red("No existing session found. Please login or signup."));
109
- return;
110
- }
111
-
112
- console.log(username,reponame)
113
-
114
- const response = await getVisibilityAndLatestCommit(username,reponame,configData)
115
- if(response.status === false) {
116
- console.log(chalk.red(`${response.message}`))
117
- return
118
- }
119
-
120
- const latestCommit = response.commitId
121
- // find the repo from drive GithubClone/username/reponame/commit_lastestCommit
122
-
123
- const rootFolder = await findFolderIdByName("GithubClone")
124
- if(!rootFolder) throw new Error("Root folder not found on cloud.");
125
-
126
- const userFolder = await findFolderIdByName(username, rootFolder);
127
- if (!userFolder) throw new Error(`User folder '${username}' not found.`);
128
-
129
- const repoFolder = await findFolderIdByName(reponame, userFolder);
130
- if (!repoFolder) throw new Error(`Repository folder '${reponame}' not found.`);
131
-
132
- const commitFolder = await findFolderIdByName(`commit_${latestCommit}`, repoFolder);
133
- if (!commitFolder) throw new Error(`Commit folder 'commit_${latestCommit}' not found.`);
134
-
135
- console.log(chalk.green(`Found repository, downloading...`));
136
-
137
- const destPath = path.join(process.cwd(), reponame);
138
- if(fs.existsSync(destPath)) {
139
- console.log(chalk.red(`Destination '${reponame}' already exists. Remove or rename it and retry.`));
140
- return;
141
- }
142
-
143
- await downloadFolderFromDrive(commitFolder, destPath);
144
-
145
- console.log(chalk.green(`Repository cloned successfully into ./${reponame}`));
146
- }
147
-
148
- module.exports = cloneCmd
@@ -1,148 +0,0 @@
1
- const fs = require("fs");
2
- const path = require("path");
3
- const chalk = require("chalk");
4
- const { checkGlobalConfig, getGlobalConfig } = require("./utility");
5
- const { getDriveClient } = require("../config/drive-config");
6
- const drive = getDriveClient()
7
-
8
- async function findFolderIdByName(name,parentId=null) {
9
-
10
- const safeName = name.replace(/'/g, "\\'");
11
- let q = `name='${safeName}' and mimeType='application/vnd.google-apps.folder' and trashed=false`;
12
- if (parentId) parts.push(`'${parentId}' in parents`);
13
-
14
- const q = parts.join(" and ");
15
- const res = await drive.files.list({
16
- q,
17
- fields: "files(id, name)",
18
- });
19
-
20
- const files = (res.data && res.data.files) || [];
21
- if (files.length === 0) return null;
22
- return files[0].id;
23
- }
24
-
25
- async function downloadFolderFromDrive(folderId, destPath) {
26
- try {
27
- fs.mkdirSync(destPath, { recursive: true });
28
-
29
- const res = await drive.files.list({
30
- q: `'${folderId}' in parents and trashed=false`,
31
- fields: "files(id, name, mimeType)",
32
- });
33
-
34
- const files = res.data.files;
35
- if(!files.length) return;
36
-
37
- for(const file of files) {
38
-
39
- if(file.name === "jvcs_hashcode.json" || file.name === "meta.json")
40
- continue
41
-
42
- const filePath = path.join(destPath, file.name);
43
-
44
- if(file.mimeType === "application/vnd.google-apps.folder") {
45
- // create folder locally
46
- if(!fs.existsSync(filePath)) fs.mkdirSync(filePath);
47
- await downloadFolderFromDrive(file.id, filePath);
48
- }
49
- else {
50
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
51
- const dest = fs.createWriteStream(filePath);
52
- const resFile = await drive.files.get(
53
- { fileId: file.id, alt: "media" },
54
- { responseType: "stream" }
55
- )
56
-
57
- await new Promise((resolve,reject)=> {
58
- resFile.data.on("end", resolve)
59
- .on("error",reject)
60
- .pipe(dest)
61
- })
62
- }
63
-
64
- console.log(chalk.gray(`Downloaded: ${file.name}`));
65
- }
66
- }
67
- catch (err) {
68
- console.log(chalk.red("Error downloading folder: " + err.message));
69
- }
70
- }
71
-
72
-
73
- async function getVisibilityAndLatestCommit(username,reponame,configData) {
74
-
75
- try {
76
-
77
- const response = await fetch("https://version-control-system-mebn.onrender.com/cloneRepo", {
78
- method:"POST",
79
- headers: {
80
- 'Content-Type':"application/json"
81
- },
82
- body: JSON.stringify({username,reponame,token:configData.token})
83
- })
84
-
85
- const data = await response.json()
86
- return data
87
- }
88
- catch(error) {
89
- console.log(chalk.red(error || error.message))
90
- }
91
- }
92
-
93
- async function cloneCmd(username,reponame) {
94
-
95
- if(!username || !reponame) {
96
- console.log(chalk.red("Path is not provided properly"))
97
- return
98
- }
99
-
100
- if(!checkGlobalConfig()) {
101
- console.log(chalk.red("No existing session found. Please login or signup."));
102
- console.log(chalk.green("jvcs --help for help"));
103
- return;
104
- }
105
-
106
- const configData = getGlobalConfig();
107
- if(!configData) {
108
- console.log(chalk.red("No existing session found. Please login or signup."));
109
- return;
110
- }
111
-
112
- console.log(username,reponame)
113
-
114
- const response = await getVisibilityAndLatestCommit(username,reponame,configData)
115
- if(response.status === false) {
116
- console.log(chalk.red(`${response.message}`))
117
- return
118
- }
119
-
120
- const latestCommit = response.commitId
121
- // find the repo from drive GithubClone/username/reponame/commit_lastestCommit
122
-
123
- const rootFolder = await findFolderIdByName("GithubClone")
124
- if(!rootFolder) throw new Error("Root folder not found on cloud.");
125
-
126
- const userFolder = await findFolderIdByName(username, rootFolder);
127
- if (!userFolder) throw new Error(`User folder '${username}' not found.`);
128
-
129
- const repoFolder = await findFolderIdByName(reponame, userFolder);
130
- if (!repoFolder) throw new Error(`Repository folder '${reponame}' not found.`);
131
-
132
- const commitFolder = await findFolderIdByName(`commit_${latestCommit}`, repoFolder);
133
- if (!commitFolder) throw new Error(`Commit folder 'commit_${latestCommit}' not found.`);
134
-
135
- console.log(chalk.green(`Found repository, downloading...`));
136
-
137
- const destPath = path.join(process.cwd(), reponame);
138
- if(fs.existsSync(destPath)) {
139
- console.log(chalk.red(`Destination '${reponame}' already exists. Remove or rename it and retry.`));
140
- return;
141
- }
142
-
143
- await downloadFolderFromDrive(commitFolder, destPath);
144
-
145
- console.log(chalk.green(`Repository cloned successfully into ./${reponame}`));
146
- }
147
-
148
- module.exports = cloneCmd
@@ -1,148 +0,0 @@
1
- const fs = require("fs");
2
- const path = require("path");
3
- const chalk = require("chalk");
4
- const { checkGlobalConfig, getGlobalConfig } = require("./utility");
5
- const { getDriveClient } = require("../config/drive-config");
6
- const drive = getDriveClient()
7
-
8
- async function findFolderIdByName(name,parentId=null) {
9
-
10
- const safeName = name.replace(/'/g, "\\'");
11
- let q = `name='${safeName}' and mimeType='application/vnd.google-apps.folder' and trashed=false`;
12
- if (parentId) parts.push(`'${parentId}' in parents`);
13
-
14
- const q = parts.join(" and ");
15
- const res = await drive.files.list({
16
- q,
17
- fields: "files(id, name)",
18
- });
19
-
20
- const files = (res.data && res.data.files) || [];
21
- if (files.length === 0) return null;
22
- return files[0].id;
23
- }
24
-
25
- async function downloadFolderFromDrive(folderId, destPath) {
26
- try {
27
- fs.mkdirSync(destPath, { recursive: true });
28
-
29
- const res = await drive.files.list({
30
- q: `'${folderId}' in parents and trashed=false`,
31
- fields: "files(id, name, mimeType)",
32
- });
33
-
34
- const files = res.data.files;
35
- if(!files.length) return;
36
-
37
- for(const file of files) {
38
-
39
- if(file.name === "jvcs_hashcode.json" || file.name === "meta.json")
40
- continue
41
-
42
- const filePath = path.join(destPath, file.name);
43
-
44
- if(file.mimeType === "application/vnd.google-apps.folder") {
45
- // create folder locally
46
- if(!fs.existsSync(filePath)) fs.mkdirSync(filePath);
47
- await downloadFolderFromDrive(file.id, filePath);
48
- }
49
- else {
50
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
51
- const dest = fs.createWriteStream(filePath);
52
- const resFile = await drive.files.get(
53
- { fileId: file.id, alt: "media" },
54
- { responseType: "stream" }
55
- )
56
-
57
- await new Promise((resolve,reject)=> {
58
- resFile.data.on("end", resolve)
59
- .on("error",reject)
60
- .pipe(dest)
61
- })
62
- }
63
-
64
- console.log(chalk.gray(`Downloaded: ${file.name}`));
65
- }
66
- }
67
- catch (err) {
68
- console.log(chalk.red("Error downloading folder: " + err.message));
69
- }
70
- }
71
-
72
-
73
- async function getVisibilityAndLatestCommit(username,reponame,configData) {
74
-
75
- try {
76
-
77
- const response = await fetch("https://version-control-system-mebn.onrender.com/cloneRepo", {
78
- method:"POST",
79
- headers: {
80
- 'Content-Type':"application/json"
81
- },
82
- body: JSON.stringify({username,reponame,token:configData.token})
83
- })
84
-
85
- const data = await response.json()
86
- return data
87
- }
88
- catch(error) {
89
- console.log(chalk.red(error || error.message))
90
- }
91
- }
92
-
93
- async function cloneCmd(username,reponame) {
94
-
95
- if(!username || !reponame) {
96
- console.log(chalk.red("Path is not provided properly"))
97
- return
98
- }
99
-
100
- if(!checkGlobalConfig()) {
101
- console.log(chalk.red("No existing session found. Please login or signup."));
102
- console.log(chalk.green("jvcs --help for help"));
103
- return;
104
- }
105
-
106
- const configData = getGlobalConfig();
107
- if(!configData) {
108
- console.log(chalk.red("No existing session found. Please login or signup."));
109
- return;
110
- }
111
-
112
- console.log(username,reponame)
113
-
114
- const response = await getVisibilityAndLatestCommit(username,reponame,configData)
115
- if(response.status === false) {
116
- console.log(chalk.red(`${response.message}`))
117
- return
118
- }
119
-
120
- const latestCommit = response.commitId
121
- // find the repo from drive GithubClone/username/reponame/commit_lastestCommit
122
-
123
- const rootFolder = await findFolderIdByName("GithubClone")
124
- if(!rootFolder) throw new Error("Root folder not found on cloud.");
125
-
126
- const userFolder = await findFolderIdByName(username, rootFolder);
127
- if (!userFolder) throw new Error(`User folder '${username}' not found.`);
128
-
129
- const repoFolder = await findFolderIdByName(reponame, userFolder);
130
- if (!repoFolder) throw new Error(`Repository folder '${reponame}' not found.`);
131
-
132
- const commitFolder = await findFolderIdByName(`commit_${latestCommit}`, repoFolder);
133
- if (!commitFolder) throw new Error(`Commit folder 'commit_${latestCommit}' not found.`);
134
-
135
- console.log(chalk.green(`Found repository, downloading...`));
136
-
137
- const destPath = path.join(process.cwd(), reponame);
138
- if(fs.existsSync(destPath)) {
139
- console.log(chalk.red(`Destination '${reponame}' already exists. Remove or rename it and retry.`));
140
- return;
141
- }
142
-
143
- await downloadFolderFromDrive(commitFolder, destPath);
144
-
145
- console.log(chalk.green(`Repository cloned successfully into ./${reponame}`));
146
- }
147
-
148
- module.exports = cloneCmd