-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
55 lines (41 loc) · 1.26 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
47
48
49
50
51
52
53
54
55
/* sòm
* Compute checksum of a file.
*
* started at 25/09/2015
*/
"use strict";
var chalk = require( "chalk" ),
path = require( "path" ),
fs = require( "fs" ),
humanSize = require( "human-size" ),
crc32 = require( "easy-crc32" ).calculate;
var sFileName, sFilePath;
var fShowError = function( sErrorMessage ) {
console.log( chalk.red.bold.underline( "✘ error:" ), sErrorMessage );
process.exit( 1 );
};
if( !( sFileName = process.argv[ 2 ] ) ) {
fShowError( "You need to give a file as argument!" );
}
sFilePath = path.resolve( process.cwd(), sFileName );
fs.stat( sFilePath, function( oError, oStats ) {
var aLogLines = [];
if( oError ) {
fShowError( oError.message );
}
if( !oStats.isFile() ) {
fShowError( "The given path must be a file!" );
}
// name
aLogLines.push( chalk.yellow.bold( sFileName ) );
// size
aLogLines.push( chalk.gray( "(" + humanSize( oStats.size ) + ")" ) );
// checksum
fs.readFile( sFilePath, { "encoding": "utf-8" }, function( oError, sData ) {
if( oError ) {
fShowError( oError );
}
aLogLines.push( chalk.green.bold( "sum:" ) + " " + crc32( sData ) );
console.log( aLogLines.join( " " ) );
} );
} );