-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·114 lines (99 loc) · 2.94 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#! /usr/bin/env node
/*
* Module dependencies.
*/
var program = require('commander');
var chalk = require('chalk');
// Googleit's version
// Only change this in release branch
program.version('1.2.1');
// Print red error message
function printConsoleError(message) {
var fullMessage = '\n ' + message + '\n';
console.error(chalk.red(fullMessage));
}
// Check system is running on macOS or not => Show error message and retrun false
function checkEnvironment(callback) {
var exec = require('child_process').exec;
var cmd = 'uname -s';
var result;
exec(cmd, function(error, stdout) {
if (!stdout.includes('Darwin')) {
var errorMessage = 'googleit only supports macOS. Using other OS may causing error.';
printConsoleError(errorMessage);
result = false;
callback(result);
process.exit(1);
} else {
result = true;
callback(result);
}
});
}
// Callback for checkEnvironment
var checkEnvironmentIsTrue = function(param) {
if (param === true) {
argumentsExist(typeof wordValue);
}
};
// Error message when no any argument
function argumentsExist(wordValue) {
if (wordValue === 'undefined') {
var errorMessage = 'Please enter search terms. "googleit <terms>"';
printConsoleError(errorMessage);
process.exit(1);
}
}
// Use in multiple terms search
function replaceAll(target, search, replacement) {
return target.split(search).join(replacement);
};
// CheckEnvironment before check argumentsExistence
checkEnvironment(checkEnvironmentIsTrue);
// Example command usages in --help
program.on('--help', function() {
console.log(' Examples:');
console.log('');
console.log(' $ googleit weather-tomorrow');
console.log(' $ googleit -i cat');
console.log(' $ googleit -b harry-potter');
console.log('');
});
var wordValue;
program
.arguments('<word>')
.option('-i, --image', 'Search Image on Google')
.option('-n, --news', 'Search News on Google')
.option('-v, --video', 'Search Video on Google')
.option('-p, --patent', 'Search Patent on Google')
.option('-b, --book', 'Search Book on Google')
.option('-e, --except <exception>', 'Search with exception')
.action(function(word) {
wordValue = word;
word = replaceAll(word, '-', '%20');
if (program.except) {
word = word + '%20-' + program.except;
}
var exec = require('child_process').exec;
var cmd = 'open https://google.com/#q=' + word;
var base = "open \'https://google.com/search?q=";
if (program.image) {
cmd = base + word + "&tbm=isch\'";
exec(cmd);
} else if (program.news) {
cmd = base + word + "&tbm=nws\'";
exec(cmd);
} else if (program.video) {
cmd = base + word + "&tbm=vid\'";
exec(cmd);
} else if (program.patent) {
cmd = base + word + "&tbm=pts\'";
exec(cmd);
} else if (program.book) {
cmd = base + word + "&tbm=bks\'";
exec(cmd);
} else {
exec(cmd);
}
})
.parse(process.argv);