-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.js
132 lines (103 loc) · 3.83 KB
/
update.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const path = require("path");
const VERSION_TYPE = {
MAJOR: "major",
MINOR: "minor",
PATCH: "patch"
}
// Redeclaring the Nodejs global variable object
global.root = path.resolve(__dirname + "/");
function semver(version, type, prefix = '') {
let [major, minor, patch] = version.split(".");
major = parseInt(major);
minor = parseInt(minor);
patch = parseInt(patch);
switch (type) {
case VERSION_TYPE.MAJOR:
major++;
minor = 0;
patch = 0;
break;
case VERSION_TYPE.MINOR:
minor++;
patch = 0;
break;
default:
patch++;
break;
}
return `${major}.${minor}.${patch}${prefix}`;
}
function main(env, version, update = true){
console.log("\n--------------------------------------");
console.log(" VALHALLA VERSION MANAGER ");
console.log("--------------------------------------");
console.log(" - Environment : " + (env == "d" ? "development":"production"));
console.log(" - Version: " + version );
console.log(" - Update directive: " + update + "\n");
const fs = require("fs");
if(update !== true){
update = false;
}
// Read version.json
console.log(" Reading version file...");
const versionFile = fs.openSync(path.join(global.root,"version.json"), "r");
const versionJsonFile = JSON.parse(fs.readFileSync(versionFile, "utf8"));
fs.closeSync(versionFile);
console.log(" DONE. \n");
// Read package.json
console.log(" Reading package.json file...");
const packageFile = fs.openSync(path.join(global.root,"package.json"), "r");
const packageJsonFile = JSON.parse(fs.readFileSync(packageFile, "utf8"));
fs.closeSync(packageFile);
console.log(" DONE. \n");
// update version
if(update){
console.log(" Updating version info...");
versionJsonFile.VERSION = version
}
// Change the environment
if(env == "d") {
console.log(" Setting environment to development.");
versionJsonFile.ENVIRONMENT = "development";
} else {
console.log(" Setting environment to production.");
versionJsonFile.ENVIRONMENT = "production";
}
// Update package.json
packageJsonFile.version = versionJsonFile.VERSION;
// write version.json
console.log(" Reading version file...");
const file2 = fs.openSync(path.join(global.root,"version.json"), "w");
fs.writeFileSync(file2, JSON.stringify(versionJsonFile, null, 4), "utf8");
fs.closeSync(file2);
console.log(" DONE. \n");
// write package.json
console.log(" Reading package.json file...");
const file3 = fs.openSync(path.join(global.root,"package.json"), "w");
fs.writeFileSync(file3, JSON.stringify(packageJsonFile, null, 4), "utf8");
fs.closeSync(file3);
console.log(" DONE. \n");
// open config.ts
console.log(" Reading valhalla config file...");
const configFile = fs.openSync(path.join(global.root,"client/src/config/config.ts"), "r");
const configString = fs.readFileSync(configFile, "utf8");
fs.closeSync(configFile);
console.log(" DONE. \n");
// update
let configStringUpdated = "";
console.log(" Setting index.html file");
if(env == "d") {
configStringUpdated = configString.replaceAll("index-prod.html", "index.html");
}
else {
configStringUpdated = configString.replaceAll("index.html", "index-prod.html");
}
console.log(" DONE. \n");
console.log(" Updating valhalla config file...");
const configFileUpdated = fs.openSync("client/src/config/config.ts", "w");
fs.writeFileSync(configFileUpdated, configStringUpdated, "utf8");
fs.closeSync(configFileUpdated);
console.log(" DONE. \n");
}
const args = process.argv.slice(2);
main(args[0], args[1], args[2]);