npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

listr2-scheduler

v1.4.0

Published

Declarative Listr2 task scheduling

Downloads

30

Readme

Opinionated library for declaring Listr2 task structure

Listr2 is a very flexible library for running tasks that depend on each other and it allows presenting task status in a beautiful way. However declaring how to run those tasks is rather tedious. Listr2 has 4 renderers but to achieve better clarity they need to be customized. Listr2 supports parallel tasks, but exception in any of those terminates whole process. It's not obvious how to pipe output from external processes to Listr2 so that verbose logs are investigatable. listr2-scheduler is an attempt to address these shortcomings.

Simple example

import { schedule } from "listr2-scheduler";

const driver = schedule((when, make) => {
  when(null, make("bundle")).call(createAppBundle);
  when("bundle").call(uploadAppToStaging);
  when("bundle").call(uploadBundleMapFileForCrashAnalytics);
});

driver.run({ printer: "summary" });

Hopefully it's obvious that createAppBundle function will be executed at the beginning and after it finishes uploadAppToStaging and uploadBundleMapFileForCrashAnalytics will be executed in parallel. Here's equivalent ordering in pure Listr2:

import { Listr } from "listr2";

new Listr(
  [
    {
      title: "Create app bundle",
      task: async (_, task) => {
        await createAppBundle();

        return task.newListr(
          [
            {
              title: "Upload app to staging",
              task: uploadAppToStaging,
            },
            {
              title: "Upload bundle map file for crash analytics",
              task: uploadBundleMapFileForCrashAnalytics,
            },
          ],
          { concurrent: true }
        );
      },
    },
  ],
  {
    renderer: "default",
    rendererOptions: {
      collapseSubtasks: false,
    },
  }
).run();

Besides "summary" printer (which uses default Listr2 renderer with some customizations) listr2-scheduler offers only "verbose" alternative which prints all events with timestamp prefix.

Configuration

When call is provided only executor function, it constructs task title from function name, but task title can be provided separately as shown below.

Conditional tasks can be declared by using ? or ! prefix in input keys. Tasks can have more than one input. Tasks can have maximum one output.

const ci = Boolean(process.env.CI);

schedule<"ci" | "silent" | "testResults" | "fmtResults">((when, make) => {
  when("?ci", make("testResults")).call("All tests", runAllTests);
  when("!ci", make("testResults")).call("Quick tests", runQuickTests);
  when(null, make("fmtResults")).call("Check code formatting", runPrettier);
  when(["!ci", "!silent", "fmtResults", "testResults"]).call("Finish", showDoneAlert);
}).run({ printer: ci ? "verbose" : "summary" }, { ci, silent: false });

For task to start values for all the input keys have to be provided either by config parameter (the second argument to run) or by another task as a result value. When task input key is prefixed with ? the value provided must be truthy, when prefix is ! value must be falsy. If value does not match requirements, task is skipped. When there's no prefix in the key any value for that key is allowed, including undefined and null. However config passed to run cannot provide undefined values. Any keys that are undefined in config must be provided by tasks.

Executor functions

Executor functions for listr2-scheduler receive one argument of type Worker that allows task to update task rendering and execution.

export type Worker<T = any> = {
  readonly data: T;
  readonly printer: "verbose" | "summary";
  readonly setTitleSuffix: (suffix: string) => void;
  readonly updateTitle: (title: string) => void;
  readonly reportStatus: (text: string) => void;
  readonly publish: (text: string) => void;
  readonly getTag: (options?: { colored: boolean }) => string;
  readonly on: (event: "finalize", callback: ErrorCallback<void>) => void;
  readonly assertCanContinue: (tag?: string) => void;
  readonly toolkit: Toolkit;
};
  • data is an object that is given as a second argument to run function, and it is augmented by return values of executor functions.

  • setTitleSuffix appends to task title in "summary" mode. Suffix remains in the final "summary" report.

  • updateTitle updates task title in "summary" mode. Clears title suffix. Title get restored to original in final "summary" report.

  • reportStatus prints task status in both "verbose" and "summary" modes.

  • publish in "summary" mode prints provided message at the end, in "verbose" mode prints provided message without any prefixes. Useful for warnings and similar important messages.

  • getTag returns worker tag that can be used to decorate piped output from external processes to differentiate text coming from multiple parallel tasks.

import { decorateLines, Worker } from "listr2-scheduler";

export function checkServerConnection(worker: Worker) {
  decorateLines(worker, "Start pinging", process.stdout);
  const sub = execa("ping", ["-c", "3", "npmjs.com"]);
  sub.stdout && decorateLines(worker, sub.stdout, process.stdout);
  sub.stderr && decorateLines(worker, sub.stderr, process.stderr);
  await sub;
}
  • on allows executor to be informed about errors in other parallely running executors. NOTE: Only the last registered callback will be invoked.
async function prepareTestFiles(worker) {
  const controller = new AbortController();
  worker.on('finalize', () => controller.abort());
  await fetch(testFilesBundleUrl, { signal: controller.signal });
}
  • assertCanContinue throws if some parallel executor has thrown an exception.
async function analyzeSourceFiles(worker) {
  let completed = 0;
  for (const filePath of all) {
    worker.reportStatus('Analyzing ' + filePath);
    await analyzeFile(filePath);
    completed += 1;
    worker.assertCanContinue('Analyzed ' + completed + '/' + all.length);
  }
}
  • toolkit is by default an empty object. It's type can be augmented with TypeScript declaration and it can be provided as an argument to run directly or via constructor function attach.
import { schedule } from "listr2-scheduler";

declare module "listr2-scheduler" {
  interface Toolkit {
    download: (url: string) => Promise<Maybe<Buffer>>;
  }
}

schedule((when) => {
  when(null).call("Fetch lint rules", ({ toolkit }) => toolkit.download(lintUrl));
}).run({ printer: "verbose", toolkit: { download } });