Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit 0bedefc

Browse files
KristianDDrosen-vladimirov
authored andcommitted
fix: clean up snpashot tool on download fail (#893)
1 parent 5f7f70a commit 0bedefc

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

snapshot/android/snapshot-generator.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const fs = require("fs");
2-
const { dirname, join } = require("path");
2+
const { dirname, join, EOL } = require("path");
33
const os = require("os");
44
const child_process = require("child_process");
55

@@ -12,6 +12,7 @@ const BUNDLE_PREAMBLE_PATH = join(__dirname, "snapshot-generator-tools/bundle-pr
1212
const BUNDLE_ENDING_PATH = join(__dirname, "snapshot-generator-tools/bundle-ending.js");
1313
const INCLUDE_GRADLE_PATH = join(__dirname, "snapshot-generator-tools/include.gradle");
1414
const MKSNAPSHOT_TOOLS_DOWNLOAD_ROOT_URL = "https://raw.githubusercontent.com/NativeScript/mksnapshot-tools/production/";
15+
const MKSNAPSHOT_TOOLS_DOWNLOAD_TIMEOUT = 60000;
1516
const SNAPSHOT_BLOB_NAME = "TNSSnapshot";
1617

1718
function shellJsExecuteInDir(dir, action) {
@@ -66,7 +67,20 @@ SnapshotGenerator.prototype.downloadMksnapshotTool = function(snapshotToolsPath,
6667

6768
const downloadUrl = MKSNAPSHOT_TOOLS_DOWNLOAD_ROOT_URL + mksnapshotToolRelativePath;
6869
createDirectory(dirname(mksnapshotToolPath));
69-
snapshotToolsDownloads[mksnapshotToolPath] = downloadFile(downloadUrl, mksnapshotToolPath);
70+
snapshotToolsDownloads[mksnapshotToolPath] = downloadFile(downloadUrl, mksnapshotToolPath, MKSNAPSHOT_TOOLS_DOWNLOAD_TIMEOUT);
71+
snapshotToolsDownloads[mksnapshotToolPath].catch(err => {
72+
const errorMessage = err && err.message ? err.message : "";
73+
let cleanupError = "";
74+
try {
75+
fs.unlinkSync(mksnapshotToolPath);
76+
} catch (unlinkErr) {
77+
if (unlinkErr && unlinkErr.code !== "ENOENT") {
78+
cleanupError = `${EOL}Failed to cleanup mksnapshot tool.`;
79+
}
80+
}
81+
82+
throw new Error(`Failed to download mksnapshot tool. Error: ${errorMessage}.${cleanupError}`);
83+
});
7084
return snapshotToolsDownloads[mksnapshotToolPath];
7185
}
7286

snapshot/android/utils.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ const CONSTANTS = {
1212

1313
const createDirectory = dir => mkdir('-p', dir);
1414

15-
const downloadFile = (url, destinationFilePath) =>
15+
const downloadFile = (url, destinationFilePath, timeout) =>
1616
new Promise((resolve, reject) => {
17-
getRequestOptions(url)
17+
getRequestOptions(url, timeout)
1818
.then(options =>
1919
get(options)
2020
.on("error", reject)
@@ -49,9 +49,9 @@ const getJsonFile = url =>
4949
).catch(reject);
5050
});
5151

52-
const getRequestOptions = (url) =>
52+
const getRequestOptions = (url, timeout) =>
5353
new Promise((resolve, reject) => {
54-
const options = { url };
54+
const options = { url, timeout };
5555
getProxySettings()
5656
.then(proxySettings => {
5757
const allOptions = Object.assign(options, proxySettings);

0 commit comments

Comments
 (0)