import-runner

Low code control flow with dynamically imported functions

Usage no npm install needed!

<script type="module">
  import importRunner from 'https://cdn.skypack.dev/import-runner';
</script>

README

👟 Import Runner

Low code control flow using dynamic imports

⚙ī¸ Install

npm install import-runner

🏃 Run

Import runner provides a shorthand for executing complex control flows using dynamic imports:

import importRunner from "import-runner"

export default async (input: {
  someOption: boolean
}): Promise<any> => {
  return await importRunner({
    input,
    each: [
      import("./myFunction"),
      import("./thatFunction"),
      {
        all: [
          import("./otherFunction"),
          import("./andAnotherFunction"),
          {
            route: [
              import("./wowAnotherFunction"),
              import("./enoughFunction"),
            ],
          },
        ],
      },
    ],
  })
}

ℹī¸ Import runner calls the default function of the dynamic import (export default)

ℹī¸ If a function call returns an object, it is assigned to the final output (Object.assign)

Option Description
input Input object
all Concurrent execution
each Sequential execution
route Concurrent conditional execution (uses "or" on input types)

🤖 Low code

Wait a second! What is up with the any output of the importRunner call? Did I just lose all my types? How do the child functions know what their inputs will be?

Because we have a somewhat formal structure, we can programmatically parse importRunner calls and generate input and output types for the runner function and its component functions.

Example code to process source files with chokidar, eslint, prettier, file-replacer, and fs-extra:

import chokidar from "chokidar"
import { ESLint } from "eslint"
import fileReplacer from "file-replacer"
import fsExtra from "fs-extra"
import prettier from "prettier"
import sourceProcessor from "import-runner/dist/cjs/sourceProcessor"

chokidar.watch("./**/*.ts").on("change", (event, path) => {
  sourceProcessor({
    eslint: new ESLint({ fix: true }),
    fileReplacer,
    fsExtra,
    path,
    prettier,
    srcRootPath: __dirname,
  })
})