-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
90 lines (77 loc) · 2.57 KB
/
index.js
File metadata and controls
90 lines (77 loc) · 2.57 KB
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
#!/usr/bin/env node
const { program } = require('commander');
const { spawnSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const os = require('os');
async function reviewProposal(input, option) {
try {
const pythonScript = path.join(__dirname, 'evaluator.py');
let filePath = null;
if (option === '-f') {
const validExtensions = ['.pdf', '.docx', '.txt'];
const fileExt = path.extname(input).toLowerCase();
if (!fs.existsSync(input) || !validExtensions.includes(fileExt)) {
throw new Error('Please provide a valid PDF or DOCX file path');
}
filePath = input;
} else if (option === '-t') {
// Write the text to a temporary file
const tempDir = os.tmpdir();
const tempFileName = `proposal_${Date.now()}.txt`;
filePath = path.join(tempDir, tempFileName);
fs.writeFileSync(filePath, input, 'utf-8');
} else {
throw new Error('Invalid option. Use -f for file path or -t for text.');
}
const result = spawnSync('python', [pythonScript, filePath], {
encoding: 'utf-8'
});
// Clean up the temporary file if it was created
if (option === '-t' && fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
if (result.error) {
throw new Error(`Error executing Python script: ${result.error.message}`);
}
if (result.status !== 0) {
throw new Error(`Python script exited with status ${result.status}: ${result.stderr}`);
}
return JSON.parse(result.stdout);
} catch (error) {
console.error('Error:', error.message);
throw error; // Re-throw the error for the caller to handle
}
}
// CLI Logic
if (require.main === module) {
program
.name('proposal-reviewer')
.description('CLI tool to evaluate research proposals')
.version('1.0.0')
.argument('<input>', 'File path or text to evaluate')
.option('-f, --file', 'Input is a file path')
.option('-t, --text', 'Input is text content')
.action(async (input, options) => {
let option = null;
if (options.file) {
option = '-f';
} else if (options.text) {
option = '-t';
} else {
console.error('Please specify either -f for file path or -t for text.');
program.help();
}
try {
const evaluation = await reviewProposal(input, option);
console.log(JSON.stringify(evaluation, null, 2));
} catch (error) {
// Error already logged in reviewProposal
process.exit(1);
}
});
program.parse();
}
module.exports = {
reviewProposal
};