-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcli.js
executable file
·107 lines (93 loc) · 2.36 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
#!/usr/bin/env node
'use strict';
const meow = require('meow');
const ora = require('ora');
const chalk = require('chalk');
const clipboardy = require('clipboardy');
const inquirer = require('inquirer');
const { retrieve, search, sources } = require('./');
const MAX_ARTICLES = 10;
const validSources = Object.keys(sources)
.map(s => s.toLowerCase())
.join(', ');
const cli = meow(
`
Usage:
$ bibtex-search <query>
Options:
--source, -s Where to find papers from (default: acm) - valid options: [${
validSources
}]
Examples:
$ bibtex-search bayou
$ bibtex-search --source google zaharia spark
`,
{
flags: {
source: {
type: 'string',
default: 'acm',
alias: 's'
}
}
}
);
function buildQuestions(articles) {
const choices = articles.map(({ id, title, authors }, i) => ({
value: id,
name: `${title} ${chalk.dim(`(${authors})`)}`
}));
return [
{
choices,
pageSize: Infinity,
type: 'list',
name: 'article',
message: 'Which article are you looking for?'
}
];
}
async function main() {
const query = cli.input.join(' ');
const source = cli.flags.source.toUpperCase();
if (!query) cli.showHelp();
if (!sources[source]) {
const symbol = chalk.yellow('⚠');
console.log(`${symbol} Valid sources are: ${validSources}`);
process.exit(1);
}
const spinner = ora(`Searching for '${query}'`).start();
let articles;
try {
articles = await search(source, query);
articles = articles.slice(0, MAX_ARTICLES);
spinner.stop();
} catch (e) {
spinner.fail(`Something went wrong while searching: ${e}`);
process.exit(1);
}
if (!articles.length) {
spinner.info(`No results found for query '${query}'.`);
process.exit(0);
}
const questions = buildQuestions(articles);
const { article } = await inquirer.prompt(questions);
spinner.start('Retrieving BibTeX reference');
let reference;
try {
reference = await retrieve(source, article);
} catch (e) {
spinner.fail(`Something went wrong while retrieving reference: ${e}`);
process.exit(1);
}
spinner.stop();
console.log(reference);
try {
clipboardy.writeSync(reference);
spinner.succeed('Copied to clipboard!');
} catch (_e) {
// Ignore clipboard related errors - we've already
// printed the reference nonetheless.
}
}
main();