jvcs 1.3.5 → 1.3.6

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.
@@ -0,0 +1,148 @@
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) q = `'${parentId}' in parents and ` + q
13
+
14
+ const res = await drive.files.list({
15
+ q,
16
+ fields: "files(id, name)",
17
+ spaces: "drive"
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
@@ -0,0 +1,148 @@
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) q = `'${parentId}' in parents and ` + q
13
+
14
+ const res = await drive.files.list({
15
+ q,
16
+ fields: "files(id, name)",
17
+ spaces: "drive"
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
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "jvcs",
3
+ "version": "1.3.6",
4
+ "bin": {
5
+ "jvcs": "./index.js"
6
+ },
7
+ "keywords": [],
8
+ "author": "",
9
+ "license": "ISC",
10
+ "dependencies": {
11
+ "chalk": "^4.1.2",
12
+ "dotenv": "^17.2.3",
13
+ "googleapis": "^164.1.0",
14
+ "inquirer": "^8.2.7",
15
+ "uuid": "^13.0.0",
16
+ "validator": "^13.15.20",
17
+ "yargs": "^18.0.0"
18
+ }
19
+ }
@@ -9,7 +9,7 @@ async function findFolderIdByName(name,parentId=null) {
9
9
 
10
10
  const safeName = name.replace(/'/g, "\\'");
11
11
  let q = `name='${safeName}' and mimeType='application/vnd.google-apps.folder' and trashed=false`;
12
- if (parentId) q = `'${parentId}' in parents and` + q
12
+ if (parentId) q = `'${parentId}' in parents and ` + q
13
13
 
14
14
  const res = await drive.files.list({
15
15
  q,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jvcs",
3
- "version": "1.3.5",
3
+ "version": "1.3.6",
4
4
  "bin": {
5
5
  "jvcs": "./index.js"
6
6
  },