forked from open-sauced/pizza-cli
-
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
Showing
3 changed files
with
102 additions
and
0 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,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); |
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,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(); |
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,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" | ||
} | ||
} |