Skip to content

Commit dfdd042

Browse files
committed
Merge pull request #11 from neel1996/npm-config-fix
1 parent c654b41 commit dfdd042

File tree

4 files changed

+64
-29
lines changed

4 files changed

+64
-29
lines changed

env_config.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

global/envConfigReader.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,24 @@ const fs = require("fs");
22
const path = require("path");
33

44
function getEnvData() {
5-
const envFileData = fs.readFileSync(path.relative(".", "env_config.json"));
5+
try {
6+
const envFileData = fs.readFileSync(
7+
path.join(__dirname, "..", "env_config.json")
8+
);
69

7-
const envContent = envFileData.toString();
8-
let envData = JSON.parse(envContent)[0];
10+
const envContent = envFileData.toString();
11+
let envData = JSON.parse(envContent)[0];
912

10-
return {
11-
DATABASE_FILE: envData.databaseFile,
12-
GITCONVEX_PORT: envData.port,
13-
};
13+
return {
14+
DATABASE_FILE: envData.databaseFile,
15+
GITCONVEX_PORT: envData.port,
16+
};
17+
} catch (e) {
18+
return {
19+
DATABASE_FILE: "",
20+
GITCONVEX_PORT: "",
21+
};
22+
}
1423
}
1524

1625
module.exports.getEnvData = getEnvData;

global/fetchGitRepoPath.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
const fs = require("fs");
2+
const path = require("path");
23
const dotenv = require("dotenv").config();
34
const { DATABASE_FILE } = require("./envConfigReader").getEnvData();
45

56
const getRepoPath = (repoId) => {
6-
const dataEntry = fs
7-
.readFileSync(DATABASE_FILE || "./database/repo-datastore.json")
8-
.toString();
7+
const dataEntry = fs.readFileSync(DATABASE_FILE).toString();
98

109
const repoObject = JSON.parse(dataEntry);
1110
var repoPath = "";

server.js

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,62 @@ const express = require("express");
55
const path = require("path");
66
const fs = require("fs");
77
const dotenv = require("dotenv").config();
8-
const {
8+
const { updateDbFile } = require("./API/settingsApi");
9+
const app = globalAPI;
10+
const log = console.log;
11+
var envConfigFilename = "env_config.json";
12+
var envConfigFilePath = path.join(__dirname, envConfigFilename);
13+
14+
app.use(express.static(path.join(__dirname, "build")));
15+
16+
log("INFO: Checking for config file");
17+
18+
let configStatus = "";
19+
try {
20+
configStatus = fs.accessSync(envConfigFilePath);
21+
} catch (e) {
22+
log("ERROR: No config file found. Falling back to config creation module");
23+
const configData = [
24+
{
25+
databaseFile: path.join(__dirname, "database/repo-datastore.json"),
26+
port: 9001,
27+
},
28+
];
29+
log(
30+
"INFO: Creating config file with default config -> " +
31+
JSON.stringify(configData)
32+
);
33+
fs.writeFileSync(envConfigFilePath, JSON.stringify(configData), {
34+
flag: "w",
35+
});
36+
}
37+
38+
let {
939
DATABASE_FILE,
1040
GITCONVEX_PORT,
1141
} = require("./global/envConfigReader").getEnvData();
12-
13-
const app = globalAPI;
1442
const port = GITCONVEX_PORT;
1543

16-
app.use(express.static(path.join(__dirname, "build")));
44+
log("INFO: Config file is present");
45+
log("INFO: Reading from config file " + envConfigFilePath);
1746

1847
app.get("/*", (req, res) => {
1948
res.sendFile(path.join(__dirname, "build", "index.html"));
2049
});
2150

2251
globalAPI.listen(port || 9001, async (err) => {
2352
if (err) {
24-
console.log(err);
53+
log(err);
2554
}
2655

27-
console.log("GitConvex API connected!");
56+
log("GitConvex API connected!");
2857

29-
console.log("\n#Checking data file availability...");
58+
log("\n#Checking data file availability...");
3059

3160
await fs.promises
3261
.access(DATABASE_FILE)
3362
.then(() => {
34-
console.log(
63+
log(
3564
`INFO: Data file ${DATABASE_FILE} is present and it will be used as the active data file!\n\n## You can change this under the settings menu
3665
`
3766
);
@@ -41,41 +70,40 @@ globalAPI.listen(port || 9001, async (err) => {
4170
return await fs.promises
4271
.writeFile(DATABASE_FILE, "[]")
4372
.then((res) => {
44-
console.log(
73+
log(
4574
"\nINFO: New data file created and it will be used as the active file\n\n## You can change this under the settings menu"
4675
);
4776
})
4877
.catch((err) => {
49-
console.log(
78+
log(
5079
"INFO: New data file creation failed!\nINFO: Falling back to directory creation module"
5180
);
5281
});
5382
};
5483

55-
console.log(
84+
log(
5685
`INFO: Data file is missing\nCreating new file under ${DATABASE_FILE}`
5786
);
5887

5988
await dataFileCreator();
6089

61-
if (fs.existsSync()) {
62-
} else {
63-
console.log("INFO: Database directory is missing");
90+
if (!fs.existsSync(DATABASE_FILE)) {
91+
log("INFO: Database directory is missing");
6492
await fs.promises
65-
.mkdir("./database")
93+
.mkdir(path.join(__dirname, ".", "/database"))
6694
.then(async () => {
67-
console.log(
95+
log(
6896
"INFO: Created database directory\nINFO: Setting up new data file in database directory"
6997
);
7098
await dataFileCreator();
7199
})
72100
.catch((err) => {
73-
console.log("ERROR: database directory creation failed!");
101+
log("ERROR: database directory creation failed!");
74102
});
75103
}
76104
});
77105

78-
console.log(
106+
log(
79107
`\n## Gitconvex is running on port ${port}
80108
81109
Open http://localhost:${port}/ to access gitconvex

0 commit comments

Comments
 (0)