jvcs 1.7.4 → 1.7.5
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/controllers/add.js +14 -48
- package/controllers/begin.js +7 -33
- package/controllers/init.js +23 -32
- package/controllers/status.js +33 -33
- package/package.json +1 -1
package/controllers/add.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { getGlobalConfig, checkforjvcs } = require("./utility")
|
|
1
|
+
const { checkGlobalConfig, getGlobalConfig, checkforjvcs } = require("./utility")
|
|
2
2
|
const path = require("path")
|
|
3
3
|
const fssync = require("fs")
|
|
4
4
|
const chalk = require("chalk")
|
|
@@ -15,7 +15,7 @@ async function getFileHash(filepath) {
|
|
|
15
15
|
// load .jvcsignore file
|
|
16
16
|
async function loadIgnorePatterns() {
|
|
17
17
|
const ignorePath = path.join(process.cwd(), ".jvcsignore")
|
|
18
|
-
if
|
|
18
|
+
if(!fssync.existsSync(ignorePath)) return []
|
|
19
19
|
|
|
20
20
|
const content = await fs.readFile(ignorePath, "utf-8")
|
|
21
21
|
return content
|
|
@@ -61,20 +61,26 @@ async function addCmd(paths) {
|
|
|
61
61
|
|
|
62
62
|
if (!paths || paths.length === 0) {
|
|
63
63
|
console.log(chalk.yellow("Please specify files or folders to add."));
|
|
64
|
-
|
|
64
|
+
return
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
if (!checkGlobalConfig()) {
|
|
68
|
+
console.log(chalk.red("No existing session found. Please login or signup."))
|
|
69
|
+
console.log(chalk.green("jvcs --help for help"))
|
|
70
|
+
return
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
let configData = getGlobalConfig()
|
|
68
74
|
|
|
69
75
|
if (!configData) {
|
|
70
76
|
console.log(chalk.red("No existing session found. Please login or signup."))
|
|
71
77
|
console.log(chalk.green("jvcs --help for help"))
|
|
72
|
-
|
|
78
|
+
return
|
|
73
79
|
}
|
|
74
80
|
|
|
75
81
|
if (!checkforjvcs()) {
|
|
76
82
|
console.log(chalk.red("Repository is not initialized or is deleted. Please create it."))
|
|
77
|
-
|
|
83
|
+
return
|
|
78
84
|
}
|
|
79
85
|
|
|
80
86
|
const repoPath = path.join(process.cwd(), ".jvcs")
|
|
@@ -93,43 +99,6 @@ async function addCmd(paths) {
|
|
|
93
99
|
// load ignore patterns
|
|
94
100
|
const ignorePatterns = await loadIgnorePatterns()
|
|
95
101
|
|
|
96
|
-
// -------------------------------------------------------------
|
|
97
|
-
// NEW: Handle Deletions sync before adding new files
|
|
98
|
-
// -------------------------------------------------------------
|
|
99
|
-
let deletionHandledFor = new Set();
|
|
100
|
-
|
|
101
|
-
for (const key of Object.keys(hashData)) {
|
|
102
|
-
const cwdPath = path.join(process.cwd(), key);
|
|
103
|
-
|
|
104
|
-
// Check if the staged file falls under any of the target paths
|
|
105
|
-
let isUnderTarget = false;
|
|
106
|
-
if (paths.length === 1 && paths[0] === ".") {
|
|
107
|
-
isUnderTarget = true;
|
|
108
|
-
} else {
|
|
109
|
-
for (const p of paths) {
|
|
110
|
-
const targetPath = path.resolve(process.cwd(), p);
|
|
111
|
-
const relToTarget = path.relative(targetPath, cwdPath);
|
|
112
|
-
// If it's the exact file or inside the target directory
|
|
113
|
-
if (relToTarget === "" || (!relToTarget.startsWith("..") && !path.isAbsolute(relToTarget))) {
|
|
114
|
-
isUnderTarget = true;
|
|
115
|
-
break;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
if (isUnderTarget && !fssync.existsSync(cwdPath)) {
|
|
121
|
-
// File was deleted from working directory, remove it from staging
|
|
122
|
-
delete hashData[key];
|
|
123
|
-
const stagedTarget = path.join(staging, key);
|
|
124
|
-
if (fssync.existsSync(stagedTarget)) {
|
|
125
|
-
await fs.rm(stagedTarget);
|
|
126
|
-
}
|
|
127
|
-
console.log(chalk.red(`Removed deleted file: ${key}`));
|
|
128
|
-
deletionHandledFor.add(cwdPath);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
|
|
133
102
|
let targets = []
|
|
134
103
|
if (paths.length === 1 && paths[0] === ".") {
|
|
135
104
|
let rootEntries = await fs.readdir(process.cwd(), { withFileTypes: true })
|
|
@@ -145,10 +114,7 @@ async function addCmd(paths) {
|
|
|
145
114
|
try {
|
|
146
115
|
|
|
147
116
|
if (!fssync.existsSync(target)) {
|
|
148
|
-
|
|
149
|
-
if (!deletionHandledFor.has(target)) {
|
|
150
|
-
console.log(chalk.yellow(`Path not found: ${path.relative(process.cwd(), target)}`));
|
|
151
|
-
}
|
|
117
|
+
console.log(chalk.red(`Path not found: ${target}`))
|
|
152
118
|
continue
|
|
153
119
|
}
|
|
154
120
|
|
|
@@ -189,7 +155,7 @@ async function addCmd(paths) {
|
|
|
189
155
|
}
|
|
190
156
|
}
|
|
191
157
|
catch (error) {
|
|
192
|
-
console.log(chalk.red(`Unexpected error
|
|
158
|
+
console.log(chalk.red(`Unexpected error: ${error.message}`));
|
|
193
159
|
}
|
|
194
160
|
}
|
|
195
161
|
|
package/controllers/begin.js
CHANGED
|
@@ -10,24 +10,14 @@ const fs = require("fs")
|
|
|
10
10
|
|
|
11
11
|
async function beginCmd() {
|
|
12
12
|
|
|
13
|
-
const config = path.join(require("os").homedir(),
|
|
13
|
+
const config = path.join(require("os").homedir(),".jvcs","config.json")
|
|
14
14
|
let isInitialized = false
|
|
15
15
|
let configData = null
|
|
16
|
-
if
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
isInitialized = true
|
|
20
|
-
}
|
|
21
|
-
catch (error) {
|
|
22
|
-
// corrupted file, delete it and force re-login
|
|
23
|
-
try {
|
|
24
|
-
fs.unlinkSync(config)
|
|
25
|
-
}
|
|
26
|
-
catch (e) { }
|
|
27
|
-
isInitialized = false
|
|
28
|
-
}
|
|
16
|
+
if(fs.existsSync(config)) {
|
|
17
|
+
isInitialized = true
|
|
18
|
+
configData = JSON.parse(fs.readFileSync(config,"utf-8"))
|
|
29
19
|
}
|
|
30
|
-
|
|
20
|
+
|
|
31
21
|
if (isInitialized) {
|
|
32
22
|
console.log(chalk.green(`You are already logged in as ${configData.username} (${configData.email})`))
|
|
33
23
|
console.log(chalk.yellow(`[1] Continue \n[2] Logout \nChoose an option (1/2) : `))
|
|
@@ -48,20 +38,14 @@ async function beginCmd() {
|
|
|
48
38
|
}
|
|
49
39
|
])
|
|
50
40
|
|
|
41
|
+
console.log(`Your choice was ${answer.choice}`)
|
|
51
42
|
if (answer.choice === "1") {
|
|
52
43
|
console.log(chalk.green("Continuing as current user..."))
|
|
53
44
|
console.log(chalk.green("jvcs --help for help"))
|
|
54
45
|
}
|
|
55
46
|
else {
|
|
56
47
|
console.log(chalk.green("logging out..."))
|
|
57
|
-
|
|
58
|
-
if (fs.existsSync(config)) {
|
|
59
|
-
fs.unlinkSync(config)
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
catch (error) {
|
|
63
|
-
console.log(chalk.yellow("Warning: Could not remove config file properly."));
|
|
64
|
-
}
|
|
48
|
+
fs.unlinkSync(config)
|
|
65
49
|
console.log(chalk.green("Logged out Successfully"))
|
|
66
50
|
console.log(chalk.green("Please login or signup again (jvcs begin) to use version control system"))
|
|
67
51
|
console.log(chalk.green("jvcs --help for help"))
|
|
@@ -96,14 +80,6 @@ async function beginCmd() {
|
|
|
96
80
|
{
|
|
97
81
|
type: 'input',
|
|
98
82
|
name: 'username',
|
|
99
|
-
validate: function (input) {
|
|
100
|
-
// Ensure no spaces or weird special characters in username
|
|
101
|
-
if (!/^[a-zA-Z0-9_]+$/.test(input)) {
|
|
102
|
-
return "Username can only contain alphanumeric characters and underscores";
|
|
103
|
-
}
|
|
104
|
-
if (input.length < 3) return "Username must be at least 3 characters long";
|
|
105
|
-
return true;
|
|
106
|
-
},
|
|
107
83
|
filter: function (input) {
|
|
108
84
|
return input.trim()
|
|
109
85
|
}
|
|
@@ -163,7 +139,6 @@ async function beginCmd() {
|
|
|
163
139
|
}
|
|
164
140
|
catch (error) {
|
|
165
141
|
console.log(chalk.red(error))
|
|
166
|
-
process.exit(1);
|
|
167
142
|
}
|
|
168
143
|
}
|
|
169
144
|
else {
|
|
@@ -219,7 +194,6 @@ async function beginCmd() {
|
|
|
219
194
|
}
|
|
220
195
|
catch (error) {
|
|
221
196
|
console.log(chalk.red(error))
|
|
222
|
-
process.exit(1);
|
|
223
197
|
}
|
|
224
198
|
}
|
|
225
199
|
}
|
package/controllers/init.js
CHANGED
|
@@ -6,16 +6,16 @@ const { getGlobalConfig, checkGlobalConfig } = require("./utility")
|
|
|
6
6
|
|
|
7
7
|
async function initCmd() {
|
|
8
8
|
|
|
9
|
-
if
|
|
9
|
+
if(!checkGlobalConfig()) {
|
|
10
10
|
console.log(chalk.red("No existing session found. Please login or signup."))
|
|
11
11
|
console.log(chalk.green("jvcs --help for help"))
|
|
12
12
|
return
|
|
13
13
|
}
|
|
14
|
-
|
|
14
|
+
|
|
15
15
|
let configData = null
|
|
16
16
|
configData = getGlobalConfig()
|
|
17
|
-
|
|
18
|
-
if
|
|
17
|
+
|
|
18
|
+
if(!configData) {
|
|
19
19
|
console.log(chalk.red("No existing session found. Please login or signup."))
|
|
20
20
|
console.log(chalk.green("jvcs --help for help"))
|
|
21
21
|
return
|
|
@@ -25,45 +25,36 @@ async function initCmd() {
|
|
|
25
25
|
const cwd = process.cwd()
|
|
26
26
|
const home = require("os").homedir()
|
|
27
27
|
|
|
28
|
-
if
|
|
28
|
+
if(cwd === home) {
|
|
29
29
|
console.log(chalk.red("Cannot initialize a repository in your home directory."));
|
|
30
30
|
console.log(chalk.yellow("Hint: navigate to a project folder and run 'jvcs init' there."));
|
|
31
31
|
process.exit(1);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
const repoPath = path.join(cwd,
|
|
35
|
-
if
|
|
34
|
+
const repoPath = path.join(cwd,".jvcs")
|
|
35
|
+
if(fssync.existsSync(repoPath)) {
|
|
36
36
|
console.log(chalk.yellow("Repository already is initialized."));
|
|
37
|
-
process.exit(
|
|
37
|
+
process.exit(1);
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
email: configData.email
|
|
52
|
-
}
|
|
40
|
+
fssync.mkdirSync(repoPath)
|
|
41
|
+
fssync.mkdirSync(path.join(repoPath,"commits"))
|
|
42
|
+
fssync.mkdirSync(path.join(repoPath,"staging"))
|
|
43
|
+
|
|
44
|
+
const config = {
|
|
45
|
+
repoName: path.basename(cwd),
|
|
46
|
+
createdAt: new Date().toISOString(),
|
|
47
|
+
remote: null,
|
|
48
|
+
owner : {
|
|
49
|
+
username: configData.username,
|
|
50
|
+
email: configData.email
|
|
53
51
|
}
|
|
52
|
+
}
|
|
54
53
|
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
fssync.writeFileSync(path.join(repoPath, "config.json"), JSON.stringify(config, null, 2))
|
|
55
|
+
fssync.writeFileSync(path.join(repoPath, "HEAD"), "");
|
|
57
56
|
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
catch (error) {
|
|
61
|
-
if (fssync.existsSync(repoPath)) {
|
|
62
|
-
fssync.rmSync(repoPath, { recursive: true, force: true });
|
|
63
|
-
}
|
|
64
|
-
console.log(chalk.red(`Failed to initialize repository: ${error.message}`));
|
|
65
|
-
process.exit(1);
|
|
66
|
-
}
|
|
57
|
+
console.log(chalk.green(`Initialized empty JVCS repository`));
|
|
67
58
|
}
|
|
68
59
|
|
|
69
60
|
module.exports = initCmd
|
package/controllers/status.js
CHANGED
|
@@ -23,32 +23,32 @@ function hashfile(filepath) {
|
|
|
23
23
|
const data = fs.readFileSync(filepath)
|
|
24
24
|
return crypto.createHash("sha256").update(data).digest("hex")
|
|
25
25
|
}
|
|
26
|
-
catch
|
|
26
|
+
catch(error) {
|
|
27
27
|
// return null
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
|
|
32
|
-
function getAllFiles(dir, rootDir
|
|
32
|
+
function getAllFiles(dir, rootDir=dir, collected=[]) {
|
|
33
33
|
|
|
34
34
|
if (!fs.existsSync(dir)) return collected;
|
|
35
35
|
|
|
36
|
-
const entries = fs.readdirSync(dir,
|
|
36
|
+
const entries = fs.readdirSync(dir,{withFileTypes:true})
|
|
37
37
|
|
|
38
|
-
for
|
|
39
|
-
const fullPath = path.join(dir,
|
|
40
|
-
const rel = normalizeRel(path.relative(rootDir,
|
|
38
|
+
for(const entry of entries) {
|
|
39
|
+
const fullPath = path.join(dir,entry.name)
|
|
40
|
+
const rel = normalizeRel(path.relative(rootDir,fullPath))
|
|
41
41
|
|
|
42
|
-
if
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if
|
|
42
|
+
if(entry.isDirectory() && (entry.name === ".jvcs" || entry.name === "node_modules"))
|
|
43
|
+
continue;
|
|
44
|
+
|
|
45
|
+
if(entry.isFile() && (entry.name === "meta.json" || entry.name === "jvcs_hashcode.json"))
|
|
46
|
+
continue;
|
|
47
|
+
|
|
48
|
+
if(entry.isFile()) {
|
|
49
49
|
collected.push(rel);
|
|
50
50
|
}
|
|
51
|
-
else if
|
|
51
|
+
else if(entry.isDirectory()) {
|
|
52
52
|
getAllFiles(fullPath, rootDir, collected);
|
|
53
53
|
}
|
|
54
54
|
|
|
@@ -59,30 +59,30 @@ function getAllFiles(dir, rootDir = dir, collected = []) {
|
|
|
59
59
|
|
|
60
60
|
async function statusCmd() {
|
|
61
61
|
|
|
62
|
-
if
|
|
62
|
+
if(!checkGlobalConfig()) {
|
|
63
63
|
console.log(chalk.red("No existing session found. Please login or signup."));
|
|
64
64
|
console.log(chalk.green("jvcs --help for help"));
|
|
65
65
|
return;
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
const configData = getGlobalConfig();
|
|
69
|
-
if
|
|
69
|
+
if(!configData) {
|
|
70
70
|
console.log(chalk.red("No existing session found. Please login or signup."));
|
|
71
71
|
return;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
if
|
|
74
|
+
if(!checkforjvcs()) {
|
|
75
75
|
console.log(chalk.red("Repository is not initialized or is deleted."));
|
|
76
76
|
return;
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
const cwd = process.cwd()
|
|
80
|
-
const jvcsDir = path.join(cwd,
|
|
81
|
-
const commitDir = path.join(jvcsDir,
|
|
82
|
-
const stagingDir = path.join(jvcsDir,
|
|
80
|
+
const jvcsDir = path.join(cwd,".jvcs")
|
|
81
|
+
const commitDir = path.join(jvcsDir,"commits")
|
|
82
|
+
const stagingDir = path.join(jvcsDir,"staging")
|
|
83
83
|
const headFile = path.join(jvcsDir, "HEAD");
|
|
84
84
|
|
|
85
|
-
if
|
|
85
|
+
if(!fs.existsSync(jvcsDir)) {
|
|
86
86
|
console.log(chalk.red("No repository exists. Please create one using 'jvcs init'"))
|
|
87
87
|
return
|
|
88
88
|
}
|
|
@@ -91,9 +91,9 @@ async function statusCmd() {
|
|
|
91
91
|
const cwdFiles = getAllFiles(cwd, cwd, []);
|
|
92
92
|
const stagedFiles = fs.existsSync(stagingDir) ? getAllFiles(stagingDir, stagingDir, []).map(f => normalizeRel(f)) : [];
|
|
93
93
|
let commitedFiles = []
|
|
94
|
-
if
|
|
94
|
+
if(fs.existsSync(commitDir)) {
|
|
95
95
|
const commits = fs.readdirSync(commitDir)
|
|
96
|
-
if
|
|
96
|
+
if(commits.length > 0) {
|
|
97
97
|
const lastCommit = `${fs.readFileSync(headFile, "utf-8").trim()}`;
|
|
98
98
|
const commitPath = path.join(commitDir, lastCommit);
|
|
99
99
|
commitedFiles = getAllFiles(commitPath, commitPath, []).map(f => normalizeRel(f))
|
|
@@ -111,12 +111,12 @@ async function statusCmd() {
|
|
|
111
111
|
const toBeCommitted = stagedFiles.filter(f => !committedSet.has(f));
|
|
112
112
|
|
|
113
113
|
// Changes not staged for commit: present in staging and in cwd, but changed in cwd compared to staged copy
|
|
114
|
-
const modified = stagedFiles.filter((file)
|
|
115
|
-
const cwdFilePath = path.join(process.cwd(),
|
|
116
|
-
const stagedFilePath = path.join(stagingDir,
|
|
117
|
-
|
|
118
|
-
if
|
|
119
|
-
|
|
114
|
+
const modified = stagedFiles.filter((file)=> {
|
|
115
|
+
const cwdFilePath = path.join(process.cwd(),file)
|
|
116
|
+
const stagedFilePath = path.join(stagingDir,file)
|
|
117
|
+
|
|
118
|
+
if(!fs.existsSync(cwdFilePath) || !fs.existsSync(stagedFilePath))
|
|
119
|
+
return false
|
|
120
120
|
|
|
121
121
|
const cwdHash = hashfile(cwdFilePath)
|
|
122
122
|
const stagingHash = hashfile(stagedFilePath)
|
|
@@ -129,7 +129,7 @@ async function statusCmd() {
|
|
|
129
129
|
console.log(chalk.bold.blue(`\nOn branch: main (default)`));
|
|
130
130
|
|
|
131
131
|
console.log(chalk.bold.green("\nChanges to be committed (files that are staged but not commited):"));
|
|
132
|
-
if
|
|
132
|
+
if(toBeCommitted.length > 0) {
|
|
133
133
|
toBeCommitted.forEach(f => console.log(chalk.green(`\t${f}`)));
|
|
134
134
|
}
|
|
135
135
|
else {
|
|
@@ -137,15 +137,15 @@ async function statusCmd() {
|
|
|
137
137
|
}
|
|
138
138
|
|
|
139
139
|
console.log(chalk.bold.yellow("\nChanges not staged for commit (files that are modified after adding to staging area):"));
|
|
140
|
-
if
|
|
140
|
+
if(modified.length > 0) {
|
|
141
141
|
modified.forEach(f => console.log(chalk.yellow(`\t${f}`)));
|
|
142
|
-
}
|
|
142
|
+
}
|
|
143
143
|
else {
|
|
144
144
|
console.log(chalk.gray("\tNo modified files detected"));
|
|
145
145
|
}
|
|
146
146
|
|
|
147
147
|
console.log(chalk.bold.red("\nUntracked files (files that are not staged or commited):"));
|
|
148
|
-
if
|
|
148
|
+
if(untracked.length > 0) {
|
|
149
149
|
untracked.forEach(f => console.log(chalk.red(`\t${f}`)));
|
|
150
150
|
}
|
|
151
151
|
else {
|