Skip to content

Commit

Permalink
feat: npm i
Browse files Browse the repository at this point in the history
  • Loading branch information
Anush008 committed Aug 20, 2023
1 parent 421a429 commit 97fb5c8
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
10 changes: 10 additions & 0 deletions bin/runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env node

const { spawnSync } = require("child_process");
const path = require("path");
const { name } = require("../package.json");
const binPath = path.join(__dirname, name);

const command_args = process.argv.slice(2);
const child = spawnSync(binPath, command_args, { stdio: "inherit" });
process.exit(child.status);
79 changes: 79 additions & 0 deletions install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const fs = require("fs");
const https = require("https");
const path = require("path");

const packageJson = require("./package.json");
const { version, name, repository } = packageJson;

async function install() {
const downloadURL = getDownloadURL();

try {
const binDir = path.join(__dirname, "bin");
const outputPath = path.join(binDir, name);

await downloadBinary(downloadURL, outputPath);

fs.chmodSync(outputPath, 0o755);
} catch (error) {
console.error("Installation failed:", error.message);
}
}

function getDownloadURL() {
let goOS, arch;

switch (process.platform) {
case "win32":
case "cygwin":
goOS = "windows";
break;
case "darwin":
goOS = "darwin";
break;
case "linux":
goOS = "linux";
break;
default:
throw new Error(`Unsupported OS: ${process.platform}`);
}

switch (process.arch) {
case "x64":
arch = "amd64";
break;
case "arm64":
arch = "arm64";
break;
default:
throw new Error(`Unsupported architecture: ${process.arch}`);
}

return `${repository}/releases/download/v${version}/${name}-${goOS}-${arch}`;
}

const downloadBinary = (url, outputPath) => {
return new Promise((resolve, reject) => {
https
.get(url, (response) => {
if (response.statusCode === 302) {
resolve(downloadBinary(response.headers.location, outputPath));
} else if (response.statusCode === 200) {
const file = fs.createWriteStream(outputPath);
response.pipe(file);
file.on("finish", () => {
file.close(resolve);
});
} else {
reject(
new Error(
`Failed to download ${name}. Status code: ${response.statusCode}`
)
);
}
})
.on("error", reject);
});
};

install();
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "pizzaroni-cli",
"version": "1.0.0",
"description": "Something something CLI",
"repository": "https://github.com/Anush008/pizzaroni-cli",
"keywords": [],
"author": "",
"license": "ISC",
"bin": "./bin/runner.js",
"scripts": {
"install": "node install.js"
}
}

0 comments on commit 97fb5c8

Please sign in to comment.