-
Notifications
You must be signed in to change notification settings - Fork 4
/
cli.js
214 lines (197 loc) · 6.92 KB
/
cli.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env node
const downloadFile = require('./download-file');
const fs = require('fs-extra');
const { promisify } = require('util');
const extractZip = promisify(require('extract-zip'));
const path = require('path');
const { JSDOM } = require('jsdom');
const requestPromise = require('request-promise-native');
const BASE_URL = "https://addons-ecs.forgesvc.net/api/v2";
function createModpacksFolder() {
try {
fs.mkdirSync("./modpacks");
} catch (err) {
if (err.code != "EEXIST") {
console.error("ERROR: Can't create modpacks folder! Make sure that program has access to current folder.");
process.exit(1);
}
}
}
function createProjectFolder(projectName) {
try {
fs.mkdirSync('./modpacks/' + projectName);
} catch(err) {
if(err.code == "EEXIST") {
console.error(`ERROR: There's already folder for a project ${projectName}. To download it again, delete this folder.`);
}
else {
console.error("ERROR: Can't create project folder! Make sure that program has access to current folder.");
}
process.exit(1);
}
}
/**
*
* @param {string} projectSlug project slug
* @returns { project } project
*/
async function getProjectBySlug(projectSlug) {
const searchUrl = `${BASE_URL}/addon/search?gameId=432&categoryId=0&searchFilter=${projectSlug}`
+ `&pageSize=20&index=$index&sort=1&sortDescending=true§ionId=4471`;
let results;
let index = 0;
while (index == 0 || results.length) {
try {
let url = searchUrl.replace("$index", index);
let searchRes = await requestPromise.get(url);
results = JSON.parse(searchRes);
} catch (err) {
console.error(err);
process.exit(1);
}
let project = results.filter(x => x.slug == projectSlug);
if (project.length) return project[0];
index += 20;
}
console.error(`Can't find project ${projectSlug}.`);
process.exit(1);
}
/**
*
* @param {string} projectId project ID
* @returns { project } project
*/
async function getProjectById(projectId) {
try {
const res = await requestPromise.get(`${BASE_URL}/addon/${projectId}`);
return JSON.parse(res);
} catch (err) {
console.error(err);
process.exit(1);
}
}
/**
*
* @param {string} projectId project ID
* @returns { { id: number, fileName: string, downloadUrl: string }[] } array of files
*/
async function getProjectFiles(projectId) {
try {
const res = await requestPromise.get(`${BASE_URL}/addon/${projectId}/files`);
return JSON.parse(res);
} catch (err) {
console.error(err);
process.exit(1);
}
}
/**
*
* @param {string} project project name
* @returns { { url: string, version: string, fileName: string } }
*/
async function getLatestProjectFileUrl(projectSlug) {
const project = await getProjectBySlug(projectSlug);
const defaultFile = project.latestFiles.filter(x => x.id == project.defaultFileId)[0];
return {
url: defaultFile.downloadUrl,
version: defaultFile.displayName,
fileName: defaultFile.fileName
}
}
/**
*
* @param {string} projectId project ID
* @param {string} fileId file ID
* @returns { file } file
*/
async function getProjectFile(projectId, fileId) {
const project = await getProjectById(projectId);
const file = project.latestFiles.filter(x => x.id == fileId);
if (file.length) {
return file[0];
}
const projectFiles = await getProjectFiles(projectId);
const file2 = projectFiles.filter(x => x.id == fileId);
if (file2.length)
{
return file2[0];
}
console.error(`File ${fileId} not found in project ${projectId}.`);
process.exit(1);
}
function loadManifest(path) {
let manifest = fs.readFileSync(path).toString();
return JSON.parse(manifest);
}
async function generateFileListFromManifest(manifest) {
return Promise.all(manifest.files
.map(async file => {
let f = await getProjectFile(file.projectID, file.fileID);
return {
name: f.fileName,
downloadUrl: f.downloadUrl
};
}));
}
function removeIllegalCharactersFromFilename(filename) {
return filename.replace(/[/\\?%*:|"<>]/g, '-');
}
/**
*
* @param {string[]} argv
*/
async function main(argv) {
if(argv.length < 3) {
console.error("Usage: cmpdl <project name>");
process.exit(1);
}
const project = argv[2];
console.log("Searching for project main file");
const latest = await getLatestProjectFileUrl(project);
createModpacksFolder();
const projectFolderName = removeIllegalCharactersFromFilename(latest.version);
createProjectFolder(projectFolderName);
const projectFolderPath = path.resolve(`./modpacks/${projectFolderName}`);
const projectArchivePath = `${projectFolderPath}/${latest.fileName}`
console.log("Downloading project main file v." + latest.version);
await downloadFile(latest.url, projectArchivePath);
console.log("Extracting...");
await extractZip(projectArchivePath, {dir: path.join(projectFolderPath, 'extracted')});
console.log("Extracted");
const manifest = loadManifest(path.join(projectFolderPath, "extracted", "manifest.json"));
const dotMinecraft = path.join(projectFolderPath, ".minecraft");
fs.mkdirSync(dotMinecraft);
const modsPath = path.join(dotMinecraft, "mods");
fs.mkdirSync(modsPath);
console.log("Generating file list...");
const fileList = await generateFileListFromManifest(manifest);
console.log("Generated file list!");
const total = fileList.length;
let downloaded = 0;
console.log(`There's ${total} mods to download...`);
console.log("Starting downloading mods...");
for(let i = 0; i < total; i++) {
let progress = `(${downloaded + 1}/${total}) `;
let maxWidth = `(${total}/${total}) `.length;
if(progress.length < maxWidth)
progress = progress + ' '.repeat(maxWidth - progress.length);
await downloadFile(fileList[i].downloadUrl, path.join(modsPath, fileList[i].name), progress);
downloaded++;
}
console.log("Finished downloading");
console.log("Finishing job...");
if(manifest.overrides) {
const overridesDir = path.join(projectFolderPath, "extracted", manifest.overrides);
console.log("Copying overrides...");
fs.copySync(overridesDir, dotMinecraft, { overwrite: true });
console.log("Copied overrides!");
}
console.log("Finished!")
console.log(`Now you have to install minecraft ${manifest.minecraft.version}`);
if(manifest.minecraft.modLoaders) {
console.log('Then you need to install mod loaders: ');
manifest.minecraft.modLoaders.forEach(modLoader => console.log(modLoader.id));
}
console.log(`After that copy everything from ${dotMinecraft}\nto your downloaded .minecraft and you're ready to go!`);
}
main(process.argv);