-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
executable file
·62 lines (54 loc) · 1.79 KB
/
app.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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const { loadDb } = require('./src/db.js');
const colors = require('colors');
const { searchBookmarks, presentResults, presentAlfredResults } = require('./src/search-bookmarks.js');
const cacheBookmarks = require('./src/cache-bookmarks.js');
const configure = require('./src/configure.js');
const args = process.argv.slice(2);
let searchTerm = args.join(' ').trim();
let isAlfred = false;
if (searchTerm === '') {
console.error('Please specify a search term or --cache or --configure');
process.exit(1);
}
loadDb()
.then(() => {
if (searchTerm === '--configure') {
return configure().then(() => {
console.log(colors.green("Configured successfully. Don't forget to run rds --cache"));
process.exit(0);
});
}
if (searchTerm.indexOf('--alfred') === 0) {
isAlfred = true;
searchTerm = searchTerm.replace('--alfred', '').trim();
}
if (!fs.existsSync(path.resolve(__dirname, 'config.json'))) {
const err = new Error('Please configure first with rds --configure');
err.title = 'Configuration required';
err.subtitle = 'Run the following on the terminal: rds --configure';
throw err;
}
if (searchTerm === '--cache') {
return cacheBookmarks().then(() => {
console.log('Cache successful');
process.exit(0);
});
}
return searchBookmarks(searchTerm).then((results) => {
if (isAlfred) {
return presentAlfredResults(results);
}
return presentResults(results, searchTerm);
});
})
.catch((err) => {
if (isAlfred) {
console.log(JSON.stringify({ items: [{ title: err.title, subtitle: err.subtitle }] }));
} else {
console.error(err.message);
}
process.exit(1);
});