-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·56 lines (48 loc) · 1.24 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
#!/usr/bin/env node
'use strict';
var inquirer = require('inquirer');
var os = require('os');
var config = require((os.homedir()+'/.dirx/config.json'));
var args = process.argv;
var dirs = args.slice(2);
var defalutStrategy = config.defalutStrategy;
dirs = dirs.map(function (element) {
var parts = element.split(/#|@/);
return {
dirname: parts[0],
frequency: parts[1],
accessTime: parts[2]
};
});
// Descending sort by frequency
function sortByFrequencey (dirA, dirB) {
if (dirA.frequency < dirB.frequency) return 1;
if (dirA.frequency > dirB.frequency) return -1;
return 0;
}
// Descending sort by accessTime
function sortByAccessTime (dirA, dirB) {
if (dirA.accessTime < dirB.accessTime) return 1;
if (dirA.accessTime > dirB.accessTime) return -1;
return 0;
}
var strategies = {
frequency: sortByFrequencey,
accessTime: sortByAccessTime
};
var choices = dirs.sort(strategies[defalutStrategy])
.map(function(element) {
return element.dirname;
});
inquirer
.prompt([
{
type: 'list',
name: 'cd',
message: "Which directory to change into?",
paginated: true,
choices: choices
}])
.then(answers => {
console.error(answers.cd);
});