/
console.coffee
executable file
·356 lines (291 loc) · 7.71 KB
/
console.coffee
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# Requires
{cliColor} = require('caterpillar')
# Console Interface
class ConsoleInterface
# Setup the CLI
constructor: (opts,next) ->
# Prepare
consoleInterface = @
@docpad = docpad = opts.docpad
@commander = commander = require('commander')
# Version information
version = require(__dirname+'/../../../package.json').version
# -----------------------------
# Global config
commander
.version(version)
.option(
'-e, --env <environment>'
"the environment name to use for this instance, multiple names can be separated with a comma"
)
.option(
'-d, --debug [logLevel]'
"the level of debug messages you would like to display, if specified defaults to 7, otherwise 6"
parseInt
)
.option(
'-f, --force'
"force a re-install of all modules"
)
# -----------------------------
# Commands
# run
commander
.command('run')
.description('does everyting: skeleton, generate, watch, server')
.option(
'-s, --skeleton <skeleton>'
"for new projects, instead of being asked for the skeleton, you can specify it here"
)
.option(
'-p, --port <port>'
"a custom port to use for the server <port>"
parseInt
)
.action(consoleInterface.wrapAction(consoleInterface.run))
# server
commander
.command('server')
.description('creates a server for your generated project')
.option(
'-p, --port <port>'
"a custom port to use for the server <port>"
parseInt
)
.action(consoleInterface.wrapAction(consoleInterface.server))
# skeleton
commander
.command('skeleton')
.description('will create a new project in your cwd based off an existing skeleton')
.option(
'-s, --skeleton <skeleton>'
"instead of being asked for the skeleton, you can specify it here"
)
.action(consoleInterface.wrapAction(consoleInterface.skeleton))
# render
commander
.command('render [path]')
.description("render the file at <path> and output its results to stdout")
.action(consoleInterface.wrapAction(consoleInterface.render))
# generate
commander
.command('generate')
.description("(re)generates your project")
.action(consoleInterface.wrapAction(consoleInterface.generate))
# watch
commander
.command('watch')
.description("watches your project for changes, and (re)generates whenever a change is made")
.action(consoleInterface.wrapAction(consoleInterface.watch))
# install
commander
.command('install')
.description("ensure everything is installed correctly")
.action(consoleInterface.wrapAction(consoleInterface.install))
# clean
commander
.command('clean')
.description("ensure everything is cleaned correctly (will remove your out directory)")
.action(consoleInterface.wrapAction(consoleInterface.clean))
# info
commander
.command('info')
.description("display the information about your docpad instance")
.action(consoleInterface.wrapAction(consoleInterface.info))
# help
commander
.command('help')
.description("output the help")
.action(consoleInterface.wrapAction(consoleInterface.help))
# unknown
commander
.command('*')
.description("anything else ouputs the help")
.action ->
commander.emit('help', [])
# -----------------------------
# Finish Up
# Plugins
docpad.emitSync 'consoleSetup', {consoleInterface,commander}, (err) ->
return consoleInterface.handleError(err) if err
next(null,consoleInterface)
# Chain
@
# =================================
# Helpers
# Start the CLI
start: (argv) =>
@commander.parse(argv or process.argv)
@
# Get the commander
getCommander: =>
@commander
# Handle Error
handleError: (err) =>
# Prepare
docpad = @docpad
# Handle
docpad.log('error', "Something went wrong with the action")
docpad.error(err)
process.exit(1)
# Chain
@
# Wrap Action
wrapAction: (action) =>
consoleInterface = @
return (command) -> consoleInterface.performAction(command,action)
# Perform Action
performAction: (command,action) =>
# Create
instanceConfig = @extractConfig(command)
@docpad.action 'load ready', instanceConfig, (err) =>
return @completeAction(err) if err
action(@completeAction)
# Chain
@
# Complete an anction
completeAction: (err) =>
# Handle the error
if err
@handleError(err)
else
@docpad.log('info', "The action completed successfully")
# Chain
@
# Extract Configuration
extractConfig: (customConfig={}) =>
# Prepare
config = {}
commanderConfig = @commander
sourceConfig = @docpad.initialConfig
# Rename special configuration
if commanderConfig.debug
commanderConfig.debug = 7 if commanderConfig.debug is true
commanderConfig.logLevel = commanderConfig.debug
# Apply global configuration
for own key, value of commanderConfig
if sourceConfig[key]?
config[key] = value
# Apply custom configuration
for own key, value of customConfig
if sourceConfig[key]?
config[key] = value
# Return config object
config
# Select a skeleton
selectSkeletonCallback: (skeletonsCollection,next) =>
# Prepare
commander = @commander
docpad = @docpad
skeletonNames = []
# Show
console.log cliColor.bold '''
You are about to create your new project inside your current directory. Below is a list of skeletons to bootstrap your new project:
'''
skeletonsCollection.forEach (skeletonModel) ->
skeletonName = skeletonModel.get('name')
skeletonDescription = skeletonModel.get('description').replace(/\n/g,'\n\t')
skeletonNames.push(skeletonName)
console.log """
\t#{cliColor.bold(skeletonName)}
\t#{skeletonDescription}
"""
# Select
console.log cliColor.bold '''
Which skeleton will you use?
'''
commander.choose skeletonNames, (i) ->
return next(null, skeletonsCollection.at(i))
# Chain
@
# =================================
# Actions
generate: (next) =>
@docpad.action('generate',next)
@
help: (next) =>
help = @commander.helpInformation()
console.log(help)
next()
@
info: (next) =>
info = require('util').inspect(@docpad.config)
console.log(info)
next()
@
install: (next) =>
@docpad.action('install',next)
@
render: (next) =>
# Prepare
docpad = @docpad
docpad.setLogLevel(5) unless docpad.getLogLevel() is 7
commander = @commander
opts = {}
# Prepare filename
filename = commander.args[0] or null
if !filename or filename.split('.').length <= 2 # [name,ext,ext] = 3 parts
opts.renderSingleExtensions = true
opts.filename = filename
# Prepare text
opts.data = ''
# Render
useStdin = true
renderDocument = ->
docpad.action 'render', opts, (err,result) ->
return docpad.fatal(err) if err
process.stdout.write(result+'\n')
next()
# Timeout if we don't have stdin
timeout = setTimeout(
->
# Clear timeout
timeout = null
# Skip if we are using stdin
return if opts.data.replace(/\s+/,'')
# Close stdin as we are not using it
useStdin = false
stdin.pause()
# Render the document
renderDocument()
,1000
)
# Read stdin
stdin = process.stdin
stdin.resume()
stdin.setEncoding('utf8')
stdin.on 'data', (data) ->
opts.data += data.toString()
process.stdin.on 'end', ->
return unless useStdin
if timeout
clearTimeout(timeout)
timeout = null
renderDocument()
@
run: (next) =>
@docpad.action(
'all'
{selectSkeletonCallback: @selectSkeletonCallback}
next
)
@
server: (next) =>
@docpad.action('server',next)
@
clean: (next) =>
@docpad.action('clean',next)
@
skeleton: (next) =>
@docpad.action(
'skeleton'
{selectSkeletonCallback: @selectSkeletonCallback}
next
)
@
watch: (next) =>
@docpad.action('watch',next)
@
# =================================
# Export
module.exports = ConsoleInterface