Skip to content

Commit

Permalink
fix(refactor): top-level-await and less steps
Browse files Browse the repository at this point in the history
- use top-level await in try/catch blocks
- decompress file without saving it first directly from buffer

- enable all xo rules
- update bun to 1.0.32
- clarify source in bin/cli.js
- change package.json to newer version
- update lockfile
  • Loading branch information
z0rrn committed Mar 17, 2024
1 parent 4f9a749 commit 67d0edb
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
FROM mcr.microsoft.com/devcontainers/base:ubuntu-22.04

# install Bun globally
RUN curl -fsSL https://bun.sh/install | BUN_INSTALL=/usr/local bash -s "bun-v1.0.30"
RUN curl -fsSL https://bun.sh/install | BUN_INSTALL=/usr/local bash -s "bun-v1.0.32"
5 changes: 1 addition & 4 deletions .xo-config.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
{
"space": true,
"extends": ["prettier"],
"rules": {
"max-params": "off",
"unicorn/prefer-top-level-await": "off"
}
"rules": {}
}
2 changes: 1 addition & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import path from "node:path";
import process from "node:process";
import url from "node:url";

// Declare file to execute (again, copied from ../install.js)
// Declare file to execute (copied from ../install.js)
const targetDirectory = path.join(
url.fileURLToPath(new URL("../vendor/", import.meta.url)),
);
Expand Down
Binary file modified bun.lockb
Binary file not shown.
84 changes: 24 additions & 60 deletions install.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,11 @@ import { packageConfig } from "package-config";
// Get namespaced config for hugo-install
const config = await packageConfig("hugo-install");

// Set path to download to
// Set path to download to (./node_modules/hugo-install/vendor/)
const targetDirectory = path.join(
url.fileURLToPath(new URL("vendor/", import.meta.url)),
);

// Append compressed file
const compressedFile =
process.platform === "win32"
? `${targetDirectory}hugo.zip`
: `${targetDirectory}hugo.tar.gz`;
// Append extracted file
const extractedFile =
process.platform === "win32"
? `${targetDirectory}hugo.exe`
: `${targetDirectory}hugo`;

// Specify filemap of downloadable files
const downloadFileMap = {
darwin: {
Expand Down Expand Up @@ -69,55 +58,30 @@ const downloadUrl =
].find(Boolean) ||
`https://github.com/gohugoio/hugo/releases/download/v${config.hugoVersion}/${downloadFile}`;

// Function to download file
// variable names all lowercase
const download = async (
downloadurl,
targetdirectory,
compressedfile,
extractedfile,
callback,
) => {
// Create folder, no error if folder exists
try {
await fs.mkdir(targetdirectory);
console.log(`Folder ${targetdirectory} created!`);
} catch {
console.log(`Folder ${targetdirectory} already created!`);
}
// Create folder, no error if folder exists
try {
await fs.mkdir(targetDirectory);
console.log(`Folder ${targetDirectory} created`);
} catch {
console.log(`Folder ${targetDirectory} already created`);
}

// Download and decompress file (file to download, where to download to)
try {
// Fetch file and throw error if response isn't ok
const response = await fetch(downloadurl);

if (!response.ok) {
throw new Error(`Unexpected response ${response.statusText}!`);
const fetchedFile = await fetch(downloadUrl);
if (!fetchedFile.ok) {
throw new Error(`Unexpected response ${fetchedFile.statusText}`);
}

// Write file to buffer -> file
const buffer = Buffer.from(await response.arrayBuffer());
await fs.writeFile(compressedfile, buffer);
console.log(`Compressed file downloaded to ${compressedfile}`);

// Call extract function so that it's executed after this
callback(compressedfile, targetdirectory, extractedfile);
};

// Extract the tar.gz/zip to vendor folder
const extract = async (compressedfile, targetdirectory, extractedfile) => {
try {
await decompress(compressedfile, targetdirectory);
console.log(`Hugo successfully installed to: ${extractedfile}!`);
} catch (error) {
// Give output on error
console.log(`Failed to install hugo: ${error}`);
}
};

// Call download and extract function
download(
downloadUrl,
targetDirectory,
compressedFile,
extractedFile,
extract,
).catch(console.error);
// Decompress directly from buffer
await decompress(
Buffer.from(await fetchedFile.arrayBuffer()),
targetDirectory,
);
console.log(
`Hugo installed to ${targetDirectory} (as hugo.exe (Windows) or hugo (everything else))`,
);
} catch (error) {
throw new Error(`Failed to download and decompress Hugo: ${error}`);
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
"bin": {
"hugo": "bin/cli.js"
},
"repository": "https://github.com/z0rrn/hugo-install.git",
"repository": {
"type": "git",
"url": "git+https://github.com/z0rrn/hugo-install.git"
},
"scripts": {
"postinstall": "node ./install.js",
"lint:fix": "xo --fix && prettier --write . ",
Expand Down

0 comments on commit 67d0edb

Please sign in to comment.