jvcs 1.1.8 → 1.2.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.
- package/.jvcs/HEAD +1 -0
- package/.jvcs/commits/cd2a0ec7-04b8-4ac9-b539-617bb5fea1f3/controllers/add.js +122 -0
- package/.jvcs/commits/cd2a0ec7-04b8-4ac9-b539-617bb5fea1f3/controllers/begin.js +201 -0
- package/.jvcs/commits/cd2a0ec7-04b8-4ac9-b539-617bb5fea1f3/controllers/clone.js +138 -0
- package/.jvcs/commits/cd2a0ec7-04b8-4ac9-b539-617bb5fea1f3/controllers/commit.js +83 -0
- package/.jvcs/commits/cd2a0ec7-04b8-4ac9-b539-617bb5fea1f3/controllers/driveUtility.js +56 -0
- package/.jvcs/commits/cd2a0ec7-04b8-4ac9-b539-617bb5fea1f3/controllers/init.js +60 -0
- package/.jvcs/commits/cd2a0ec7-04b8-4ac9-b539-617bb5fea1f3/controllers/log.js +99 -0
- package/.jvcs/commits/cd2a0ec7-04b8-4ac9-b539-617bb5fea1f3/controllers/login.js +33 -0
- package/.jvcs/commits/cd2a0ec7-04b8-4ac9-b539-617bb5fea1f3/controllers/push.js +165 -0
- package/.jvcs/commits/cd2a0ec7-04b8-4ac9-b539-617bb5fea1f3/controllers/revert.js +208 -0
- package/.jvcs/commits/cd2a0ec7-04b8-4ac9-b539-617bb5fea1f3/controllers/signup.js +28 -0
- package/.jvcs/commits/cd2a0ec7-04b8-4ac9-b539-617bb5fea1f3/controllers/status.js +160 -0
- package/.jvcs/commits/cd2a0ec7-04b8-4ac9-b539-617bb5fea1f3/controllers/unstage.js +104 -0
- package/.jvcs/commits/cd2a0ec7-04b8-4ac9-b539-617bb5fea1f3/controllers/utility.js +29 -0
- package/.jvcs/commits/cd2a0ec7-04b8-4ac9-b539-617bb5fea1f3/controllers/verifyOtp.js +55 -0
- package/.jvcs/commits/cd2a0ec7-04b8-4ac9-b539-617bb5fea1f3/jvcs_hashcode.json +62 -0
- package/.jvcs/commits/cd2a0ec7-04b8-4ac9-b539-617bb5fea1f3/meta.json +7 -0
- package/.jvcs/config.json +9 -0
- package/.jvcs/staging/controllers/add.js +122 -0
- package/.jvcs/staging/controllers/begin.js +201 -0
- package/.jvcs/staging/controllers/clone.js +138 -0
- package/.jvcs/staging/controllers/commit.js +83 -0
- package/.jvcs/staging/controllers/driveUtility.js +56 -0
- package/.jvcs/staging/controllers/init.js +60 -0
- package/.jvcs/staging/controllers/log.js +99 -0
- package/.jvcs/staging/controllers/login.js +33 -0
- package/.jvcs/staging/controllers/push.js +165 -0
- package/.jvcs/staging/controllers/revert.js +208 -0
- package/.jvcs/staging/controllers/signup.js +28 -0
- package/.jvcs/staging/controllers/status.js +160 -0
- package/.jvcs/staging/controllers/unstage.js +104 -0
- package/.jvcs/staging/controllers/utility.js +29 -0
- package/.jvcs/staging/controllers/verifyOtp.js +55 -0
- package/.jvcs/staging/jvcs_hashcode.json +62 -0
- package/controllers/clone.js +25 -15
- package/package.json +1 -1
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
const fs = require("fs").promises;
|
|
2
|
+
const fssync = require("fs");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
const chalk = require("chalk");
|
|
5
|
+
const { checkGlobalConfig, getGlobalConfig, checkforjvcs } = require("./utility");
|
|
6
|
+
|
|
7
|
+
function removeHashesForFolder(hashData,folderPath) {
|
|
8
|
+
|
|
9
|
+
for(const key of Object.keys(hashData)) {
|
|
10
|
+
if(key.startsWith(folderPath+path.sep)) {
|
|
11
|
+
delete hashData[key]
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async function unstageCmd(paths) {
|
|
17
|
+
|
|
18
|
+
if(!paths || paths.length === 0) {
|
|
19
|
+
console.log(chalk.yellow("Please specify files or folders to unstage."));
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if(!checkGlobalConfig()) {
|
|
24
|
+
console.log(chalk.red("No existing session found. Please login or signup."));
|
|
25
|
+
console.log(chalk.green("jvcs --help for help"));
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const configData = getGlobalConfig();
|
|
30
|
+
if(!configData) {
|
|
31
|
+
console.log(chalk.red("No existing session found. Please login or signup."));
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if(!checkforjvcs()) {
|
|
36
|
+
console.log(chalk.red("Repository is not initialized or is deleted."));
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const repoPath = path.join(process.cwd(), ".jvcs");
|
|
41
|
+
const stagingPath = path.join(repoPath, "staging");
|
|
42
|
+
const hashPath = path.join(stagingPath, "jvcs_hashcode.json");
|
|
43
|
+
|
|
44
|
+
if(!fssync.existsSync(stagingPath)) {
|
|
45
|
+
console.log(chalk.yellow("Nothing is staged yet."));
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const stagedItems = fssync.readdirSync(stagingPath).filter((item)=> item !== "jvcs_hashcode.json")
|
|
50
|
+
if(stagedItems.length === 0) {
|
|
51
|
+
console.log(chalk.yellow("Staging area is empty. Nothing to unstage."));
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
let hashData = {};
|
|
56
|
+
if(fssync.existsSync(hashPath)) {
|
|
57
|
+
hashData = JSON.parse(await fs.readFile(hashPath, "utf-8"));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if(paths.length === 1 && paths[0] === ".") {
|
|
61
|
+
await fs.rm(stagingPath, {recursive: true, force: true})
|
|
62
|
+
await fs.mkdir(stagingPath, {recursive: true})
|
|
63
|
+
console.log(chalk.cyan("Unstaged all files and folders."));
|
|
64
|
+
return
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const targets = paths.map((p) => path.resolve(process.cwd(), p));
|
|
68
|
+
|
|
69
|
+
for(const target of targets) {
|
|
70
|
+
|
|
71
|
+
const stagedTarget = path.join(stagingPath, path.relative(process.cwd(),target))
|
|
72
|
+
|
|
73
|
+
if(!fssync.existsSync(stagedTarget)) {
|
|
74
|
+
console.log(chalk.yellow(`Not found in staging: ${path.relative(process.cwd(), target)}`));
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const stats = await fs.stat(stagedTarget);
|
|
79
|
+
|
|
80
|
+
if(stats.isDirectory()) {
|
|
81
|
+
await fs.rm(stagedTarget, {recursive: true, force: true})
|
|
82
|
+
removeHashesForFolder(hashData,path.relative(process.cwd(),target))
|
|
83
|
+
console.log(chalk.cyan(`Unstaged folder: ${path.relative(process.cwd(), target)}`));
|
|
84
|
+
}
|
|
85
|
+
else if(stats.isFile()) {
|
|
86
|
+
await fs.rm(stagedTarget)
|
|
87
|
+
delete hashData[path.relative(process.cwd(),target)]
|
|
88
|
+
console.log(chalk.green(`Unstaged file: ${path.relative(process.cwd(), target)}`));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if(Object.keys(hashData).length === 0) {
|
|
94
|
+
if(fssync.existsSync(hashPath)) {
|
|
95
|
+
await fs.rm(hashPath)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
await fs.writeFile(hashPath,JSON.stringify(hashData,null,2))
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
module.exports = unstageCmd
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const fs = require("fs")
|
|
2
|
+
const path = require("path")
|
|
3
|
+
|
|
4
|
+
const config = path.join(require("os").homedir(),".jvcs","config.json")
|
|
5
|
+
|
|
6
|
+
function checkGlobalConfig() {
|
|
7
|
+
return fs.existsSync(config)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function getGlobalConfig() {
|
|
11
|
+
if(checkGlobalConfig()) {
|
|
12
|
+
const configData = JSON.parse(fs.readFileSync(config,"utf-8"))
|
|
13
|
+
if(configData.username && configData.email && configData.token)
|
|
14
|
+
return configData
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return null
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function checkforjvcs() {
|
|
21
|
+
const jvcsPath = path.join(process.cwd(),".jvcs")
|
|
22
|
+
return fs.existsSync(jvcsPath)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = {
|
|
26
|
+
checkGlobalConfig,
|
|
27
|
+
getGlobalConfig,
|
|
28
|
+
checkforjvcs
|
|
29
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
const inquirer = require("inquirer");
|
|
2
|
+
const fs = require("fs")
|
|
3
|
+
const path = require("path")
|
|
4
|
+
const chalk = require("chalk")
|
|
5
|
+
|
|
6
|
+
async function verifyOtp(signupData) {
|
|
7
|
+
|
|
8
|
+
const otp = await inquirer.prompt([
|
|
9
|
+
{
|
|
10
|
+
type: 'password',
|
|
11
|
+
name: 'otp',
|
|
12
|
+
validate: function(input) {
|
|
13
|
+
if(input.length !== 6)
|
|
14
|
+
return "OTP must be of 6 length"
|
|
15
|
+
|
|
16
|
+
return true
|
|
17
|
+
},
|
|
18
|
+
filter: function(input) {
|
|
19
|
+
return input.trim()
|
|
20
|
+
},
|
|
21
|
+
mask: '*',
|
|
22
|
+
}
|
|
23
|
+
])
|
|
24
|
+
|
|
25
|
+
console.log(`otp is : ${otp.otp}`)
|
|
26
|
+
const response = await fetch("http://localhost:3000/verifyEmail", {
|
|
27
|
+
method: "POST",
|
|
28
|
+
headers: { 'Content-Type': 'application/json' },
|
|
29
|
+
credentials: "include",
|
|
30
|
+
body: JSON.stringify({email:signupData.email,otp:otp.otp,cli:true})
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const data = await response.json();
|
|
34
|
+
|
|
35
|
+
if (data.status === true) {
|
|
36
|
+
console.log(chalk.green(data.message))
|
|
37
|
+
|
|
38
|
+
const dirPath = path.join(require("os").homedir(), ".jvcs");
|
|
39
|
+
if (!fs.existsSync(dirPath)) {
|
|
40
|
+
fs.mkdirSync(dirPath, { recursive: true });
|
|
41
|
+
}
|
|
42
|
+
|
|
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//04dWkzGCpoIgACgYIARAAGAQSNwF-L9IrRV-B67zRcIkhs7USx3vLewtE764bZPv7d5d7hAlH7QThZxj2CQUr5flQIG12Ad64dQI"},null,2));
|
|
45
|
+
}
|
|
46
|
+
else if(data.status === "user") {
|
|
47
|
+
console.log(chalk.yellow(data.message))
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
throw new Error(data.message)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
module.exports = verifyOtp
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"controllers\\add.js": {
|
|
3
|
+
"hash": "1acab05c2c549de3100d5f5e8298268ce83a7f3722ea97a1fb9a151403756024",
|
|
4
|
+
"time": "2025-10-29T12:53:25.244Z"
|
|
5
|
+
},
|
|
6
|
+
"controllers\\begin.js": {
|
|
7
|
+
"hash": "228ccbb0e8965765aa039b27cd4a6ffaad388b9b09c52fafe8f807b7b568f378",
|
|
8
|
+
"time": "2025-10-29T12:53:25.247Z"
|
|
9
|
+
},
|
|
10
|
+
"controllers\\clone.js": {
|
|
11
|
+
"hash": "4c184aa2116c6cccf2d50986d550b3ebbfa41a09ba916be5d165ef955e6fe474",
|
|
12
|
+
"time": "2025-10-29T12:53:25.248Z"
|
|
13
|
+
},
|
|
14
|
+
"controllers\\commit.js": {
|
|
15
|
+
"hash": "a4549dba1503666557b945d4013e0e668412721e9d8b5065cc1053b779dce9bc",
|
|
16
|
+
"time": "2025-10-29T12:53:25.249Z"
|
|
17
|
+
},
|
|
18
|
+
"controllers\\driveUtility.js": {
|
|
19
|
+
"hash": "7f7b7b4d1aac215e707465a4ba43c0fb7b7567e9ce30269d7b980d4effb57e5b",
|
|
20
|
+
"time": "2025-10-29T12:53:25.249Z"
|
|
21
|
+
},
|
|
22
|
+
"controllers\\init.js": {
|
|
23
|
+
"hash": "32343d60e405454a913fddb507749648f9888a9da0d600e521c2c629216d3692",
|
|
24
|
+
"time": "2025-10-29T12:53:25.250Z"
|
|
25
|
+
},
|
|
26
|
+
"controllers\\log.js": {
|
|
27
|
+
"hash": "ae40cae361dafd35ccb776ae7ea0abb397f3af47bdb914ffa7ce69207ab1ed46",
|
|
28
|
+
"time": "2025-10-29T12:53:25.250Z"
|
|
29
|
+
},
|
|
30
|
+
"controllers\\login.js": {
|
|
31
|
+
"hash": "4b96046c76c0f610651ca71e3ebadf4fd9ca310a0be2e29c373b9c25a35e059c",
|
|
32
|
+
"time": "2025-10-29T12:53:25.251Z"
|
|
33
|
+
},
|
|
34
|
+
"controllers\\push.js": {
|
|
35
|
+
"hash": "3c89401eb4ebc695b14271dd5498428364715e50a9f62a2f100d112d0f701a65",
|
|
36
|
+
"time": "2025-10-29T12:53:25.251Z"
|
|
37
|
+
},
|
|
38
|
+
"controllers\\revert.js": {
|
|
39
|
+
"hash": "4737f8ca7f3c58c1a83588d0e6a1da97f47f5f323defb9ab11707cbe43aa883b",
|
|
40
|
+
"time": "2025-10-29T12:53:25.252Z"
|
|
41
|
+
},
|
|
42
|
+
"controllers\\signup.js": {
|
|
43
|
+
"hash": "67c1f5ad80f8a64e92fd8cd685a06a605831011fd4246256a60e09a6b5d161b8",
|
|
44
|
+
"time": "2025-10-29T12:53:25.253Z"
|
|
45
|
+
},
|
|
46
|
+
"controllers\\status.js": {
|
|
47
|
+
"hash": "ca7e71c71824098c805ae4c677801d901259a05ce108025d5847764de9175182",
|
|
48
|
+
"time": "2025-10-29T12:53:25.254Z"
|
|
49
|
+
},
|
|
50
|
+
"controllers\\unstage.js": {
|
|
51
|
+
"hash": "32038137ffcb0862d716b14fa966f7ccaa8470188f89353b8356e2e1be9dc00a",
|
|
52
|
+
"time": "2025-10-29T12:53:25.254Z"
|
|
53
|
+
},
|
|
54
|
+
"controllers\\utility.js": {
|
|
55
|
+
"hash": "06b49dcf44db91b90d82334c4187eaca13e580c826976343d4c8b3d1deaeb6c7",
|
|
56
|
+
"time": "2025-10-29T12:53:25.256Z"
|
|
57
|
+
},
|
|
58
|
+
"controllers\\verifyOtp.js": {
|
|
59
|
+
"hash": "a94ca1ba0018f1ed661ef963b166873319b9f862e51a29c666e62364e56d662f",
|
|
60
|
+
"time": "2025-10-29T12:53:25.256Z"
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
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" && target.name !== "node_modules").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
|
|
@@ -0,0 +1,201 @@
|
|
|
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
|