A screenshot of the script along with the colorized output in the terminal.

Deno 2.0 and the npm: specifier

The Deno 2.0 announcement mentions many cool new features. I’m still working through things in the blog post, but something as simple as using the npm: specifier will be great for writing one-off scripts using Typescript and any number of great packages in the NPM ecosystem.

For example, here’s a quick script that uses chalk to colorize the args to a index.ts script. With Deno installed, it’s as simple as running with:

deno run --allow-env --allow-read index.ts The quick brown fox jumps over the lazy dog

import { parseArgs } from "jsr:@std/cli/parse-args";
import chalk from "npm:chalk@5.3.0";

const message = parseArgs(Deno.args)._;

const colorMsg = (words: string[]): void => {
  let i = 0;
  // chalk, why you no support the Roy G. Biv!?
  const colors = [
    "red",
    // "orange",
    "yellow",
    "green",
    "blue",
    // "indigo",
    // "violet",
  ];
  const coloredWords = words.map((word) => {
    const color = chalk[colors[i]];
    i = (i + 1) % colors.length;
    return color(word);
  });
  console.log(coloredWords.join(" "));
};
colorMsg(message);


Posted

in

by