-
Notifications
You must be signed in to change notification settings - Fork 0
/
alfred-xml-generator.js
78 lines (52 loc) · 1.91 KB
/
alfred-xml-generator.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
import dotenv from 'dotenv'
import xml2js from 'xml2js'
import { similaritySearch } from './similarity-search.js'
import fs from 'fs'
dotenv.config()
/*
convert the similarity search results to alfred format
echo "<?xml version='1.0'?><items>"
"<item uid='$project' arg='$project$unicode_split' query='$query' valid='YES'><title>$project</title><subtitle>Append to Resources.md in this project</subtitle></item>"
echo "</items>"
*/
const convertToAlfred = async (query) => {
try {
let idIndex = 0;
const results = await similaritySearch(query);
const sortedResults = results.sort((a, b) => b.similarity - a.similarity);
const items = sortedResults.map(result => ({
'$': {
uid: idIndex++,
arg: result.text,
query: query,
valid: 'YES',
},
title: [result.text],
subtitle: [`${result.title} - ${result.similarity}`]
}));
const builder = new xml2js.Builder();
const xml = builder.buildObject({ items: { item: items } });
console.log(xml); // alfred will read this from stdout
return results
} catch (err) {
return err
}
}
const writeToLogFile = (query, results) => {
// capture json of query and results in log file
const logPath = process.env.LOG_PATH
const log = {
query,
timestamp: new Date(),
results
}
// get existing log
const existingLog = fs.existsSync(logPath) ? fs.readFileSync(logPath) : ""
fs.writeFileSync(logPath, existingLog + "\n\n" + "```json\n" + JSON.stringify(log, null, 2) + "\n```")
}
(async () => {
// run semantic search on terminal arg (include spaces) and remove the ? at the end
const query = process.argv.slice(2).join(" ").replace("?", "")
const results = await convertToAlfred(query)
writeToLogFile(query, results)
})()