-
Notifications
You must be signed in to change notification settings - Fork 3
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 #5 from daddykotex/dfrancoeur/download-coursier
Setup coursier w/o embedding in the extension
- Loading branch information
Showing
16 changed files
with
2,605 additions
and
151 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 @@ | ||
yarn.lock linguist-generated=true |
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 |
---|---|---|
|
@@ -14,13 +14,7 @@ jobs: | |
- uses: actions/checkout@v2 | ||
- uses: borales/[email protected] | ||
with: | ||
cmd: install | ||
- uses: borales/[email protected] | ||
with: | ||
cmd: format-check | ||
- uses: borales/[email protected] | ||
with: | ||
cmd: build | ||
cmd: ci | ||
|
||
deploy: | ||
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v')) | ||
|
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,14 @@ | ||
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ | ||
module.exports = { | ||
preset: "ts-jest", | ||
testEnvironment: "node", | ||
preset: "ts-jest/presets/default-esm", // or other ESM presets | ||
globals: { | ||
"ts-jest": { | ||
useESM: true, | ||
}, | ||
}, | ||
moduleNameMapper: { | ||
"^(\\.{1,2}/.*)\\.js$": "$1", | ||
}, | ||
}; |
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,12 @@ | ||
import { downloadCoursierIfRequired } from "./download-coursier"; | ||
import { findCoursierOnPath } from "./path-check"; | ||
|
||
export function getCoursierExecutable(extensionPath: string): Promise<string> { | ||
return findCoursierOnPath(extensionPath).then((paths) => { | ||
if (paths.length > 0) { | ||
return paths[0]; | ||
} else { | ||
return downloadCoursierIfRequired(extensionPath, "v2.0.6"); | ||
} | ||
}); | ||
} |
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,99 @@ | ||
import * as path from "path"; | ||
import { https } from "follow-redirects"; | ||
import { IncomingMessage } from "http"; | ||
import * as fs from "fs"; | ||
import { access, mkdir } from "fs/promises"; | ||
|
||
export function downloadCoursierIfRequired( | ||
extensionPath: string, | ||
versionPath: string | ||
): Promise<string> { | ||
function binPath(filename: string) { | ||
return path.join(extensionPath, filename); | ||
} | ||
|
||
function createDir() { | ||
return mkdir(extensionPath).catch((err: { code?: string }) => { | ||
return err && err.code === "EEXIST" | ||
? Promise.resolve() | ||
: Promise.reject(err); | ||
}); | ||
} | ||
|
||
const urls = { | ||
darwin: `https://github.com/coursier/coursier/releases/download/${versionPath}/cs-x86_64-apple-darwin`, | ||
linux: `https://github.com/coursier/coursier/releases/download/${versionPath}/cs-x86_64-pc-linux`, | ||
win32: `https://github.com/coursier/coursier/releases/download/${versionPath}/cs-x86_64-pc-win32.exe`, | ||
}; | ||
const targets = { | ||
darwin: binPath("coursier"), | ||
linux: binPath("coursier"), | ||
win32: binPath("coursier.exe"), | ||
}; | ||
|
||
const targetFile = targets[process.platform]; | ||
return validBinFileExists(targetFile).then((valid) => { | ||
return valid | ||
? targetFile | ||
: createDir().then(() => | ||
downloadFile(urls[process.platform], targetFile) | ||
); | ||
}); | ||
} | ||
|
||
function validBinFileExists(file: string): Promise<boolean> { | ||
return access(file, fs.constants.X_OK) | ||
.then(() => true) | ||
.catch(() => false); | ||
} | ||
|
||
function downloadFile(url: string, targetFile: string): Promise<string> { | ||
function promiseGet(url: string): Promise<IncomingMessage> { | ||
return new Promise((resolve, reject) => { | ||
https.get(url, (response) => { | ||
if (response.statusCode === 200) { | ||
resolve(response); | ||
} else { | ||
reject( | ||
new Error( | ||
`Server responded with ${response.statusCode}: ${response.statusMessage}` | ||
) | ||
); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
function writeToDisk(response: IncomingMessage): Promise<string> { | ||
return new Promise((resolve, reject) => { | ||
const file = fs.createWriteStream(targetFile, { | ||
flags: "wx", | ||
mode: 0o755, | ||
}); | ||
response.pipe(file); | ||
|
||
file.on("finish", () => { | ||
console.log(`Finished downloaded file at ${targetFile}`); | ||
resolve(targetFile); | ||
}); | ||
|
||
file.on("error", (err: { code: string | undefined }) => { | ||
if (file) { | ||
file.close(); | ||
fs.unlink(targetFile, () => {}); // Delete temp file | ||
} | ||
|
||
if (err.code === "EEXIST") { | ||
console.log(`File already exists at ${targetFile}`); | ||
resolve(targetFile); | ||
} else { | ||
console.error(`File error while downloading file at ${targetFile}`); | ||
console.error(err); | ||
reject(err); | ||
} | ||
}); | ||
}); | ||
} | ||
// adapted from https://stackoverflow.com/a/45007624 | ||
return promiseGet(url).then((resp) => writeToDisk(resp)); | ||
} |
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,45 @@ | ||
import { spawn } from "child_process"; | ||
|
||
/** | ||
* This type is used to bypass the `defaultImpl` when running the tests. | ||
*/ | ||
export type ExecForCode = { | ||
run: (execName: string, args: Array<string>, cwd: string) => Promise<number>; | ||
}; | ||
|
||
const defaultImpl: ExecForCode = { | ||
run: (execName: string, args: Array<string>, cwd: string) => { | ||
console.log("real"); | ||
return new Promise((resolve, reject) => { | ||
const options = { cwd }; | ||
const resolveProcess = spawn(execName, args, options); | ||
resolveProcess.on("exit", (exitCode) => { | ||
resolve(exitCode); | ||
}); | ||
resolveProcess.on("error", (err) => { | ||
reject(err); | ||
}); | ||
}); | ||
}, | ||
}; | ||
|
||
export function findCoursierOnPath( | ||
cwd: string, | ||
execForCode: ExecForCode = defaultImpl | ||
): Promise<Array<string>> { | ||
function availableOnPath(execName: string): Promise<boolean> { | ||
return execForCode | ||
.run(execName, ["--help"], cwd) | ||
.then((ec) => ec === 0) | ||
.catch(() => false); | ||
} | ||
|
||
const possibleCoursierNames = ["cs", "coursier"]; | ||
return possibleCoursierNames.reduce((accP, current) => { | ||
return accP.then((acc) => { | ||
return availableOnPath(current).then((succeeeded) => { | ||
return succeeeded ? [...acc, current] : acc; | ||
}); | ||
}); | ||
}, Promise.resolve([])); | ||
} |
Oops, something went wrong.