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

fsm2mermaid

v1.3.0

Published

Finite State Machine (FSM) to Mermaid State diagram text generator.

Downloads

28

Readme

FSM to mermaid

A tool to generate mermaid state diagrams from existing Finite State Machines (FSM).

Why do we need this tool? Especially if we already have awesome visual editors?

  • This tool generates static mermaid state diagram for documentation purposes.

  • There is no need for interactivity and two-way roundtrip synchronisation. Just one-way, cheap, fast and easy to maintain manner.

  • Because it is one-way, source of truth remains with the code and we iterate from there.

  • Diagram is generated from statically declared state machine objects and not from parsing code. We traverse the FSM object model to produce the mermaid script.

  • Diagram could be generated from CI/CD pipeline by invocation via unit test framework like Jest or Mocha.

  • The generated mermaid script could be inserted into existing markdown like README.md or any other forms of markdown based documentation and static sites.

Roadmap

Currently, this tool is at Minimum Viable Product (MVP) phase. There could be certain cases that it might not handle correctly.

Any problems and feedback, just log an issue here.

Thank you for your contributions to making things better!

For the start, only XState is supported.

  • XState

Other FSM libraries to be supported.

XState to mermaid

npm install xstate fsm2mermaid

You could test drive from Node interactive mode.

node

Welcome to Node.js v18.13.0.
Type ".help" for more information.
>
const xstate = require("xstate");
const fsm2mermaid = require("fsm2mermaid");

let simpleMachine = xstate.createMachine(
  {
    id: "simple",
    initial: "lightsOff",
    states: {
      lightsOn: {
        on: {
          toggle: {
            target: "lightsOff",
          },
        },
      },
      lightsOff: {
        on: {
          toggle: {
            target: "lightsOn",
          },
        },
      }
    }
  },
  {
    actions: {},
    actors: {},
    guards: {},
    delays: {},
  });

let simpleMermaid = generateMermaidFromXState(simpleMachine);
console.log(simpleMermaid);

stateDiagram-v2
simple : simple
state simple {
  [*] --> simple.lightsOff
  simple.lightsOn : lightsOn
  simple.lightsOn --> simple.lightsOff : toggle
  simple.lightsOff : lightsOff
  simple.lightsOff --> simple.lightsOn : toggle
}

Rendering the markdown with mermaid plugin gives us the following.

stateDiagram-v2
simple : simple
state simple {
  [*] --> simple.lightsOff
  simple.lightsOn : lightsOn
  simple.lightsOn --> simple.lightsOff : toggle
  simple.lightsOff : lightsOff
  simple.lightsOff --> simple.lightsOn : toggle
}

And the equivalent using ECMAScript Module (ESM) syntax.

import {createMachine} from "xstate"
import {generateMermaidFromXState} from "fsm2mermaid"

let simpleMachine = createMachine(
  {
    id: "simple",
    initial: "lightsOff",
    states: {
      lightsOn: {
        on: {
          toggle: {
            target: "lightsOff",
          },
        },
      },
      lightsOff: {
        on: {
          toggle: {
            target: "lightsOn",
          },
        },
      }
    }
  },
  {
    actions: {},
    actors: {},
    guards: {},
    delays: {},
  });

let simpleMermaid = generateMermaidFromXState(simpleMachine);

console.log(simpleMermaid);

stateDiagram-v2
simple : simple
state simple {
  [*] --> simple.lightsOff
  simple.lightsOn : lightsOn
  simple.lightsOn --> simple.lightsOff : toggle
  simple.lightsOff : lightsOff
  simple.lightsOff --> simple.lightsOn : toggle
}

Developer Documentation

The following are quick notes for development use.

Set npm cache to local

Docker build will rely on this to speed up

npm config set cache .npm

Install all dependencies and setup commit hook

npm install

Running linters

To perform a check

npm run lint

To Auto fix as much as possible.

npm run lint-fix

Check on commit message format

npx commitlint --from=HEAD

Running unit tests

For local run.

npm run test

For running tests in Gitlab CI.

npm run test:ci

Docker image for test and publish in Gitlab CI/CD

Building and updating image on Gitlab registry.

docker login registry.gitlab.com
docker build -t registry.gitlab.com/kaikokchew/fsm2mermaid/runner:latest --build-arg GL_PROJ_GROUP=kaikokchew --build-arg GL_PROJ_NAME=fsm2mermaid --progress=plain .
docker push registry.gitlab.com/kaikokchew/fsm2mermaid/runner:latest

Running and testing image on local.

docker run -it -v $(pwd):/builds/kaikokchew/fsm2mermaid --rm registry.gitlab.com/kaikokchew/fsm2mermaid/runner sh

Configuring pre-commit hooks

Typically this is already done as part of first time npm installation.

npm install --save-dev husky
npm pkg set scripts.commitlint="commitlint --edit"
echo "npm run commitlint \${1}" > .husky/commit-msg
npm pkg set scripts.pre-commit="npm run lint"
echo "npm run pre-commit \${1}" > .husky/pre-commit

Building your package

npm run build