-
-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: npm install -g ttyper #97
Open
Anush008
wants to merge
11
commits into
max-niederman:main
Choose a base branch
from
Anush008:npm-install
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7342fb1
feat: npm install
Anush008 624dd68
chore: pin to 0.0.0
Anush008 1282149
chore: typo fix
Anush008 65aed40
Merge branch 'main' into npm-install
Anush008 78b1764
Merge branch 'main' into npm-install
Anush008 30002af
Merge branch 'max-niederman:main' into npm-install
Anush008 d33894f
chore: resolve download URL
Anush008 9898368
Merge branch 'main' into npm-install
Anush008 5abc0f7
Merge branch 'main' into npm-install
Anush008 ccbf0ec
Merge branch 'main' into npm-install
Anush008 12a8b91
Merge branch 'main' into npm-install
Anush008 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,129 @@ | ||
import fs from "fs"; | ||
import https from "https"; | ||
import path from "path"; | ||
import { fileURLToPath } from "url"; | ||
|
||
// ESM doesn't support __dirname and __filename by default | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(__filename); | ||
|
||
const packageJson = JSON.parse(fs.readFileSync("./package.json", "utf8")); | ||
// The version number is expected to be in parity with the ttyper release version | ||
const { version, releasesUrl, name, binDir: directory } = packageJson; | ||
|
||
// All the binary files will be stored in the /bin directory | ||
const binDir = path.join(__dirname, directory); | ||
console.log(`Installing ${name} v${version}`); | ||
|
||
try { | ||
void install(); | ||
} catch (error) { | ||
console.error("Installation failed:", error.message); | ||
} | ||
|
||
async function install() { | ||
if (fs.existsSync(binDir)) { | ||
fs.rmSync(binDir, { recursive: true }); | ||
} | ||
fs.mkdirSync(binDir, { | ||
mode: 0o777, | ||
}); | ||
await getBinary(); | ||
|
||
// Remove the node_modules as we'll only need the binary | ||
fs.rmSync(path.join(__dirname, "node_modules"), { recursive: true }); | ||
} | ||
|
||
function getBinaryDownloadURL() { | ||
let os, arch; | ||
|
||
// Possible values are : 'aix' | 'android' | 'darwin' | 'freebsd' | 'haiku' | 'linux' | 'openbsd' | 'sunos' | 'win32' | 'cygwin' | 'netbsd' | ||
switch (process.platform) { | ||
case "win32": | ||
case "cygwin": | ||
os = "pc-windows-msvc"; | ||
break; | ||
case "darwin": | ||
os = "apple-darwin"; | ||
break; | ||
case "linux": | ||
os = "unknown-linux-gnu"; | ||
break; | ||
default: | ||
throw new Error(`Unsupported OS: ${process.platform}`); | ||
} | ||
|
||
// Possible values are: 'arm' | 'arm64' | 'ia32' | 'mips' | 'mipsel' | 'ppc' | 'ppc64' | 's390' | 's390x' | 'x64' | ||
switch (process.arch) { | ||
case "x64": | ||
arch = "x86_64"; | ||
break; | ||
case "arm64": | ||
arch = "aarch64"; | ||
break; | ||
case "ia32": | ||
arch = "i686"; | ||
break; | ||
default: | ||
throw new Error(`Unsupported architecture: ${process.arch}`); | ||
} | ||
|
||
const extension = os === "pc-windows-msvc" ? "zip" : "tar.gz"; | ||
|
||
return `${releasesUrl}/download/v${version}/${name}-${arch}-${os}.${extension}`; | ||
} | ||
|
||
function downloadPackage(url, outputPath) { | ||
// We use https.get instead of fetch to get a readable stream from the response without additional dependencies | ||
return new Promise((resolve, reject) => { | ||
https | ||
.get(url, (response) => { | ||
// If the response is a redirect, we download the package from the new location | ||
if (response.statusCode === 302) { | ||
resolve(downloadPackage(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); | ||
}); | ||
} | ||
|
||
async function extractPackage(inputPath, outputPath) { | ||
if (path.extname(inputPath) === ".gz") { | ||
const tar = await import("tar"); | ||
await tar.x({ | ||
file: inputPath, | ||
cwd: outputPath, | ||
}); | ||
} else if (path.extname(inputPath) === ".zip") { | ||
const AdmZip = (await import("adm-zip")).default; | ||
const zip = new AdmZip(inputPath); | ||
zip.extractAllTo(outputPath, true, true); | ||
} | ||
} | ||
|
||
async function getBinary() { | ||
const downloadURL = getBinaryDownloadURL(); | ||
console.log(`Downloading ${name} from ${downloadURL}`); | ||
|
||
const pkgName = ["win32", "cygwin"].includes(process.platform) | ||
? `package.zip` | ||
: `package.tar.gz`; | ||
const packagePath = path.join(binDir, pkgName); | ||
|
||
await downloadPackage(downloadURL, packagePath); | ||
await extractPackage(packagePath, binDir); | ||
|
||
fs.rmSync(packagePath); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "ttyper", | ||
"version": "0.0.0", | ||
"description": "ttyper is a terminal-based typing test built with Rust and tui-rs.", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/Anush008/ttyper.git" | ||
}, | ||
"releasesUrl": "https://github.com/Anush008/ttyper/releases", | ||
"license": "MIT", | ||
"bin": { | ||
"ttyper": "runner.js" | ||
}, | ||
"type": "module", | ||
"scripts": { | ||
"install": "node install.js" | ||
}, | ||
"binDir": "bin", | ||
"dependencies": { | ||
"adm-zip": "^0.5.10", | ||
"tar": "^6.1.15" | ||
} | ||
} |
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,23 @@ | ||
#!/usr/bin/env node | ||
|
||
import { spawn } from "child_process"; | ||
import fs from "fs"; | ||
import path from "path"; | ||
import { fileURLToPath } from "url"; | ||
|
||
// ESM doesn't support __dirname and __filename by default | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(__filename); | ||
|
||
const packageJsonPath = path.join(__dirname, "package.json"); | ||
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")); | ||
const { name, binDir } = packageJson; | ||
|
||
const commandArgs = process.argv.slice(2); | ||
const exeName = ["win32", "cygwin"].includes(process.platform) | ||
? `${name}.exe` | ||
: name; | ||
const cwd = path.join(__dirname, binDir); | ||
const binPath = path.join(cwd, exeName); | ||
|
||
const child = spawn(binPath, commandArgs, { stdio: "inherit", cwd }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: Update these URLs to
https://github.com/max-niederman/ttyper