-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (72 loc) · 2.92 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#! /usr/bin/env node
// This is hashbang comment this is used to tell the shell environment
// that when the program is executed which program to handover this ,
// which in our case is NodeJS.
// we give the path of the node here.But because diff environment
// have diff places where node installed so we use env path instead
// of absolute path
// By the time this program executed the #! line is cleared out
"use Strict";
var path = require("path");
var fs = require("fs");
var minimist = require("minimist");
var stdin = require("get-stdin");
// we are requiring this get-stdin because different shells handle stdin in different way so
// its hard to work with these differences thatswhy we are importing this package
// process.argv is used to get an array of the arguments that passed during execution
// but this doesn't do neccessary parsing required like --hello=World to hello:World or -c9 to c:9
// to do this parsing we use a package minimist which is most popular and preinstalled in node
var args = minimist(process.argv.slice(2), {
boolean: ["help", "in"],
string: ["file"],
});
// $ node example/parse.js -a beep -b boop
// { _: [], a: 'beep', b: 'boop' }
// _ is for overflow that minimist couldn't understand
if (args.help) {
printHelp();
} else if (args.in || args._.includes("-")) {
// TODO take std in
stdin().then(processFile).catch(printError);
} else if (args.file) {
var contents = fs.readFile(path.resolve(args.file), (err, contents) => {
if (err) {
printError(err.toString());
} else {
contents = contents.toString();
processFile(contents);
}
});
} else {
printError("Incorrect Usage.", true);
}
// *********************************
function processFile(contents) {
// var contents = fs.readFileSync(filepath);
// This above function will create a buffer and if you pass the contents to
// the console.log it will do the strigification of the buffer and produce an strange
// output string like "<buffer> 01 94 49 66 79 12" instead of "Hello world" , this is because the console.log
// is a wrapper on the process.write , if you display the contents using process.write instead of the console.log
// it don't do the stringification but provide the buffer directily to the shell and the shell will translate
// the buffer and give the output "Hello World".
// console.log(contents);
contents = contents.toUpperCase();
process.stdout.write(contents);
}
// ****************** Help Section **************************
function printError(msg, includeHELP = false) {
console.error(msg);
if (includeHELP) {
console.log("");
printHelp();
}
}
function printHelp() {
console.log("indes.js usage");
console.log(" index.js --help");
console.log("");
console.log("--help print this help");
console.log("--file={FILENAME} process the file");
console.log("--in,- process stdin");
console.log("");
}