-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtfs-unlock.js
More file actions
237 lines (211 loc) · 6.7 KB
/
tfs-unlock.js
File metadata and controls
237 lines (211 loc) · 6.7 KB
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
/*
* tfs-unlock
* https://github.com/danactive/tfs-unlock
*
* Copyright (c) 2013 Dan BROOKS
* Licensed under the MIT license.
*/
'use strict';
var _handlePaths,
findVisualStudioPath,
fs = require('fs'),
messages = {
"shell": {
"beginCommand": 'Begin shell command',
"noCommand": 'Missing shell command',
"stderr": 'Interpreter Error: ',
"stdout": 'Interpreter Output: ',
"exitCode": 'Child process exited with code # '
}
},
path = require('path'),
paths,
Q = require('q'),
shellCallback,
shell,
tfs,
wait = 3, // sec default delay for TFS to apply commands
workingDirectory;
shell = {
"exe": function (command, calleeCallback) {
var process,
deferred = Q.defer();
if (command) {
process = require('child_process').exec(command, {
"cwd": workingDirectory
});
process.stdout.on('data', function (message) {
deferred.notify({
stream: 'stdout',
message: messages.shell.stdout + message + '.'
});
});
process.stderr.on('data', function (message) {
deferred.notify({
stream: 'stderr',
message: messages.shell.stderr + message + '.'
});
});
process.on('close', function (code, signal) {
var out = '';
out += messages.shell.exitCode + (code || '0') + '.';
if (signal != null) {
out += 'Child process terminated due to receipt of signal ' + signal + '.';
}
deferred.resolve({
exitCode: code,
message: out
});
});
deferred.notify({
message: messages.shell.beginCommand
});
} else {
deferred.reject(messages.shell.noCommand);
}
return deferred.promise;
}
};
tfs = function (paths, command) { // verfied meaning the path and file exisit
var commandLine,
exists,
log = '',
deferred = Q.defer();
if (Object.prototype.toString.call(paths) !== '[object Array]') {
throw new TypeError("paths parameter must be an array");
}
_handlePaths(paths, command).done(function (logs) {
if (shellCallback) {
shellCallback(logs);
}
deferred.resolve(logs);
}, function (err) {
deferred.reject(err);
}, function (progress) {
deferred.notify(progress);
});
return deferred.promise;
};
_handlePaths = function (paths, command) {
return Q.all(paths.map(function (filepath) {
var commandLine,
deferred = Q.defer(),
exists,
log = '';
filepath = path.resolve(filepath); // resolve full path
commandLine = "tf.exe " + command + " \"" + filepath + "\"";
exists = fs.existsSync(filepath);
if (exists) {
shell.exe(commandLine).then(function (out) {
log += out.message;
if (out.exitCode && out.exitCode !== 0) {
deferred.reject(out);
} else {
deferred.resolve(log);
}
}, function (err) {
deferred.reject(err);
}, function (progress) {
if (log && progress && progress.message) {
log += progress.message;
}
deferred.notify(progress);
});
} else {
throw new ReferenceError('File path is not found: ' + filepath);
}
return deferred.promise;
}));
};
findVisualStudioPath = function () {
var wd;
var testEnvVar = function(envVarPath) {
var tfPath = path.join(envVarPath, '../IDE/tf.exe');
return fs.existsSync(tfPath);
};
if (process.env.VS140COMNTOOLS && testEnvVar(process.env.VS140COMNTOOLS)) {
return path.join(process.env.VS140COMNTOOLS, '../IDE');
} else if (process.env.VS120COMNTOOLS && testEnvVar(process.env.VS120COMNTOOLS)) {
return path.join(process.env.VS120COMNTOOLS, '../IDE');
} else if (process.env.VS110COMNTOOLS && testEnvVar(process.env.VS110COMNTOOLS)) {
return path.join(process.env.VS110COMNTOOLS, '../IDE');
} else{
for (var ver in paths) {
if (paths.hasOwnProperty(ver)) {
for (var dirPath in paths[ver]) {
if (paths[ver].hasOwnProperty(dirPath)) {
if (fs.existsSync(paths[ver][dirPath]) && fs.existsSync(path.join(paths[ver][dirPath], 'tf.exe'))) {
wd = paths[ver][dirPath];
}
}
}
}
}
return wd;
}
};
exports.init = function (param) {
param = param || {
callback: function () {}
};
shellCallback = param.callback;
workingDirectory = param.visualStudioPath;
if (!workingDirectory || !fs.existsSync(workingDirectory)) {
workingDirectory = findVisualStudioPath();
}
wait = param.wait || wait;
};
// TFS `command-line http://msdn.microsoft.com/en-us/library/z51z7zy0(v=vs.100).aspx
exports.checkout = function (paths) {
return tfs(paths, 'checkout');
};
exports.undo = function (paths) {
return tfs(paths, 'undo /noprompt');
};
// Enumeration for visualStudioPath
paths = {
vs2008: {
"bit32": 'C:\\Program Files\\Microsoft Visual Studio 9.0\\Common7\\IDE\\',
"bit64": 'C:\\Program Files (x86)\\Microsoft Visual Studio 9.0\\Common7\\IDE\\'
},
vs2010: {
"bit32": 'C:\\Program Files\\Microsoft Visual Studio 10.0\\Common7\\IDE\\',
"bit64": 'C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\Common7\\IDE\\'
},
vs2012: {
"bit32": 'C:\\Program Files\\Microsoft Visual Studio 11.0\\Common7\\IDE\\',
"bit64": 'C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\Common7\\IDE\\'
},
vs2013: {
"bit32": 'C:\\Program Files\\Microsoft Visual Studio 12.0\\Common7\\IDE\\',
"bit64": 'C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\Common7\\IDE\\'
},
vs2015: {
"bit32": 'C:\\Program Files\\Microsoft Visual Studio 14.0\\Common7\\IDE\\',
"bit64": 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\IDE\\'
},
vs2017: {
"bit32": 'C:\\Program Files\\Microsoft Visual Studio\\2017\\Professional\\Common7\\IDE\\CommonExtensions\\Microsoft\\TeamFoundation\\Team Explorer\\',
"bit64": 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Professional\\Common7\\IDE\\CommonExtensions\\Microsoft\\TeamFoundation\\Team Explorer\\'
},
vs2017Community: {
"bit32": 'C:\\Program Files\\Microsoft Visual Studio\\2017\\Community\\Common7\\IDE\\CommonExtensions\\Microsoft\\TeamFoundation\\Team Explorer\\',
"bit64": 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\Common7\\IDE\\CommonExtensions\\Microsoft\\TeamFoundation\\Team Explorer\\'
},
vs2017Enterprise: {
"bit32": 'C:\\Program Files\\Microsoft Visual Studio\\2017\\Enterprise\\Common7\\IDE\\CommonExtensions\\Microsoft\\TeamFoundation\\Team Explorer\\',
"bit64": 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Enterprise\\Common7\\IDE\\CommonExtensions\\Microsoft\\TeamFoundation\\Team Explorer\\'
}
};
exports.vs2008 = paths.vs2008;
exports.vs2010 = paths.vs2010;
exports.vs2012 = paths.vs2012;
exports.vs2013 = paths.vs2013;
exports.vs2015 = paths.vs2015;
exports.vs2017 = paths.vs2017;
exports.vs2017Professional = paths.vs2017;
exports.vs2017Community = paths.vs2017Community;
exports.vs2017Enterprise = paths.vs2017Enterprise;
// for test suite
exports.shell = shell;
exports.messages = messages;