-
Notifications
You must be signed in to change notification settings - Fork 4
/
sider.js
executable file
·98 lines (73 loc) · 2.63 KB
/
sider.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
#!/usr/bin/env node
const { processArgv } = require('./commands/main/usage');
const [, , ...rest] = process.argv;
const [subcommand] = rest;
function getBashRcAndSiderLine() {
const fsExtra = require('fs-extra');
const untiltdify = require('untildify');
const bashRcPath = untiltdify('~/.bashrc');
let fileContents;
try {
fileContents = fsExtra.readFileSync(bashRcPath, 'utf-8');
} catch (e) {
console.error(`Could not read ~/.bashrc.`);
console.error(`Create it first if it does not exists. Otherwise check it's read permissions.`);
console.error(`To manually enable completion, run the line:`);
console.error(`. <(sider completion)`);
process.exit(1);
}
const splitBashRcLines = fileContents.split('\n');
const siderCompletionLine = splitBashRcLines.findIndex(line => line.match(/\s*\.\s+<\(sider\s+completion\)\s*$/));
return { siderCompletionLine, splitBashRcLines };
}
function writeBashRc(splitFileContents) {
const fsExtra = require('fs-extra');
const untiltdify = require('untildify');
const bashRcPath = untiltdify('~/.bashrc');
try {
fsExtra.writeFileSync(bashRcPath, splitFileContents.join('\n'));
} catch (e) {
console.error(`Could not read ~/.bashrc.`);
console.error(`Create it first if it does not exists. Otherwise check it's write permissions.`);
console.error(`To manually enable completion, run the line:`);
console.error(`. <(sider completion)`);
process.exit(1);
}
}
switch (subcommand) {
case 'completion': {
const fsExtra = require('fs-extra');
const path = require('path');
const completion = fsExtra.readFileSync(path.join(__dirname, 'completion.sh'), 'utf-8');
console.log(completion);
break;
}
case 'install-completion': {
const { siderCompletionLine, splitBashRcLines } = getBashRcAndSiderLine();
if (siderCompletionLine !== -1) {
console.error('Completion already installed - forgot to source it?');
process.exit(1);
}
const bashRcLine = `. <(sider completion)`;
splitBashRcLines.push(bashRcLine);
writeBashRc(splitBashRcLines);
console.log("Completion installed, don't forget to close this terminal or reload your .bashrc");
break;
}
case 'uninstall-completion': {
const { siderCompletionLine, splitBashRcLines } = getBashRcAndSiderLine();
if (siderCompletionLine === -1) {
console.error('Completion not installed - cannot remove it.');
process.exit(1);
}
splitBashRcLines.splice(siderCompletionLine, 1);
writeBashRc(splitBashRcLines);
break;
}
case 'migrate': {
require('./migrations/index');
break;
}
default:
processArgv(rest);
}