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

@foxxmd/get-version

v0.0.3

Published

Get a version identifier from multiple sources

Downloads

179

Readme

@foxxmd/get-version

Latest Release NPM Version License: MIT

A convenient way to get a version identifier from a set of ordered sources (ENV, Git, File, NPM Package, default value...)

Features:

  • Fully typed for Typescript projects
  • Supports ESM and CJS projects
  • Get a version identifier from any of these sources in an order you define:
    • Environment Variables
    • Working Git repository
      • Template your version from last commit (branch, hash, tag)
    • version in NPM Packages
      • package.json
      • package-lock.json
      • npm-shrinkwrap.json
    • Arbitrary json/text files
  • Return a default value if no other version found

Documentation best viewed on https://foxxmd.github.io/get-version

Why?

You have a project that can be distributed, or run, in multiple ways. You want to output the version of the project so users know what they are running or for debugging purposes.

The problem you have is you have:

  • Tagged releases on Github
  • Build Docker images
  • Have PR/branches used by end-users
  • etc...

and there's no one way to make sure all of these instances display an accurate version unless you want to manually add/commit a Source of Truth value somewhere every time you make a change.

@foxxmd/get-version solves this problem. By using multiple sources, in an order you define, to glean a "version identifier" you can programmatically determine the most accurate identifier to display.

Example Scenario

We will look for a version from sources in this order=: ENV, Git, NPM, Fallback

First look for ENV...

  • Build your images with ENV APP_VERSION parsed from release tag with GH Actions
  • Build and push image from local to registry with a custom APP_VERSION for one-offs

ENV not found then with Git...

  • End-user (or you) clones/forks project and runs natively => version parsed from last commit as {branch}-{commit}

Git not found then with NPM...

  • End-user downloads copy of project from Release asset or .zip on github => finds package-lock.json in project folder and uses version set by you for release

NPM not found...

  • Uses fallback value set in project so you know user is using an uncommon setup (or something is wrong!)

Install

npm install @foxxmd/get-version

Quick Start

import { getVersion } from "@foxxmd/get-version";

// defaults to ENV => Git => Files => NPM => Fallback
const version = await getVersion();
console.log(version); // 1.0.0

Configuring Version Options

Pass an object implementing VersionOpts to getVersion

import { getVersion, VersionOpts } from "@foxxmd/get-version";
import path from 'node:path';

const opts: VersionOpts = {
    priority: ['file', 'env', 'git'],
    env: {
        names: ['CUSTOM_VERSION','APP_VERSION']
    },
    file: {
        npmPackage: false,
        additionalFiles: [path.join(process.cwd(), 'version.txt')],
    },
    git: {
        gitTemplate: 'GIT-{branch}-{hash}'
    },
    fallback: 'unknown'
}

const version = await getVersion(opts);
console.log(version); // 1.0.0

Read the docs for all options