Skip to content

Commit 232c3cc

Browse files
committed
Added settings API module to change data file and port. Updated file diff module to remove terminal dependency
1 parent e7d0b3c commit 232c3cc

10 files changed

+165
-19
lines changed

API/settingsApi.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { exec } = require("child_process");
22
const dotenv = require("dotenv").config();
33
const fs = require("fs");
4+
const { parse, stringify } = require("envfile");
45

56
const fetchDatabaseFile = async () => {
67
const dbPath = process.env.DATABASE_FILE || "NO_DATABASE_FILE";
@@ -34,6 +35,73 @@ const deleteRepo = async (repoId) => {
3435
});
3536
};
3637

38+
const updateDbFile = async (newFileName) => {
39+
console.log("FILE NAME : ", newFileName);
40+
// DATABASE_FILE = "database/repo-datastore.json"
41+
// GITCONVEX_PORT = 9001
42+
43+
return await fs.promises
44+
.access(newFileName)
45+
.then(async (res) => {
46+
const envContent = fs.readFileSync(".env").toString();
47+
48+
const parsedEnvContent = parse(envContent);
49+
parsedEnvContent.DATABASE_FILE = `"${newFileName.toString()}"`;
50+
51+
console.log(stringify(parsedEnvContent));
52+
53+
return await fs.promises
54+
.writeFile(".env", stringify(parsedEnvContent))
55+
.then(() => {
56+
return "DATAFILE_UPDATE_SUCCESS";
57+
})
58+
.catch((err) => {
59+
console.log(err);
60+
return "DATAFILE_UPDATE_FAILED";
61+
});
62+
})
63+
.catch((err) => {
64+
console.log(err);
65+
return "DATAFILE_UPDATE_FAILED";
66+
});
67+
};
68+
69+
const getPortDetails = async () => {
70+
const envContent = fs.readFileSync(".env").toString();
71+
72+
const parsedEnvContent = parse(envContent);
73+
74+
console.log(stringify(parsedEnvContent));
75+
76+
return { settingsPortDetails: Number(parsedEnvContent.GITCONVEX_PORT) };
77+
};
78+
79+
const updatePortDetails = async (newPort) => {
80+
if (!isNaN(newPort)) {
81+
const envContent = fs.readFileSync(".env").toString();
82+
83+
const parsedEnvContent = parse(envContent);
84+
parsedEnvContent.GITCONVEX_PORT = Number(newPort);
85+
86+
console.log(stringify(parsedEnvContent));
87+
88+
return await fs.promises
89+
.writeFile(".env", stringify(parsedEnvContent))
90+
.then(() => {
91+
return "PORT_UPDATE_SUCCESS";
92+
})
93+
.catch((err) => {
94+
console.log(err);
95+
return "PORT_UPDATE_FAILED";
96+
});
97+
} else {
98+
return "PORT_UPDATE_FAILED";
99+
}
100+
};
101+
102+
module.exports.updateDbFile = updateDbFile;
37103
module.exports.fetchDatabaseFile = fetchDatabaseFile;
38104
module.exports.fetchRepoDetails = fetchRepoDetails;
39105
module.exports.deleteRepo = deleteRepo;
106+
module.exports.updatePortDetails = updatePortDetails;
107+
module.exports.getPortDetails = getPortDetails;

database/repo-file.json

Whitespace-only changes.

git/gitFetchPullApi.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const gitFetchApi = async (repoId) => {
2222
};
2323
}
2424
} else {
25+
console.log(stderr);
2526
return {
2627
status: "FETCH_ABSENT",
2728
};

git/gitFileDifferenceAPI.js

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const fetchRepopath = require("../global/fetchGitRepoPath");
2+
const fs = require("fs");
23
const { exec } = require("child_process");
34
const util = require("util");
45
const execPromisified = util.promisify(exec);
@@ -13,23 +14,56 @@ async function gitFileDifferenceHandler(repoId, fileName) {
1314

1415
async function getGitFileDifference(repoId, fileName) {
1516
const repoPath = fetchRepopath.getRepoPath(repoId);
16-
return await execPromisified(
17-
`git diff --stat ${fileName} && echo "SPLIT___LINE" && git diff -U$(wc -l ${fileName} | xargs)`,
18-
{ cwd: repoPath }
19-
).then((res) => {
20-
const { stdout, stderr } = res;
21-
22-
if (stdout && !stderr) {
23-
var splitLines = stdout.split("SPLIT___LINE");
24-
var diffStat = splitLines[0].trim().split("\n");
25-
var fileDiff = splitLines[1].trim().split("\n");
26-
27-
return {
28-
diffStat,
29-
fileDiff,
30-
};
17+
18+
const fileContentLength = await fs.promises
19+
.readFile(repoPath + "/" + fileName)
20+
.then((data) => {
21+
const interData = data.toString().split("\n");
22+
return interData.length;
23+
})
24+
.catch((err) => {
25+
console.log(err);
26+
});
27+
28+
const diffStat = await execPromisified(`git diff --stat ${fileName}`, {
29+
cwd: repoPath,
30+
})
31+
.then(({ stdout, stderr }) => {
32+
if (stdout && !stderr) {
33+
return stdout.trim().split("\n");
34+
} else {
35+
console.log(stderr);
36+
return ["NO_STAT"];
37+
}
38+
})
39+
.catch((err) => {
40+
console.log(err);
41+
return ["NO_STAT"];
42+
});
43+
44+
const fileDiff = await execPromisified(
45+
`git diff -U${fileContentLength} ${fileName}`,
46+
{
47+
cwd: repoPath,
3148
}
32-
});
49+
)
50+
.then(({ stdout, stderr }) => {
51+
if (stdout && !stderr) {
52+
return stdout.trim().split("\n");
53+
} else {
54+
console.log(stderr);
55+
return ["NO_DIFF"];
56+
}
57+
})
58+
.catch((err) => {
59+
console.log(err);
60+
return ["NO_DIFF"];
61+
});
62+
63+
return {
64+
diffStat,
65+
fileDiff,
66+
};
3367
}
3468

3569
module.exports.gitFileDifferenceHandler = gitFileDifferenceHandler;

global/globalAPIHandler.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const {
1515
GIT_UNPUSHED_COMMITS,
1616
SETTINGS_DBPATH,
1717
SETTINGS_REPODETAILS,
18+
SETTINGS_PORT,
1819
} = require("./globalRouteStore");
1920

2021
const graphqlHTTP = require("express-graphql");
@@ -44,6 +45,9 @@ const {
4445
gitPullFromRemote,
4546
deleteRepo,
4647
addBranch,
48+
updateDbFileApi,
49+
settingsGetPortDetails,
50+
settingsUpdatePortDetail,
4751
} = require("./globalFunctionStore");
4852

4953
app.use(
@@ -84,6 +88,8 @@ app.use(
8488
return gitUnpushedCommits(parsedPayload);
8589
case SETTINGS_DBPATH:
8690
return settingsFetchDbPath();
91+
case SETTINGS_PORT:
92+
return settingsGetPortDetails();
8793
case SETTINGS_REPODETAILS:
8894
return settingsFetchRepoDetails();
8995
default:
@@ -133,7 +139,15 @@ app.use(
133139
},
134140
addBranch: async (args) => {
135141
const { repoId, branchName } = args;
136-
return await addBranch(repoId, branchName)
142+
return await addBranch(repoId, branchName);
143+
},
144+
updateRepoDataFile: async (args) => {
145+
const { newDbFile } = args;
146+
return await updateDbFileApi(newDbFile);
147+
},
148+
settingsEditPort: async (args) => {
149+
const { newPort } = args;
150+
return settingsUpdatePortDetail(newPort);
137151
},
138152
},
139153
})

global/globalFunctionStore.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ const { gitStageAllItemsApi } = require("../git/gitStageAllItemsAPI");
1212
const { gitCommitChangesApi } = require("../git/gitCommitChangesAPI");
1313
const { gitPushToRemoteApi } = require("../git/gitPushToRemoteAPI");
1414
const { gitStageItem } = require("../git/gitStageItem");
15-
const { fetchDatabaseFile, fetchRepoDetails } = require("../API/settingsApi");
15+
const {
16+
fetchDatabaseFile,
17+
fetchRepoDetails,
18+
updateDbFile,
19+
updatePortDetails,
20+
getPortDetails,
21+
} = require("../API/settingsApi");
1622
const {
1723
gitRemoveAllStagedItemApi,
1824
gitRemoveStagedItemApi,
@@ -195,6 +201,16 @@ module.exports.settingsFetchDbPath = settingsFetchDbPath = async () => {
195201
return await fetchDatabaseFile();
196202
};
197203

204+
module.exports.settingsGetPortDetails = settingsGetPortDetails = async () => {
205+
return await getPortDetails();
206+
};
207+
208+
module.exports.settingsUpdatePortDetail = settingsUpdatePortDetail = async (
209+
newPort
210+
) => {
211+
return await updatePortDetails(newPort);
212+
};
213+
198214
module.exports.settingsFetchRepoDetails = settingsFetchRepoDetails = async () => {
199215
return await fetchRepoDetails();
200216
};
@@ -232,3 +248,7 @@ module.exports.deleteRepo = deleteRepo = async (
232248
module.exports.addBranch = addBranch = async (repoId, branchName) => {
233249
return await gitAddBranchApi(repoId, branchName);
234250
};
251+
252+
module.exports.updateDbFileApi = updateDbFileApi = async (fileName) => {
253+
return await updateDbFile(fileName);
254+
};

global/globalRouteStore.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ module.exports.REPO_FILE_DIFF = "REPO_FILE_DIFF";
88
module.exports.GIT_STAGED_FILES = "GIT_STAGED_FILES";
99
module.exports.GIT_UNPUSHED_COMMITS = "GIT_UNPUSHED_COMMITS";
1010
module.exports.SETTINGS_DBPATH = "SETTINGS_DBPATH";
11+
module.exports.SETTINGS_DBPATH = "SETTINGS_DBPATH";
12+
module.exports.SETTINGS_PORT = "SETTINGS_PORT";
1113
module.exports.SETTINGS_REPODETAILS = "SETTINGS_REPODETAILS";

global/gqlGlobalAPISchema.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const globalAPISchema = new buildSchema(
1717
gitStagedFiles: gitStagedFileResults!
1818
gitUnpushedCommits: gitUnpushedCommitResults!
1919
settingsDatabasePath: String!
20+
settingsPortDetails: Int!
2021
settingsRepoDetails: [settingsFetchRepoResults]!
2122
}
2223
@@ -97,12 +98,14 @@ const globalAPISchema = new buildSchema(
9798
commitChanges(repoId: String!, commitMessage: String!): String!
9899
pushToRemote(repoId: String!, remoteHost: String!, branch: String!): String!
99100
settingsEditDbPath(newPath: String!): String!
101+
settingsEditPort(newPort: Int!): String!
100102
settingsDeleteRepo(repoId: String!): String!
101103
removeStagedItem(repoId: String!, item: String!): String!
102104
removeAllStagedItem(repoId: String!): String!
103105
addBranch(repoId: String!, branchName: String!): String!
104106
fetchFromRemote(repoId: String!): gitFetchStatus!
105107
pullFromRemote(repoId: String!): gitPullStatus!
108+
updateRepoDataFile(newDbFile: String!): String!
106109
deleteRepo(repoId: String!, name: String!, pathName: String!, time: String!): deleteRepoStatus!
107110
}
108111

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"child_process": "^1.0.2",
2929
"cors": "^2.8.5",
3030
"dotenv": "^8.2.0",
31+
"envfile": "^6.8.0",
3132
"express": "^4.17.1",
3233
"express-graphql": "^0.9.0",
3334
"graphiql": "^0.17.5",

server.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
const globalAPI = require("./global/globalAPIHandler");
22
const express = require("express");
33
const path = require("path");
4+
5+
const dotenv = require("dotenv").config();
6+
47
const app = globalAPI;
58

69
app.use(express.static(path.join(__dirname, "build")));
@@ -9,7 +12,7 @@ app.get("/*", (req, res) => {
912
res.sendFile(path.join(__dirname, "build", "index.html"));
1013
});
1114

12-
globalAPI.listen(9001, (err) => {
15+
globalAPI.listen(process.env.GITCONVEX_PORT || 9001, (err) => {
1316
if (err) {
1417
console.log(err);
1518
}

0 commit comments

Comments
 (0)