-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
124 lines (104 loc) · 3.17 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
#!/usr/bin/env node
const inquirer = require('inquirer');
const autocomplete = require('inquirer-autocomplete-prompt');
const fs = require('fs-extra');
const { exec } = require('child_process');
const path = require('path');
const meow = require('meow');
const fuzzy = require('fuzzy');
inquirer.registerPrompt('autocomplete', autocomplete);
(async () => {
const [allBranches = [], history = [], remotes = []] = await Promise.all([
getAllBranches(),
getHistory(),
getRemotes(),
]);
showCli({ allBranches, history, remotes });
})();
const searchBranch = async ({ history, allBranches }, input) => {
if (!input) {
return history.length ? history : allBranches;
}
return fuzzy.filter(input, allBranches).map((x) => x.original);
};
function showCli(data) {
const commands = { createPromt, setup };
const cli = meow(`
Usage
$ git prev
Setup git hook for checkout history
$ git prev setup
`);
const [command = 'createPromt'] = cli.input;
if (command in commands) {
commands[command](data, cli.flags);
} else {
cli.showHelp();
}
}
async function createPromt(data) {
const promts = [
{
type: 'autocomplete',
name: 'branch',
message: 'Select branch',
source: (_, input) => searchBranch(data, input),
pageSize: 15,
},
];
const { remotes } = data;
const regexp = new RegExp(`(${remotes.join('|')})\/`);
const { branch } = await inquirer.prompt(promts);
await execute(`git checkout ${branch.replace(regexp, '')}`);
}
async function setup() {
const root = await getGitRoot();
const hook = await fs.readFile(path.join(__dirname, 'post-checkout'));
await fs.writeFile(path.join(root, 'hooks', 'post-checkout'), hook);
await fs.writeFile(path.join(root, 'checkout-history'), '');
console.info('success');
}
async function getRemotes() {
try {
const stdout = await execute('git remote');
return stdout
.split('\n')
.map((x) => x.trim())
.filter(Boolean);
} catch (e) {
console.warn(e);
return [];
}
}
async function getAllBranches() {
const stdout = await execute('git for-each-ref refs --format="%(refname:short)"');
return stdout
.split('\n')
.map((x) => x.trim())
.filter(Boolean);
}
async function getGitRoot() {
const root = await execute(`git rev-parse --show-toplevel`);
return path.join(root.trim(), '.git');
}
async function getHistory() {
try {
const root = await getGitRoot();
const data = await fs.readFile(path.join(root, 'checkout-history'), 'utf8');
return [
...new Set(
data
.split('\n')
.map((x) => x.trim())
.filter(Boolean)
.reverse(),
),
];
} catch (e) {
console.warn("Can't find checkout history. Please use git prev setup");
return [];
}
}
function execute(command) {
return new Promise((resolve, reject) => exec(command, (err, stdout) => (err ? reject(err) : resolve(stdout))));
}