-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
519f313
commit 9b20aaa
Showing
5 changed files
with
103 additions
and
17 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
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,62 @@ | ||
import { greaterThan, parse } from "jsr:@std/semver"; | ||
|
||
import { program } from "../main.ts"; | ||
import { drenvBinPath, version } from "../constants.ts"; | ||
|
||
type Asset = { | ||
url: string; | ||
id: number; | ||
node_id: string; | ||
name: string; | ||
label: string | null; | ||
uploader: any; | ||
content_type: string; | ||
state: string; | ||
size: number; | ||
download_count: number; | ||
created_at: string; | ||
updated_at: string; | ||
browser_download_url: string; | ||
}; | ||
|
||
export default async function upgrade() { | ||
console.log(`Current version is v${program.version()}`); | ||
console.log("Checking for updates..."); | ||
|
||
const dataResponse = await fetch( | ||
"https://api.github.com/repos/nitemaeric/drenv/releases/latest", | ||
); | ||
const data = await dataResponse.json(); | ||
|
||
const localVersion = parse(version); | ||
const remoteVersion = parse(data.tag_name.slice(1)); | ||
|
||
if (greaterThan(remoteVersion, localVersion)) { | ||
console.log(`New version found: ${data.tag_name}`); | ||
|
||
const file = await Deno.open(drenvBinPath, { write: true, create: true }); | ||
|
||
const targetAsset = data.assets.find((asset: Asset) => | ||
asset.name.includes(Deno.build.target) | ||
); | ||
|
||
if (targetAsset) { | ||
console.log(`Downloading new version...`); | ||
|
||
const downloadResponse = await fetch(targetAsset.browser_download_url); | ||
|
||
if (!downloadResponse.body) { | ||
throw new Error("Could not download the new version"); | ||
} | ||
|
||
await downloadResponse.body.pipeTo(file.writable); | ||
await Deno.chmod(drenvBinPath, 0o755); | ||
|
||
console.log(`Upgraded to ${data.tag_name}`); | ||
} else { | ||
console.log("No asset found for your platform"); | ||
} | ||
} else { | ||
console.log("Already up-to-date"); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export const version = "0.3.0"; | ||
export const homePath = `${Deno.env.get("HOME")}/.drenv`; | ||
export const versionsPath = `${homePath}/versions`; | ||
export const binPath = `${homePath}/bin`; | ||
export const drenvBinPath = `${binPath}/drenv`; | ||
export const shell = Deno.env.get("SHELL"); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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