Skip to content

Commit b6c9869

Browse files
Uday HiwaraleUday Hiwarale
Uday Hiwarale
authored and
Uday Hiwarale
committed
init
1 parent 1340e75 commit b6c9869

10 files changed

+303
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

+44-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,44 @@
1-
# md5-file-cli
2-
A CLI to display MD5 Checksum of a file's contents.
1+
# File Checksum CLI
2+
A CLI tool to calculate checksum of file's content using **md5** or **sha1** algorithms.
3+
4+
## Installation
5+
```
6+
$ npm install -g file-checksum
7+
```
8+
9+
## Help and Version
10+
```
11+
$ file-checksum -v
12+
1.0.0
13+
14+
$ file-checksum --help
15+
Calculate checksum of a file.
16+
17+
Usage: file-checksum <filepath> [options]
18+
19+
Options:
20+
-v, --version Print current CLI version.
21+
-a, --algorithm <algorithm> Algorithm to be used for hashing (md5 or sha1) (default: "md5")
22+
-h, --help output usage information
23+
```
24+
25+
## Calculate checksum
26+
Once this package is installed globally, you can use `file-checksum` command.
27+
```
28+
$ file-checksum <filepath>
29+
$ file-checksum <filepath> -a sha1
30+
$ file-checksum <filepath> --algorithm sha1
31+
```
32+
33+
#### Example
34+
```
35+
$ file-checksum test/file.txt
36+
37+
5e7c683623bdabaeae97f8157e80f85c
38+
```
39+
40+
> If `-a` or `--algorithm` flag is missing, by default **md5** algorithm is used.
41+
42+
---
43+
44+
To create your own CLI tool using node.js, follow my article on [**Medium**](https://itnext.io/making-cli-app-with-ease-using-commander-js-and-inquirer-js-f3bbd52977ac).

index.js

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

lib/calculate-md5.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const md5 = require( 'md5' );
2+
3+
// export function that returns `md5` hash
4+
module.exports = ( buffer ) => {
5+
return md5( buffer );
6+
};

lib/calculate-sha1.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const sha1 = require( 'node-sha1' );
2+
3+
// export function that returns `sha1` hash
4+
module.exports = ( buffer ) => {
5+
return sha1( buffer );
6+
};

lib/checksum.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const fs = require( 'fs-extra' );
2+
const path = require( 'path' );
3+
const chalk = require( 'chalk' );
4+
const _ = require( 'lodash' );
5+
6+
// import valid algorithms
7+
const { ALGORITHMS } = require( './constants' );
8+
9+
// create a map of hash calculators based on algorith names
10+
// HASHERS = { <algorithm>: require(<algorithm-hash-calculator-file>) }
11+
const HASHERS = _.fromPairs( _.map( ALGORITHMS, ( ALGORITHM ) => {
12+
return [ ALGORITHM, require( `./calculate-${ ALGORITHM }` ) ];
13+
} ) );
14+
15+
// current working directory (where terminal is opened)
16+
const CWD = process.cwd();
17+
18+
// export a common function to calculate checksum
19+
exports.calculate = ( { filepath, algorithm } ) => {
20+
21+
// get absolue file path
22+
const file = path.resolve( CWD, filepath );
23+
24+
// check if file exists
25+
if( ! fs.existsSync( file ) ) {
26+
return console.log( chalk.red( `File at location ${ chalk.bold( file ) } does not exist.` ) );
27+
}
28+
29+
// get file contents in buffer
30+
const fileBuffer = fs.readFileSync( file );
31+
32+
// calculate hash of the buffer
33+
const hash = HASHERS[ algorithm ]( fileBuffer );
34+
35+
// print hash
36+
console.log( chalk.green.bold( hash ) );
37+
38+
};

lib/constants.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// hashing algorithms
2+
exports.ALGORITHMS = [ 'md5', 'sha1' ];

package-lock.json

+125
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "file-checksum",
3+
"version": "1.0.0",
4+
"description": "A CLI tool to display MD5 or SHA1 checksum of a file's contents.",
5+
"main": "index.js",
6+
"bin": "index.js",
7+
"scripts": {
8+
"test": "node test/index.js"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/thatisuday/file-checksum.git"
13+
},
14+
"keywords": [
15+
"checksum",
16+
"cli",
17+
"md5",
18+
"sha1"
19+
],
20+
"author": "Uday Hiwarale <[email protected]>",
21+
"license": "MIT",
22+
"bugs": {
23+
"url": "https://github.com/thatisuday/file-checksum/issues"
24+
},
25+
"homepage": "https://github.com/thatisuday/file-checksum#readme",
26+
"dependencies": {
27+
"chalk": "^2.4.2",
28+
"commander": "^3.0.0",
29+
"fs-extra": "^8.1.0",
30+
"lodash": "^4.17.15",
31+
"md5": "^2.2.1",
32+
"node-sha1": "^1.0.1"
33+
}
34+
}

test/file.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"Hello World"

0 commit comments

Comments
 (0)