I find this function really interesting because in the wild, OSS projects use Picocolors or chalk to apply colors to the console output in the CLI.
There’s codes and noColor is set to true if there is an env, NO_COLOR, or when the platform is ‘win32’ and TERM is set (not sure what this TERM is).
The trick is in the below code:
for (const key in codes) { fns[key] = msg => noColor ? msg : `\u001b[${codes[key]}m${msg}\u001b[39m` }
Here a \u001b[${codes[key]}m${msg}\u001b[39m is a template string commonly seen in JavaScript or TypeScript used to format console output with colors using ANSI escape codes. Interesting.
Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.