0.0.6 ā€¢ Published 3 years ago

nanov2 v0.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

nanoV

šŸ“¦ get the latest version for your package from npm

šŸŒ great for global installation packages -g

āœŒļø semantic versions

0ļøāƒ£ zero dependency

šŸš€ small and fast (~ 1 kbytes)

Table of Contents:

  1. Installation

  2. Usage

  3. Return Types

  4. Options

Installation

npm i nanov or yarn add nanov

const { getVersion } = require("nanov"); // node < 12

import { getVersion } = from "nanov"; // node 14 >

Usage

const { getVersion } = require('nanov');

const currentVersion = require(./package.json).version;  /* pick your current version */

getVersion("my-package", currentVersion).then(({isMajor, isMinor, isPatch, latestVersion})=> {
 if(isMajor || isMinor || isPatch,) console.log('new version available: ', latestVersion)
})

//further code ....

async / await

const { isMajor, isMinor, isPatch, latestVersion } = await getVersion(
  "my-package",
  currentVersion
);

if (isMajor || isMinor || isPatch)
  console.log("new version available: ", latestVersion);

//further code ....

Return Types

// if the latest version is the current version: will result in:
false (boolean)
// if the latest version is greather than the current version
{
  isMajor: boolean, // 1.0.0 > 0.0.0
  isMinor: boolean, // 0.1.0 > 0.0.0
  isPatch: boolean, // 0.0.1 > 0.0.0
  latestVersion: string,
  packageName: string
}: object

// if hit the cache will skip the http request and return null

null
  // semantic version comparison
  //               latest / current
  isMajor: true, // 2.0.0 > 1.0.0 // x - -
  //
  isMinor: true, // 2.1.0 > 1.0.0 // - x -

  isPatch: true, // 2.0.1 > 1.0.0 // - - x

Options

getVersion(packageName, currentVersion, options)
//options
{
    method: string,
    cache: true, // should use caching strategy / true by default
    global: true, // just check for global package version -g / true by default
    return: "boolean",
    customMessages: {
      major: "",
      minor: "",
      patch: "",
}: object

Examples

Simple

getVersion("my-package", version).then((res, latestVersion) => {
  if (res) console.log("new version available: ", latestVersion);
});

Options

getVersion(
   packageName,
  currentVersion,
{
    cache: true,
    global: true,
    customMessages: {
      major: "",
      minor: "",
      patch: "",
}).then((res, latestVersion) => {
  if (res) console.log("new version available: ", latestVersion);
});

Any (Async / await)

const { isMajor, isMinor, isPatch, latestVersion } = await getVersion(
  "my-package",
  version
);
if (isMajor || isMinor || isPatch)
  console.log("new version available: ", latestVersion);

Major only

getVersion("my-package", version).then(({ isMajor, latestVersion }) => {
  if (isMajor) console.log("new major version available: ", latestVersion);
});

Minor only

getVersion("my-package", version).then(({ isMinor, latestVersion }) => {
  if (isMinor) console.log("new minor version available: ", latestVersion);
});

Patch only

getVersion("my-package", version).then(({ isPatch, latestVersion }) => {
  if (isPatch) console.log("new patch version available: ", latestVersion);
});

Basically you can do anything

getVersion("my-package", version).then(({ isMinor, latestVersion }) => {
  if (isMinor) {
    console.log("new version available: ", latestVersion);
    console.log("run: npm i -g packageName to update");
    //further functions
  }
});

Automatic deprecation (when new major version)

getVersion("my-package", version).then(
  ({ isMajor, latestVersion, packageName }) => {
    if (isMajor) {
      console.log("this version is deprecated");
      console.log("latest version is required: ", latestVersion);
      console.log(`run: npm i -g ${packageName} to update`);
      process.exit(); // quit your program from running
    }
  }
);