forked from ferrislucas/promptr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
61 lines (51 loc) · 1.63 KB
/
main.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
import readline from 'readline'
import PluginService from './pluginService.js'
import CliState from './cliState.js'
import fs from 'fs/promises'
export default class Main {
static async call() {
CliState.init(process.argv, await this.getVersion())
if (process.argv.length <= 2) {
console.log("Usage: promptr -m (gpt3|gpt4) <input filepath(s)> -o <output filepath> -p \"Cleanup the code in this file\"");
process.exit(-1);
}
// Interactive mode
if (CliState.isInteractive()) {
await this.loopUntilUserExit()
process.exit(0);
}
// Non-interactive mode
const prompt = CliState.getPrompt()
if (!prompt && CliState.getMode() != "execute") {
console.log("No prompt was specified. Please specify a prompt with the --prompt option, or use interactive mode by using the --interactive option.");
process.exit(-1);
}
await PluginService.call(prompt)
process.exit(0)
}
static async loopUntilUserExit() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
while (true) {
let userInput = await this.getUserInput(rl);
if (!userInput) continue
if (userInput == 'exit' || userInput == "\\q") break
await PluginService.call(userInput)
}
rl.close()
}
static async getUserInput(rl) {
return new Promise(resolve => {
rl.question('promptr# ', _input => {
resolve(_input)
})
})
}
static async getVersion() {
const packageJson = await fs.readFile('./package.json', 'utf8')
const packageData = JSON.parse(packageJson)
return packageData.version
}
}