forked from runytry1/azure-pipelines-tool-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-util.js
174 lines (149 loc) · 3.91 KB
/
make-util.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
var fs = require('fs');
var os = require('os');
var path = require('path');
var process = require('process');
var ncp = require('child_process');
var semver = require('semver');
var shell = require('shelljs');
//------------------------------------------------------------------------------
// shell functions
//------------------------------------------------------------------------------
var shellAssert = function() {
var errMsg = shell.error();
if (errMsg) {
throw new Error(errMsg);
}
}
var cd = function(dir) {
shell.cd(dir);
shellAssert();
}
exports.cd = cd;
var cp = function(options, source, dest) {
if (dest) {
shell.cp(options, source, dest);
} else {
shell.cp(options, source);
}
shellAssert();
}
exports.cp = cp;
var mkdir = function(options, target) {
if (target) {
shell.mkdir(options, target);
} else {
shell.mkdir(options);
}
shellAssert();
}
exports.mkdir = mkdir;
var rm = function(options, target) {
if (target) {
shell.rm(options, target);
} else {
shell.rm(options);
}
shellAssert();
}
exports.rm = rm;
var test = function(options, p) {
var result = shell.test(options, p);
shellAssert();
return result;
}
exports.test = test;
//------------------------------------------------------------------------------
var assert = function(value, name) {
if (!value) {
throw new Error('"' + name + '" cannot be null or empty.');
}
}
exports.assert = assert;
var banner = function(message, noBracket) {
console.log();
if (!noBracket) {
console.log('------------------------------------------------------------');
}
console.log(message);
if (!noBracket) {
console.log('------------------------------------------------------------');
}
console.log();
}
exports.banner = banner;
var rp = function(relPath) {
return path.join(pwd() + '', relPath);
}
exports.rp = rp;
var fail = function(message) {
console.error('ERROR: ' + message);
process.exit(1);
}
exports.fail = fail;
var ensureExists = function(checkPath) {
assert(checkPath, 'checkPath');
var exists = test('-d', checkPath) || test('-f', checkPath);
if (!exists) {
fail(checkPath + ' does not exist');
}
}
exports.ensureExists = ensureExists;
var pathExists = function(checkPath) {
return test('-d', checkPath) || test('-f', checkPath);
}
exports.pathExists = pathExists;
var run = function(cl, inheritStreams, noHeader) {
if (!noHeader) {
console.log();
console.log('> ' + cl);
}
var options = {
stdio: inheritStreams ? 'inherit' : 'pipe'
};
var rc = 0;
var output;
try {
output = ncp.execSync(cl, options);
} catch (err) {
if (!inheritStreams) {
console.error(err.output ? err.output.toString() : err.message);
}
process.exit(1);
}
return (output || '').toString().trim();
}
exports.run = run;
var ensureTool = function(name, versionArgs, validate) {
console.log(name + ' tool:');
var toolPath = which(name);
if (!toolPath) {
fail(name + ' not found. might need to run npm install');
}
if (versionArgs) {
var result = exec(name + ' ' + versionArgs);
if (typeof validate == 'string') {
if (result.output.trim() != validate) {
fail('expected version: ' + validate);
}
} else {
validate(result.output.trim());
}
}
console.log(toolPath + '');
}
exports.ensureTool = ensureTool;
var addPath = function(directory) {
var separator;
if (os.platform() == 'win32') {
separator = ';';
} else {
separator = ':';
}
var existing = process.env['PATH'];
if (existing) {
process.env['PATH'] = directory + separator + existing;
} else {
process.env['PATH'] = directory;
}
}
exports.addPath = addPath;