jvcs 1.0.1 → 1.0.3

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 (53) hide show
  1. package/.env +4 -4
  2. package/.jvcs/HEAD +1 -1
  3. package/.jvcs/commits/3a1169c8-ec67-4ce5-8024-ab4f08d7b609/index.js +146 -0
  4. package/.jvcs/commits/3a1169c8-ec67-4ce5-8024-ab4f08d7b609/jvcs_hashcode.json +6 -0
  5. package/.jvcs/commits/3a1169c8-ec67-4ce5-8024-ab4f08d7b609/meta.json +7 -0
  6. package/.jvcs/commits/c362f97f-997b-499a-bb45-9e67f298f61e/index.js +146 -0
  7. package/.jvcs/commits/c362f97f-997b-499a-bb45-9e67f298f61e/jvcs_hashcode.json +6 -0
  8. package/.jvcs/commits/c362f97f-997b-499a-bb45-9e67f298f61e/meta.json +7 -0
  9. package/.jvcs/config.json +1 -1
  10. package/.jvcs/staging/index.js +146 -0
  11. package/.jvcs/staging/jvcs_hashcode.json +6 -1
  12. package/controllers/driveUtility.js +56 -0
  13. package/controllers/push.js +121 -111
  14. package/index.js +17 -2
  15. package/package.json +1 -1
  16. package/.jvcs/commits/292abee1-42ea-4ddf-b590-be7c98abaf45/jvcs_hashcode.json +0 -1
  17. package/.jvcs/commits/292abee1-42ea-4ddf-b590-be7c98abaf45/meta.json +0 -7
  18. package/.jvcs/commits/302af5ea-5789-4ee9-98fe-eab9308b2e27/jvcs_hashcode.json +0 -1
  19. package/.jvcs/commits/302af5ea-5789-4ee9-98fe-eab9308b2e27/meta.json +0 -7
  20. package/.jvcs/commits/376d44d4-c595-429e-b711-ae6ec7c9ef74/jvcs_hashcode.json +0 -1
  21. package/.jvcs/commits/376d44d4-c595-429e-b711-ae6ec7c9ef74/meta.json +0 -7
  22. package/.jvcs/commits/484ac37d-c1a9-4ddd-8796-6e3facda1e11/controllers/add.js +0 -122
  23. package/.jvcs/commits/484ac37d-c1a9-4ddd-8796-6e3facda1e11/controllers/begin.js +0 -201
  24. package/.jvcs/commits/484ac37d-c1a9-4ddd-8796-6e3facda1e11/controllers/commit.js +0 -82
  25. package/.jvcs/commits/484ac37d-c1a9-4ddd-8796-6e3facda1e11/controllers/init.js +0 -60
  26. package/.jvcs/commits/484ac37d-c1a9-4ddd-8796-6e3facda1e11/controllers/login.js +0 -33
  27. package/.jvcs/commits/484ac37d-c1a9-4ddd-8796-6e3facda1e11/controllers/pull.js +0 -98
  28. package/.jvcs/commits/484ac37d-c1a9-4ddd-8796-6e3facda1e11/controllers/push.js +0 -135
  29. package/.jvcs/commits/484ac37d-c1a9-4ddd-8796-6e3facda1e11/controllers/revert.js +0 -110
  30. package/.jvcs/commits/484ac37d-c1a9-4ddd-8796-6e3facda1e11/controllers/signup.js +0 -28
  31. package/.jvcs/commits/484ac37d-c1a9-4ddd-8796-6e3facda1e11/controllers/unstage.js +0 -96
  32. package/.jvcs/commits/484ac37d-c1a9-4ddd-8796-6e3facda1e11/controllers/utility.js +0 -28
  33. package/.jvcs/commits/484ac37d-c1a9-4ddd-8796-6e3facda1e11/controllers/verifyOtp.js +0 -55
  34. package/.jvcs/commits/484ac37d-c1a9-4ddd-8796-6e3facda1e11/jvcs_hashcode.json +0 -50
  35. package/.jvcs/commits/484ac37d-c1a9-4ddd-8796-6e3facda1e11/meta.json +0 -7
  36. package/.jvcs/commits/c3f6ff7a-13bd-4697-a80e-041f7dae8a09/controllers/add.js +0 -122
  37. package/.jvcs/commits/c3f6ff7a-13bd-4697-a80e-041f7dae8a09/controllers/begin.js +0 -201
  38. package/.jvcs/commits/c3f6ff7a-13bd-4697-a80e-041f7dae8a09/controllers/commit.js +0 -82
  39. package/.jvcs/commits/c3f6ff7a-13bd-4697-a80e-041f7dae8a09/controllers/init.js +0 -60
  40. package/.jvcs/commits/c3f6ff7a-13bd-4697-a80e-041f7dae8a09/controllers/login.js +0 -33
  41. package/.jvcs/commits/c3f6ff7a-13bd-4697-a80e-041f7dae8a09/controllers/pull.js +0 -98
  42. package/.jvcs/commits/c3f6ff7a-13bd-4697-a80e-041f7dae8a09/controllers/push.js +0 -135
  43. package/.jvcs/commits/c3f6ff7a-13bd-4697-a80e-041f7dae8a09/controllers/revert.js +0 -110
  44. package/.jvcs/commits/c3f6ff7a-13bd-4697-a80e-041f7dae8a09/controllers/signup.js +0 -28
  45. package/.jvcs/commits/c3f6ff7a-13bd-4697-a80e-041f7dae8a09/controllers/unstage.js +0 -96
  46. package/.jvcs/commits/c3f6ff7a-13bd-4697-a80e-041f7dae8a09/controllers/utility.js +0 -28
  47. package/.jvcs/commits/c3f6ff7a-13bd-4697-a80e-041f7dae8a09/controllers/verifyOtp.js +0 -55
  48. package/.jvcs/commits/c3f6ff7a-13bd-4697-a80e-041f7dae8a09/jvcs_hashcode.json +0 -50
  49. package/.jvcs/commits/c3f6ff7a-13bd-4697-a80e-041f7dae8a09/meta.json +0 -7
  50. package/.jvcs/commits/c7aa89bd-5016-4f21-8979-02e3c0a2c8ee/jvcs_hashcode.json +0 -1
  51. package/.jvcs/commits/c7aa89bd-5016-4f21-8979-02e3c0a2c8ee/meta.json +0 -7
  52. package/controllers/pull.js +0 -98
  53. package/controllers/revert.js +0 -110
@@ -1,135 +1,145 @@
1
- const fs = require("fs");
2
- const fsPromises = require("fs").promises;
3
- const path = require("path");
4
- const chalk = require("chalk");
5
- const { drive } = require("../config/drive-config");
6
- const saveData = require("../controllers/saveData");
7
- const { checkInitialization, checkRepoExists, getCLIConfig } = require("./utils");
8
-
9
- async function pushCmd(reponame) {
10
- try {
11
- if (!checkInitialization()) return;
12
- if (!checkRepoExists(reponame)) return;
1
+ const fs = require("fs")
2
+ const path = require("path")
3
+ const chalk = require("chalk")
4
+ const { drive } = require("../config/drive-config")
5
+ const { getGlobalConfig, checkGlobalConfig, checkforjvcs } = require("./utility")
6
+ const getDirectoryStructure = require("./driveUtility")
13
7
 
14
- const repoPath = path.join(process.cwd(), `.${reponame}`);
15
- const commitFolder = path.join(repoPath, "commits");
8
+ async function uploadFile(localFile, parentId) {
16
9
 
17
- const config = await getCLIConfig();
18
- if (!config) return console.log(chalk.red("Could not read CLI configuration."));
10
+ try {
19
11
 
20
- console.log(chalk.blue("Pushing commits for user:"), chalk.green(config.username));
12
+ const fileName = path.basename(localFile)
13
+ const fileMetaData = {
14
+ name: fileName,
15
+ parents: [parentId]
16
+ }
21
17
 
22
- // Check commits locally
23
- const commitDirs = await fsPromises.readdir(commitFolder);
24
- if (!commitDirs.length) {
25
- console.log(chalk.yellow("⚠ No commits to push."));
26
- return;
18
+ const media = {
19
+ mimeType: "application/octet-stream",
20
+ body: fs.createReadStream(localFile),
27
21
  }
28
22
 
29
- // Ensure user folder exists in Google Drive
30
- let userFolderId;
31
- const userFolderRes = await drive.files.list({
32
- q: `name='${config.username}' and '1ahuoMCN_Ls5kGF2KPUGLbRZb9kGVMe0V' in parents and mimeType='application/vnd.google-apps.folder' and trashed=false`,
33
- fields: "files(id, name)"
34
- });
23
+ const res = await drive.files.create({
24
+ resource: fileMetaData,
25
+ media,
26
+ fields: "id"
27
+ })
28
+
29
+ console.log(chalk.gray(` ↳ Uploaded: ${fileName}`));
30
+ return res.data.id;
31
+ }
32
+ catch(error) {
33
+ console.log(chalk.red(`Failed to upload ${filePath}: ${err.message}`));
34
+ }
35
+ }
36
+
37
+ async function uploadDirectory(localDir, parentId) {
38
+
39
+ const entries = fs.readdirSync(localDir, {withFileTypes: true})
40
+
41
+ for(const entry of entries) {
42
+
43
+ const entryPath = path.join(localDir,entry.name)
44
+
45
+ if(entry.isDirectory()) {
46
+ const folderMeta = {
47
+ name: entry.name,
48
+ mimeType: "application/vnd.google-apps.folder",
49
+ parents: [parentId],
50
+ }
35
51
 
36
- if (userFolderRes.data.files.length) {
37
- userFolderId = userFolderRes.data.files[0].id;
38
- } else {
39
52
  const folder = await drive.files.create({
40
- requestBody: {
41
- name: config.username,
42
- mimeType: "application/vnd.google-apps.folder",
43
- parents: ["1ahuoMCN_Ls5kGF2KPUGLbRZb9kGVMe0V"]
44
- },
45
- fields: "id, name"
46
- });
47
- userFolderId = folder.data.id;
48
- console.log(chalk.green(`Created user folder in Drive.`));
53
+ resource: folderMeta,
54
+ fields: "id"
55
+ })
56
+
57
+ await uploadDirectory(entryPath,folder.data.id)
58
+ }
59
+ else {
60
+ await uploadFile(entryPath,parentId)
49
61
  }
62
+ }
63
+ }
50
64
 
51
- // Ensure repo folder exists
52
- let repoFolderId;
53
- const repoFolderRes = await drive.files.list({
54
- q: `name='${reponame}' and '${userFolderId}' in parents and mimeType='application/vnd.google-apps.folder' and trashed=false`,
55
- fields: "files(id, name)"
56
- });
65
+ async function pushCmd() {
57
66
 
58
- if (repoFolderRes.data.files.length) {
59
- repoFolderId = repoFolderRes.data.files[0].id;
60
- } else {
61
- const folder = await drive.files.create({
62
- requestBody: {
63
- name: reponame,
64
- mimeType: "application/vnd.google-apps.folder",
65
- parents: [userFolderId]
66
- },
67
- fields: "id, name"
68
- });
69
- repoFolderId = folder.data.id;
70
- console.log(chalk.green(`Created repo folder '${reponame}' in Drive.`));
67
+ try {
68
+
69
+ if(!checkGlobalConfig()) {
70
+ console.log(chalk.red("No existing session found. Please login or signup."))
71
+ console.log(chalk.green("jvcs --help for help"))
72
+ return
73
+ }
74
+
75
+ let configData = getGlobalConfig()
76
+
77
+ if(!configData) {
78
+ console.log(chalk.red("No existing session found. Please login or signup."))
79
+ console.log(chalk.green("jvcs --help for help"))
80
+ return
81
+ }
82
+
83
+ if(!checkforjvcs()) {
84
+ console.log(chalk.red("Repository is not initialized or is deleted. Please create it."))
85
+ return
71
86
  }
87
+
88
+ const cwd = process.cwd()
89
+ const jvcsDir = path.join(cwd,".jvcs")
90
+ const commitDir = path.join(jvcsDir,"commits")
91
+ const reponame = path.basename(process.cwd())
92
+
93
+ if(!fs.existsSync(commitDir)) {
94
+ console.log(chalk.yellow("No commits to push"))
95
+ return
96
+ }
97
+
98
+ // storing the name of commit folders
99
+ const commitFolders = fs.readdirSync(commitDir, {withFileTypes: true}).filter((e)=> e.isDirectory()).map((e)=> e.name)
100
+ if(commitFolders.length === 0) {
101
+ console.log(chalk.yellow('No commits to push'))
102
+ return
103
+ }
104
+
105
+ console.log(chalk.blue("Pushing commits of ",reponame," to cloud storage..."))
72
106
 
73
- // List existing commits in Drive
74
- const driveCommitsRes = await drive.files.list({
75
- q: `'${repoFolderId}' in parents and mimeType='application/vnd.google-apps.folder' and trashed=false`,
76
- fields: "files(name, id)"
77
- });
78
- const driveCommitNames = driveCommitsRes.data.files.map(f => f.name);
79
-
80
- // Upload commits
81
- for (const commitDir of commitDirs) {
82
- if (driveCommitNames.includes(commitDir)) {
83
- console.log(chalk.yellow(`Commit '${commitDir}' already exists on Drive, skipping.`));
107
+ for(const commitId of commitFolders) {
108
+
109
+ const commitFolder = path.join(commitDir,commitId)
110
+ const metaPath = path.join(commitFolder,"meta.json")
111
+
112
+ if(!fs.existsSync(metaPath)) {
113
+ console.log(chalk.yellow(`Skipping ${commitId} (no meta.json found)`));
84
114
  continue;
85
115
  }
86
116
 
87
- const folderPath = path.join(commitFolder, commitDir);
117
+ const metaData = JSON.parse(fs.readFileSync(metaPath,"utf-8"))
118
+ const { author, message, timeStamp } = metaData;
88
119
 
89
- const commitFolderRes = await drive.files.create({
90
- requestBody: {
91
- name: commitDir,
92
- mimeType: "application/vnd.google-apps.folder",
93
- parents: [repoFolderId]
94
- },
95
- fields: "id, name"
96
- });
97
- const commitFolderId = commitFolderRes.data.id;
120
+ console.log(chalk.green(`\n Uploading commit:`));
121
+ console.log(chalk.gray(` id: ${commitId}`));
122
+ console.log(chalk.gray(` message: ${message}`));
123
+ console.log(chalk.gray(` author: ${author}`));
124
+ console.log(chalk.gray(` time: ${timeStamp}`));
98
125
 
99
- await uploadFolder(folderPath, commitFolderId);
126
+ const { commitFolderId : driveCommitId , commitAlreadyExists } = await getDirectoryStructure(configData.username,reponame,commitId)
127
+
128
+ if (commitAlreadyExists) {
129
+ console.log(chalk.yellow(`Skipping ${commitId} (already uploaded)`));
130
+ continue;
131
+ }
100
132
 
101
- console.log(chalk.green(`Commit '${commitDir}' pushed successfully.`));
133
+ await uploadDirectory(commitFolder,driveCommitId)
134
+ console.log(chalk.green(`Commit ${commitId} uploaded successfully!`));
102
135
  }
103
-
104
- } catch(error) {
105
- console.log(chalk.red("Error in pushCmd:"), error.message);
136
+
137
+ console.log(chalk.bold.green("\nAll commits pushed successfully!"));
106
138
  }
107
- }
108
-
109
- // Helper to recursively upload folder to Drive
110
- async function uploadFolder(folderPath, parentId) {
111
- const items = await fsPromises.readdir(folderPath);
112
- for (const item of items) {
113
- const fullPath = path.join(folderPath, item);
114
- const stats = await fsPromises.stat(fullPath);
115
- if (stats.isDirectory()) {
116
- const folder = await drive.files.create({
117
- requestBody: {
118
- name: item,
119
- mimeType: "application/vnd.google-apps.folder",
120
- parents: [parentId]
121
- },
122
- fields: "id, name"
123
- });
124
- await uploadFolder(fullPath, folder.data.id);
125
- } else {
126
- await drive.files.create({
127
- requestBody: { name: item, parents: [parentId] },
128
- media: { body: fs.createReadStream(fullPath) },
129
- fields: "id, name"
130
- });
131
- }
139
+ catch(error) {
140
+ console.log(chalk.red.bold("\nPush Failed"));
141
+ console.error(chalk.red(error.stack || error.message || error));
132
142
  }
133
143
  }
134
144
 
135
- module.exports = pushCmd;
145
+ module.exports = pushCmd
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");
@@ -11,7 +11,9 @@ const initCmd = require("./controllers/init")
11
11
  const addCmd = require("./controllers/add")
12
12
  const commitCmd = require("./controllers/commit")
13
13
  const unstageCmd = require("./controllers/unstage")
14
- const logCmd = require("./controllers/log")
14
+ const logCmd = require("./controllers/log");
15
+ const pushCmd = require("./controllers/push");
16
+
15
17
 
16
18
  yargs(hideBin(process.argv))
17
19
  .scriptName("jvcs")
@@ -126,6 +128,19 @@ yargs(hideBin(process.argv))
126
128
  }
127
129
  }
128
130
  )
131
+ .command(
132
+ "push",
133
+ "Push all the commits to remote",
134
+ {},
135
+ async ()=> {
136
+ try {
137
+ await pushCmd()
138
+ }
139
+ catch(error) {
140
+ console.log(chalk.red(error))
141
+ }
142
+ }
143
+ )
129
144
  .demandCommand(1, chalk.yellow("You need at least one command"))
130
145
  .help()
131
146
  .parse();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jvcs",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "bin": {
5
5
  "jvcs": "./index.js"
6
6
  },
@@ -1,7 +0,0 @@
1
- {
2
- "author": "jagdish",
3
- "id": "292abee1-42ea-4ddf-b590-be7c98abaf45",
4
- "message": "commit 4",
5
- "timeStamp": "2025-10-27T15:14:00.695Z",
6
- "parentId": "302af5ea-5789-4ee9-98fe-eab9308b2e27"
7
- }
@@ -1,7 +0,0 @@
1
- {
2
- "author": "jagdish",
3
- "id": "302af5ea-5789-4ee9-98fe-eab9308b2e27",
4
- "message": "commit 3",
5
- "timeStamp": "2025-10-27T15:13:40.822Z",
6
- "parentId": "484ac37d-c1a9-4ddd-8796-6e3facda1e11"
7
- }
@@ -1,7 +0,0 @@
1
- {
2
- "author": "jagdish",
3
- "id": "376d44d4-c595-429e-b711-ae6ec7c9ef74",
4
- "message": "commit 5",
5
- "timeStamp": "2025-10-27T15:14:03.635Z",
6
- "parentId": "292abee1-42ea-4ddf-b590-be7c98abaf45"
7
- }
@@ -1,122 +0,0 @@
1
- const { checkGlobalConfig, getGlobalConfig, checkforjvcs } = require("./utility")
2
- const path = require("path")
3
- const fssync = require("fs")
4
- const chalk = require("chalk")
5
- const fs = require("fs").promises
6
- const crypto = require("crypto")
7
-
8
- async function getFileHash(filepath) {
9
- const buffer = await fs.readFile(filepath)
10
- return crypto.createHash("sha256").update(buffer).digest("hex")
11
- }
12
-
13
- async function hashDirectoryRecursive(dir,hashData) {
14
-
15
- const entries = await fs.readdir(dir, {withFileTypes: true})
16
- console.log(entries)
17
-
18
- for(const entry of entries) {
19
-
20
- const fullPath = path.join(dir,entry.name)
21
- const relativePath = path.relative(process.cwd(),fullPath)
22
-
23
- if(entry.isFile()) {
24
- const hash = await getFileHash(fullPath)
25
- hashData[relativePath] = {
26
- hash,
27
- time: new Date().toISOString(),
28
- }
29
- }
30
- else if(entry.isDirectory()) {
31
- await hashDirectoryRecursive(fullPath,hashData)
32
- }
33
- }
34
- }
35
-
36
- async function addCmd(paths) {
37
-
38
- if(!paths || paths.length === 0) {
39
- console.log(chalk.yellow("Please specify files or folders to add."));
40
- return
41
- }
42
-
43
- if(!checkGlobalConfig()) {
44
- console.log(chalk.red("No existing session found. Please login or signup."))
45
- console.log(chalk.green("jvcs --help for help"))
46
- return
47
- }
48
-
49
- let configData = getGlobalConfig()
50
-
51
- if(!configData) {
52
- console.log(chalk.red("No existing session found. Please login or signup."))
53
- console.log(chalk.green("jvcs --help for help"))
54
- return
55
- }
56
-
57
- if(!checkforjvcs()) {
58
- console.log(chalk.red("Repository is not initialized or is deleted. Please create it."))
59
- return
60
- }
61
-
62
- const repoPath = path.join(process.cwd(),".jvcs")
63
- const staging = path.join(repoPath,"staging")
64
-
65
- if(!fssync.existsSync(staging))
66
- fssync.mkdirSync(staging, {recursive: true})
67
-
68
- const hashPath = path.join(staging,"jvcs_hashcode.json")
69
- let hashData = {}
70
-
71
- if(fssync.existsSync(hashPath)) {
72
- hashData = JSON.parse(await fs.readFile(hashPath,"utf-8"))
73
- }
74
-
75
- let targets = []
76
- if(paths.length === 1 && paths[0] === ".") {
77
- targets = await fs.readdir(process.cwd(),{withFileTypes: true})
78
- targets = targets.filter((target)=> target.name !== ".jvcs").map((item)=> path.resolve(process.cwd(),item.name))
79
- }
80
- else {
81
- targets = paths.map((p)=> path.resolve(process.cwd(),p))
82
- }
83
-
84
- // copying the files and folders to staging area
85
- for(const target of targets) {
86
-
87
- try {
88
-
89
- if(!fssync.existsSync(target)) {
90
- console.log(chalk.red(`Path not found: ${target}`))
91
- continue
92
- }
93
-
94
- const destination = path.join(staging,path.relative(process.cwd(),target))
95
- await fs.mkdir(path.dirname(destination), {recursive: true})
96
-
97
- const stats = await fs.stat(target)
98
-
99
- if(stats.isFile()) {
100
- await fs.copyFile(target,destination)
101
- const hash = await getFileHash(target)
102
- hashData[path.relative(process.cwd(),target)] = {
103
- hash,
104
- time: new Date().toISOString(),
105
- }
106
- console.log(chalk.green(`Added file: ${path.relative(process.cwd(), target)}`));
107
- }
108
- else if(stats.isDirectory()) {
109
- await fs.cp(target,destination,{recursive: true})
110
- await hashDirectoryRecursive(target,hashData)
111
- console.log(chalk.cyan(`Added folder: ${path.relative(process.cwd(), target)}`));
112
- }
113
- }
114
- catch(error) {
115
- console.log(chalk.red(`Unexpected error: ${error.message}`));
116
- }
117
- }
118
-
119
- await fs.writeFile(hashPath, JSON.stringify(hashData, null, 2));
120
- }
121
-
122
- module.exports = addCmd
@@ -1,201 +0,0 @@
1
- const inquirer = require("inquirer");
2
- const chalk = require("chalk");
3
- const validator = require("validator");
4
- const signup = require("./signup")
5
- const login = require("./login")
6
- const path = require("path")
7
- const fs = require("fs")
8
-
9
-
10
-
11
- async function beginCmd() {
12
-
13
- const config = path.join(require("os").homedir(),".jvcs","config.json")
14
- let isInitialized = false
15
- let configData = null
16
- if(fs.existsSync(config)) {
17
- isInitialized = true
18
- configData = JSON.parse(fs.readFileSync(config,"utf-8"))
19
- }
20
-
21
- if (isInitialized) {
22
- console.log(chalk.green(`You are already logged in as ${configData.username} (${configData.email})`))
23
- console.log(chalk.yellow(`[1] Continue \n[2] Logout \nChoose an option (1/2) : `))
24
-
25
- const answer = await inquirer.prompt([
26
- {
27
- type: 'input',
28
- name: 'choice',
29
- validate: function (input) {
30
- if (input === "1" || input === "2")
31
- return true
32
-
33
- return "Please enter 1 or 2"
34
- },
35
- filter: function (input) {
36
- return input.trim()
37
- }
38
- }
39
- ])
40
-
41
- console.log(`Your choice was ${answer.choice}`)
42
- if (answer.choice === "1") {
43
- console.log(chalk.green("Continuing as current user..."))
44
- console.log(chalk.green("jvcs --help for help"))
45
- }
46
- else {
47
- console.log(chalk.green("logging out..."))
48
- fs.unlinkSync(config)
49
- console.log(chalk.green("Logged out Successfully"))
50
- console.log(chalk.green("Please login or signup again (jvcs begin) to use version control system"))
51
- console.log(chalk.green("jvcs --help for help"))
52
- }
53
-
54
- return
55
- }
56
-
57
- console.log(chalk.red("No existing session found. Please login or signup."))
58
- console.log(chalk.yellow(`[1] Signup \n[2] login \nChoose an option (1/2) : `))
59
-
60
- const answer = await inquirer.prompt([
61
- {
62
- type: 'input',
63
- name: 'choice',
64
- validate: function (input) {
65
-
66
- if (input === "1" || input === "2")
67
- return true
68
-
69
- return "Please enter 1 or 2"
70
- },
71
- filter: function (input) {
72
- return input.trim(); // optional: remove whitespace
73
- }
74
- }
75
- ])
76
-
77
- if (answer.choice === "1") {
78
-
79
- const signupData = await inquirer.prompt([
80
- {
81
- type: 'input',
82
- name: 'username',
83
- filter: function (input) {
84
- return input.trim()
85
- }
86
- },
87
- {
88
- type: 'input',
89
- name: 'email',
90
- validate: function (input) {
91
- // validate email
92
- if (validator.isEmail(input))
93
- return true
94
-
95
- return "Please enter an valid email"
96
- },
97
- filter: function (input) {
98
- return input.trim()
99
- }
100
- },
101
- {
102
- type: 'password',
103
- name: 'password',
104
- validate: function (input) {
105
- // validate password
106
- if (validator.isStrongPassword(input, {
107
- minLength: 8,
108
- maxLength: 20,
109
- minLowercase: 1,
110
- minUppercase: 1,
111
- minNumbers: 1,
112
- minSymbols: 1,
113
- }))
114
- return true
115
-
116
- return "Please enter a strong password"
117
- },
118
- filter: function (input) {
119
- return input.trim()
120
- }
121
- },
122
- {
123
- type: 'password',
124
- name: 'confirmPassword',
125
- validate: function (input, answers) {
126
- if (answers.password !== input)
127
- return "password must be same as above"
128
-
129
- return true
130
- },
131
- filter: function (input) {
132
- return input.trim()
133
- }
134
- }
135
- ])
136
-
137
- try {
138
- await signup(signupData)
139
- }
140
- catch (error) {
141
- console.log(chalk.red(error))
142
- }
143
- }
144
- else {
145
-
146
- const loginData = await inquirer.prompt([
147
- {
148
- type: 'input',
149
- name: 'username',
150
- filter: function (input) {
151
- return input.trim()
152
- },
153
- validate: function (input) {
154
- if (input === "")
155
- return "email cannot be empty"
156
-
157
- return true
158
- }
159
- },
160
- {
161
- type: 'input',
162
- name: 'email',
163
- filter: function (input) {
164
- return input.trim()
165
- },
166
- validate: function (input) {
167
- if (!validator.isEmail(input))
168
- return "email is not valid"
169
-
170
- if (input === "")
171
- return "email cannot be empty"
172
-
173
- return true
174
- }
175
- },
176
- {
177
- type: 'password',
178
- name: 'password',
179
- filter: function (input) {
180
- return input.trim()
181
- },
182
- validate: function (input) {
183
-
184
- if (input === "")
185
- return "password cannot be empty"
186
-
187
- return true
188
- }
189
- }
190
- ])
191
-
192
- try {
193
- await login(loginData)
194
- }
195
- catch (error) {
196
- console.log(chalk.red(error))
197
- }
198
- }
199
- }
200
-
201
- module.exports = beginCmd