-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
94 lines (84 loc) · 2.07 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
#!/usr/bin/env node
const fs = require('fs');
const Fuse = require('fuse.js');
const inq = require('inquirer');
const path = require('path');
const shell = require('shelljs');
const clipboardy = require('clipboardy');
const argv = require('yargs')
.demandCommand(1)
.help('help', 'Show this message and exit.')
.option('f', {
type: 'string',
demandOption: true,
nargs: 1,
desc: 'Task file to search in.'
})
.usage('usage: $0 -f <task_file> <query> [--help]\n\n'
+ 'Searches for command syntax from natural language query.')
.argv;
var taskFileName = argv.f;
var tasks = JSON.parse(fs.readFileSync(taskFileName));
var allTasks = [];
var traverseTask = function (task) {
task.value = allTasks.length;
allTasks.push(task);
if (!task.subtasks) { return; }
task.subtasks.forEach(traverseTask);
};
tasks.forEach(traverseTask);
var query = argv._.join(' ');
var fuse = new Fuse(allTasks, {
keys: [
{ name: 'name', weight: 0.6 },
{ name: 'keywords', weight: 0.4 }
],
sort: true,
tokenize: true
});
var taskMatches = fuse.search(query);
var ask = function (lastAnswers) {
var task = allTasks[lastAnswers['task']];
if (!task.subtasks) {
console.log(task.cmdText);
if (task.numParams === 0) {
inq.prompt({
type: 'confirm',
name: 'run',
message: 'Run?',
default: true
})
.then(function (answers) {
if (answers['run']) shell.exec(task.cmdText)
});
} else {
inq.prompt({
type: 'confirm',
name: 'copy',
message: 'Copy?',
default: true
})
.then(function (answers) {
if (answers['copy']) {
clipboardy.writeSync(task.cmdText + ' # ' + task.name);
}
})
.then( () => process.exit() );
}
} else {
return (inq.prompt({
type: 'list',
name: 'task',
message: 'Select subtask',
pageSize: 5,
choices: task.subtasks})
.then(ask));
}
};
inq.prompt({
type: 'list',
name: 'task',
message: 'Select task',
pageSize: 3,
choices: taskMatches })
.then(ask);