This repository has been archived by the owner on Mar 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
make-utils.js
355 lines (275 loc) · 9.02 KB
/
make-utils.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
var os = require('os');
var shell = require('shelljs');
var path = require('path');
var childprocess = require('child_process');
var fs = require('fs');
var semver = require('semver');
// shell helpers
var exec = function (command) {
var r = command();
var msg = shell.error();
if (msg)
throw new Error(msg.toString());
return r;
}
var test = function (option, path) {
return exec(() => shell.test(option, path));
}
exports.test = test;
var rm = function (options, files) {
exec(() => shell.rm(options, files));
}
exports.rm = rm;
var mkdir = function (options, dir) {
return exec(() => shell.mkdir(options, dir));
}
exports.mkdir = mkdir;
var pushd = function (options, dir) {
exec(() => shell.pushd(options, dir));
}
exports.pushd = pushd;
var popd = function (options) {
exec(() => shell.popd(options));
}
exports.popd = popd;
var cp = function (options, source, dest) {
exec(() => shell.cp(options, source, dest));
}
exports.cp = cp;
var find = function(path) {
return exec(() => shell.find(path));
}
exports.find = find;
// misc helpers
var addPath = function (directory) {
log(`preprending PATH with '${directory}'`);
var separator = os.platform() === 'win32'
? ';'
: ':';
var existingPath = process.env['PATH'];
process['PATH'] = existingPath
? process.env['PATH'] = directory + separator + existingPath
: directory;
}
exports.addPath = addPath;
var fail = function (message) {
console.error(`ERROR: ${message}`);
process.exit(1);
}
exports.fail = fail;
var banner = function (message) {
console.log('--------------------------------------------------');
console.log(message);
console.log('--------------------------------------------------');
}
exports.banner = banner;
var log = function (message) {
console.log(`> ${message}`);
}
exports.log = log;
var run = function (command, inheritStreams) {
log(command);
var options = {
stdio: inheritStreams ? 'inherit' : 'pipe'
};
var output;
try {
output = childprocess.execSync(command, 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 fileToJson = function (file) {
var json = JSON.parse(fs.readFileSync(file).toString());
return json;
}
var jsonToFile = function(json, file) {
fs.writeFileSync(file, JSON.stringify(json, null, 4));
}
// build
var buildTask = function (taskPath, outputPath) {
pushd('-q', taskPath);
try {
// paths
const taskPackagePath = path.join(pwd() + '', 'package.json');
const testPackagePath = path.join(pwd() + '', 'tests', 'package.json');
// restore node modules
if (test('-f', taskPackagePath)) {
log(`restoring modules '${taskPackagePath}'`);
run('npm install');
}
if (test('-f', testPackagePath)) {
log(`restoring modules '${testPackagePath}'`);
pushd('-q', path.join(pwd() + '', 'tests'));
try {
run('npm install');
}
finally {
popd('-q');
}
}
// build task
run(`tsc --outDir "${outputPath}" --rootDir "${taskPath}"`);
}
finally {
popd('-q');
}
}
exports.buildTask = buildTask;
var copyTaskResources = function (taskPath, outputPath) {
const resources = [
'icon.png',
'node_modules',
'task.json',
'tests'
];
resources.forEach(pattern => {
const sourcePath = path.join(taskPath, pattern);
if (test('-d', sourcePath) || test('-f', sourcePath)) {
log(`copying '${pattern}'`);
cp('-Rf', sourcePath, outputPath + '/');
}
});
}
exports.copyTaskResources = copyTaskResources;
var updateTaskMetadata = function (options, taskPath) {
const manifestPath = path.join(taskPath, 'task.json');
var manifest = fileToJson(manifestPath);
options.version = options.public
? `${manifest.version.Major}.${manifest.version.Minor}.${manifest.version.Patch}`
: generateVersion(manifest, manifest.version.Major);
updateTaskManifest(manifest, options);
jsonToFile(manifest, manifestPath);
const telemetryPath = path.join(taskPath, 'telemetry.js');
updateTaskTelemetry(
telemetryPath,
{
version: options.version,
instrumentationKey: options.instrumentationKey
});
}
exports.updateTaskMetadata = updateTaskMetadata;
var minor = null;
var patch = null;
var generateVersion = function (manifest, major) {
const ref = new Date(2000, 1, 1);
const now = new Date();
minor = minor || Math.floor((now - ref) / 86400000);
patch = patch || Math.floor(Math.floor(now.getSeconds() + (60 * (now.getMinutes() + (60 * now.getHours())))) * 0.5)
return `${major}.${minor}.${patch}`
}
var updateTaskManifest = function (manifest, options) {
log(`updating task version '${options.version}'`);
manifest.version.Major = semver.major(options.version);
manifest.version.Minor = semver.minor(options.version);
manifest.version.Patch = semver.patch(options.version);
manifest.helpMarkDown = `${manifest.helpMarkDown} (v${options.version})`;
if (!options.public) {
log('updating task as dev');
manifest.friendlyName = `${manifest.friendlyName} (dev ${options.version})`;
}
if (options.taskId) {
log(`updating task id '${options.taskId}'`);
manifest.id = options.taskId;
}
};
var updateTaskTelemetry = function (telemetryPath, options) {
var script = fs.readFileSync(telemetryPath, { encoding: 'utf8' });
if (options.instrumentationKey)
{
log(`updating telemetry instrumentation key '${options.instrumentationKey}'`);
script = script.replace(/const\s+instrumentationKey\s*=\s*'[^']*'\s*;/, `const instrumentationKey = '${options.instrumentationKey}';`);
}
log(`updating telemetry version '${options.version}'`);
script = script.replace(/const\s+version\s*=\s*'[^']*'\s*;/, `const version = '${options.version}';`);
fs.writeFileSync(telemetryPath, script);
}
var copyExtensionResources = function (extensionPath, outputPath) {
const resources = [
'*.md',
'*.txt',
'vss-extension.json',
'images/*.png'
];
resources.forEach(pattern => {
const sourcePath = path.join(extensionPath, pattern);
//if (test('-d', sourcePath) || test('-f', sourcePath))
{
log(`copying '${pattern}'`);
var dest = pattern.indexOf('/') < 0
? outputPath
: path.join(outputPath, pattern.substring(0, pattern.lastIndexOf('/')));
if (!test('-d', dest))
mkdir('-p', dest);
cp('-Rf', sourcePath, dest);
}
});
}
exports.copyExtensionResources = copyExtensionResources;
var updateExtensionMetadata = function (options, extensionPath) {
const manifestPath = path.join(extensionPath, 'vss-extension.json');
var manifest = fileToJson(manifestPath);
if (options.extensionId) {
log(`updating extension id '${options.extensionId}'`);
manifest.id = options.extensionId;
}
options.version = options.public
? manifest.version
: generateVersion(manifest, semver.major(manifest.version));
log(`updating extension version '${options.version}'`);
manifest.version = options.version;
if (!options.public) {
log('updating extension as dev');
manifest.id = `${manifest.id}-dev`;
manifest.name = `${manifest.name} (dev)`;
}
log(`updating extension visibility '${options.public}'`);
manifest.public = options.public;
jsonToFile(manifest, manifestPath);
}
exports.updateExtensionMetadata = updateExtensionMetadata;
// test
var testTask = function (testPath, suite) {
pushd('-q', testPath);
try {
// paths
const suitePath = path.join(testPath, `${suite}.js`);
if (!test('-f', suitePath))
fail(`test suite '${suitePath}' not found.`);
// run tests
run(`mocha ${suitePath}`, true);
}
finally {
popd('-q');
}
}
exports.testTask = testTask;
// package
var packageExtension = function (extensionPath, outputPath) {
// copy build output and remove tests
log('copying build output without tests')
const tmpPath = path.join(outputPath, 'tmp');
mkdir('-p', tmpPath);
cp('-Rf', path.join(extensionPath, '*'), tmpPath);
find(tmpPath).filter(file => file.match(/\/ReplaceTokensV\d+\/tests$/)).forEach(file => {
rm('-Rf', file);
});
try {
// create extension
run(`tfx extension create --root "${tmpPath}" --output-path "${outputPath}"`);
}
finally {
// clean tmp
if (test('-d', tmpPath)) {
log('removing tmp');
rm('-Rf', tmpPath);
}
}
}
exports.packageExtension = packageExtension;