-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from snyk/develop
Merge develop into master for release
- Loading branch information
Showing
5 changed files
with
174 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { getSnykDownloadInfo, downloadExecutable } from "../../install"; | ||
import { Platform } from "azure-pipelines-task-lib/task"; | ||
|
||
describe("getSnykDownloadInfo", () => { | ||
it("retrieves the correct download info for Linux", () => { | ||
const dlInfo = getSnykDownloadInfo(Platform.Linux); | ||
expect(dlInfo).toEqual({ | ||
snyk: { | ||
filename: "snyk-linux", | ||
downloadUrl: "https://static.snyk.io/cli/latest/snyk-linux" | ||
}, | ||
snykToHtml: { | ||
filename: "snyk-to-html-linux", | ||
downloadUrl: | ||
"https://static.snyk.io/snyk-to-html/latest/snyk-to-html-linux" | ||
} | ||
}); | ||
}); | ||
|
||
it("retrieves the correct download info for Windows", () => { | ||
const dlInfo = getSnykDownloadInfo(Platform.Windows); | ||
expect(dlInfo).toEqual({ | ||
snyk: { | ||
filename: "snyk-win.exe", | ||
downloadUrl: "https://static.snyk.io/cli/latest/snyk-win.exe" | ||
}, | ||
snykToHtml: { | ||
filename: "snyk-to-html-win.exe", | ||
downloadUrl: | ||
"https://static.snyk.io/snyk-to-html/latest/snyk-to-html-win.exe" | ||
} | ||
}); | ||
}); | ||
|
||
it("retrieves the correct download info for MacOS", () => { | ||
const dlInfo = getSnykDownloadInfo(Platform.MacOS); | ||
expect(dlInfo).toEqual({ | ||
snyk: { | ||
filename: "snyk-macos", | ||
downloadUrl: "https://static.snyk.io/cli/latest/snyk-macos" | ||
}, | ||
snykToHtml: { | ||
filename: "snyk-to-html-macos", | ||
downloadUrl: | ||
"https://static.snyk.io/snyk-to-html/latest/snyk-to-html-macos" | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Platform } from "azure-pipelines-task-lib/task"; | ||
import * as fs from "fs"; | ||
import * as path from "path"; | ||
import * as https from "https"; | ||
|
||
export type Executable = { | ||
filename: string; | ||
downloadUrl: string; | ||
}; | ||
|
||
export type SnykDownloads = { | ||
snyk: Executable; | ||
snykToHtml: Executable; | ||
}; | ||
|
||
export function getSnykDownloadInfo(platform: Platform): SnykDownloads { | ||
const baseUrl = "https://static.snyk.io"; | ||
|
||
const filenameSuffixes: Record<Platform, string> = { | ||
[Platform.Linux]: "linux", | ||
[Platform.Windows]: "win.exe", | ||
[Platform.MacOS]: "macos" | ||
}; | ||
|
||
return { | ||
snyk: { | ||
filename: `snyk-${filenameSuffixes[platform]}`, | ||
downloadUrl: `${baseUrl}/cli/latest/snyk-${filenameSuffixes[platform]}` | ||
}, | ||
snykToHtml: { | ||
filename: `snyk-to-html-${filenameSuffixes[platform]}`, | ||
downloadUrl: `${baseUrl}/snyk-to-html/latest/snyk-to-html-${filenameSuffixes[platform]}` | ||
} | ||
}; | ||
} | ||
|
||
export async function downloadExecutable( | ||
targetDirectory: string, | ||
executable: Executable | ||
) { | ||
const fileWriter = fs.createWriteStream( | ||
path.join(targetDirectory, executable.filename), | ||
{ | ||
mode: 0o766 | ||
} | ||
); | ||
return new Promise<void>((resolve, reject) => { | ||
https.get(executable.downloadUrl, response => { | ||
response.on("end", () => resolve()); | ||
response.on("error", err => reject(err)); | ||
response.pipe(fileWriter); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters