Bun.js as CLI Tool
×

Bun.js as CLI Tool

138

๐Ÿ› ๏ธ 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 like pkg 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 bunx and bun 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? Use bunx 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 via Bun.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 like chalk (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? Use fs 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!


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat