Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 509 lines (445 sloc) 13.947 kB
b18d2ad @andrzejsliwa inital import
authored
1 #!/usr/bin/env coffee
2 #
3 # CoffeeApp - coffee-script wrapper for CouchApp
4 # Copyright 2010 Andrzej Sliwa (andrzej.sliwa@i-tool.eu)
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18
19
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
20 # fast way to imports using pattern matching
21 {existsSync, join, extname} = require 'path'
ed29823 add two config options: makeReleaseVersions: <boolean> and designdocN…
Szaby Grünwald authored
22 {mkdirSync, rmdirSync, readdirSync, writeFileSync, readFileSync, statSync, symlinkSync, unlinkSync} = require 'fs'
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
23 {compile} = require 'coffee-script'
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
24 {exec, spawn} = require 'child_process'
7e78ebd @andrzejsliwa bugfixing, broken generators
authored
25 {print, gets} = require 'util'
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
26 {_} = require 'underscore'
bc57d74 @andrzejsliwa fix for using process.argv
authored
27 {log} = console
28 request = require 'request'
29
30 headers =
31 accept: 'application/json',
32 'content-type': 'application/json'
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
33
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
34 dumps_folder_name = '.dumps'
35 releases_folder_name = '.releases'
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
36
58512a6 @andrzejsliwa fix spell error.
authored
37 #### Command wrapping configuration.
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
38 commandWraps = [
39 {
40 name: 'push',
41 type: 'before',
42 callback: -> grindCoffee()
43 },
44 {
45 name: 'help',
46 type: 'after',
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
47 desc: 'show this message',
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
48 callback: -> help()
49 },
50 {
51 name: 'cgenerate',
52 type: 'before',
53 desc: '[ view | list | show | filter ] generate .coffee versions',
54 callback: -> generate()
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
55 },
56 {
57 name: 'destroy',
58 type: 'before',
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
59 desc: '[ view | list | show | filter ] destroy (remove directory/files also .js files).',
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
60 callback: -> destroy()
7e78ebd @andrzejsliwa bugfixing, broken generators
authored
61 },
62 {
63 name: 'prepare',
64 type: 'before',
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
65 desc: 'prepare (.gitignore...)',
7e78ebd @andrzejsliwa bugfixing, broken generators
authored
66 callback: -> prepare()
67 },
68 {
69 name: 'clean',
70 type: 'before',
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
71 desc: "remove #{releases_folder_name} & #{dumps_folder_name} directories"
7e78ebd @andrzejsliwa bugfixing, broken generators
authored
72 callback: -> clean()
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
73 },
74 {
75 name: 'restore',
76 type: 'before',
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
77 desc: "restore database from #{dumps_folder_name}/last"
78 callback: -> restore()
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
79 }
80 ]
81
82 #### File templates
83
7e78ebd @andrzejsliwa bugfixing, broken generators
authored
84 # .gitignore
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
85 gitIgnore = """
7e78ebd @andrzejsliwa bugfixing, broken generators
authored
86 .DS_Store
87 .couchapprc
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
88 #{dumps_folder_name}/*
89 #{dumps_folder_name}/**/*
90 #{releases_folder_name}/*
91 #{releases_folder_name}/**/*
92 """
7e78ebd @andrzejsliwa bugfixing, broken generators
authored
93
bc57d74 @andrzejsliwa fix for using process.argv
authored
94 # .couchappignore
95 couchAppIgnore = '''
96 [
97 "spec",
98 "features"
99 ]
100 '''
101
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
102 # map function
103 mapCoffee = '''
104 (doc) ->
105 ...
106 '''
107
108 # reduce function
109 reduceCoffee = '''
110 (keys, values, rereduce) ->
111 ...
112 '''
113
114 # list function
115 listCoffee = '''
116 (head, req) ->
117 ...
118 '''
119
120 # show function
121 showCoffee = '''
122 (doc, req) ->
123 ...
124 '''
125
126 # filter function
127 filterCoffee = '''
128 (doc, req) ->
129 ...
130 '''
131
2e99153 @andrzejsliwa small fixes, added docco documentation
authored
132 #### Helper Methods
133
6f6a058 @andrzejsliwa adding version resolving via package.json
authored
134 getVersion = ->
135 readFileSync(join(__dirname, '..', 'package.json')).toString().match(/"version"\s*:\s*"([\d.]+)"/)[1]
136
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
137 showGreatings = ->
138 # Shows greatings ...
6f6a058 @andrzejsliwa adding version resolving via package.json
authored
139 log "CoffeeApp (#{getVersion()}) - simple coffee-script wrapper for CouchApp (http://couchapp.org)"
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
140 log 'http://github.com/andrzejsliwa/coffeeapp\n'
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
141
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
142 spaces = (string, max) ->
143 count = max - string.length
144 result = ""
145 while count > 0
146 result += " "
147 count--
148 result
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
149
2e99153 @andrzejsliwa small fixes, added docco documentation
authored
150 # Zero padding for format '0x'
b18d2ad @andrzejsliwa inital import
authored
151 padTwo = (number) ->
152 result = if number < 10 then '0' else ''
1191b59 @andrzejsliwa small cleaning, more verbose...
authored
153 "#{result}#{number}"
b18d2ad @andrzejsliwa inital import
authored
154
2e99153 @andrzejsliwa small fixes, added docco documentation
authored
155 # Timestamp string based on current date
b18d2ad @andrzejsliwa inital import
authored
156 getTimestamp = ->
2e99153 @andrzejsliwa small fixes, added docco documentation
authored
157 date = new Date
158 date.getFullYear() + padTwo(date.getMonth() + 1) +
159 padTwo(date.getDate()) + padTwo(date.getHours() + 1) +
160 padTwo(date.getMinutes() + 1) + padTwo(date.getSeconds() + 1)
b18d2ad @andrzejsliwa inital import
authored
161
2e99153 @andrzejsliwa small fixes, added docco documentation
authored
162 # Display outputs if presents
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
163 handleOutput = (callbackOk, callbackError) ->
164 (error, stdout, stderr) ->
ecebaf1 @andrzejsliwa bugfixing ... problem with parsing options
authored
165 log(stdout) if stdout.length > 0
166 log(stderr) if stderr.length > 0
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
167 if error != null
ecebaf1 @andrzejsliwa bugfixing ... problem with parsing options
authored
168 callbackError() if callbackError != undefined
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
169 else
ecebaf1 @andrzejsliwa bugfixing ... problem with parsing options
authored
170 callbackOk() if callbackOk != undefined
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
171
172 getConfig = ->
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
173 JSON.parse readFileSync '.couchapprc', 'utf8'
174
175 getDirectories = (currentDir, ignores) ->
176 callback = (name) ->
177 statSync(join currentDir, name).isDirectory()
178 filterDirectory currentDir, callback, ignores
179
180 getFiles = (currentDir, ignores) ->
181 callback = (name) ->
182 !statSync(join currentDir, name).isDirectory()
183 filterDirectory currentDir, callback, ignores
184
185 filterDirectory = (currentDir, callback, ignores) ->
186 list = readdirSync currentDir
187 if ignores
188 list = _.without(list, ignores...)
189 results = _.filter list, callback
1191b59 @andrzejsliwa small cleaning, more verbose...
authored
190
2e99153 @andrzejsliwa small fixes, added docco documentation
authored
191 #### Main Methods
f7d2493 @andrzejsliwa fast hot fix for broken binary files
authored
192
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
193 # Process directory "recursivly", normal files
2e99153 @andrzejsliwa small fixes, added docco documentation
authored
194 # are copied, directories are recreated and .coffee
195 # files are "compiled" to javascript
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
196 processDirectory = (baseDir, destination) ->
197 dirs = [baseDir]
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
198 isError = false
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
199 while (dirs.length > 0)
200 currentDir = dirs.pop()
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
201 subDirs = getDirectories currentDir, ['.git', releases_folder_name, dumps_folder_name]
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
202 _.each subDirs, (dirName) ->
203 dirPath = join currentDir, dirName
204 dirs.push dirPath
205 destDirPath = join destination, join dirPath
206 mkdirSync destDirPath, 0700
207
208
209 files = getFiles currentDir
210 _.each files, (fileName) ->
211 filePath = join currentDir, fileName
212 destFilePath = join destination, filePath
213 if extname(filePath) == ".coffee"
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
214 log " * processing #{filePath}..."
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
215 try
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
216 writeFileSync destFilePath.replace(/\.coffee$/, '.js'),
52e168b @andrzejsliwa fix for using bare option - without wrapping in function
authored
217 compile(readFileSync(filePath, 'utf8'), bare: yes), 'utf8'
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
218 catch error
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
219 log "Compilation Error: #{error.message}\n"
c35c406 @andrzejsliwa small bugfix.
authored
220 isError = true
b18d2ad @andrzejsliwa inital import
authored
221 else
f765114 @andrzejsliwa hotfix for #1 - problem with exec command runned many times.
authored
222 writeFileSync destFilePath, readFileSync(filePath, 'binary'), 'binary'
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
223 !isError
224
2e99153 @andrzejsliwa small fixes, added docco documentation
authored
225 #### Grinding of coffee
226
227 # Starts wrapping push command of couchapps
228 #
229 # each deploy is moved to .releases/[timestamp] directory
230 # with processing coffee-script files and then pushed from
231 # deploy directory.
b18d2ad @andrzejsliwa inital import
authored
232 grindCoffee = ->
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
233 log "Wrapping 'push' of couchapp"
ed29823 add two config options: makeReleaseVersions: <boolean> and designdocN…
Szaby Grünwald authored
234 config = getConfig()
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
235 timestamp = getTimestamp()
0cdadba Fixing default behaviour for the config option makeReleaseVersions.
Szaby Grünwald authored
236 makeReleaseVersions = config['makeReleaseVersions'] isnt false
c5c9c90 designdocName has the default value "app"
Szaby Grünwald authored
237 designdocName = config['designdocName'] or "app"
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
238 releasesDir = releases_folder_name
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
239 unless existsSync releasesDir
240 log "initialize #{releasesDir} directory"
241 mkdirSync releasesDir, 0700
0cdadba Fixing default behaviour for the config option makeReleaseVersions.
Szaby Grünwald authored
242 if makeReleaseVersions
ed29823 add two config options: makeReleaseVersions: <boolean> and designdocN…
Szaby Grünwald authored
243 releasePath = join releasesDir, timestamp
244 else
c5c9c90 designdocName has the default value "app"
Szaby Grünwald authored
245 releasePath = join releasesDir, designdocName
ed29823 add two config options: makeReleaseVersions: <boolean> and designdocN…
Szaby Grünwald authored
246 log "Removing #{releasePath}"
247 rmTreeSync releasePath
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
248
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
249 [options, database] = processOptions()
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
250
ecebaf1 @andrzejsliwa bugfixing ... problem with parsing options
authored
251 log "database : '#{database}'"
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
252 processCallback = ->
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
253 log "preparing release: #{releasePath}"
254 mkdirSync releasePath, 0700
255 if processDirectory '.', releasePath
256 process.chdir releasePath
ecebaf1 @andrzejsliwa bugfixing ... problem with parsing options
authored
257 exec "couchapp #{options} #{database}", handleOutput()
258 process.cwd
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
259
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
260 if config['env'][database]['make_dumps']
261 dumpsDir = join dumps_folder_name, database
262 unless existsSync dumps_folder_name
263 log "initialize #{dumps_folder_name} directory"
264 mkdirSync dumps_folder_name, 0700
265 unless existsSync dumpsDir
266 log "initialize #{dumpsDir} directory"
267 mkdirSync dumpsDir, 0700
268 dumpsPath = join dumpsDir, timestamp
269
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
270 log "making dump: #{dumpsPath}"
271 url = config['env'][database]['db']
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
272 exec "couchdb-dump #{url} > #{dumpsPath}", handleOutput ->
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
273 lastPath = join dumpsDir, 'last'
274 unlinkSync lastPath if existsSync lastPath
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
275 log " * linking dump: #{dumpsPath} -> #{lastPath}"
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
276 symlinkSync timestamp, "#{lastPath}"
277 processCallback()
b05123c @andrzejsliwa hotfix
authored
278 else
279 processCallback()
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
280
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
281 processOptions = ->
bc57d74 @andrzejsliwa fix for using process.argv
authored
282 [options..., database] = process.argv[2..]
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
283 options = (options || []).join ' '
ecebaf1 @andrzejsliwa bugfixing ... problem with parsing options
authored
284 database = "default" if database == undefined
285 if process.argv[2..].length == 1
286 options = database
287 database = "default"
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
288 return [options, database]
b18d2ad @andrzejsliwa inital import
authored
289
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
290 # Shows available options.
291 help = ->
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
292 log "Wrapping 'help' of couchapp\n"
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
293 showGreatings()
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
294
295 usage = '''
296 Usage: coffeeapp [OPTIONS] [CMD] [CMDOPTIONS] [ARGS,...]
297
298 Commands:
299 '''
300 log usage
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
301 _.each commandWraps, (command) ->
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
302 if command.desc
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
303 log " #{command.name}#{spaces(command.name, 9)} [OPTIONS]..."
304 log " #{command.desc}\n"
b18d2ad @andrzejsliwa inital import
authored
305
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
306 # generate file from template verbosly
307 generateFile = (path, template) ->
308 log " * creating #{path}..."
309 if existsSync path
310 log "File #{path} already exist!"
311 else
312 writeFileSync path, template, 'utf8'
f7d2493 @andrzejsliwa fast hot fix for broken binary files
authored
313
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
314
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
315 # cgenerate and destory handling
316 generate = -> operateOn('generate')
317 destroy = -> operateOn('destroy')
318
319 # common handling for cgenerate/destroy
320 operateOn = (command) ->
bc57d74 @andrzejsliwa fix for using process.argv
authored
321 generator = process.argv[2..][1]
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
322 unless generator
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
323 log "missing name of #{command} - [ view | list | show | filter ]"
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
324 return
bc57d74 @andrzejsliwa fix for using process.argv
authored
325 name = process.argv[2..][2]
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
326 unless name
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
327 log 'missing name of element'
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
328 return
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
329 print "Running #{generator} #{command}:\n"
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
330 fun = switch generator
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
331 when 'view'
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
332 handleView
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
333 when 'show'
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
334 handleShow
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
335 when 'list'
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
336 handleList
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
337 when 'filter'
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
338 handleFilter
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
339 else
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
340 (method, name) -> log "unknown #{command}"
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
341 log 'done.' if fun(command, name)
342
343 # handling view generate/destroy
344 handleView = (method, name) ->
345 unless existsSync 'views'
346 mkdirSync 'views', 0700
347
348 viewDirPath = "views/#{name}"
349 [mapFilePath, reduceFilePath] = [join(viewDirPath, "map.coffee"), join(viewDirPath, "reduce.coffee")]
350 switch method
351 when 'generate'
352 if existsSync viewDirPath
353 log "directory '#{viewDirPath}' already exist!"
354 false
355 else
356 mkdirSync viewDirPath, 0700
357 generateFile mapFilePath, mapCoffee
358 generateFile reduceFilePath, reduceCoffee
359 true
360 when 'destroy'
361 if existsSync viewDirPath
7e78ebd @andrzejsliwa bugfixing, broken generators
authored
362 log "'#{viewDirPath}'."
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
363 exec "rm -r #{viewDirPath}", handleOutput()
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
364 true
365 else
366 log "there is no view '#{name}' ('#{viewDirPath}') !!!"
367 false
368 else
369 throw 'unknown method'
370
371 # handling generic generate/destroy of file
372 handleFile = (method, folder, template, name) ->
373 filePathCoffee = join folder, "#{name}.coffee"
374 filePathJS = join folder, "#{name}.js"
375 switch method
376 when 'generate'
377 unless existsSync folder
378 mkdirSync folder, 0700
7e78ebd @andrzejsliwa bugfixing, broken generators
authored
379 generateFile filePathCoffee, template
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
380 true
381 when 'destroy'
7e78ebd @andrzejsliwa bugfixing, broken generators
authored
382 if existsSync filePathCoffee
383 log "'#{filePathCoffee}'."
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
384 exec "rm #{filePathCoffee}", handleOutput()
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
385 true
386 else if existsSync filePathJS
7e78ebd @andrzejsliwa bugfixing, broken generators
authored
387 log "'#{filePathJS}'."
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
388 exec "rm #{filePathJS}", handleOutput()
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
389 true
390 else
7e78ebd @andrzejsliwa bugfixing, broken generators
authored
391 log "there is no '#{name}' ('#{filePathJS}' or '#{filePathCoffee}') !!!"
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
392 false
393 else
394 throw 'unknown method'
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
395
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
396 # shortcuts of handling generate/destroy
397 handleShow = (method, name) -> handleFile(method, 'shows', showCoffee, name)
398 handleList = (method, name) -> handleFile(method, 'lists', listCoffee, name)
399 handleFilter = (method, name) -> handleFile(method, 'filters', filterCoffee, name)
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
400
7e78ebd @andrzejsliwa bugfixing, broken generators
authored
401 # make clean up
402 clean = ->
403 log "cleaning up:"
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
404 log " * remove '#{releases_folder_name}' ..."
405 exec "rm -r #{releases_folder_name}", handleOutput ->
406 log " * remove '#{dumps_folder_name}' ..."
407 exec "rm -r #{dumps_folder_name}", handleOutput ->
408 log "done."
409 , ->
410 log "there is no '#{dumps_folder_name}' directory!"
411 , ->
412 log "there is no '#{releases_folder_name}' directory!"
7e78ebd @andrzejsliwa bugfixing, broken generators
authored
413
414 # prepare project
415 prepare = ->
416 log "preparing project:"
417 generateFile '.gitignore', gitIgnore
bc57d74 @andrzejsliwa fix for using process.argv
authored
418 generateFile '.couchappignore', couchAppIgnore
7e78ebd @andrzejsliwa bugfixing, broken generators
authored
419 log "done."
420
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
421
422 restore = ->
423 [options, database] = processOptions()
424 config = getConfig()
425 lastPath = join dumps_folder_name, database, 'last'
bc57d74 @andrzejsliwa fix for using process.argv
authored
426 unless existsSync lastPath
427 log "There is no dump!"
428 return
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
429 if config['env'][database]['make_dumps']
430 log "restoring dump from #{lastPath} to database: #{}"
431 url = config['env'][database]['db']
d3c05d5 @andrzejsliwa fixed error handling on restore
authored
432 request {uri: url, method: 'DELETE', headers: headers}, (error, response, body) ->
433 if response.statusCode == 200
434 request {uri: url, method: 'PUT', headers: headers}, (error, response, body) ->
435 unless response.statusCode == 200
436 message = JSON.parse(body)
ecebaf1 @andrzejsliwa bugfixing ... problem with parsing options
authored
437 if message.error
438 log "Error: #{message.error} - #{message.reason}"
d3c05d5 @andrzejsliwa fixed error handling on restore
authored
439 else
440 message = JSON.parse(body)
ecebaf1 @andrzejsliwa bugfixing ... problem with parsing options
authored
441 if message.error
442 log "Error: #{message.error} - #{message.reason}"
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
443 exec "couchdb-load #{url} --input #{lastPath}", handleOutput ->
444 log "done."
445 else
446 log "you don't using dumps for this database... look in couchapprc for make_dumps."
447
ed29823 add two config options: makeReleaseVersions: <boolean> and designdocN…
Szaby Grünwald authored
448 rmTreeSync = (path) ->
449 return unless existsSync(path)
450 files = readdirSync(path)
451 unless files.length
452 rmdirSync path
453 return
454 else
455 files.forEach (file) ->
456 fullName = join(path, file)
457 if statSync(fullName).isDirectory()
458 rmTreeSync fullName
459 else
460 unlinkSync fullName
461 rmdirSync path
462
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
463 # Handle wrapping
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
464 handleCommand = (type) ->
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
465 handled = false
bc57d74 @andrzejsliwa fix for using process.argv
authored
466 name = process.argv[2..][0]
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
467 name = "help" if name == undefined
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
468 _.each commandWraps, (cmd) ->
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
469 if cmd.type == type && cmd.name == name
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
470 handled = true
471 cmd.callback()
472 handled
473
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
474 missingPythonDeps = (commandName, packageName) ->
475 log " * missing #{commandName} !"
476 log " try... pip install #{packageName}"
ecebaf1 @andrzejsliwa bugfixing ... problem with parsing options
authored
477 log " or... easy_install #{packageName}"
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
478 process.exit -1
9a679f3 @andrzejsliwa added generators, refactorized, prepared version 1.0
authored
479
480 #### Well, let's dance baby
6f6a058 @andrzejsliwa adding version resolving via package.json
authored
481 exports.run = ->
482 showGreatings()
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
483
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
484 ok_callback = ->
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
485 unless handleCommand 'before'
486 # convert options back to string
bc57d74 @andrzejsliwa fix for using process.argv
authored
487 options = process.argv[2..].join ' '
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
488 # execute couchapp command
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
489 exec "couchapp #{options}", handleOutput ->
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
490 handleCommand 'after'
491
dde29d6 @andrzejsliwa hotfix
authored
492 exec 'couchapp --version > /dev/null', handleOutput ->
493 config = getConfig()
494 [options, database] = processOptions()
495 if config['env'][database]['make_dumps']
496 exec 'couchdb-dump --version > /dev/null', handleOutput ok_callback, ->
ecebaf1 @andrzejsliwa bugfixing ... problem with parsing options
authored
497 missingPythonDeps("couch-dump", "couchdb")
dde29d6 @andrzejsliwa hotfix
authored
498 else
499 ok_callback()
ae21944 @andrzejsliwa added dump/restore option, refactorization (rewrited recurency to ite…
authored
500 , ->
b735bb6 @andrzejsliwa added prototype handling dumps.
authored
501 missingPythonDeps "couchdb-dump", "couchdb"
502
503
504
505
506
507
877c80c @andrzejsliwa added destroy, better error handling & refactorized
authored
508
Something went wrong with that request. Please try again.