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

fix: clean up snpashot tool on download fail #893

Merged
merged 1 commit into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions snapshot/android/snapshot-generator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require("fs");
const { dirname, join } = require("path");
const { dirname, join, EOL } = require("path");
const os = require("os");
const child_process = require("child_process");

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

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

const downloadUrl = MKSNAPSHOT_TOOLS_DOWNLOAD_ROOT_URL + mksnapshotToolRelativePath;
createDirectory(dirname(mksnapshotToolPath));
snapshotToolsDownloads[mksnapshotToolPath] = downloadFile(downloadUrl, mksnapshotToolPath);
snapshotToolsDownloads[mksnapshotToolPath] = downloadFile(downloadUrl, mksnapshotToolPath, MKSNAPSHOT_TOOLS_DOWNLOAD_TIMEOUT);
snapshotToolsDownloads[mksnapshotToolPath].catch(err => {
const errorMessage = err && err.message ? err.message : "";
let cleanupError = "";
try {
fs.unlinkSync(mksnapshotToolPath);
} catch (unlinkErr) {
if (unlinkErr && unlinkErr.code !== "ENOENT") {
cleanupError = `${EOL}Failed to cleanup mksnapshot tool.`;
}
}

throw new Error(`Failed to download mksnapshot tool. Error: ${errorMessage}.${cleanupError}`);
});
return snapshotToolsDownloads[mksnapshotToolPath];
}

Expand Down
8 changes: 4 additions & 4 deletions snapshot/android/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const CONSTANTS = {

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

const downloadFile = (url, destinationFilePath) =>
const downloadFile = (url, destinationFilePath, timeout) =>
new Promise((resolve, reject) => {
getRequestOptions(url)
getRequestOptions(url, timeout)
.then(options =>
get(options)
.on("error", reject)
Expand Down Expand Up @@ -49,9 +49,9 @@ const getJsonFile = url =>
).catch(reject);
});

const getRequestOptions = (url) =>
const getRequestOptions = (url, timeout) =>
new Promise((resolve, reject) => {
const options = { url };
const options = { url, timeout };
getProxySettings()
.then(proxySettings => {
const allOptions = Object.assign(options, proxySettings);
Expand Down