This repository has been archived by the owner on Apr 3, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
168 lines (137 loc) · 4.26 KB
/
index.ts
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) 2019 Zekromaster
//
// GNU GENERAL PUBLIC LICENSE
// Version 3, 29 June 2007
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Various declarations
import * as fs from 'fs';
import * as request from 'request';
import * as rmrf from 'rimraf';
import { promisify } from 'es6-promisify';
function generateUrl(url: string | undefined | null): string{
if (url === undefined || url === null) return "IGNORE";
return url;
}
class Mod {
private _name:string;
private _version:string;
private _url:string;
private _directory:string;
private _downloadedFlag: boolean;
// A pretty straightforward constructor
constructor(name:string, version:string, url:string | undefined | null, directory:string){
this._name = name;
this._version = version;
this._url = generateUrl(url);
this._directory = directory;
this._downloadedFlag = false;
}
// Getters
get name(): string {
return this._name;
}
get version(): string {
return this._version;
}
get url(): string {
return this._url;
}
get directory(): string{
return this._directory;
}
// A single setter
set downloadedFlag(downloadedFlag: boolean){
this._downloadedFlag = downloadedFlag;
}
// More complex getters - they expose filename and path
get filename(): string{
return this.name + " [" + this.version + "].jar";
}
get path():string{
return this.directory + "/" + this.filename;
}
// This downloads the mod
async download(){
// If the mod was already downloaded, nothing happenss
if (this._downloadedFlag) {
console.log(this.filename + " has already been downloaded. No need to download again.");
return;
}
// Checking if the file must be downloaded or not.
if (this.url === "IGNORE") {
console.log(this.filename + " must be supplied by the user");
return;
}
// Executing the download
console.log(this.filename + " will be downloaded");
var wsFile: fs.WriteStream = fs.createWriteStream(this.path);
request(this.url).pipe(wsFile);
await new Promise(fulfill => wsFile.on("finish", fulfill));
// Logging the completion of the download
console.log(this.filename + " has been downloaded!");
}
}
export async function download(jsModlist:any){
var marrModlist:Array<Mod> = [];
var sModFolder: string = jsModlist.directory;
// If the mod folder does not exist, we create it
if (!fs.existsSync(sModFolder)){
await fs.mkdir(
sModFolder,
()=>{
console.log("Created mod folder")
}
);
}
// This writes the directory as a list of files
var sarrDirread:Array<string> = await promisify(fs.readdir)(sModFolder) as Array<string>;
// This generates an array of "mod" objects
for (let mod of jsModlist.mods){
marrModlist.push(
new Mod(
mod.name,
mod.version,
mod.url,
sModFolder
)
);
}
// Deciding what to download - Iterating over the modlist
for (let mod of marrModlist){
mod.downloadedFlag = sarrDirread.includes(mod.filename);
}
// Deleting removed mods
for (let file of sarrDirread){
let bIsContained = false;
// We iterate over the list of mods searching for the [file]
for (let mod of marrModlist){
if (file == mod.filename) bIsContained = true;
}
// If [file] is not in the modlist, we remove it
if (!bIsContained){
await rmrf(
sModFolder + "/" + file,
()=>{
console.log(file + " was removed and thus the file is now deleted.")
}
);
}
}
// Downloading the missing mods
for (let mod of marrModlist){
await mod.download();
}
console.log("Remember to use forge " + jsModlist.forgeVersion);
}