Skip to content

Commit

Permalink
Support printing the current version of the cli tool (#54)
Browse files Browse the repository at this point in the history
* Support printing the current version of the cli tool

* Update README
  • Loading branch information
remisture authored Feb 11, 2024
1 parent aa0a7a6 commit 4d99475
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ A CLI tool for BMI calculation.
## Install

```sh
npm i -g cli-bmi
$ npm i -g cli-bmi
```

## Usage

```sh
bmi [<options>]
bmi
bmi --weight 85 --height 180
$ bmi [<options>]
$ bmi
$ bmi --weight 85 --height 180
```

## Commands
Expand All @@ -24,7 +24,8 @@ bmi --weight 85 --height 180
--height number Height in centimeters, e.g. 180
--weight number Weight in kilos, e.g. 85
-c, --clear Clear local data
-h, --help Show help section
-h, --help Displays this usage guide.
-v, --version Displays the version of the cli
```

## Examples
Expand Down
2 changes: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { logError } from './lib/utils.js';

const run = async () => {
try {
const cliOptions = parseCliOptions();
const cliOptions = await parseCliOptions();
const height = await determineHeight(cliOptions);
const weight = await determineWeight(cliOptions);
printResults(height, weight);
Expand Down
5 changes: 4 additions & 1 deletion lib/cli-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ export const cliOptions = [
{ name: 'height', type: Number, description: 'Height in centimeters, e.g. 180' },
{ name: 'weight', type: Number, description: 'Weight in kilos, e.g. 85' },
{ name: 'clear', alias: 'c', type: Boolean, description: 'Clear local data' },
{ name: 'help', alias: 'h', type: Boolean, description: 'Show help section' },
{ name: 'help', alias: 'h', type: Boolean, description: 'Displays this usage guide.' },
{ name: 'version', alias: 'v', type: Boolean, description: 'Displays the version of the cli' },
];

export const commonCliSectionUsage = (command) => [`$ ${command} {bold -h}`, `$ ${command} {bold -v}`];
31 changes: 28 additions & 3 deletions lib/lib.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env node

import process from 'node:process';
import { dirname, join } from 'node:path';
import { readFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import chalk from 'chalk';
import clear from 'clear';
import commandLineArgs from 'command-line-args';
Expand All @@ -9,9 +12,12 @@ import figlet from 'figlet';
import input from '@inquirer/input';
import { LocalStorage } from 'node-localstorage';
import message from './message.js';
import { cliOptions } from './cli-options.js';
import { cliOptions, commonCliSectionUsage } from './cli-options.js';
import { isPositiveNumber } from './utils.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const STORAGE_PATH = './storage-data';
const STORAGE_NAME = 'data';
const STORAGE_KEYS = {
Expand Down Expand Up @@ -39,9 +45,10 @@ const clearStorage = () => {
console.log(' ');
};

export const parseCliOptions = () => {
export const parseCliOptions = async () => {
const cmdOptions = commandLineArgs(cliOptions, { partial: true });
const help = Boolean(cmdOptions.help);
const version = Boolean(cmdOptions.version);
const clear = Boolean(cmdOptions.clear);
const weight = cmdOptions.weight;
const height = cmdOptions.height;
Expand All @@ -52,6 +59,12 @@ export const parseCliOptions = () => {
return;
}

if (version) {
await getVersion();
process.exit(0);
return;
}

if (clear) {
clearStorage();
}
Expand Down Expand Up @@ -138,7 +151,7 @@ const helpSection = () => {
commandLineUsage([
{
header: 'Usage',
content: ['$ bmi [<options>]', '$ bmi', '$ bmi --weight 85 --height 180'],
content: ['$ bmi [<options>]', '$ bmi', '$ bmi --weight 85 --height 180', ...commonCliSectionUsage('bmi')],
},
{ header: 'Commands', optionList: cliOptions },
{ content: 'A cli tool by Remi Sture' },
Expand Down Expand Up @@ -181,3 +194,15 @@ export const getBMIScore = (bmi) => {

return 'Invalid BMI';
};

export const getVersion = async () => {
const packageJsonPath = join(__dirname, '../package.json');
try {
const packageJson = await readFile(packageJsonPath, 'utf8');
const packageObject = JSON.parse(packageJson);
console.log(packageObject.version);
return packageObject.version;
} catch {
console.error("Couldn't find package.json");
}
};

0 comments on commit 4d99475

Please sign in to comment.