Giter Club home page Giter Club logo

yurnalist's Introduction

Yurnalist

An elegant console reporter, borrowed from Yarn.

Introduction

Pretty console output makes developers happy and Yarn is doing a nice job. Yurnalist takes the internal console reporter code from Yarn and makes it available for use in other Node.js applications.

The current version is based on code from Yarn v1.13.0.

Yurnalist can be used to report many different things besides simple messages.

Features

  • log, info, warn, succes, error & command messages
  • progress bars
  • activity spinners
  • process steps
  • object inspection
  • lists
  • emojis
  • trees
  • tables
  • user question
  • user select
  • program header & footer

Install

yarn add yurnalist

Or if your prefer NPM

npm install yurnalist

How to use

Here is an example showing a combination of different reporter API functions.

import report from 'yurnalist'

/* A function to fake some async task */
function waitNumberOfSecs(secs) {
  return new Promise((resolve) => setTimeout(resolve, secs * 1000));
}

async function fetchSomething() {
  report.info('Please wait while I fetch something for you.');
  report.warn('It might take a little while though.');

  const spinner = report.activity();
  spinner.tick('I am on it!');

  try {
    await waitNumberOfSecs(1);
    spinner.tick('Still busy...');
    await waitNumberOfSecs(1);
    spinner.tick('Almost there...');
    await waitNumberOfSecs(1);
    report.success('Done!');
  } catch (err) {
    report.error(err);
  }

  spinner.end();
}

fetchSomething();

Requirements

Node >= 4

Examples

Examples showing different API functions are found in /examples. You can run them directly with node >= 7.6 (because of async/await syntax). For older versions you could use the --harmony flag, or otherwise Babel.

To run the activity example:

node examples/activity.js

Configuration

A normal import gives you a reporter instance configured with defaults for easy use. If you want something else you can call createReporter(options) to give you an instance with different options.

Options

These are the options of the reporter as defined by Flow:

type ReporterOptions = {
  verbose?: boolean,
  stdout?: Stdout,
  stderr?: Stdout,
  stdin?: Stdin,
  emoji?: boolean,
  noProgress?: boolean,
  silent?: boolean,
  nonInteractive?: boolean,
  peekMemoryCounter?: boolean
};

The defaults used are:

const defaults = {
  verbose: false,
  stdout: process.stdout,
  stderr: process.stderr,
  stdin: process.stdin,
  emoji: true,
  noProgress: false,
  silent: false,
  nonInteractive: false,
  peekMemoryCounter: false
}

The peekMemoryCounter is disabled by default. If you enable it, you'll have to call reporter.close() to stop its running timer. Otherwise your program will not exit. The memory counter can be used to display in the footer data.

Silent Mode and CI

Silent mode can be set via the options passed to createReporter. It disables output for various functions like info, list, activity and progress. The output from warning and error messages is not silenced.

Silent mode can also be enabled with the YURNALIST_SILENT environment variable.

In CI environments the output from activity and progress is disabled.

API

The API still needs some documentation, but most methods are straightforward. In the meantime you can also look at the examples and possibly even the tests.

The following functions are available:

table

step

inspect( thing: mixed )

Pretty-prints the thing.

list(title: string, items: Array, hints?: Object)

Generates a list of the provided items. Turns into a definition list if hints are provided.

Example of a simple list:

report.list('My grocery list', ['bananas', 'tulips', 'eggs', 'bamischijf']);

Outputs:

list My grocery list
   - bananas
   - tulips
   - eggs
   - bamischijf

Example with hints:

const items = ['bananas', 'tulips', 'eggs', 'bamischijf'];

const hints = {
  bananas: 'for baking',
  tulips: 'because it makes you happy',
  eggs: 'not the cheap ones though',
  bamischijf: 'if they have it',
};

report.list('My grocery list', items, hints);

Outputs:

list My grocery list
    - bananas
      for baking
   - tulips
      because it makes you happy
   - eggs
      not the cheap ones though
   - bamischijf
      if they have it

header

footer

log

success

error

info

command

warn

question

tree

activitySet

activity

select

progress

close

createReporter

Language

Yarn uses a language file for certain messages. For example if you try to skip a required question, or when you pick an invalid item from a select. This language file is not yet exposed in the Yurnalist API. The only supported language is English, as it is in Yarn at the moment.

I plan to make this configurable so that you can define your own messages in your own language .

Emojis

You can use Emojis in your output. Yurnalist should disable them if they are not allowed in the application environment.

Check:

Credits

Of course โค๏ธ and credits to all the contributers of Yarn. The ease with which I was able to extract this module from their codebase is proving some awesome engineering skills.

yurnalist's People

Contributors

0x80 avatar bluelovers avatar dependabot[bot] avatar yanick avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

yurnalist's Issues

Research the use and configuration of colors

How to color for example a complete list output in one color. Yarn is doing it, but we probably need to expose it though API functions.

Also it might be useful to expose API to configure the color palette, but I'm not sure it's worth it yet.

Missing `strip-ansi` dependency

Hi, I tried installing this package with pnpm, but running the activity example fails because of a missing strip-ansi dependency.

Steps to reproduce:

$ git clone https://github.com/0x80/yurnalist.git
$ pnpm i
$ npm run build
$ node examples/activity.js
module.js:550
throw err;
^
Error: Cannot find module 'strip-ansi'
at Function.Module._resolveFilename (module.js:548:15)
at Function.Module._load (module.js:475:25)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/home/cdfa/Projects/yurnalist/dist/reporters/console/console-reporter.js:63:19)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)

Strip unused depencencies

There are probably still a few dependencies listed that are not actually used in the code. Hunt them down.

Cannot import in Typescript

I was just trying out yurnalist and installed it via npm i --save yurnalist (v2.1.0). I couldn't get my project to build because tsc was complaining that no type definitions were found for yurnalist. I checked the node_modules folder and the yurnalist folder is present. For now I declared yurnalist as a module in a .d.ts file, however it would be nice to know how I can install the type definitions of yurnalist

Upgrade to babel 7

This project is using the old babel-runtime package. I suggest to use the new babel 7 @babel/runtime package.

The main concern behind this is to remove the corejs2 dependency present in babel-runtime, since corejs2 is now deprecated.

What do you think of the name Yurnalist?

The code for this module is taken from ConsoleReporter in Yarn. Therefor I initially wanted to name the module console-reporter. It is pretty clear from that name what it does, but it doesn't sound very exciting.

A synonym for reporter is journalist, and I like that name, but of course it is already taken. Since we're literally borrowing the vast majority of code from Yarn, I think it is nice to make a reference to it and that's how I came up with the name yurnalist.

The advantage of an ambiguous name like yurnalist is that the scope is not immediately fixed. For example if we decide to expose other reporters, like JSON, in addition to the console one, we easily could under the same name.

A lot of times I find "cleaver" names to be annoying, so I'm wondering what you think. Do you prefer something plain like console-reporter?

Respond with a ๐Ÿ‘ if you like the name yurnalist or a ๐Ÿ‘Ž if you think it is absolute rubbish.

If you have a better idea please leave your suggestion below ๐Ÿ™‚.

Add more examples

We need examples for all the API methods. Some might be dropped since I have no clue yet how they work. The ones that are still missing are:

  • table
  • header
  • footer
  • tree
  • activitySet
  • createReporter

Make it possible to configure the language

Some messages use the internal Yarn language file. This means you have no influence over what is printed when for example you gave a wrong selection as input to reporter.select. It would be nice to make it configurable.

Progress spinner should be disabled in CI mode

Seeing rather ugly output when I build my gatsby app in Circle CI.

image

I see that the library already supports CI for other pieces, so I was hoping it would be easy to disable the spinner as well in CI, and instead just output a new line of text for each status update.

cc/ @KyleAMathews

Documentation error

Quick note: import report from 'yurnalist' does not work (undefined), but import * as report from 'yurnalist' does.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.