Skip to content

Commit 8a35000

Browse files
committed
Code view API modules
1 parent 80fa87e commit 8a35000

8 files changed

+108
-4
lines changed

API/codeFileViewApi.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
const { getRepoPath } = require("../global/fetchGitRepoPath");
4+
const { gitFileBasedCommit } = require("../git/gitFileBasedCommit");
5+
const { isText } = require("istextorbinary");
6+
const { LangLine } = require("@itassistors/langline");
7+
8+
async function codeFileViewApi(repoId, fileName) {
9+
const repoPath = await getRepoPath(repoId);
10+
const targetFile = path.join(repoPath, fileName);
11+
const langData = await new LangLine().withFile(targetFile);
12+
let fileContent = [];
13+
14+
if (isText(targetFile)) {
15+
const commit = await gitFileBasedCommit(repoPath, targetFile);
16+
17+
let fileData = await fs.promises
18+
.readFile(targetFile)
19+
.then((res) => {
20+
return res.toString();
21+
})
22+
.catch((err) => {
23+
console.log(err);
24+
return "";
25+
});
26+
27+
if (langData && langData.name) {
28+
if (fileData) {
29+
fileContent = fileData.split("\n");
30+
return {
31+
codeFileDetails: {
32+
language: langData.name,
33+
fileData: fileContent,
34+
fileCommit: commit,
35+
prism: langData.prismIndicator
36+
? langData.prismIndicator
37+
: "markdown",
38+
},
39+
};
40+
}
41+
} else {
42+
return {
43+
codeFileDetails: {
44+
language: "",
45+
fileData: fileContent,
46+
fileCommit: commit,
47+
prism: "markdown",
48+
},
49+
};
50+
}
51+
}
52+
}
53+
54+
module.exports.codeFileViewApi = codeFileViewApi;

git/gitFileBasedCommit.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const { exec } = require("child_process");
2+
const fs = require("fs");
3+
const util = require("util");
4+
const execPromised = util.promisify(exec);
5+
const fetchRepopath = require("../global/fetchGitRepoPath");
6+
const path = require("path");
7+
8+
async function gitFileBasedCommit(repoPath, fileItem) {
9+
return await execPromised(`git log -1 --oneline "${fileItem}"`, {
10+
cwd: repoPath,
11+
windowsHide: true,
12+
})
13+
.then(({ stderr, stdout }) => {
14+
if (stderr) {
15+
console.log(stderr);
16+
return "";
17+
}
18+
19+
const splitString = stdout.split(" ");
20+
return splitString.slice(1, splitString.length).join(" ");
21+
})
22+
.catch((err) => {
23+
console.log(err);
24+
return "";
25+
});
26+
}
27+
28+
module.exports.gitFileBasedCommit = gitFileBasedCommit;

git/gitFolderDetailsApi.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ const gitFetchFolderContentApi = async (repoId, directoryName) => {
2626
const gitCommits = folderContent.map(async (item) => {
2727
let commitCommand = "";
2828
if (directoryName) {
29-
commitCommand = `git log -1 --oneline "${
30-
directoryName + "/" + item
31-
}"`;
29+
commitCommand = `git log -1 --oneline "${path.join(
30+
directoryName,
31+
item
32+
)}"`;
3233
} else {
3334
commitCommand = `git log -1 --oneline "${item}"`;
3435
}

git/gitRepoAPI.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ async function getGitRepoStatus(repoId) {
1818
return repoDetails;
1919
}
2020

21-
module.exports.getGitRepoStatus = getGitRepoStatus;
21+
module.exports.getGitRepoStatus = getGitRepoStatus;

global/globalAPIHandler.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const {
1717
SETTINGS_REPODETAILS,
1818
SETTINGS_PORT,
1919
GIT_FOLDER_CONTENT,
20+
CODE_FILE_VIEW,
2021
} = require("./globalRouteStore");
2122

2223
const graphqlHTTP = require("express-graphql");
@@ -54,6 +55,7 @@ const {
5455
gitDeleteBranchApi,
5556
gitFolderContentApi,
5657
gitCommitLogSearchFunction,
58+
codeFileViewFunction,
5759
} = require("./globalFunctionStore");
5860

5961
app.use(
@@ -100,6 +102,8 @@ app.use(
100102
return settingsFetchRepoDetails();
101103
case GIT_FOLDER_CONTENT:
102104
return gitFolderContentApi(parsedPayload);
105+
case CODE_FILE_VIEW:
106+
return codeFileViewFunction(parsedPayload);
103107
default:
104108
return { message: "Query Termination" };
105109
}

global/globalFunctionStore.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const { gitStageItem } = require("../git/gitStageItem");
1717
const { gitAddRemoteApi } = require("../git/gitAddRemoteApi");
1818
const { gitDeleteBranchApi } = require("../git/gitBranchDeleteApi");
1919
const { gitFetchFolderContentApi } = require("../git/gitFolderDetailsApi");
20+
const { codeFileViewApi } = require("../API/codeFileViewApi");
2021
const {
2122
fetchDatabaseFile,
2223
fetchRepoDetails,
@@ -101,6 +102,13 @@ module.exports.addRepoFunction = addRepoFunction = async (
101102
}
102103
};
103104

105+
module.exports.codeFileViewFunction = codeFileViewFunction = async (
106+
parsedPayload
107+
) => {
108+
const { repoId, fileItem } = JSON.parse(parsedPayload);
109+
return await codeFileViewApi(repoId, fileItem);
110+
};
111+
104112
/**
105113
* @param {String} parsedPayload - strigified json with repoId: String and skipLimit: number
106114
* @returns {Object} - containing staggered commits and total commits available in the repo

global/globalRouteStore.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ module.exports.SETTINGS_DBPATH = "SETTINGS_DBPATH";
1212
module.exports.SETTINGS_PORT = "SETTINGS_PORT";
1313
module.exports.SETTINGS_REPODETAILS = "SETTINGS_REPODETAILS";
1414
module.exports.GIT_FOLDER_CONTENT = "GIT_FOLDER_CONTENT";
15+
module.exports.CODE_FILE_VIEW = "CODE_FILE_VIEW";

global/gqlGlobalAPISchema.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const globalAPISchema = new buildSchema(
2020
settingsDatabasePath: String!
2121
settingsPortDetails: Int!
2222
settingsRepoDetails: [settingsFetchRepoResults]!
23+
codeFileDetails: langType!
2324
}
2425
2526
type healthCheckResults{
@@ -103,6 +104,13 @@ const globalAPISchema = new buildSchema(
103104
repoPath: String!
104105
}
105106
107+
type langType{
108+
language: String
109+
fileCommit: String
110+
fileData: [String!]
111+
prism: String
112+
}
113+
106114
type GitConvexMutation{
107115
addRepo(repoName: String!, repoPath: String!, initSwitch: Boolean!, cloneSwitch: Boolean!, cloneUrl: String): addRepoStatus!
108116
setBranch(repoId: String!, branch: String!): String!

0 commit comments

Comments
 (0)