1 | Configuration = require './configuration'
|
2 | _ = require('underscore')._
|
3 |
|
4 | Github = Configuration.Github
|
5 | Deploy = Configuration.Deploy
|
6 | Aliases = Configuration.Nconf.get('aliases')
|
7 | Async = require 'async'
|
8 | Spawn = require('child_process').spawn
|
9 | Mkdirp = require 'mkdirp'
|
10 | Temp = require 'temp'
|
11 | Fs = require 'fs'
|
12 | Path = require 'path'
|
13 |
|
14 | Commands = require './commands'
|
15 | Utils = require './utils'
|
16 |
|
17 | deploy = (fallback, repo, branch, extra) ->
|
18 | repo = Aliases[repo] || repo
|
19 | branch = if branch? then branch else "master"
|
20 | if branch? and Deploy.branchArg? then Deploy.args.push(Deploy.branchArg.replace("{{branch}}", branch))
|
21 | if extra? and Deploy.extraArg? then Deploy.args.push(Deploy.extraArg.replace("{{extra}}", extra))
|
22 |
|
23 | comments = if extra? then "(#{branch} / #{extra})" else "(#{branch})"
|
24 |
|
25 | prepareEnv repo, branch, (error, dirPath) ->
|
26 | if (error?)
|
27 | Utils.fallback_printError(fallback, error)
|
28 | else
|
29 | if (not Deploy.exec? or _.isEmpty(Deploy.exec))
|
30 | Utils.fallback_printError(fallback, "Deploy not configured.")
|
31 | else
|
32 |
|
33 | proc = Spawn Deploy.exec, Deploy.args, { cwd: dirPath }
|
34 |
|
35 | Utils.fallback_print(fallback)
|
36 | title: "Deploy started", infos: repo, comments: comments, status: true
|
37 |
|
38 | logPath = Temp.path {suffix: ".log"}
|
39 | log = Fs.createWriteStream logPath
|
40 | proc.stdout.pipe log
|
41 | proc.stderr.pipe log
|
42 |
|
43 | proc.on 'exit', (code) ->
|
44 | if (code is 0)
|
45 | gist log, logPath, (err, url) ->
|
46 | Utils.fallback_print(fallback)
|
47 | title: "Successful deploy !", url: url, infos: repo, comments: comments, status: true
|
48 | else
|
49 | gist log, logPath, (err, url) ->
|
50 | Utils.fallback_print(fallback) {
|
51 | title: "A problem occured during the deploy",
|
52 | url: url,
|
53 | infos: repo,
|
54 | comments: comments,
|
55 | status: false
|
56 | }
|
57 |
|
58 | proc.on 'error', (error) ->
|
59 | gist log, logPath, (err, url) ->
|
60 | Utils.fallback_print(fallback) {
|
61 | title: "A problem occured during the deploy: #{error}",
|
62 | url: url,
|
63 | infos: repo,
|
64 | comments: comments,
|
65 | status: false
|
66 | }
|
67 |
|
68 | prepareEnv = (repo, ref, callback) ->
|
69 | Temp.mkdir 'repo', (error, dirPath) ->
|
70 | if (error)
|
71 | callback(error)
|
72 | else
|
73 | Async.each Deploy.env, (file, cb) ->
|
74 | script = {}
|
75 | filePath = ""
|
76 | Async.waterfall [
|
77 | (waterCallback) ->
|
78 | Github.Api.repos.getContent { user: Github.Org, repo: repo, ref: ref, path: file }, waterCallback
|
79 |
|
80 | , (fileDetails, waterCallback) ->
|
81 | script = new Buffer(fileDetails.content, fileDetails.encoding).toString()
|
82 | filePath = fileDetails.path
|
83 | Mkdirp Path.join(dirPath, Path.dirname(file)), waterCallback
|
84 |
|
85 | , (made, waterCallback) ->
|
86 | if (_.isFunction(Deploy.postProcessFunction))
|
87 | Deploy.postProcessFunction filePath, script, waterCallback
|
88 | else
|
89 | waterCallback(null, script)
|
90 |
|
91 | , (postProcessedScript, waterCallback) ->
|
92 | Fs.writeFile Path.join(dirPath, filePath), postProcessedScript, waterCallback
|
93 |
|
94 | ], (waterr) ->
|
95 | cb(waterr)
|
96 | , (err) ->
|
97 | callback(err, dirPath)
|
98 |
|
99 | gist = (log, logPath, callback) ->
|
100 | Fs.readFile logPath, (error, data) ->
|
101 | data = data.toString()
|
102 | data = data.replace(new RegExp('\\\u001b\\[0m', 'g'), '')
|
103 | data = data.replace(new RegExp('\\\u001b\\[31m', 'g'), '')
|
104 | data = data.replace(new RegExp('\\\u001b\\[32m', 'g'), '')
|
105 | data = data.replace(new RegExp('✘', 'g'), '-')
|
106 | data = data.replace(new RegExp('✔', 'g'), '+')
|
107 |
|
108 | Github.Api.gists.edit { id: Deploy.gistId, files: {"history": { content: data } }}, (err, gist) ->
|
109 | if (err?) then callback(err) else callback(null, gist.html_url + "/" + gist.history[0].version)
|
110 |
|
111 | module.exports = {
|
112 | name: 'Deploy',
|
113 | description: '-project- [ | -alias-] [-branch-] [-extra-] Deploy your projects with the configured command'
|
114 | action: deploy,
|
115 | prepareEnv: prepareEnv
|
116 | }
|