Fix not building in some cases and wrongly following download redirects
This commit is contained in:
parent
69c7fb8c2f
commit
c06cf8b5d3
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
69
src/index.ts
69
src/index.ts
|
@ -48,7 +48,7 @@ async function run(): Promise<{ code: number, msg?: string }> {
|
|||
console.log('Installed Java-Version:');
|
||||
await runCmd('java', ['-version'], workingDir.base, appLogFile);
|
||||
|
||||
console.log(`Downloading '${buildTool.url}'...`);
|
||||
console.log(`\nDownloading '${buildTool.url}'...`);
|
||||
await downloadFile(buildTool.url, joinPath(workingDir.cache, 'BuildTools.jar'));
|
||||
|
||||
const gotTemplateDirectory = versions.length != 1;
|
||||
|
@ -90,8 +90,8 @@ async function run(): Promise<{ code: number, msg?: string }> {
|
|||
|
||||
const tasks = [];
|
||||
for (const ver of versions) {
|
||||
tasks.push((callback: (err?: Error, result?: unknown) => void) => {
|
||||
try {
|
||||
tasks.push(async (): Promise<void> => {
|
||||
return new Promise(async (resolveTask, rejectTask): Promise<void> => {
|
||||
const start = Date.now();
|
||||
|
||||
const logFile = joinPath(workingDir.logs, `${ver}.log`);
|
||||
|
@ -102,45 +102,44 @@ async function run(): Promise<{ code: number, msg?: string }> {
|
|||
const versionDir = gotTemplateDirectory ? joinPath(workingDir.base, `${ver}`) : workingDir.cache;
|
||||
|
||||
if (gotTemplateDirectory) {
|
||||
copy(workingDir.cache, versionDir)
|
||||
.then(() => {
|
||||
runCmd('java', [...buildToolsArgs, '--rev', ver],
|
||||
versionDir, logFile, true) // set to silent because multiple builds can run at once
|
||||
.then(() => {
|
||||
rmdirSync(versionDir, {recursive: true}); // delete our task dir
|
||||
|
||||
const end = Date.now();
|
||||
|
||||
console.log(`Finished building '${ver}' in ${((end - start) / 60_000)} minutes`);
|
||||
callback();
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(`An error occurred while building '${ver}'`);
|
||||
console.error(err);
|
||||
|
||||
console.error(`\nPrinting last 25 lines from '${resolvePath(logFile)}':`);
|
||||
rll.read(logFile, 25)
|
||||
.then((lines: string[]) => {
|
||||
for (const line of lines) {
|
||||
console.error(line);
|
||||
}
|
||||
})
|
||||
.catch(console.error)
|
||||
.finally(() => callback(err));
|
||||
});
|
||||
await copy(workingDir.cache, versionDir);
|
||||
}
|
||||
} catch (err) {
|
||||
callback(err);
|
||||
}
|
||||
|
||||
try {
|
||||
// set to silent because multiple builds can run at once
|
||||
await runCmd('java', [...buildToolsArgs, '--rev', ver], versionDir, logFile, true);
|
||||
|
||||
if (gotTemplateDirectory) {
|
||||
rmdirSync(versionDir, {recursive: true}); // delete our task dir
|
||||
}
|
||||
|
||||
const end = Date.now();
|
||||
|
||||
console.log(`Finished '${ver}' in ${((end - start) / 60_000).toFixed(2)} minutes`);
|
||||
resolveTask();
|
||||
} catch (err) {
|
||||
console.log(`An error occurred while building '${ver}'`);
|
||||
console.error(err);
|
||||
|
||||
console.error(`\nPrinting last 25 lines from '${resolvePath(logFile)}':`);
|
||||
rll.read(logFile, 25)
|
||||
.then((lines: string[]) => {
|
||||
for (const line of lines) {
|
||||
console.error(line);
|
||||
}
|
||||
})
|
||||
.catch(console.error)
|
||||
.finally(() => rejectTask(err));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
parallelLimit(tasks, threadCount, ((err, results) => {
|
||||
parallelLimit(tasks, threadCount, (err) => {
|
||||
if (err) return reject(err);
|
||||
|
||||
resolve({code: 0});
|
||||
}));
|
||||
});
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
|
12
src/utils.ts
12
src/utils.ts
|
@ -83,11 +83,19 @@ export async function downloadFile(url: string, dest: string, currRedirectDepth:
|
|||
}
|
||||
}, (httpRes) => {
|
||||
if (httpRes.statusCode != 200) {
|
||||
const locHeader = httpRes.headers.location;
|
||||
|
||||
// Follow redirect
|
||||
if (currRedirectDepth < 12 &&
|
||||
if (currRedirectDepth < 12 && locHeader &&
|
||||
(httpRes.statusCode == 301 || httpRes.statusCode == 302 || httpRes.statusCode == 303 ||
|
||||
httpRes.statusCode == 307 || httpRes.statusCode == 308)) {
|
||||
return downloadFile(url, dest, ++currRedirectDepth)
|
||||
done(false);
|
||||
|
||||
if (!/https?:\/\//g.test(locHeader)) {
|
||||
return reject(new Error(`Server responded with ${httpRes.statusCode} and a relative Location-Header value (${locHeader})`));
|
||||
}
|
||||
|
||||
return downloadFile(locHeader, dest, ++currRedirectDepth)
|
||||
.then(resolve)
|
||||
.catch(reject);
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue