-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
202 lines (180 loc) · 5.23 KB
/
index.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
191
192
193
194
195
196
197
198
199
200
201
var crypto = require('crypto')
var EventEmitter = require('events').EventEmitter
var fs = require('fs')
var path = require('path')
var os = require('os')
var spawn = require('child_process').spawn
var Step = require('step')
var isWindows = /^win/.test(process.platform)
var PATH = isWindows ? process.env.Path : process.env.PATH;
var SEPARATOR = isWindows ? ';' : ':';
var emitter
// Template string for wrapper script.
var GIT_SSH_TEMPLATE = '#!/bin/sh\n' +
'exec ssh -i $key -o StrictHostKeyChecking=no "$@"\n'
function mkTempFile(prefix, suffix) {
var randomStr = crypto.randomBytes(4).toString('hex')
var name = prefix + randomStr + suffix
var file = path.join(os.tmpdir(), name)
// Hack for weird environments (nodejitsu didn't guarantee os.tmpdir() to already exist)
try {
fs.mkdirSync(os.tmpdir())
} catch (e) {}
return file
}
//
// Write the Git script template to enable use of the SSH private key
//
// *privKey* SSH private key.
// *file* (optional) filename of script.
// *keyMode* (optional) mode of key.
// *cb* callback function of signature function(err, tempateFile, keyFile).
//
function writeFiles(privKey, file, keyMode, cb) {
// No file name - generate a random one under the system temp dir
if (!file) {
file = mkTempFile("_gitane", ".sh")
}
if (typeof(keyMode) === 'function') {
cb = keyMode
keyMode = 0600
}
var keyfile = mkTempFile("_gitaneid", ".key")
var keyfileName = keyfile
if (isWindows) {
keyfileName = "\"" + keyfile.replace(/\\/g,"\\\\") + "\"";
}
var data = GIT_SSH_TEMPLATE.replace('$key', keyfileName)
Step(
function() {
fs.writeFile(file, data, this.parallel())
fs.writeFile(keyfile, privKey, this.parallel())
},
function(err) {
if (err) {
return cb(err, null)
}
// make script executable
fs.chmod(file, 0755, this.parallel())
// make key secret
fs.chmod(keyfile, keyMode, this.parallel())
},
function(err) {
if (err) {
return cb(err, null)
}
return cb(null, file, keyfile)
}
)
}
//
// Run a command in a subprocess with GIT_SSH set to the correct value for
// SSH key.
//
// *baseDir* current working dir from which to execute git
// *privKey* SSH private key to use
// *cmd* command to run
// *keyMode* optional unix file mode of key
// *cb* callback function of signature function(err, stdout, stderr)
//
// or first argument may be an object with params same as above,
// with addition of *emitter* which is an EventEmitter for real-time stdout
// and stderr events. An optional *detached* option specifies whether the
// spawned process should be detached from this one, and defaults to true.
// Detachment means the git process won't hang trying to prompt for a password.
function run(baseDir, privKey, cmd, keyMode, cb) {
var detached = true
var spawnFn = spawn
if (typeof(keyMode) === 'function') {
cb = keyMode
keyMode = 0600
}
if (typeof(baseDir) === 'object') {
var opts = baseDir
cb = privKey
cmd = opts.cmd
privKey = opts.privKey
keyMode = opts.keyMode || 0600
emitter = opts.emitter
baseDir = opts.baseDir
spawnFn = opts.spawn || spawn
if (typeof(opts.detached) !== 'undefined') {
detached = opts.detached
}
}
var args;
if (Array.isArray(cmd)) {
args = cmd.slice(1);
cmd = cmd[0];
}
else {
var split = cmd.match(/"[^"]+"|'[^']+'|\S+/g)
cmd = split[0]
args = split.slice(1)
}
Step(
function() {
writeFiles(privKey, null, keyMode, this)
},
function(err, file, keyfile) {
if (err) {
console.log("Error writing files: %s", err)
return cb(err, null)
}
this.file = file
this.keyfile = keyfile
var proc = spawnFn(cmd, args, {cwd: baseDir, env: {GIT_SSH: file, PATH:PATH}, detached: detached})
proc.stdoutBuffer = ""
proc.stderrBuffer = ""
proc.stdout.setEncoding('utf8')
proc.stderr.setEncoding('utf8')
proc.stdout.on('data', function(buf) {
if (typeof(emitter) === 'object') {
emitter.emit('stdout', buf)
}
proc.stdoutBuffer += buf
})
proc.stderr.on('data', function(buf) {
if (typeof(emitter) === 'object') {
emitter.emit('stderr', buf)
}
proc.stderrBuffer += buf
})
var self = this
proc.on('close', function(exitCode) {
var err = null
if (exitCode !== 0) {
err = "process exited with status " + exitCode
}
self(err, proc.stdoutBuffer, proc.stderrBuffer, exitCode)
})
proc.on('error', function (err) {
// prevent node from throwing an error. The error handling is
// done in the 'close' handler.
})
},
function(err, stdout, stderr, exitCode) {
// cleanup temp files
try {
fs.unlinkSync(this.file)
fs.unlinkSync(this.keyfile)
} catch(e) {}
cb(err, stdout, stderr, exitCode)
}
)
}
//
// convenience wrapper for clone. maybe add more later.
//
function clone(args, baseDir, privKey, cb) {
run(baseDir, privKey, "git clone " + args, cb)
}
function addPath(str) {
PATH = PATH + SEPARATOR + str
}
module.exports = {
clone:clone,
run:run,
writeFiles:writeFiles,
addPath:addPath,
}