-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaperture.js
executable file
·190 lines (158 loc) · 4.23 KB
/
aperture.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env node
var EventEmitter = require('events').EventEmitter
var config = require('./commands/config')
var resolve = require('path').resolve
var optimist = require('optimist')
var chalk = require('chalk')
var fs = require('fs')
var commands = {}
var argv = optimist
.describe('b', 'Exit early on reaching an error during "aperture bulk".')
.alias('b', 'bail')
.boolean('b')
.describe('v', 'Output the current version and exit')
.alias('v', 'version')
.boolean('v')
.describe('d', 'Target a different directory for this command. Default: current directory')
.alias('d', 'cwd')
.wrap(65)
.argv
var cwd = resolve(argv.cwd || process.cwd())
defineCommands()
var command = argv._.shift()
if (argv.version) command = 'version'
if (!command) return help()
if (!commands[command]) return help()
config(cwd, function(err, config) {
if (err) throw err
var events = new EventEmitter
commands[command](cwd
, config
, events
, function(err) {
if (err) throw err
})
})
function help() {
var usage = fs.readFileSync(
__dirname + '/usage.txt', 'utf8'
).trim()
optimist
.usage(usage)
.showHelp()
}
function defineCommands() {
commands.ln =
commands.link = function(root, config, events, done) {
// require commands directly to shave startup time.
require('./commands/link')(
root
, config
, events.on('link', log)
, done
)
function log(src) {
console.log(src)
}
}
commands.dedupe =
commands.purge = function(root, config, events, done) {
require('./commands/purge')(
root
, config
, events.on('queued', console.log)
, done
)
}
commands.isntall =
commands.install = function(root, config, events, done) {
console.log('checking package versions...')
require('./commands/install')(
root
, config
, events.on('info progress', function(p) {
process.stdout.write(((p * 100)|0) + '% \r')
}).on('spawn', function(cwd, cmd, args) {
console.log(chalk.magenta('spawning'), cmd, args, chalk.grey(cwd))
})
, done
)
}
commands.each =
commands.bulk = function(root, config, events, done) {
config.bail = 'bail' in argv
? argv.bail
: config.bail
config.bulk = {
command: argv._[0]
, args: argv._.slice(1)
}
if (!config.bulk.command) return done(new Error(
'You must supply bulk command with a ' +
'command object.'
))
require('./commands/bulk')(
root
, config
, events
, function(err, info) {
if (err) throw err
if (info.failed.length) process.exit(1)
}
)
}
commands.init =
commands.open = function(root, config, events, done) {
var prefix = chalk.green('aperture')
events.on('link', function(mod) {
console.log(prefix, chalk.magenta('linking module'), mod)
})
events.on('queued', function(mod) {
console.log(prefix, chalk.magenta('removing duplicate'), mod)
})
events.on('spawn', function(cwd, cmd, args) {
console.log(prefix, chalk.magenta('spawning'), cmd, args, chalk.grey(cwd))
})
events.once('info progress', function(p) {
console.log(chalk.magenta('checking registry'))
}).on('info progress', function(p) {
process.stdout.write(chalk.green('progress: '))
process.stdout.write(String((p * 100)|0))
process.stdout.write('% \r')
})
require('./commands/open')(
root
, config
, events
, done
)
}
commands.version = function() {
console.log(require('./package.json').version)
}
commands.ls =
commands.list = function(root, config, events, done) {
require('./commands/list')(
root
, config
, function(err, modules) {
if (err) throw err
modules = modules.map(function(mod) {
return mod.directory
})
console.log(modules.join('\n'))
}
)
}
commands.config = function(root, config, events, done) {
console.log(JSON.stringify(config, null, 2))
}
commands.expand = function(root, config, events, done) {
require('./commands/expand')(
root
, config
, events
, done
)
}
}