-
-
Notifications
You must be signed in to change notification settings - Fork 846
Expand file tree
/
Copy pathupdate.js
More file actions
137 lines (124 loc) · 6.37 KB
/
update.js
File metadata and controls
137 lines (124 loc) · 6.37 KB
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
133
134
135
136
137
import fs from 'fs';
import path from 'path';
import fetch from 'node-fetch';
const args = process.argv.slice(2);
const versionArg = args.find(arg => arg.startsWith('--ejs_v='));
const devArg = args.find(arg => arg.startsWith('--dev='));
const depsArg = args.find(arg => arg.startsWith('--deps='));
const update_version = versionArg ? versionArg.split('=')[1] : process.env.ejs_v;
const dev = devArg ? devArg.split('=')[1] : null;
let version;
try {
const packageJsonPath = path.resolve('package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
version = packageJson.version;
} catch(error) {
console.error("Error reading version from package.json:", error.message);
process.exit(1);
}
const updateDependencies = async () => {
const socket_io = path.resolve('node_modules', 'socket.io', 'client-dist', 'socket.io.min.js');
const ejs_socket_io = path.resolve('data', 'src', 'vendor', 'socket.io.min.js');
const nipplejs = path.resolve('node_modules', 'nipplejs', 'dist', 'nipplejs.js');
const ejs_nipplejs = path.resolve('data', 'src', 'vendor', 'nipplejs.js');
try {
fs.copyFileSync(socket_io, ejs_socket_io);
if (!fs.readFileSync(ejs_socket_io, 'utf8').endsWith('\n')) {
fs.appendFileSync(ejs_socket_io, '\n');
}
} catch(error) {
console.error("Error updating socket.io:", error.message);
}
try {
fs.copyFileSync(nipplejs, ejs_nipplejs);
if (!fs.readFileSync(ejs_nipplejs, 'utf8').endsWith('\n')) {
fs.appendFileSync(ejs_nipplejs, '\n');
}
} catch(error) {
console.error("Error updating nipplejs:", error.message);
}
console.log("Updated socket.io and nipplejs.");
};
const updateVersion = async (newVersion) => {
const packageJsonPath = path.resolve('package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
packageJson.version = newVersion;
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 4) + '\n');
console.log(`Updated version to ${newVersion} in package.json.`);
const versionJsonPath = path.resolve('data', 'version.json');
const versionJson = JSON.parse(fs.readFileSync(versionJsonPath, 'utf8'));
versionJson.version = newVersion;
fs.writeFileSync(versionJsonPath, JSON.stringify(versionJson, null, 4) + '\n');
console.log(`Updated version to ${newVersion} in data/version.json.`);
const coresJsonPath = path.resolve('data', 'cores', 'package.json');
const coresJson = JSON.parse(fs.readFileSync(coresJsonPath, 'utf8'));
coresJson.version = newVersion;
fs.writeFileSync(coresJsonPath, JSON.stringify(coresJson, null, 4) + '\n');
console.log(`Updated version to ${newVersion} in data/cores/package.json.`);
const emulatorJsPath = path.resolve('data', 'src', 'emulator.js');
const emulatorJs = fs.readFileSync(emulatorJsPath, 'utf8');
let updatedEmulatorJs = "";
if (dev === "true") {
updatedEmulatorJs = emulatorJs.replace(/this\.ejs_version\s*=\s*".*?";/, `this.ejs_version = "${newVersion}-dev";`);
} else {
updatedEmulatorJs = emulatorJs.replace(/this\.ejs_version\s*=\s*".*?";/, `this.ejs_version = "${newVersion}";`);
}
fs.writeFileSync(emulatorJsPath, updatedEmulatorJs);
console.log(`Updated version to ${newVersion} in data/src/emulator.js.`);
};
const fetchContributors = async () => {
const url = 'https://api.github.com/repos/EmulatorJS/EmulatorJS/contributors';
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch contributors: ${response.statusText}`);
}
return await response.json();
} catch(error) {
console.error("Error fetching contributors:", error.message);
}
};
const updateContributors = async () => {
const contributors = await fetchContributors();
const ignoredContributors = JSON.parse(fs.readFileSync(path.resolve('docs', 'contributors.json'), 'utf8')).ignore;
const missingContributors = JSON.parse(fs.readFileSync(path.resolve('docs', 'contributors.json'), 'utf8')).missing;
if (!contributors) return;
const sortedContributors = contributors
.concat(missingContributors)
.sort((a, b) => b.contributions - a.contributions)
.filter(contributor => !ignoredContributors.includes(contributor.login));
const uniqueContributors = Array.from(new Set(sortedContributors.map(contributor => contributor.login)))
.map(login => sortedContributors.find(contributor => contributor.login === login));
const finalContributors = uniqueContributors.sort((a, b) => b.contributions - a.contributions);
let contributorReadme = ""
finalContributors
.forEach(contributor => {
contributorReadme += `<a href="${contributor.html_url}" target="_blank" title="${contributor.login} - Contributions: ${contributor.contributions}" alt="${contributor.login}"><img src="${contributor.avatar_url}&size=95" width="95px"></a> \n`;
});
const contributorsPath = path.resolve('docs', 'contributors.md');
const contributorsReadme = fs.readFileSync(contributorsPath, 'utf8');
const startLine = contributorsReadme.split('\n').findIndex(line => line.startsWith("<!-- Others -->")) + 1;
const endLine = contributorsReadme.split('\n').findIndex(line => line.startsWith("<!-- Others End -->")) - 1;
const updatedContributorsReadme = contributorsReadme.split('\n').filter((line, index) => index < startLine || index > endLine).join('\n');
const newContributorsReadme = updatedContributorsReadme.replace("<!-- Others -->", `<!-- Others -->\n${contributorReadme}`);
fs.writeFileSync(contributorsPath, newContributorsReadme);
console.log("Updated Contributors.md with new contributors.");
}
(async () => {
console.log(`Current EmulatorJS Version: ${version}`);
if (!update_version) {
console.warn("Warning: Version number not provided.");
} else {
console.log(`Updating EmulatorJS Version number to: ${update_version}`);
}
console.log("Updating EmulatorJS dependencies...");
if (depsArg) {
await updateDependencies();
}
if (update_version || dev === "false" || dev === "true") {
console.log("Updating EmulatorJS version...");
await updateVersion(update_version || version);
}
await updateContributors();
console.log("Updating EmulatorJS completed.");
})();