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 🙏

© 2026 – Pkg Stats / Ryan Hefner

script-journal

v1.4.2

Published

Task journal: run named task modules in a child process with JSON status and NDJSON logs (ESM/CJS)

Downloads

30

Readme

script-journal

Run a task module in a child process, persist JSON status, and capture NDJSON logs. Host apps only author tasks; this library owns execution, status, and log I/O.

Works from both ESM and CommonJS consumers. Task files may be .mjs, .js, or .cjs.

Other languages: 中文 | Deutsch | Español | Français | 日本語

Installation

npm install script-journal

Quick start

import { runTask, stopTask, readTaskJson, readTaskLog } from "script-journal";

try {
  const state = await runTask({
    cwd: "/path/to/your-app", // optional, default process.cwd()
    task: "src/tasks/helloTask.mjs", // absolute or relative to cwd
    output: "tmp/tasks/hello", // absolute or relative to cwd (no extension)
    parameters: { name: "world" },
  });
  // state is the task JSON (status: "done", ...)
} catch (state) {
  // on failure, the same JSON object is thrown (error already written to file)
  console.error(state.error);
}

// Force-stop a running task by its output path (kills pid from JSON if still alive)
await stopTask({ cwd: "/path/to/your-app", output: "tmp/tasks/hello" });

const persisted = readTaskJson({
  cwd: "/path/to/your-app",
  output: "tmp/tasks/hello",
});

// Defaults to tail=true (latest pages). totalLines is bounded by maxLogLines.
const log = readTaskLog({
  cwd: "/path/to/your-app",
  output: "tmp/tasks/hello",
  pageSize: 50,
});

CommonJS:

const { runTask, stopTask, readTaskJson, readTaskLog } = require("script-journal");

Parent process stays silent: child stdout/stderr are captured into the log file only.

Task module contract

// src/tasks/helloTask.mjs
export async function run(parameters, ctx) {
  ctx.logger.info("hello started");
  ctx.patchResults({ total: 0, failed: 0 });

  // ... work using parameters ...

  ctx.patchResults({ total: 3 });
  return { success: true }; // or { success: false, error: "..." }
}

ctx

| Field | Description | |---|---| | logger.debug/info/warn/error(msg) | Writes [LEVEL] msg; captured into NDJSON log | | results | Current results object (shared reference) | | updateResults(patch) | Shallow-merge into results and persist JSON | | patchResults(patch) | Same merge, returns results |

Return value

  • undefined / null — keep results accumulated via patchResults
  • object — shallow-merged into results; success decides done/failed; errorstate.error

Exit code: 0 if success, else 1.

Status JSON

Written to <output>.json:

{
  "task": "/abs/path/to/helloTask.mjs",
  "status": "pending|running|done|failed|error|stopped",
  "pid": 12345,
  "startedAt": "ISO|null",
  "finishedAt": "ISO|null",
  "durationMs": 0,
  "success": true,
  "parameters": {},
  "error": null,
  "results": {}
}

pid is set while the runner is alive and cleared (null) when the task finishes or is stopped.

Log file

<output>.log — one NDJSON object per line:

{"timestamp":"2026-07-19T01:00:00.000Z","level":"info","message":"hello started"}

When the log exceeds maxLogLines (default 10000), older lines are deleted from the head so only the newest lines remain. Pass maxLogLines: 0 to disable trimming.

API

runTask(options)

| Field | Type | Required | Description | |---|---|---|---| | cwd | string | ❌ | Working directory, default process.cwd() | | task | string | ✅ | Task file path (absolute or relative to cwd) | | output | string | ✅ | Output base path without extension (absolute or relative to cwd) | | parameters | object | ❌ | Passed to run(parameters, ctx) | | maxLogLines | number | ❌ | Max retained log lines; older lines dropped from head. Default 10000. ≤0 disables |

Before writing a new pending state, runTask reads any existing <output>.json and force-terminates a still-alive pid (if present). This prevents orphan runners when the same output is reused.

Returns the task JSON state on success. On failure, rejects with that same JSON object (error is already persisted to <output>.json).

Windows: process liveness uses tasklist; termination uses taskkill /T /F (process tree). Spawn uses windowsHide: true. Runner SIGTERM handlers are not invoked by taskkill /F; stopTask still writes status: "stopped" in the parent after kill.

stopTask({ cwd?, output })

Force-stop the task for output: terminate a live pid from <output>.json, then write status: "stopped", success: false, pid: null. Idempotent if the process is already gone. Throws if the state file is missing.

readTaskJson({ cwd?, output }) / readTaskLog({ cwd?, output, page?, pageSize?, tail? })

Same cwd / output resolution rules as runTask.

readTaskLog defaults to tail: true (latest pages). Set tail: false to read from the start.

License

MIT