-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.coffee
More file actions
executable file
·129 lines (103 loc) · 2.81 KB
/
app.coffee
File metadata and controls
executable file
·129 lines (103 loc) · 2.81 KB
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
fs = require 'fs'
touch = require 'touch'
path = require 'path'
pretty = require 'prettyjson'
mkdirp = require 'mkdirp'
CWD = process.cwd()
DIR = CWD.split('/').pop()
args = require('yargs')
.usage 'Usage: $0 <command> [options] or $0 <directory>'
.wrap null
.option 'a',
describe: 'Add the current directory to the alias list'
type: 'boolean'
alias: 'add'
.option 's',
describe: 'Add the given server to the alias list'
type: 'string'
alias: 'server'
.implies 's', 'as'
.option 'as',
describe: 'Add the current directory to the alias list under the specified alias'
example: '$0 -a --as foo', 'Add the current directory as "foo"'
type: 'string'
.option 'd',
describe: 'Remove the given alias'
type: 'boolean'
alias: ['delete', 'r', 'remove']
.help 'h'
.alias 'h', 'help'
.argv
FILE = "#{process.env.HOME}/.jumprc"
COMPLETIONS_DIR = "#{process.env.HOME}/.jump_completions"
COMPLETIONS_FILE = "#{process.env.HOME}/.jump_completions/_jump"
escape = (str) -> "\\\"#{str}\\\""
read = -> JSON.parse (fs.readFileSync(FILE, 'utf8') or '{}')
write = (paths) ->
completions = Object.keys(paths).map(escape).join(' ')
fs.writeFileSync FILE, JSON.stringify(paths)
fs.writeFileSync COMPLETIONS_FILE, "#compdef jump\n\n_arguments \"1: :(#{completions})\""
# Make sure the file exists so we don't blow up
touch.sync FILE
mkdirp.sync COMPLETIONS_DIR
touch.sync COMPLETIONS_FILE
data = read()
add = (dir=DIR) ->
data[dir] = CWD
write data
console.log "Added `#{CWD}` as `#{dir}`"
addServer = (server, alias) ->
unless server?
"Sorry, you must provide a server to alias (e.g. `j -s foo --as bar`)"
else
data[alias] = '$' + server
write data
console.log "Added `#{server}` as `#{alias}`"
remove = (target) ->
if target is true
target = args._
if !data[target]
console.log "`#{target}` is not a current alias"
else
old = data[target]
delete data[target]
write data
console.log "Removed `#{target}`:`#{old}`"
list = ->
if Object.keys(data).length is 0
console.log "You don't have any aliases yet."
console.log "Why don't you try adding one: `j -a`"
else
out = pretty.render data#, {noColor: true}
console.log out
match = (dir) ->
matches = Object.keys(data).filter (k) ->
k.indexOf(dir) is 0
data[matches[0]]
jump = (dir) ->
target = data[dir]
matched = match dir
if target
if target[0] is '$'
process.stdout.write target
else
process.stdout.write "%#{target}"
else if matched
if matched[0] is '$'
process.stdout.write matched
else
process.stdout.write "%#{matched}"
else
console.log "`#{dir}` is not a currently defined alias"
if args.l
list()
else if args.a
add args.as
else if args.s
addServer args.s, args.as
else if args.d
remove args.d
else if args.r
remove args.r
else
jump args._