-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
130 lines (117 loc) · 3.19 KB
/
index.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* @since 2019-08-14 02:43
* @author vivaxy
*/
import path from 'node:path';
import alfy from 'alfy';
import { getProjectDirectories } from './utils/get-project-directories.js';
const PROJECT_CACHE_KEY = 'projects';
async function updateProjectsCache() {
const projectsDirectories = await getProjectDirectories(process.env.projects);
const projects = projectsDirectories.map(function(directory) {
return {
name: path.basename(directory),
absolutePath: directory,
};
});
alfy.cache.set(PROJECT_CACHE_KEY, JSON.stringify(projects));
}
/**
* @returns {Array<{ name: string, absolutePath: string }>}
*/
function getProjects() {
const projectsCache = /** @type {string} */(alfy.cache.get(PROJECT_CACHE_KEY));
if (projectsCache) {
return JSON.parse(projectsCache);
}
return [];
}
/**
* @typedef {(value: string, input: string) => boolean} SearchStrategy
*/
/**
*
* @type {{matchFromStart: SearchStrategy, keywordIncludes: SearchStrategy, matchIncludes: SearchStrategy}}
*/
const searchStrategies = {
matchFromStart(value, input) {
function format(v) {
return v.toLowerCase().replace(/[^a-z0-9]/g, '');
}
return format(value).startsWith(format(input));
},
matchIncludes(value, input) {
function format(v) {
return v.toLowerCase().replace(/[^a-z0-9]/g, '');
}
return format(value).includes(format(input));
},
keywordIncludes(value, input) {
const keywords = input
.replace(/[^a-z0-9]/g, ' ')
.split(' ')
.filter(function(v) {
return !!v;
});
return keywords.every((keyword) => {
return searchStrategies.matchIncludes(value, keyword);
});
},
};
function debug() {
alfy.output([
{
title: 'Debug alfred-open-in-vscode',
subtitle: `node.version=${process.version}`,
},
]);
}
/**
* @param {Array<{name: string, absolutePath: string}>} projects
* @param {string} input
* @returns {Array<{name: string, absolutePath: string}>}
*/
function search(projects, input) {
if (input) {
const searchResultsByStrategy = [];
const searchStrategyNames = [
'matchFromStart',
'matchIncludes',
'keywordIncludes',
];
projects.forEach(function (project) {
for (let i = 0; i < searchStrategyNames.length; i++) {
const searchStrategy = searchStrategies[searchStrategyNames[i]];
if (searchStrategy(project.name, alfy.input)) {
searchResultsByStrategy[i] = searchResultsByStrategy[i] || [];
searchResultsByStrategy[i].push(project);
return;
}
}
});
return searchResultsByStrategy.reduce(function (all, searchResults) {
return [...all, ...searchResults];
}, []);
}
return projects;
}
function main() {
if (alfy.input === 'DEBUG') {
return debug();
}
const projects = getProjects();
const searchResults = search(projects, alfy.input);
const output = searchResults.map(function ({ name, absolutePath }) {
return {
title: name,
uid: absolutePath,
subtitle: absolutePath,
arg: absolutePath,
autocomplete: name,
type: /** @type {'file'} */('file'),
};
});
alfy.output(output, { rerunInterval: 1 });
updateProjectsCache();
}
main();