Bun.js as CLI Tool
0 1981
ðŸ› ï¸ Bun.js as CLI Tool — Supercharge Your Scripts!
Bun.js isn't just a blazing fast JavaScript runtime — it's also an excellent tool for creating and running your own command-line interfaces (CLI). Whether you're automating workflows, generating boilerplates, or building productivity tools, Bun makes CLI development effortless, efficient, and fun! 🚀⚡ Why use Bun.js for CLI Development?
Traditionally, CLI tools were built with Node.js and packaged with tools likepkg or nexe.
But with Bun.js, you get:
- Native speed thanks to Bun’s Zig-powered runtime
- Instant execution of JavaScript and TypeScript files
- Built-in utilities like
bunxandbun run - Easy permissions using shebangs for script execution
📠Setting Up a Bun CLI Project
Start by initializing a new Bun project:
bun init
Then create a new CLI script file, for example: cli.ts
🚀 Creating a Simple CLI Script
Here's a basic CLI script that echoes user input:
// cli.ts
#!/usr/bin/env bun
const name = Bun.argv[2] || "World";
console.log(`👋 Hello, ${name}!`);
Note: Don’t forget the shebang #!/usr/bin/env bun at the top — it tells the system to run the file using Bun!
🔒 Making the Script Executable
To make your file executable on Unix-based systems (Linux/macOS):
chmod +x cli.ts
Now you can run your CLI like this:
./cli.ts BunUser
📦 Distributing Your CLI Tool
Want to share your CLI with the world? Usebunx for a zero-install experience:
bunx githubuser/repo
Or create a custom command in your package.json:
// package.json
{
"scripts": {
"greet": "./cli.ts"
}
}
Then simply run:
bun run greet BunUser
📚 Handling Arguments with Deno-style Simplicity
Bun provides access to command-line arguments viaBun.argv.
You can easily parse flags or values:
// cli.ts
#!/usr/bin/env bun
const args = Bun.argv.slice(2);
if (args.includes("--help")) {
console.log(`Usage: ./cli.ts [name]`);
} else {
const name = args[0] || "Bun";
console.log(`👋 Hello, ${name}!`);
}
🔧 Add Styling & UX to your CLI
Use colors, emojis, and prompts to enhance CLI UX. You can install and use packages likechalk (with ESM support):
bun add chalk
// cli.ts
#!/usr/bin/env bun
import chalk from "chalk";
console.log(chalk.green("âœ”ï¸ Success! Your CLI is working."));
💡 Pro Tip: Use Bun with Templates
Want to scaffold projects with your CLI? Usefs module to copy files or write templates:
// cli.ts
#!/usr/bin/env bun
import { writeFileSync } from "fs";
writeFileSync("hello.txt", "Generated by Bun CLI! 💥");
console.log("📄 File created: hello.txt");
✅ Conclusion
Bun.js makes building CLI tools a breeze — fast, modern, and with zero setup. Whether you're building developer utilities or productivity hacks, Bun has all the power you need packed in a lightweight runtime. 🌟 Ready to script your way to greatness? Try Bun.js as a CLI tool today! 💻✨If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!
Share:



Comments
Waiting for your comments