-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake.js
280 lines (229 loc) · 9.29 KB
/
make.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
// parse command line options
var minimist = require('minimist');
var editJsonFile = require("edit-json-file");
var mopts = {
string: [
'extension',
'token',
'artifactsPath',
'suite'
]
};
var options = minimist(process.argv, mopts);
// remove well-known parameters from argv before loading make,
// otherwise each arg will be interpreted as a make target
process.argv = options._;
var make = require('shelljs/make');
var path = require('path');
var util = require('./make-util');
var semver = require('semver');
var ToolRunner = require('vsts-task-lib/toolrunner');
const semverRegex = require('semver-regex');
var banner = util.banner;
var miniBanner = util.miniBanner;
var getDirectories = util.getDirectories;
var matchFind = util.matchFind;
var buildExtension = util.buildExtension;
var ensureTool = util.ensureTool;
var fail = util.fail;
var getTaskVersions = util.getTaskVersions;
var getTasks = util.getTasks;
var run = util.run;
var rm = util.rm;
var ensureExists = util.ensureExists
// global variables
var extensionsPath = path.join(__dirname, 'Extensions');
var buildPath = path.join(__dirname, '_build', 'Tasks');
var buildTestsPath = path.join(__dirname, '_build', 'Tests');
var outDir = path.join(__dirname, '_build', "Outdir");
// node min version
var minNodeVer = '6.10.3';
if (semver.lt(process.versions.node, minNodeVer)) {
fail('requires node >= ' + minNodeVer + '. installed: ' + process.versions.node);
}
// Resolve list of versions
var extensionList;
if (options.extension) {
// find using --extension parameter
extensionList = matchFind(options.extension, extensionsPath, { noRecurse: true, matchBase: true, nocase: true })
.map(function (item) {
return item;
});
if (!extensionList.length) {
fail('Unable to find any extensions matching pattern ' + options.extension);
}
}
else {
// load the default list
extensionList = getDirectories(extensionsPath);
}
target.clean = function(){
banner('Cleaning the global extension output')
rm('-Rf', outDir);
mkdir('-p', outDir);
extensionList.forEach(function (currentExtensionPath){
var currentExtensionName = currentExtensionPath.split(path.sep).pop();
banner(`Cleaning Extension [${currentExtensionName}]...`)
var taskList = getTasks(currentExtensionPath);
taskList.forEach(function (taskPath){
console.log('Deleting js files')
rm('-f', path.join(taskPath, "*.js"));
rm('-f', path.join(taskPath, "*.js.map"));
});
});
}
target.install = function(){
extensionList.forEach(function (currentExtensionPath){
var currentExtensionName = currentExtensionPath.split(path.sep).pop();
banner(`${currentExtensionName}`);
var taskList = getTasks(currentExtensionPath);
taskList.forEach(function (taskPath){
// look for tsconfig.json
var shouldBuildNode = test('-f', path.join(taskPath, 'tsconfig.json'));
if (shouldBuildNode){
console.log(`npm install [${taskPath}]`)
try{
run(`npm i`, { env: process.env, cwd: taskPath, stdio: 'inherit' })
}catch(error){
fail(error);
}
} else {
console.log(`Skipping npm install as this does not look like a node task [${taskPath}]`);
}
});
});
};
target.build = function() {
// Required for npx
ensureTool('npm', '--version', function (output) {
if (semver.lt(output, '5.2.0')) {
fail('Expected 5.2.0 or higher. To fix, run: npm install -g npm');
}
});
extensionList.forEach(function (currentExtensionPath){
buildExtension(currentExtensionPath);
});
};
target.package = function(){
target.clean();
target.install();
target.build();
extensionList.forEach(function (currentExtensionPath){
var currentExtensionName = currentExtensionPath.split(path.sep).pop();
banner(`${currentExtensionName}`);
var taskList = getTasks(currentExtensionPath);
taskList.forEach(function (taskPath){
// look for tsconfig.json
var shouldBuildNode = test('-f', path.join(taskPath, 'tsconfig.json'));
if (shouldBuildNode){
// npm prune
console.log('Running npm prune')
try{
util.run(`npm prune --production`, { env: process.env, cwd: taskPath, stdio: 'inherit' })
}catch(error){
fail(error);
}
}
});
// create extension
console.log('Creating extension')
try{
// Create the standard one
console.log('Packaging release version');
var extensionPath = path.join(currentExtensionPath, 'vss-extension.json');
var originalManifest = editJsonFile(extensionPath);
try {
var manifest = editJsonFile(extensionPath);
manifest.set("galleryFlags", ["public"])
manifest.save();
util.run(`tfx extension create --manifest-globs ${extensionPath} --root ${currentExtensionPath} --output-path ${path.join(outDir, "public")}`, { env: process.env, cwd: __dirname, stdio: 'inherit' })
// Create the preview version
console.log('Packaging preview version');
manifest = editJsonFile(extensionPath);
manifest.set("id", manifest.get('id') + '-preview');
manifest.set("name", 'Preview: ' + manifest.get('name'));
manifest.set("galleryFlags", ["preview"])
manifest.save();
util.run(`tfx extension create --manifest-globs ${extensionPath} --root ${currentExtensionPath} --output-path ${path.join(outDir, "preview")}`, { env: process.env, cwd: __dirname, stdio: 'inherit' })
} finally {
// ensure the manifest is reset
originalManifest.save();
}
} catch(error) {
fail(error);
}
});
}
target.publishPreview = function(){
publish("preview");
}
target.publishLive = function(){
publish("public");
}
function publish(publishType) {
if (!options.artifactsPath){
console.log('ArtifactsPath was not supplied - using the standard OutDir');
options.artifactsPath = outDir;
}
console.log(`Token: ${options.token}`)
ensureExists(options.artifactsPath);
var publishCount = 0;
var vsixFiles = matchFind("*.vsix", path.join(options.artifactsPath, publishType), { noRecurse: true, matchBase: true })
.map(function (item) {
return item;
});
vsixFiles.forEach(function (vsix){
banner(`Publishing [${vsix}]`);
// extract the version of the vsix
var versionFromVsix = semverRegex().exec(vsix)[0];
console.log(`Checking to see if this version is already published...`);
var version = "0.0.0";
var output;
output = util.run(`tfx extension show --vsix ${vsix} --token ${options.token} --json`, { env: process.env, cwd: __dirname }, true, true);
try {
const json = JSON.parse(output);
version = json.versions[0].version;
} catch (err) {
// Failed to parse the JSON - no JSON was returned. This means the extension probably doesn't exist.
if (output.indexOf('404') == -1 && output.indexOf('doesn\'t exist') == -1){
throw `Unknown error thrown from TFX: ${err}`;
}
}
console.log(`Latest version : ${version}`);
console.log(`Requested action : ${versionFromVsix}`);
// We used to use the override object to override the galleryFlags, but we now do that in the build.
// i am keeping this code here as it may be useful for other overrides.
var overrideObject = {
};
var overrideString = JSON.stringify(overrideObject).replace(/\"/g, '\\"');
if (version !== versionFromVsix){
publishCount++;
util.run(`tfx extension publish --vsix "${vsix}" --token ${options.token} --override "${overrideString}"`, { env: process.env, cwd: __dirname, stdio: 'inherit' }, true);
}else{
console.log('Skipping as it already exists in the marketplace.')
}
});
if (publishCount === 0) {
throw "No extensions were published. Are you sure you bumped the version numbers?"
}
}
//
// will run tests for the scope of tasks being built
// npm test
// node make.js test
// node make.js test --extension CampaignMonitor --suite L0
//
target.test = function() {
rm('-Rf', buildPath);
mkdir('-p', path.join(buildPath));
rm('-Rf', buildTestsPath);
mkdir('-p', path.join(buildTestsPath));
// find the tests
var suiteType = options.suite || 'L0';
var extenionName = options.extension || '*';
var pattern1 = buildPath + '/' + extenionName + '/Tests/' + suiteType + '.js';
var pattern3 = buildTestsPath + '/' + suiteType + '.js';
var testsSpec = matchFind(pattern1, buildPath)
.concat(matchFind(pattern3, buildTestsPath, { noRecurse: true }));
run('jest ' + testsSpec.join(' '), /*inheritStreams:*/true);
}