-
Notifications
You must be signed in to change notification settings - Fork 294
/
gulpfile.js
393 lines (367 loc) · 16.2 KB
/
gulpfile.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/* jshint node: true */
/* jshint esversion: 6 */
'use strict';
const gulp = require('gulp');
const glob = require('glob');
const spawn = require('cross-spawn');
const path = require('path');
const del = require('del');
const fs = require('fs-extra');
const _ = require('lodash');
const nativeDependencyChecker = require('node-has-native-dependencies');
const flat = require('flat');
const os = require('os');
const { spawnSync } = require('child_process');
const isCI = process.env.TF_BUILD !== undefined || process.env.GITHUB_ACTIONS === 'true';
const webpackEnv = { NODE_OPTIONS: '--max_old_space_size=9096' };
const { dumpTestSummary } = require('./build/webTestReporter');
const { Validator } = require('jsonschema');
const { stripVTControlCharacters } = require('util');
const common = require('./build/webpack/common');
const jsonc = require('jsonc-parser');
gulp.task('createNycFolder', async (done) => {
try {
const fs = require('fs');
fs.mkdirSync(path.join(__dirname, '.nyc_output'));
} catch (e) {
//
}
done();
});
gulp.task('validateTranslationFiles', (done) => {
const validator = new Validator();
const schema = {
type: 'object',
patternProperties: {
'^[a-z0-9.]*': {
anyOf: [
{
type: ['string'],
additionalProperties: false
},
{
type: ['object'],
properties: {
message: { type: 'string' },
comment: {
type: 'array',
items: {
type: 'string'
}
}
},
required: ['message'],
additionalProperties: false
}
]
}
},
additionalProperties: false
};
glob.sync('package.nls.*.json', { sync: true }).forEach((file) => {
// Verify we can open and parse as JSON.
try {
const js = JSON.parse(fs.readFileSync(file));
const result = validator.validate(js, schema);
if (Array.isArray(result.errors) && result.errors.length) {
console.error(result.errors);
throw new Error(result.errors.map((err) => `${err.property} ${err.message}`).join('\n'));
}
} catch (ex) {
throw new Error(`Error parsing Translation File ${file}, ${ex.message}`);
}
});
done();
});
gulp.task('printTestResults', async (done) => {
await dumpTestSummary();
done();
});
gulp.task('output:clean', () => del(['coverage']));
gulp.task('clean:cleanExceptTests', () => del(['clean:vsix', 'out', 'dist', '!out/test']));
gulp.task('clean:vsix', () => del(['*.vsix']));
gulp.task('clean:out', () => del(['out/**', 'dist/**', '!out', '!out/client_renderer/**', '!**/*nls.*.json']));
gulp.task('clean', gulp.parallel('output:clean', 'clean:vsix', 'clean:out'));
gulp.task('checkNativeDependencies', (done) => {
if (hasNativeDependencies()) {
done(new Error('Native dependencies detected'));
}
done();
});
gulp.task('checkNpmDependencies', (done) => {
/**
* Sometimes we have to update the package-lock.json file to upload dependencies.
* Thisscript will ensure that even if the package-lock.json is re-generated the (minimum) version numbers are still as expected.
*/
const packageLock = require('./package-lock.json');
const errors = [];
const expectedVersions = [
{ name: 'trim', version: '0.0.3' },
{ name: 'node_modules/trim', version: '0.0.3' }
];
function checkPackageVersions(packages, parent) {
if (!packages) {
return;
}
expectedVersions.forEach((expectedVersion) => {
if (!packages[expectedVersion.name]) {
return;
}
const version = packages[expectedVersion.name].version || packages[expectedVersion.name];
if (!version) {
return;
}
if (!version.includes(expectedVersion.version)) {
errors.push(
`${expectedVersion.name} version needs to be at least ${
expectedVersion.version
}, current ${version}, ${parent ? `(parent package ${parent})` : ''}`
);
}
});
}
function checkPackageDependencies(packages) {
if (!packages) {
return;
}
Object.keys(packages).forEach((packageName) => {
const dependencies = packages[packageName]['dependencies'];
if (dependencies) {
checkPackageVersions(dependencies, packageName);
}
});
}
checkPackageVersions(packageLock['packages']);
checkPackageVersions(packageLock['dependencies']);
checkPackageDependencies(packageLock['packages']);
if (errors.length > 0) {
errors.forEach((ex) => console.error(ex));
throw new Error(errors.join(', '));
}
done();
});
async function buildWebPackForDevOrProduction(configFile, configNameForProductionBuilds) {
if (configNameForProductionBuilds) {
await buildWebPack(configNameForProductionBuilds, ['--config', configFile], webpackEnv);
} else {
await spawnAsync('npm', ['run', 'webpack', '--', '--config', configFile, '--mode', 'development'], webpackEnv);
}
}
function modifyJson(jsonFile, cb) {
const json = fs.readFileSync(jsonFile).toString('utf-8');
const [key, value] = cb(json);
const edits = jsonc.modify(json, [key], value, {});
const updatedJson = jsonc.applyEdits(json, edits);
fs.writeFileSync(jsonFile, updatedJson);
}
gulp.task('updatePackageJsonForBundle', async () => {
// When building a web only VSIX, we need to remove the desktop entry point
// & vice versa (this is only required for platform specific builds)
const packageJsonFile = path.join(__dirname, 'package.json');
const packageJsonContents = fs.readFileSync(packageJsonFile).toString('utf-8');
const json = JSON.parse(packageJsonContents);
switch (common.getBundleConfiguration()) {
case common.bundleConfiguration.desktop: {
if (json.browser) {
modifyJson(packageJsonFile, () => ['browser', undefined]);
}
if (!json.main) {
modifyJson(packageJsonFile, () => ['main', './dist/extension.node.js']);
}
break;
}
case common.bundleConfiguration.webAndDesktop: {
if (!json.browser) {
modifyJson(packageJsonFile, () => ['browser', './dist/extension.web.bundle.js']);
}
if (!json.main) {
modifyJson(packageJsonFile, () => ['main', './dist/extension.node.js']);
}
break;
}
case common.bundleConfiguration.web: {
if (!json.browser) {
modifyJson(packageJsonFile, () => ['browser', './dist/extension.web.bundle.js']);
}
if (json.main) {
modifyJson(packageJsonFile, () => ['main', undefined]);
}
break;
}
}
});
async function buildWebPack(webpackConfigName, args, env) {
// Remember to perform a case insensitive search.
const allowedWarnings = getAllowedWarningsForWebPack(webpackConfigName).map((item) => item.toLowerCase());
const stdOut = await spawnAsync(
'npm',
['run', 'webpack', '--', ...args, ...['--mode', 'production', '--devtool', 'source-map']],
env
);
const stdOutLines = stdOut
.split('\n')
.map((item) => stripVTControlCharacters(item).trim())
.filter((item) => item.length > 0);
// Remember to perform a case insensitive search.
const warnings = stdOutLines
.filter((item) => item.startsWith('WARNING in '))
.filter(
(item) =>
allowedWarnings.findIndex((allowedWarning) =>
item.toLowerCase().startsWith(allowedWarning.toLowerCase())
) == -1
);
const errors = stdOutLines.some((item) => item.startsWith('ERROR in'));
if (errors) {
throw new Error(`Errors in ${webpackConfigName}, \n${warnings.join(', ')}\n\n${stdOut}`);
}
if (warnings.length > 0) {
throw new Error(
`Warnings in ${webpackConfigName}, Check gulpfile.js to see if the warning should be allowed., \n\n${stdOut}`
);
}
}
function getAllowedWarningsForWebPack(buildConfig) {
switch (buildConfig) {
case 'production':
return [
'WARNING in asset size limit: The following asset(s) exceed the recommended size limit (244 KiB).',
'WARNING in entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (244 KiB). This can impact web performance.',
'WARNING in webpack performance recommendations:',
'WARNING in ./node_modules/encoding/lib/iconv-loader.js',
'WARNING in ./node_modules/keyv/src/index.js',
'ERROR in ./node_modules/got/index.js',
'WARNING in ./node_modules/ws/lib/BufferUtil.js',
'WARNING in ./node_modules/ws/lib/buffer-util.js',
'WARNING in ./node_modules/ws/lib/Validation.js',
'WARNING in ./node_modules/ws/lib/validation.js',
'WARNING in ./node_modules/@jupyterlab/services/node_modules/ws/lib/buffer-util.js',
'WARNING in ./node_modules/@jupyterlab/services/node_modules/ws/lib/validation.js',
'WARNING in ./node_modules/any-promise/register.js',
'WARNING in ./node_modules/log4js/lib/appenders/index.js',
'WARNING in ./node_modules/log4js/lib/clustering.js',
'WARNING in ./node_modules/diagnostic-channel-publishers/dist/src/azure-coretracing.pub.js',
'WARNING in ./node_modules/applicationinsights/dist/AutoCollection/NativePerformance.js'
];
case 'extension':
return [
'WARNING in asset size limit: The following asset(s) exceed the recommended size limit (244 KiB).',
'WARNING in entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (244 KiB). This can impact web performance.',
'WARNING in webpack performance recommendations:',
'WARNING in ./node_modules/cacheable-request/node_modules/keyv/src/index.js',
'WARNING in ./node_modules/encoding/lib/iconv-loader.js',
'WARNING in ./node_modules/keyv/src/index.js',
'WARNING in ./node_modules/ws/lib/BufferUtil.js',
'WARNING in ./node_modules/ws/lib/buffer-util.js',
'WARNING in ./node_modules/ws/lib/Validation.js',
'WARNING in ./node_modules/ws/lib/validation.js',
'WARNING in ./node_modules/any-promise/register.js',
'WARNING in ./node_modules/@jupyterlab/services/node_modules/ws/lib/buffer-util.js',
'WARNING in ./node_modules/@jupyterlab/services/node_modules/ws/lib/validation.js',
'WARNING in ./node_modules/@jupyterlab/services/node_modules/ws/lib/Validation.js',
'WARNING in ./node_modules/diagnostic-channel-publishers/dist/src/azure-coretracing.pub.js',
'WARNING in ./node_modules/applicationinsights/dist/AutoCollection/NativePerformance.js'
];
case 'debugAdapter':
return [
'WARNING in ./node_modules/vscode-uri/lib/index.js',
'WARNING in ./node_modules/diagnostic-channel-publishers/dist/src/azure-coretracing.pub.js',
'WARNING in ./node_modules/applicationinsights/dist/AutoCollection/NativePerformance.js'
];
default:
throw new Error('Unknown WebPack Configuration');
}
}
gulp.task('prePublishBundle', async () => {
await spawnAsync('npm', ['run', 'prePublishBundle'], webpackEnv);
});
gulp.task('checkDependencies', gulp.series('checkNativeDependencies', 'checkNpmDependencies'));
gulp.task('prePublishNonBundle', async () => {
await spawnAsync('npm', ['run', 'prePublishNonBundle'], webpackEnv);
});
function spawnAsync(command, args, env, rejectOnStdErr = false) {
env = env || {};
env = { ...process.env, ...env };
return new Promise((resolve, reject) => {
let stdOut = '';
console.info(`> ${command} ${args.join(' ')}`);
const proc = spawn(command, args, { cwd: __dirname, env });
proc.stdout.on('data', (data) => {
// Log output on CI (else travis times out when there's not output).
stdOut += data.toString();
if (isCI) {
console.log(data.toString());
}
});
proc.stderr.on('data', (data) => {
console.error(data.toString());
if (rejectOnStdErr) {
reject(data.toString());
}
});
proc.on('close', () => resolve(stdOut));
proc.on('error', (error) => reject(error));
});
}
function hasNativeDependencies() {
let nativeDependencies = nativeDependencyChecker.check(path.join(__dirname, 'node_modules'));
if (!Array.isArray(nativeDependencies) || nativeDependencies.length === 0) {
return false;
}
const dependencies = JSON.parse(spawn.sync('npm', ['ls', '--json', '--prod']).stdout.toString());
const jsonProperties = Object.keys(flat.flatten(dependencies));
nativeDependencies = _.flatMap(nativeDependencies, (item) =>
path.dirname(item.substring(item.indexOf('node_modules') + 'node_modules'.length)).split(path.sep)
)
.filter((item) => item.length > 0)
.filter((item) => !item.includes('zeromq') && !item.includes('canvas') && !item.includes('keytar')) // Known native modules
.filter(
(item) =>
jsonProperties.findIndex((flattenedDependency) =>
flattenedDependency.endsWith(`dependencies.${item}.version`)
) >= 0
);
if (nativeDependencies.length > 0) {
console.error('Native dependencies detected', nativeDependencies);
return true;
}
return false;
}
async function generateTelemetry() {
const generator = require('./out/telemetryGenerator.node');
await generator.default();
}
gulp.task('generateTelemetry', async () => {
return generateTelemetry();
});
gulp.task('validateTelemetry', async () => {
const gdprTS = fs.readFileSync(path.join(__dirname, 'src', 'gdpr.ts'), 'utf-8');
await generateTelemetry();
const gdprTS2 = fs.readFileSync(path.join(__dirname, 'src', 'gdpr.ts'), 'utf-8');
if (gdprTS2.trim() !== gdprTS.trim()) {
console.error('src/gdpr.ts is not valid, please re-run `npm run generateTelemetry`');
throw new Error('src/gdpr.ts is not valid, please re-run `npm run generateTelemetry`');
}
});
gulp.task('validatePackageLockJson', async () => {
const fileName = path.join(__dirname, 'package-lock.json');
const oldContents = fs.readFileSync(fileName).toString();
spawnSync('npm', ['install', '--prefer-offline']);
const newContents = fs.readFileSync(fileName).toString();
if (oldContents.trim() !== newContents.trim()) {
throw new Error('package-lock.json has changed after running `npm install`');
}
});
gulp.task('verifyUnhandledErrors', async () => {
const fileName = path.join(__dirname, 'unhandledErrors.txt');
const contents = fs.pathExistsSync(fileName) ? fs.readFileSync(fileName, 'utf8') : '';
if (contents.trim().length) {
console.error(contents);
throw new Error('Unhandled errors detected. Please fix them before merging this PR.', contents);
}
});