-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
160 lines (155 loc) · 4.62 KB
/
utils.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const clc = require('cli-color');
const synonymMap = {
angular: ['angularjs', 'ng'],
'create-next-app': ['next', 'nextjs', 'cna'],
'create-react-app': ['cra'],
'create-web3js-app': ['cwa'],
vite: ['vitejs'],
preact: ['preactjs'],
lit: ['lit-element'],
react: ['reactjs'],
typescript: ['ts', 'type'],
js: ['vanilla', 'vanillajs'],
vue: ['vuejs']
};
const CMD_ANGULAR = `npx -p @angular/cli@latest ng new $name`;
const CMD_ANGULAR_COLOR = `npx -p ${clc.yellow('angular')}/cli@latest ng new $name`;
const CMD_NEXT = 'npx create-next-app $name --use-npm';
const CMD_NEXT_COLOR = `npx ${clc.yellow('create-next-app')} $name --use-npm`;
const CMD_CRA = 'npx create-react-app $name';
const CMD_CRA_COLOR = `npx ${clc.yellow('create-react-app')} $name`;
const CMD_CWA = 'npx create-web3js-app $name';
const CMD_CWA_COLOR = `npx ${clc.yellow('create-web3js-app')} $name`;
const CMD_VITE = 'npm init vite@latest $name -- --template';
const CMD_VITE_COLOR = `npm init ${clc.yellow('vite')}@latest $name -- --template`;
const commandArr = [
{
command: `${CMD_ANGULAR}`,
colorCommand: `${CMD_ANGULAR_COLOR}`,
keywords: ['angular']
},
{
command: `${CMD_NEXT}`,
colorCommand: `${CMD_NEXT_COLOR}`,
keywords: ['create-next-app', 'react']
},
{
command: `${CMD_NEXT} --ts`,
colorCommand: `${CMD_NEXT_COLOR} --ts`,
keywords: ['create-next-app', 'react', 'typescript']
},
{
command: `${CMD_CRA}`,
colorCommand: `${CMD_CRA_COLOR}`,
keywords: ['create-react-app', 'react']
},
{
command: `${CMD_CRA} --template typescript`,
colorCommand: `${CMD_CRA_COLOR} --template typescript`,
keywords: ['create-react-app', 'react', 'typescript']
},
{
command: `${CMD_VITE} vanilla`,
colorCommand: `${CMD_VITE_COLOR} vanilla`,
keywords: ['vite', 'vanilla']
},
{
command: `${CMD_VITE} vanilla-ts`,
colorCommand: `${CMD_VITE_COLOR} vanilla-ts`,
keywords: ['vite', 'vanilla', 'typescript']
},
{
command: `${CMD_VITE} lit-element`,
colorCommand: `${CMD_VITE_COLOR} lit-element`,
keywords: ['vite', 'lit-element']
},
{
command: `${CMD_VITE} lit-element-ts`,
colorCommand: `${CMD_VITE_COLOR} lit-element-ts`,
keywords: ['vite', 'lit-element', 'typescript']
},
{
command: `${CMD_VITE} react`,
colorCommand: `${CMD_VITE_COLOR} react`,
keywords: ['vite', 'react']
},
{
command: `${CMD_VITE} react-ts`,
colorCommand: `${CMD_VITE_COLOR} react-ts`,
keywords: ['vite', 'react', 'typescript']
},
{
command: `${CMD_VITE} preact`,
colorCommand: `${CMD_VITE_COLOR} preact`,
keywords: ['vite', 'preact']
},
{
command: `${CMD_VITE} preact-ts`,
colorCommand: `${CMD_VITE_COLOR} preact-ts`,
keywords: ['vite', 'preact', 'typescript']
},
{
command: `${CMD_VITE} svelte`,
colorCommand: `${CMD_VITE_COLOR} svelte`,
keywords: ['vite', 'svelte']
},
{
command: `${CMD_VITE} svelte-ts`,
colorCommand: `${CMD_VITE_COLOR} svelte-ts`,
keywords: ['vite', 'svelte', 'typescript']
},
{
command: `${CMD_VITE} vue`,
colorCommand: `${CMD_VITE_COLOR} vue`,
keywords: ['vite', 'vue']
},
{
command: `${CMD_VITE} vue-ts`,
colorCommand: `${CMD_VITE_COLOR} vue-ts`,
keywords: ['vite', 'vue', 'typescript']
},
{
command: `${CMD_CWA}`,
colorCommand: `${CMD_CWA_COLOR}`,
keywords: ['create-web3js-app', 'web3', 'web3js', 'hardhat', 'metamask']
}
];
commandArr.forEach((item) => {
item.allKeywords = [];
for (const kw of item.keywords) {
item.allKeywords.push(kw);
if (synonymMap[kw]) {
item.allKeywords = [...item.allKeywords, ...synonymMap[kw]];
}
}
});
export function getScore(search, cmdItem, partialMatch = false) {
let score = 0;
for (let idx = 0; idx < cmdItem.allKeywords.length; idx++) {
const kw = cmdItem.allKeywords[idx];
if (kw.toLowerCase() === search) {
score = score + 1;
score = score - idx * 0.1; // reduce score if it matches at lower index (in the case of same score).
}
}
return score;
}
export function match(appName, searchKeys) {
const matches = [];
// const searchKeys = search.split(' ');
for (const cmdItem of commandArr) {
let totalScore = 0;
for (const searchKeyword of searchKeys) {
totalScore += getScore(searchKeyword.toLowerCase(), cmdItem);
}
matches.push({ ...cmdItem, totalScore });
matches.sort((a, b) => b.totalScore - a.totalScore);
}
matches.map((item) => {
item.command = item.command.replace(/\$name/, appName);
// console.log(item.command);
});
return matches;
}