-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·46 lines (35 loc) · 1.39 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env node
const program = require( 'commander' );
const _ = require( 'lodash' );
const chalk = require( 'chalk' );
// import package.json
const package = require( './package.json' );
// import lib/checksum file
const checksum = require( './lib/checksum' );
// import valid algorithms
const { ALGORITHMS } = require( './lib/constants' );
// intialize CLI interface
program
.version( package.version, '-v, --version', 'Print current CLI version.' )
.description( 'Calculate checksum of a file.' )
.arguments( '[filepath]' )
.option( '-a, --algorithm <algorithm>', `Algorithm to be used for hashing (${ _.join( ALGORITHMS, ' or ' ) })`, _.first( ALGORITHMS ) )
.action( ( filepath, options ) => {
// extract required options
const { algorithm } = options;
// check for filepath
if( _.isEmpty( filepath ) ) {
return console.log( chalk.red( `Provide a file path with the command using command : ${ chalk.bold( 'file-checksum <filepath>' ) }` ) );
}
// check for valid algorithm
if( ! _.includes( ALGORITHMS, algorithm ) ) {
return console.log( chalk.red( `Provide a valid algorithm : use ${ chalk.bold( _.join( ALGORITHMS, ' or ' ) ) }` ) );
}
// calculate checksum
checksum.calculate( { filepath, algorithm } );
} )
;
// parse command line argument vector
program.parse( process.argv );
// pick options from commander
// console.log( program );