forked from SuperMarcus/pgoogle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·141 lines (128 loc) · 4.65 KB
/
cli.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env node
"use strict";
/**
*
* P Google
*
* Copyright (C) 2018 Marcus Zhou
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const Readline = require("readline");
const chalk = require("chalk");
const {
create,
obfuscate
} = require("pgoogle");
const {
version
} = require("pgoogle/package.json");
const eol = require("os").EOL;
const cli = require("commander");
const fs = require("fs");
cli.version(version)
.option('-o, --output [path]', "output for the generated document. Pass '-' for stdout.")
.option('-a, --answer', "answer all questions with yes")
.option('-f, --false', "specify with --answer, answer all the questions with no")
.option('-s, --syn', "substitute words with synonyms")
.option('-n, --num [number]', "number of sentences to include in the output")
.parse(process.argv);
const io = {
in: process.stdin,
out: process.stderr,
store: process.stdout
};
const rl = Readline.createInterface({
input: io.in,
output: io.out
});
if (cli.output && cli.output !== '-'){
io.store = fs.createWriteStream(cli.output);
}
const ioCleanup = () => {
if (cli.output && cli.output !== '-')
io.store.close();
rl.close();
};
const ioWriteStore = (data) => (new Promise(res => io.store.write(data, 'utf8', res)));
//Two step process!!
const ask = async (question, type, colorizer) => {
let typeHinter = (
type === 'boolean' ? " [yes|no]" :
type === 'number' ? " [number]" :
""
);
if(cli.answer){ return !cli.false }
return await new Promise(res => {
let formateed = `[?] ${ (colorizer || chalk.magenta)(question) }${ chalk.grey(typeHinter) } `;
let resultChecker;
resultChecker = (a) => {
if (type === 'boolean')
res(a.charAt(0).toLowerCase() === 'y');
else if (type === 'number'){
let parsed = parseInt(a);
if(Number.isNaN(parsed)){
io.write(`[!] ${ chalk.red("Invalid input. A number is expected.") }${eol}`);
rl.question(formateed, resultChecker);
} else { res(parsed) }
} else { res(a) }
};
rl.question(formateed, resultChecker)
});
};
const notice = async (message, colorizer) => (io.out.write(`[*] ${ (colorizer || chalk.yellow)(message) }${eol}`));
const highlight = async (snippets, highlights) => (io.out.write(
eol +
snippets.map((s, i) => ((highlights.includes(i) ? chalk.yellow : chalk.grey)(s))).join(" ") +
eol + eol
));
const status = async (message) => (notice(message, chalk.blue));
let stageHandler = {
onStart: async function(s){
this._stage = s;
io.out.write(`\r[*] ${ chalk.cyan(s) }\r`);
},
onProgress: async function(current, total){
// noinspection JSCheckFunctionSignatures
io.out.write(`\r[*] ${
chalk.cyan(this._stage + ":")
}\t${
chalk.green(parseInt(current / total * 1000) / 10 + "%")
}\t${ chalk.grey(current + "/" + total) }\r`);
},
onEnd: async function(){
io.out.write(eol);
},
_stage: ""
};
const getSearchParams = async (param) => (Object.assign({}, param, {
search: cli.args.length > 0 ? cli.args.join(" ") :
await ask("What do you want to search for?", 'string', chalk.green),
len: cli.num || await ask("How many sentences do you need?", 'number'),
useObfuscation: cli.syn ||
await ask(`${ chalk.yellow("(Experimental)") } Automatically replace the words with its synonyms?`, 'boolean'),
io, ask, notice, highlight, status, stageHandler
}));
getSearchParams({})
.then(create)
.then(obfuscate)
.then(async p => {
io.out.write(`[*] ${ chalk.green("Generating Paragraph...") }${eol}`);
await ioWriteStore(`Title: ${ p.params.search + eol }`);
await ioWriteStore(`${eol}${ p.paragraph.join(" ") }${eol+eol}`);
io.out.write(`[*] ${ chalk.green("Done!") }${eol}`)
})
.catch(e => console.error("Error while processing: " + e.message, e))
.then(ioCleanup)
.then(() => (process.exit(0)));