# check-version-modules
Check modules's version for the package.

[![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=Psychopoulet_check-version-modules&metric=ncloc)](https://sonarcloud.io/summary/new_code?id=Psychopoulet_check-version-modules)
[![Issues](https://img.shields.io/github/issues/Psychopoulet/check-version-modules.svg)](https://github.com/Psychopoulet/check-version-modules/issues)
[![Pull requests](https://img.shields.io/github/issues-pr/Psychopoulet/check-version-modules.svg)](https://github.com/Psychopoulet/check-version-modules/pulls)

[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=Psychopoulet_check-version-modules&metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=Psychopoulet_check-version-modules)
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=Psychopoulet_check-version-modules&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=Psychopoulet_check-version-modules)
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=Psychopoulet_check-version-modules&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=Psychopoulet_check-version-modules)
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=Psychopoulet_check-version-modules&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=Psychopoulet_check-version-modules)
[![Bugs](https://sonarcloud.io/api/project_badges/measure?project=Psychopoulet_check-version-modules&metric=bugs)](https://sonarcloud.io/summary/new_code?id=Psychopoulet_check-version-modules)

[![Known Vulnerabilities](https://snyk.io/test/github/Psychopoulet/check-version-modules/badge.svg)](https://snyk.io/test/github/Psychopoulet/check-version-modules)

## Installation

```bash
$ npm install check-version-modules
```

## Features

  * Extract package's dependencies / dev dependencies / optional dependencies
  * source can be a package.json file (with any name) or directly a package content
  * Extract their versions
  * Compare them to the latest package's version

## Doc

### Supported patterns

> does not support artifacts like "-beta"
> each "*" character will be understood like a "x"'
> here, "n" is a whatever number sample, like "1"

Does support following patterns:

  * x (=> x.x.x)
  * x.x.n (=> x.x.x)
  * x.n.n (=> x.x.x)
  * n (=> n.x.x)
  * n.x (=> n.x.x)
  * n.n (=> n.n.x)
  * n.n.n
  * ^n.n.n (=> n.x.x)
  * ~n.n.n (=> n.n.x)

### Methods

  * ``` (source: string | Record<string, object | string | number | boolean>, options?: iOptions) => Promise<iAnalyze> ``` extract & compare data

### Interfaces

```typescript
interface iOptions {
  "failAtMajor"?: boolean; // default: true => if no pattern, used for the returned boolean
  "failAtMinor"?: boolean; // default: true => if no pattern, used for the returned boolean
  "failAtPatch"?: boolean; // default: false => if no pattern, used for the returned boolean
  "dev"?: boolean; // default: true => analyze dev deps too
  "optional"?: boolean; // default: true => analyze optional deps too
  "npmrcFile"?: string; // default: "" => specify a npm configuration file (for private repositories)
}

interface iAnalyze {
    "result": boolean;
    "results": iResult[];
}

interface iResult extends iDep {
    "time": string;
    "result": "success" | "warning" | "fail_patch" | "fail_minor" | "fail_major";
    "message": string;
}

interface iDep {
    "dev": boolean;
    "optional": boolean;
    "name": string;
    "version": string;
    "path": string;
}
```

### Command line options

  * ``` --fail-at-major ``` => failAtMajor = true
  * ``` --no-fail-at-major ``` => failAtMajor = false
  * ``` --fail-at-minor ``` => failAtMinor = true
  * ``` --no-fail-at-minor ``` => failAtMinor = false
  * ``` --fail-at-patch ``` => failAtPatch = true
  * ``` --no-fail-at-patch ``` => failAtPatch = false
  * ``` --dev ``` => dev = true
  * ``` --no-dev ``` => dev = false
  * ``` --optional ``` => optional = true
  * ``` --no-optional ``` => optional = false
  * ``` --npmrcFile ``` => npmrcFile = next arg
  * ``` --file ``` => specify analyzed file with next argument, if not set analyze the "package.json" in the working directory

## Examples

### Command line

```bash
$ cd ./myProject/ && npx check-version-modules --file /etc/tests/package.json --fail-at-patch --no-dev
$ cd ./myProject/ && npx check-version-modules --no-fail-at-minor
```

### Native

```javascript
const checker = require("check-version-modules");

checker("/etc/tests/package.json", {
  "failAtPatch": true,
  "dev": false,
  "optional": false
}).then((analyze) => {
  console.log(analyze.result ? "ok": "old versions detected");
}).catch((err) => {
  console.error(err);
});

checker({
  "dependencies": {
    "test": "1.0.0"
  }
}, {
  "failAtPatch": true,
  "dev": false,
  "optional": false
}).then((analyze) => {
  console.log(analyze.result ? "ok": "old versions detected");
}).catch((err) => {
  console.error(err);
});
```

### Typescript

```typescript
import checker = require("check-version-modules");

checker("./package.json").then((analyze) => {
  console.log(analyze.result ? "ok": "old versions detected");
}).catch((err) => {
  console.error(err);
});

checker({
  "dependencies": {
    "test": "1.0.0"
  }
}).then((analyze) => {
  console.log(analyze.result ? "ok": "old versions detected");
}).catch((err) => {
  console.error(err);
});
```

## Tests

```bash
$ npm run tests
```

## License

  [ISC](LICENSE)
