Skip to content

Commit

Permalink
refactor: remove follow-redirects dependency (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
wojpawlik authored Oct 12, 2023
1 parent d664178 commit f259c04
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 37 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ jobs:
- os: ubuntu-latest
- os: windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- run: npm install
- uses: actions/checkout@v4
- run: npm install --ignore-scripts
- run: node install.js
- run: ./bin/deno --version
60 changes: 28 additions & 32 deletions install.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const fs = require("fs");
const os = require("os");
const path = require("path");
const { https } = require("follow-redirects");
const pkg = require("./package");
const pkg = require("./package.json");
const stream = require("stream/promises");
const AdmZip = require("adm-zip");

function filename() {
Expand Down Expand Up @@ -37,37 +37,33 @@ function executableFilename() {
}
}

function main() {
return new Promise(resolve => {
async function main() {
const dlUrl =
`https://github.com/denoland/deno/releases/download/v${pkg.version}/${filename()}`;
const binPath = path.join(__dirname, "bin");
const zipPath = path.join(
fs.mkdtempSync(path.join(os.tmpdir(), "deno-bin")),
"deno.zip",
);
const denoBin = path.join(binPath, executableFilename());

const dlUrl =
`https://github.com/denoland/deno/releases/download/v${pkg.version}/${filename()}`;
//console.log(dlUrl);
const binPath = path.join(__dirname, "bin");
const zipPath = path.join(
fs.mkdtempSync(path.join(os.tmpdir(), "deno-bin")),
"deno.zip",
);
const denoBin = path.join(binPath, executableFilename());

if (fs.existsSync(denoBin)) resolve(denoBin);

// 1. Download Deno binary zip from github release page
https.get(dlUrl, (res) => {
// 2. Saves it in temp dir
res.pipe(fs.createWriteStream(zipPath)).on("close", () => {
const filename = executableFilename();
// 3. Extracts `deno` entry to bin path.
new AdmZip(zipPath).extractEntryTo(filename, binPath, true, true);
// 4. Changes the file permission
if (process.platform !== "win32")
fs.chmodSync(path.join(binPath, filename), 0o755);
// 5. Removes the zip file
fs.unlinkSync(zipPath);
resolve(denoBin);
});
});
});
try {
await fs.promises.access(denoBin);
} catch {
// 1. Downloads Deno binary zip from github release page
// TODO: handle errors
const response = await fetch(dlUrl);
// 2. Saves it in temp dir
// TODO: avoid
await stream.pipeline(response.body, fs.createWriteStream(zipPath));
// 3. Extracts `deno` entry to bin path.
new AdmZip(zipPath).extractEntryTo(executableFilename(), binPath, true, true);
// 4. Changes the file permission
await fs.promises.chmod(denoBin, 0o755);
// 5. Removes the zip file
fs.unlinkSync(zipPath);
}
return denoBin;
}

module.exports = main();
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
},
"homepage": "https://github.com/kt3k/deno-bin#readme",
"dependencies": {
"adm-zip": "^0.5.4",
"follow-redirects": "^1.10.0"
"adm-zip": "^0.5.4"
}
}

0 comments on commit f259c04

Please sign in to comment.